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
Signed-off-by: cls_宁波本机 <908705107@qq.com>
270 lines
7.8 KiB
Dart
270 lines
7.8 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
|
|
import '../../l10n/app_localizations.dart';
|
|
import '../../shared/relay/relay.dart';
|
|
|
|
/// A top-level forum post with an optional thread summary.
|
|
@immutable
|
|
class ForumPost {
|
|
final String eventId;
|
|
final String pubkey;
|
|
final String content;
|
|
final int kind;
|
|
final int createdAt;
|
|
final String channelId;
|
|
final List<List<String>> tags;
|
|
final ForumThreadSummary? threadSummary;
|
|
|
|
const ForumPost({
|
|
required this.eventId,
|
|
required this.pubkey,
|
|
required this.content,
|
|
required this.kind,
|
|
required this.createdAt,
|
|
required this.channelId,
|
|
required this.tags,
|
|
this.threadSummary,
|
|
});
|
|
|
|
factory ForumPost.fromJson(Map<String, dynamic> json) {
|
|
final rawSummary = json['thread_summary'] as Map<String, dynamic>?;
|
|
return ForumPost(
|
|
eventId: json['event_id'] as String,
|
|
pubkey: json['pubkey'] as String,
|
|
content: json['content'] as String,
|
|
kind: json['kind'] as int,
|
|
createdAt: json['created_at'] as int,
|
|
channelId: json['channel_id'] as String,
|
|
tags: (json['tags'] as List<dynamic>)
|
|
.map((t) => (t as List<dynamic>).map((e) => e as String).toList())
|
|
.toList(),
|
|
threadSummary: rawSummary != null
|
|
? ForumThreadSummary.fromJson(rawSummary)
|
|
: null,
|
|
);
|
|
}
|
|
|
|
/// Build a [ForumPost] from a raw Nostr event (kind:45001).
|
|
factory ForumPost.fromEvent(NostrEvent event) {
|
|
return ForumPost(
|
|
eventId: event.id,
|
|
pubkey: event.pubkey,
|
|
content: event.content,
|
|
kind: event.kind,
|
|
createdAt: event.createdAt,
|
|
channelId: event.channelId ?? '',
|
|
tags: event.tags,
|
|
);
|
|
}
|
|
|
|
/// Extract mention pubkeys from p-tags.
|
|
List<String> get mentionPubkeys => [
|
|
for (final tag in tags)
|
|
if (tag.length >= 2 && tag[0] == 'p') tag[1],
|
|
];
|
|
}
|
|
|
|
/// Summary of replies on a forum post.
|
|
@immutable
|
|
class ForumThreadSummary {
|
|
final int replyCount;
|
|
final int descendantCount;
|
|
final int? lastReplyAt;
|
|
final List<String> participants;
|
|
|
|
const ForumThreadSummary({
|
|
required this.replyCount,
|
|
required this.descendantCount,
|
|
this.lastReplyAt,
|
|
required this.participants,
|
|
});
|
|
|
|
factory ForumThreadSummary.fromJson(Map<String, dynamic> json) {
|
|
return ForumThreadSummary(
|
|
replyCount: json['reply_count'] as int? ?? 0,
|
|
descendantCount: json['descendant_count'] as int? ?? 0,
|
|
lastReplyAt: json['last_reply_at'] as int?,
|
|
participants:
|
|
(json['participants'] as List<dynamic>?)
|
|
?.map((e) => e as String)
|
|
.toList() ??
|
|
const [],
|
|
);
|
|
}
|
|
}
|
|
|
|
/// A reply within a forum thread.
|
|
@immutable
|
|
class ThreadReply {
|
|
final String eventId;
|
|
final String pubkey;
|
|
final String content;
|
|
final int kind;
|
|
final int createdAt;
|
|
final String channelId;
|
|
final List<List<String>> tags;
|
|
final String? parentEventId;
|
|
final String? rootEventId;
|
|
final int depth;
|
|
|
|
const ThreadReply({
|
|
required this.eventId,
|
|
required this.pubkey,
|
|
required this.content,
|
|
required this.kind,
|
|
required this.createdAt,
|
|
required this.channelId,
|
|
required this.tags,
|
|
this.parentEventId,
|
|
this.rootEventId,
|
|
required this.depth,
|
|
});
|
|
|
|
factory ThreadReply.fromJson(Map<String, dynamic> json) {
|
|
return ThreadReply(
|
|
eventId: json['event_id'] as String,
|
|
pubkey: json['pubkey'] as String,
|
|
content: json['content'] as String,
|
|
kind: json['kind'] as int,
|
|
createdAt: json['created_at'] as int,
|
|
channelId: json['channel_id'] as String,
|
|
tags: (json['tags'] as List<dynamic>)
|
|
.map((t) => (t as List<dynamic>).map((e) => e as String).toList())
|
|
.toList(),
|
|
parentEventId: json['parent_event_id'] as String?,
|
|
rootEventId: json['root_event_id'] as String?,
|
|
depth: json['depth'] as int? ?? 0,
|
|
);
|
|
}
|
|
|
|
/// Build a [ThreadReply] from a raw Nostr event.
|
|
factory ThreadReply.fromEvent(NostrEvent event) {
|
|
final ref = event.threadReference;
|
|
return ThreadReply(
|
|
eventId: event.id,
|
|
pubkey: event.pubkey,
|
|
content: event.content,
|
|
kind: event.kind,
|
|
createdAt: event.createdAt,
|
|
channelId: event.channelId ?? '',
|
|
tags: event.tags,
|
|
parentEventId: ref.parentId,
|
|
rootEventId: ref.rootId,
|
|
depth: 0,
|
|
);
|
|
}
|
|
|
|
/// Extract mention pubkeys from p-tags.
|
|
List<String> get mentionPubkeys => [
|
|
for (final tag in tags)
|
|
if (tag.length >= 2 && tag[0] == 'p') tag[1],
|
|
];
|
|
}
|
|
|
|
/// Paginated response for forum posts.
|
|
@immutable
|
|
class ForumPostsResponse {
|
|
final List<ForumPost> posts;
|
|
final int? nextCursor;
|
|
|
|
const ForumPostsResponse({required this.posts, this.nextCursor});
|
|
|
|
factory ForumPostsResponse.fromJson(Map<String, dynamic> json) {
|
|
final messages = json['messages'] as List<dynamic>? ?? const [];
|
|
return ForumPostsResponse(
|
|
posts: messages
|
|
.cast<Map<String, dynamic>>()
|
|
.map(ForumPost.fromJson)
|
|
.toList(),
|
|
nextCursor: json['next_cursor'] as int?,
|
|
);
|
|
}
|
|
|
|
/// Build from a list of kind:45001 events. Posts are sorted newest-first.
|
|
factory ForumPostsResponse.fromEvents(List<NostrEvent> events) {
|
|
final posts = events.map(ForumPost.fromEvent).toList()
|
|
..sort((a, b) => b.createdAt.compareTo(a.createdAt));
|
|
return ForumPostsResponse(posts: posts, nextCursor: null);
|
|
}
|
|
}
|
|
|
|
/// Response for a single forum thread with replies.
|
|
@immutable
|
|
class ForumThreadResponse {
|
|
final ForumPost post;
|
|
final List<ThreadReply> replies;
|
|
final int totalReplies;
|
|
final String? nextCursor;
|
|
|
|
const ForumThreadResponse({
|
|
required this.post,
|
|
required this.replies,
|
|
required this.totalReplies,
|
|
this.nextCursor,
|
|
});
|
|
|
|
factory ForumThreadResponse.fromJson(Map<String, dynamic> json) {
|
|
final repliesJson = json['replies'] as List<dynamic>? ?? const [];
|
|
return ForumThreadResponse(
|
|
post: ForumPost.fromJson(json['root'] as Map<String, dynamic>),
|
|
replies: repliesJson
|
|
.cast<Map<String, dynamic>>()
|
|
.map(ThreadReply.fromJson)
|
|
.toList(),
|
|
totalReplies: json['total_replies'] as int? ?? 0,
|
|
nextCursor: json['next_cursor'] as String?,
|
|
);
|
|
}
|
|
|
|
/// Build from a root event and a list of reply events. Replies are sorted
|
|
/// oldest-first so the UI can render them top-down.
|
|
factory ForumThreadResponse.fromEvents({
|
|
required NostrEvent root,
|
|
required List<NostrEvent> replies,
|
|
}) {
|
|
final sortedReplies = replies.map(ThreadReply.fromEvent).toList()
|
|
..sort((a, b) => a.createdAt.compareTo(b.createdAt));
|
|
return ForumThreadResponse(
|
|
post: ForumPost.fromEvent(root),
|
|
replies: sortedReplies,
|
|
totalReplies: sortedReplies.length,
|
|
nextCursor: null,
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Format a unix timestamp as a relative time string (e.g. "2h ago").
|
|
String formatRelativeTime(int timestamp) {
|
|
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
|
final diff = now - timestamp;
|
|
|
|
if (diff < 60) return 'just now';
|
|
if (diff < 3600) return '${diff ~/ 60}m ago';
|
|
if (diff < 86400) return '${diff ~/ 3600}h ago';
|
|
if (diff < 604800) return '${diff ~/ 86400}d ago';
|
|
|
|
final dt = DateTime.fromMillisecondsSinceEpoch(
|
|
timestamp * 1000,
|
|
isUtc: true,
|
|
).toLocal();
|
|
return '${dt.month}/${dt.day}/${dt.year}';
|
|
}
|
|
|
|
/// Localized variant used by forum UI; the legacy formatter remains for
|
|
/// model-only callers and stable tests.
|
|
String formatRelativeTimeLocalized(AppLocalizations l10n, int timestamp) {
|
|
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
|
final diff = now - timestamp;
|
|
|
|
if (diff < 60) return l10n.forumTimeJustNow;
|
|
if (diff < 3600) return l10n.forumTimeMinutesAgo(diff ~/ 60);
|
|
if (diff < 86400) return l10n.forumTimeHoursAgo(diff ~/ 3600);
|
|
if (diff < 604800) return l10n.forumTimeDaysAgo(diff ~/ 86400);
|
|
|
|
final dt = DateTime.fromMillisecondsSinceEpoch(
|
|
timestamp * 1000,
|
|
isUtc: true,
|
|
).toLocal();
|
|
return l10n.forumFullDate(dt.month, dt.day, dt.year);
|
|
}
|