Skip to content

Commit d3ee9ff

Browse files
committed
fix(mobile): reconcile fallback reply cache evictions
Signed-off-by: Tom Brow <[email protected]>
1 parent 9928882 commit d3ee9ff

3 files changed

Lines changed: 79 additions & 8 deletions

File tree

‎mobile/lib/features/channels/channel_messages_provider.dart‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ class ChannelMessagesNotifier extends Notifier<AsyncValue<List<NostrEvent>>> {
464464
event.threadReference.parentId != null,
465465
);
466466
final retained = _boundedReplies(replies).map((event) => event.id).toSet();
467+
_reconcileEvictedReplies(replies, retained);
467468
return events
468469
.where(
469470
(event) =>
@@ -481,14 +482,7 @@ class ChannelMessagesNotifier extends Notifier<AsyncValue<List<NostrEvent>>> {
481482
if (replies.length <= _maxCachedRepliesPerRoot) return;
482483
final retained = _boundedReplies(replies).map((event) => event.id).toSet();
483484
if (retained.length == replies.length) return;
484-
for (final root
485-
in replies
486-
.where((event) => !retained.contains(event.id))
487-
.map((event) => event.threadReference.rootId)
488-
.whereType<String>()
489-
.toSet()) {
490-
if (!_queryThreadSummaries.containsKey(root)) _queueOverflowSummary(root);
491-
}
485+
_reconcileEvictedReplies(replies, retained);
492486
_windowStore = ChannelWindowStore(
493487
pages: _windowStore.pages,
494488
liveOverlay: _windowStore.liveOverlay

‎mobile/lib/features/channels/channel_messages_provider/thread_summaries.dart‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,20 @@ extension _ThreadSummaryState on ChannelMessagesNotifier {
246246
}
247247
}
248248

249+
void _reconcileEvictedReplies(
250+
Iterable<NostrEvent> replies,
251+
Set<String> retained,
252+
) {
253+
for (final root
254+
in replies
255+
.where((event) => !retained.contains(event.id))
256+
.map((event) => event.threadReference.rootId)
257+
.whereType<String>()
258+
.toSet()) {
259+
if (!_queryThreadSummaries.containsKey(root)) _queueOverflowSummary(root);
260+
}
261+
}
262+
249263
void _queueOverflowSummary(String root, {bool countPending = false}) {
250264
if (!_hasVisibleThreadRows(root)) return;
251265
final ids = cachedThreadReplyIds(root)..removeAll(_localReplyRoots.keys);

‎mobile/test/features/channels/channel_messages_provider_test.dart‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4545,6 +4545,69 @@ void main() {
45454545
);
45464546
}
45474547

4548+
test('fallback global eviction preserves another root summary', () async {
4549+
final reply = _event(
4550+
id: 'old-reply',
4551+
createdAt: 20,
4552+
extraTags: const [
4553+
['e', 'old-root', '', 'reply'],
4554+
],
4555+
);
4556+
final recount = Completer<List<NostrEvent>>();
4557+
final session = _RecordingRelaySessionNotifier(
4558+
queryResults: [Exception('NIP-CW unavailable'), recount.future],
4559+
historyResults: [
4560+
[
4561+
_event(id: 'old-root', createdAt: 10),
4562+
reply,
4563+
for (var i = 0; i < 16; i++) _event(id: 'other-$i', createdAt: 10),
4564+
],
4565+
],
4566+
);
4567+
final container = _buildContainer(session);
4568+
addTearDown(container.dispose);
4569+
container.listen(channelMessagesProvider(_channelId), (_, _) {});
4570+
await _pumpEventQueue();
4571+
final notifier = container.read(
4572+
channelMessagesProvider(_channelId).notifier,
4573+
);
4574+
for (var i = 0; i < 2048; i++) {
4575+
session.emit(
4576+
_event(
4577+
id: 'new-$i',
4578+
createdAt: 100 + i,
4579+
extraTags: [
4580+
['e', 'other-${i ~/ 128}', '', 'reply'],
4581+
],
4582+
),
4583+
);
4584+
}
4585+
expect(notifier.cachedThreadReplyIds('old-root'), isEmpty);
4586+
final entries = buildMainTimelineEntries(
4587+
formatTimeline(
4588+
container.read(channelMessagesProvider(_channelId)).value!,
4589+
),
4590+
relaySummaries: notifier.threadSummaries,
4591+
);
4592+
expect(
4593+
entries
4594+
.singleWhere((e) => e.message.id == 'old-root')
4595+
.summary
4596+
?.replyCount,
4597+
1,
4598+
);
4599+
await Future<void>.delayed(const Duration(milliseconds: 300));
4600+
final scans = session.queryFilters.where(
4601+
(f) => f.extensions.containsKey('depth_limit'),
4602+
);
4603+
expect(scans, hasLength(1));
4604+
expect(scans.single.tags['#e'], ['old-root']);
4605+
recount.complete([reply]);
4606+
await _pumpEventQueue();
4607+
expect(notifier.threadSummaries['old-root']?.descendantCount, 1);
4608+
expect(notifier.threadSummaries['old-root']?.isLowerBound, isFalse);
4609+
});
4610+
45484611
for (final fromHistory in [false, true]) {
45494612
for (final survives in [false, true]) {
45504613
test(

0 commit comments

Comments
 (0)