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

190 lines
5.8 KiB
Dart

import 'package:hooks_riverpod/hooks_riverpod.dart';
import '../../shared/relay/relay.dart';
import '../channels/channel_management_provider.dart';
import '../../shared/custom_emoji/custom_emoji.dart';
import '../../shared/custom_emoji/custom_emoji_provider.dart';
import 'forum_models.dart';
/// Fetches forum posts (kind:45001) for a channel from the relay.
///
/// Posts are top-level events tagged `#h:<channelId>`. Invalidate to refresh
/// (e.g. after creating a new post).
final forumPostsProvider = FutureProvider.family<ForumPostsResponse, String>((
ref,
channelId,
) async {
final session = ref.watch(relaySessionProvider.notifier);
final events = await session.fetchHistory(
NostrFilters.forumPosts(channelId, limit: 50),
);
return ForumPostsResponse.fromEvents(events);
});
/// Fetches a forum thread (root post + replies) from the relay.
final forumThreadProvider =
FutureProvider.family<
ForumThreadResponse,
({String channelId, String eventId})
>((ref, args) async {
final session = ref.watch(relaySessionProvider.notifier);
final results = await Future.wait([
// Root event lookup by id.
session.fetchHistory(
NostrFilter(
kinds: const [9, 40002, 45001, 45003],
ids: [args.eventId],
limit: 1,
),
),
// Replies pointing at this root.
session.fetchHistory(
NostrFilters.forumThread(args.eventId, args.channelId),
),
]);
final rootEvents = results[0];
final replyEvents = results[1];
if (rootEvents.isEmpty) {
throw Exception('Forum thread not found: ${args.eventId}');
}
return ForumThreadResponse.fromEvents(
root: rootEvents.first,
replies: replyEvents,
);
});
/// A forum event delivery bound to the community where composition began.
///
/// Attachment uploads can outlive their route. Capturing the relay identity,
/// signing key, and emoji palette prevents a queued draft from being delivered
/// to a different community after the user switches relays.
class ForumEventDelivery {
final ProviderContainer _container;
final String _relayUrl;
final String? _nsec;
final SignedEventRelay _relay;
final List<CustomEmoji> _customEmoji;
ForumEventDelivery._({
required ProviderContainer container,
required String relayUrl,
required String? nsec,
required SignedEventRelay relay,
required List<CustomEmoji> customEmoji,
}) : _container = container,
_relayUrl = relayUrl,
_nsec = nsec,
_relay = relay,
_customEmoji = customEmoji;
/// Captures the active community dependencies for a future delivery.
factory ForumEventDelivery.capture(ProviderContainer container) {
final config = container.read(relayConfigProvider);
return ForumEventDelivery._(
container: container,
relayUrl: config.baseUrl,
nsec: config.nsec,
relay: SignedEventRelay(
session: container.read(relaySessionProvider.notifier),
nsec: config.nsec,
),
customEmoji: List<CustomEmoji>.unmodifiable(
container.read(customEmojiListProvider),
),
);
}
/// Creates a new forum post (kind:45001).
Future<void> createPost({
required String channelId,
required String content,
List<String> mentionPubkeys = const [],
List<List<String>> mediaTags = const [],
}) async {
await _submit(
kind: EventKind.forumPost,
channelId: channelId,
content: content,
mentionPubkeys: mentionPubkeys,
mediaTags: mediaTags,
);
_container.invalidate(forumPostsProvider(channelId));
}
/// Creates a reply to a forum post (kind:45003).
Future<void> createReply({
required String channelId,
required String parentEventId,
required String content,
List<String> mentionPubkeys = const [],
List<List<String>> mediaTags = const [],
}) async {
await _submit(
kind: EventKind.forumComment,
channelId: channelId,
parentEventId: parentEventId,
content: content,
mentionPubkeys: mentionPubkeys,
mediaTags: mediaTags,
);
_container.invalidate(forumPostsProvider(channelId));
_container.invalidate(
forumThreadProvider((channelId: channelId, eventId: parentEventId)),
);
}
Future<void> _submit({
required int kind,
required String channelId,
required String content,
String? parentEventId,
required List<String> mentionPubkeys,
required List<List<String>> mediaTags,
}) async {
final currentConfig = _container.read(relayConfigProvider);
if (currentConfig.baseUrl != _relayUrl || currentConfig.nsec != _nsec) {
throw StateError(
'Forum delivery cancelled because the active community changed',
);
}
final selfPubkey = _relay.pubkey?.toLowerCase();
final seen = <String>{?selfPubkey};
final normalizedMentions = [
for (final pk in mentionPubkeys)
if (seen.add(pk.toLowerCase())) pk,
];
await _relay.submit(
kind: kind,
content: content,
tags: [
['h', channelId],
if (parentEventId != null) ['e', parentEventId, '', 'reply'],
for (final pk in normalizedMentions) ['p', pk],
...mediaTags,
...buildCustomEmojiTags(content, _customEmoji),
],
);
}
}
/// Deletes a forum post or reply and invalidates relevant caches.
Future<void> deleteForumEvent(
WidgetRef ref, {
required String channelId,
required String eventId,
String? rootEventId,
}) async {
final actions = ref.read(channelActionsProvider);
await actions.deleteMessage(channelId: channelId, eventId: eventId);
ref.invalidate(forumPostsProvider(channelId));
if (rootEventId != null) {
ref.invalidate(
forumThreadProvider((channelId: channelId, eventId: rootEventId)),
);
}
}