feat(mobile): implement message thread support (#338)

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Wes
2026-04-16 11:00:02 -06:00
committed by GitHub
parent 6c0e363bdd
commit cd6fc96fc5
8 changed files with 1539 additions and 24 deletions

View File

@@ -13,6 +13,7 @@ NostrEvent _textMsg({
String pubkey = 'alice',
String content = 'hello',
int createdAt = 1000,
List<List<String>>? extraTags,
}) => NostrEvent(
id: id,
pubkey: pubkey,
@@ -20,11 +21,39 @@ NostrEvent _textMsg({
kind: EventKind.streamMessage,
tags: [
['h', 'ch1'],
...?extraTags,
],
content: content,
sig: '',
);
/// A reply message with proper root/reply e-tag markers.
NostrEvent _replyMsg({
required String id,
required String parentId,
String? rootId,
String pubkey = 'alice',
String content = 'reply',
int createdAt = 2000,
}) {
final root = rootId ?? parentId;
final eTags = parentId == root
? [
['e', root, '', 'reply'],
]
: [
['e', root, '', 'root'],
['e', parentId, '', 'reply'],
];
return _textMsg(
id: id,
pubkey: pubkey,
content: content,
createdAt: createdAt,
extraTags: eTags,
);
}
NostrEvent _systemMsg({
required String id,
required Map<String, dynamic> payload,
@@ -443,4 +472,193 @@ void main() {
expect(result[0].content, contains('diff'));
});
});
// -------------------------------------------------------------------------
// NostrEvent.threadReference
// -------------------------------------------------------------------------
group('NostrEvent.threadReference', () {
test('returns nulls for top-level message (no e-tags)', () {
final event = _textMsg(id: 'a');
final ref = event.threadReference;
expect(ref.parentId, isNull);
expect(ref.rootId, isNull);
});
test('returns nulls for e-tags without root/reply markers', () {
final event = _textMsg(
id: 'a',
extraTags: [
['e', 'target'],
],
);
final ref = event.threadReference;
expect(ref.parentId, isNull);
expect(ref.rootId, isNull);
});
test('parses direct reply to root (single reply marker)', () {
final event = _textMsg(
id: 'reply1',
extraTags: [
['e', 'root1', '', 'reply'],
],
);
final ref = event.threadReference;
expect(ref.parentId, 'root1');
expect(ref.rootId, 'root1');
});
test('parses nested reply (root + reply markers)', () {
final event = _textMsg(
id: 'reply2',
extraTags: [
['e', 'root1', '', 'root'],
['e', 'parent1', '', 'reply'],
],
);
final ref = event.threadReference;
expect(ref.parentId, 'parent1');
expect(ref.rootId, 'root1');
});
test('last reply tag wins when multiple exist', () {
final event = _textMsg(
id: 'reply3',
extraTags: [
['e', 'root1', '', 'root'],
['e', 'old_parent', '', 'reply'],
['e', 'new_parent', '', 'reply'],
],
);
final ref = event.threadReference;
expect(ref.parentId, 'new_parent');
expect(ref.rootId, 'root1');
});
});
// -------------------------------------------------------------------------
// formatTimeline — threading fields
// -------------------------------------------------------------------------
group('formatTimeline threading', () {
test('root messages have null parentId and rootId', () {
final events = [_textMsg(id: 'a')];
final result = formatTimeline(events);
expect(result[0].parentId, isNull);
expect(result[0].rootId, isNull);
});
test('reply messages carry parentId and rootId', () {
final events = [
_textMsg(id: 'root1'),
_replyMsg(id: 'r1', parentId: 'root1', createdAt: 2000),
];
final result = formatTimeline(events);
final reply = result.firstWhere((m) => m.id == 'r1');
expect(reply.parentId, 'root1');
expect(reply.rootId, 'root1');
});
test('nested reply has distinct parentId and rootId', () {
final events = [
_textMsg(id: 'root1'),
_replyMsg(id: 'r1', parentId: 'root1', createdAt: 2000),
_replyMsg(id: 'r2', parentId: 'r1', rootId: 'root1', createdAt: 3000),
];
final result = formatTimeline(events);
final nested = result.firstWhere((m) => m.id == 'r2');
expect(nested.parentId, 'r1');
expect(nested.rootId, 'root1');
});
});
// -------------------------------------------------------------------------
// buildMainTimelineEntries
// -------------------------------------------------------------------------
group('buildMainTimelineEntries', () {
test('returns only root messages', () {
final messages = formatTimeline([
_textMsg(id: 'a', createdAt: 1000),
_replyMsg(id: 'r1', parentId: 'a', createdAt: 2000),
_textMsg(id: 'b', createdAt: 3000),
]);
final entries = buildMainTimelineEntries(messages);
expect(entries, hasLength(2));
expect(entries[0].message.id, 'a');
expect(entries[1].message.id, 'b');
});
test('root without replies has null summary', () {
final messages = formatTimeline([_textMsg(id: 'a')]);
final entries = buildMainTimelineEntries(messages);
expect(entries[0].summary, isNull);
});
test('root with replies has summary with correct count', () {
final messages = formatTimeline([
_textMsg(id: 'a', createdAt: 1000),
_replyMsg(id: 'r1', parentId: 'a', pubkey: 'bob', createdAt: 2000),
_replyMsg(id: 'r2', parentId: 'a', pubkey: 'carol', createdAt: 3000),
]);
final entries = buildMainTimelineEntries(messages);
expect(entries, hasLength(1));
expect(entries[0].summary, isNotNull);
expect(entries[0].summary!.replyCount, 2);
expect(entries[0].summary!.threadHeadId, 'a');
});
test('summary counts only direct children, not nested replies', () {
final messages = formatTimeline([
_textMsg(id: 'a', createdAt: 1000),
_replyMsg(id: 'r1', parentId: 'a', createdAt: 2000),
_replyMsg(id: 'r2', parentId: 'r1', rootId: 'a', createdAt: 3000),
]);
final entries = buildMainTimelineEntries(messages);
// Only r1 is a direct child of a; r2 is a child of r1.
expect(entries[0].summary!.replyCount, 1);
});
test('summary has up to 3 unique participant pubkeys', () {
final messages = formatTimeline([
_textMsg(id: 'a', createdAt: 1000),
_replyMsg(id: 'r1', parentId: 'a', pubkey: 'bob', createdAt: 2000),
_replyMsg(id: 'r2', parentId: 'a', pubkey: 'carol', createdAt: 3000),
_replyMsg(id: 'r3', parentId: 'a', pubkey: 'bob', createdAt: 4000),
_replyMsg(id: 'r4', parentId: 'a', pubkey: 'dave', createdAt: 5000),
_replyMsg(id: 'r5', parentId: 'a', pubkey: 'eve', createdAt: 6000),
]);
final entries = buildMainTimelineEntries(messages);
final participants = entries[0].summary!.participantPubkeys;
expect(participants, hasLength(3));
// Most recent unique: eve, dave, bob (r5, r4, r3 — carol skipped
// because bob at r3 comes before carol at r2 when walking backwards).
// Reversed to chronological order.
expect(participants, ['bob', 'dave', 'eve']);
});
test('system messages remain in entries (parentId is null)', () {
final events = [
_systemMsg(
id: 's1',
payload: {'type': 'channel_created', 'actor': 'pk1'},
createdAt: 500,
),
_textMsg(id: 'a', createdAt: 1000),
];
final messages = formatTimeline(events);
final entries = buildMainTimelineEntries(messages);
expect(entries, hasLength(2));
expect(entries[0].message.isSystem, true);
});
test('empty input returns empty', () {
expect(buildMainTimelineEntries([]), isEmpty);
});
});
}