Files
cls 9dfa06ffee
Docker image / Build (linux/amd64) (push) Has been cancelled
Docker image / Build (linux/arm64) (push) Has been cancelled
Docker image / Merge release multi-arch manifest (push) Has been cancelled
Docker image / Merge debug multi-arch manifest (push) Has been cancelled
Docker image / Build public push gateway (linux/amd64) (push) Has been cancelled
Docker image / Build public push gateway (linux/arm64) (push) Has been cancelled
Docker image / Publish public push gateway image (push) Has been cancelled
Sprig image / Build (linux/amd64) (push) Has been cancelled
Sprig image / Build (linux/arm64) (push) Has been cancelled
Sprig image / Merge multi-arch manifest (push) Has been cancelled
Harbor Buzz Orchestra / Python tests and lint (push) Has been cancelled
CI / Detect Changed Paths (push) Has been cancelled
CI / Rust Lint (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Desktop Core (push) Has been cancelled
CI / Desktop Smoke E2E (1) (push) Has been cancelled
CI / Desktop Smoke E2E (2) (push) Has been cancelled
CI / Desktop Smoke E2E (3) (push) Has been cancelled
CI / Desktop Smoke E2E (4) (push) Has been cancelled
CI / Desktop (push) Has been cancelled
CI / Desktop E2E Relay (push) Has been cancelled
CI / Desktop E2E Integration (1/2) (push) Has been cancelled
CI / Desktop E2E Integration (2/2) (push) Has been cancelled
CI / Desktop E2E Integration (push) Has been cancelled
CI / Backend Integration (relay e2e) (push) Has been cancelled
CI / Relay E2E (push) Has been cancelled
CI / Web (push) Has been cancelled
CI / Mobile (push) Has been cancelled
CI / Security (push) Has been cancelled
CI / Dead Token Reference Guard (push) Has been cancelled
CI / Server Cross-Compile (aarch64-unknown-linux-musl) (push) Has been cancelled
CI / Server Cross-Compile (x86_64-unknown-linux-musl) (push) Has been cancelled
CI / Windows Rust (x86_64-pc-windows-msvc) (push) Has been cancelled
CI / Desktop Build (macOS) (push) Has been cancelled
helm chart / lint + unittest + render matrix (push) Has been cancelled
helm chart / install on kind (gated) (push) Has been cancelled
helm chart / publish chart to GHCR (push) Has been cancelled
Mesh Lifecycle / Relay-Driven Mesh Lifecycle Smoke (push) Has been cancelled
Sprig / Build (aarch64-unknown-linux-musl) (push) Has been cancelled
Sprig / Build (x86_64-unknown-linux-musl) (push) Has been cancelled
Sprig / Publish rolling release (push) Has been cancelled
Sprig / Publish tagged release (push) Has been cancelled
feat: import Chinese-localized Buzz source snapshot
Signed-off-by: cls_宁波本机 <908705107@qq.com>
2026-08-13 18:34:25 +08:00

225 lines
6.3 KiB
Dart

import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:nostr/nostr.dart' as nostr;
import '../../shared/crypto/nip44.dart';
import '../../shared/relay/relay.dart';
/// NIP-ER event reminder kind — matches desktop's `KIND_EVENT_REMINDER`.
const kindEventReminder = 30300;
/// Message a reminder points at. Mirrors desktop's `ReminderTarget`.
@immutable
class ReminderTarget {
final String eventId;
final String channelId;
final String preview;
final String authorPubkey;
const ReminderTarget({
required this.eventId,
required this.channelId,
required this.preview,
required this.authorPubkey,
});
}
/// A decrypted NIP-ER reminder. Mirrors desktop's `Reminder`.
@immutable
class Reminder {
/// The `d`-tag (unique reminder identity).
final String id;
/// Unix seconds when due; null for done/cancelled reminders.
final int? notBefore;
final String status; // "pending" | "done" | "cancelled"
final ReminderTarget? target;
final String? note;
final int createdAt;
final String eventId;
const Reminder({
required this.id,
required this.notBefore,
required this.status,
required this.target,
required this.note,
required this.createdAt,
required this.eventId,
});
bool isDue(int nowUnixSeconds) =>
status == 'pending' && notBefore != null && notBefore! <= nowUnixSeconds;
}
/// Parse NIP-ER `not_before`: ASCII digits, no leading zero except "0".
/// Mirrors desktop's `parseNotBefore` strictness.
int? parseNotBefore(String raw) {
if (!RegExp(r'^(0|[1-9][0-9]*)$').hasMatch(raw)) return null;
return int.tryParse(raw);
}
/// Validate decrypted reminder plaintext — desktop's `parseReminderContent`.
/// Off-shape content fails closed (returns null).
({String status, ReminderTarget? target, String? note})? parseReminderContent(
String plaintext,
) {
final Object? parsed;
try {
parsed = jsonDecode(plaintext);
} catch (_) {
return null;
}
if (parsed is! Map<String, dynamic>) return null;
final status = parsed['status'];
if (status != 'pending' && status != 'done' && status != 'cancelled') {
return null;
}
final note = parsed['note'];
if (note != null && note is! String) return null;
ReminderTarget? target;
final rawTarget = parsed['target'];
if (rawTarget != null) {
if (rawTarget is! Map<String, dynamic>) return null;
final eventId = rawTarget['eventId'];
final channelId = rawTarget['channelId'];
final preview = rawTarget['preview'];
final authorPubkey = rawTarget['authorPubkey'];
if (eventId is! String ||
channelId is! String ||
preview is! String ||
authorPubkey is! String) {
return null;
}
target = ReminderTarget(
eventId: eventId,
channelId: channelId,
preview: preview,
authorPubkey: authorPubkey,
);
}
final noteText = note as String?;
if (target == null && (noteText == null || noteText.isEmpty)) return null;
return (status: status as String, target: target, note: noteText);
}
/// Decode one kind:30300 event into a [Reminder], or null when malformed.
Reminder? decodeReminderEvent(
NostrEvent event, {
required String Function(String ciphertext) decrypt,
}) {
String? dTag;
int? notBefore;
for (final tag in event.tags) {
if (tag.length >= 2 && tag[0] == 'd') dTag ??= tag[1];
if (tag.length >= 2 && tag[0] == 'not_before') {
notBefore ??= parseNotBefore(tag[1]);
}
}
if (dTag == null) return null;
final String plaintext;
try {
plaintext = decrypt(event.content);
} catch (_) {
return null;
}
final content = parseReminderContent(plaintext);
if (content == null) return null;
return Reminder(
id: dTag,
notBefore: notBefore,
status: content.status,
target: content.target,
note: content.note,
createdAt: event.createdAt,
eventId: event.id,
);
}
/// Fetches the user's NIP-ER reminders (kind 30300, self-encrypted). These
/// are the same relay events Buzz Desktop reads and writes, so reminders
/// created on Desktop appear here and vice versa.
class RemindersNotifier extends AsyncNotifier<List<Reminder>> {
@override
Future<List<Reminder>> build() {
ref.watch(relayConfigProvider);
ref.watch(relaySessionProvider);
return _fetch();
}
Future<List<Reminder>> _fetch() async {
final config = ref.read(relayConfigProvider);
final myPk = ref.read(myPubkeyProvider);
final nsec = config.nsec?.trim();
if (myPk == null || nsec == null || nsec.isEmpty) return const [];
final String privkeyHex;
try {
privkeyHex = nostr.Nip19.decode(payload: nsec).data;
} catch (_) {
return const [];
}
if (privkeyHex.isEmpty) return const [];
final conversationKey = getConversationKey(privkeyHex, myPk);
final session = ref.read(relaySessionProvider.notifier);
final events = await session.fetchHistory(
NostrFilter(
kinds: const [kindEventReminder],
authors: [myPk],
limit: 200,
),
);
// Parameterized-replaceable: keep only the newest event per d-tag.
final newestByDTag = <String, NostrEvent>{};
for (final event in events) {
final dTag = event.getTagValue('d');
if (dTag == null) continue;
final existing = newestByDTag[dTag];
if (existing == null || event.createdAt > existing.createdAt) {
newestByDTag[dTag] = event;
}
}
final reminders = <Reminder>[];
for (final event in newestByDTag.values) {
final reminder = decodeReminderEvent(
event,
decrypt: (ciphertext) => nip44Decrypt(conversationKey, ciphertext),
);
if (reminder != null) reminders.add(reminder);
}
reminders.sort(
(a, b) =>
(a.notBefore ?? a.createdAt).compareTo(b.notBefore ?? b.createdAt),
);
return reminders;
}
Future<void> refresh() async {
state = await AsyncValue.guard(_fetch);
}
}
final remindersProvider =
AsyncNotifierProvider<RemindersNotifier, List<Reminder>>(
RemindersNotifier.new,
);
/// Count of due pending reminders — powers the filter badge.
final dueReminderCountProvider = Provider<int>((ref) {
final reminders = ref.watch(remindersProvider).value ?? const <Reminder>[];
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
return reminders.where((r) => r.isDue(now)).length;
});