From 22cfffc8d73784577bda572232dca8e925e1e29f Mon Sep 17 00:00:00 2001 From: Tom Brow Date: Fri, 20 Nov 2026 12:17:47 -0800 Subject: [PATCH 1/7] perf(mobile): derive Active filter activity from the channel list, with no per-channel relay reads The Active section filter read each channel's latest message with its own relay history request, restarted on every resume, refresh and live message. Record the newest message time from the list's existing batched latest-message read and from live delivery instead, and drop the lookup failure notice and retry, which can no longer happen. Signed-off-by: Tom Brow --- .../channel_filter_activity.dart | 104 ++------------ .../channels/channel_latest_activity.dart | 84 +++++++++++ .../channels/channel_unread_catch_up.dart | 7 +- .../lib/features/channels/channels_page.dart | 6 +- .../channels_page/activity_recovery.dart | 121 ---------------- .../features/channels/channels_page/body.dart | 5 +- .../features/channels/channels_provider.dart | 56 +++----- mobile/lib/features/channels/me/me_page.dart | 1 - .../channel_filter/channel_filter_test.dart | 111 -------------- .../features/channels/channels_page_test.dart | 1 - .../filter_feedback_tests.dart | 135 ------------------ .../channels/channels_provider_test.dart | 118 +++++++++++---- .../features/channels/me/me_page_test.dart | 11 +- .../sidebar_workspaces_journey_test.dart | 15 +- 14 files changed, 222 insertions(+), 553 deletions(-) create mode 100644 mobile/lib/features/channels/channel_latest_activity.dart delete mode 100644 mobile/lib/features/channels/channels_page/activity_recovery.dart diff --git a/mobile/lib/features/channels/channel_filter/channel_filter_activity.dart b/mobile/lib/features/channels/channel_filter/channel_filter_activity.dart index de984eceb20..3b1fc074426 100644 --- a/mobile/lib/features/channels/channel_filter/channel_filter_activity.dart +++ b/mobile/lib/features/channels/channel_filter/channel_filter_activity.dart @@ -1,97 +1,15 @@ -import 'dart:async'; -import 'dart:convert'; -import 'package:flutter/foundation.dart'; -import 'package:flutter/widgets.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; -import 'package:nostr/nostr.dart' as nostr; -import '../../../shared/relay/relay.dart'; import '../channels_provider.dart'; -/// Verification stays off the UI isolate. Null after successful history means -/// known empty; a failed/invalid response stays unknown and exposes Retry. -int? verifiedFilterActivity(List> records) { - int? latest; - final now = DateTime.now().millisecondsSinceEpoch ~/ 1000; - for (final record in records) { - nostr.Event.fromJson(jsonEncode(record)); - final at = record['created_at'] as int; - if (at <= now + 300 && (latest == null || at > latest)) latest = at; - } - if (records.isNotEmpty && latest == null) { - throw const FormatException('Activity history has no acceptable timestamp'); - } - return latest; -} - -class _ActivityReadPool { - int _running = 0; - final _waiting = >[]; - Future run(Future Function() read) async { - if (_running >= 4) { - final turn = Completer(); - _waiting.add(turn); - await turn.future; - } else { - _running++; - } - try { - return await read(); - } finally { - if (_waiting.isNotEmpty) { - _waiting.removeAt(0).complete(); - } else { - _running--; - } - } - } -} - -final _activityReadPoolProvider = Provider((ref) => _ActivityReadPool()); -final channelFilterActivityProvider = FutureProvider.autoDispose - .family((ref, channelId) async { - // Every live message and foreground refresh updates this owner. Re-read - // only the affected channel; never trust its unverified presentation stamp. - final channelsNotifier = ref.watch(channelsProvider.notifier); - ref.watch( - channelsProvider.select( - (value) => ( - value.value - ?.where((c) => c.id == channelId) - .firstOrNull - ?.lastMessageAt, - channelsNotifier.activityRevisionFor(channelId), - ), - ), +/// When anyone last posted in a channel, for the Active filter. Loading until +/// the channel list's latest-message read covers the channel; data (null when +/// it has no messages) after that. It comes from the list's own reads and +/// live delivery, so it sends no relay requests of its own. +final channelFilterActivityProvider = Provider.autoDispose + .family, String>((ref, channelId) { + final notifier = ref.watch(channelsProvider.notifier); + final (:known, :at) = ref.watch( + channelsProvider.select((_) => notifier.activityFor(channelId)), ); - ref.listen(appLifecycleProvider, (_, next) { - if (next == AppLifecycleState.resumed) ref.invalidateSelf(); - }); - final session = ref.watch(relaySessionProvider.select((s) => s.status)); - if (session != SessionStatus.connected) { - throw StateError('Activity unavailable while disconnected'); - } - var disposed = false; - ref.onDispose(() => disposed = true); - final relay = ref.read(relaySessionProvider.notifier); - return ref.read(_activityReadPoolProvider).run(() async { - if (disposed) throw StateError('Activity request superseded'); - final events = await relay.fetchHistory( - NostrFilter( - kinds: EventKind.channelMessageEventKinds, - tags: { - '#h': [channelId], - }, - limit: 1, - ), - ); - final matching = events - .where( - (e) => - e.channelId == channelId && - EventKind.channelMessageEventKinds.contains(e.kind), - ) - .map((e) => e.toJson()) - .toList(); - return compute(verifiedFilterActivity, matching); - }); - }, retry: (_, _) => null); + return known ? AsyncData(at) : const AsyncLoading(); + }); diff --git a/mobile/lib/features/channels/channel_latest_activity.dart b/mobile/lib/features/channels/channel_latest_activity.dart new file mode 100644 index 00000000000..bc0c10b1587 --- /dev/null +++ b/mobile/lib/features/channels/channel_latest_activity.dart @@ -0,0 +1,84 @@ +part of 'channels_provider.dart'; + +/// When anyone last posted in each channel, self-authored and muted messages +/// included, as the list's reads and live delivery saw it. Unlike +/// [Channel.lastMessageAt], it does not affect unread or sort state. +class _ChannelActivity { + final Map _latest = {}; + final Set _known = {}; + + /// `known` is false until a read or live message covered the channel. + ({bool known, int? at}) lookup(String channelId) => + (known: _known.contains(channelId), at: _latest[channelId]); + + /// Records a latest-message read. A channel it covered is known even when it + /// has no messages; a failed batch or channel stays unknown. + void noteRead( + List requested, + Set failedIds, + List? events, + ) { + if (events == null) return; + for (final channel in requested) { + if (!failedIds.contains(channel.id)) _known.add(channel.id); + } + for (final event in events) { + if (event.channelId case final channelId?) { + note(channelId, event.createdAt); + } + } + } + + void noteLive(String channelId, int createdAt) { + _known.add(channelId); + note(channelId, createdAt); + } + + /// Returns whether [createdAt] advanced the channel's activity. + bool note(String channelId, int createdAt) { + // A future-dated message would keep the channel active indefinitely. + final now = DateTime.now().millisecondsSinceEpoch ~/ 1000; + if (createdAt > now + 300) return false; + final current = _latest[channelId]; + if (current != null && createdAt <= current) return false; + _latest[channelId] = createdAt; + return true; + } + + void clear() { + _latest.clear(); + _known.clear(); + } +} + +extension _ChannelLatestMessages on ChannelsNotifier { + /// Fetches each channel's independent latest-message window in one HTTP + /// bridge request. The relay preserves NIP-01 per-filter limits while + /// executing the filters with bounded concurrency, avoiding an unbounded + /// burst of websocket REQs on communities with many channels. + Future?> _fetchLastMessageEvents( + RelaySessionNotifier session, + List channels, { + Set? failedChannelIds, + }) async { + if (channels.isEmpty) return const []; + + final filters = [ + for (final channel in channels) + NostrFilter( + kinds: EventKind.channelMessageEventKinds, + tags: { + '#h': [channel.id], + }, + limit: channel.channelType == 'dm' ? 1 : 20, + ), + ]; + + return _fetchChannelHistoryBatch( + session, + filters, + operation: 'latest-message query', + failedChannelIds: failedChannelIds, + ); + } +} diff --git a/mobile/lib/features/channels/channel_unread_catch_up.dart b/mobile/lib/features/channels/channel_unread_catch_up.dart index 2448aca4ce4..ea3bcab5345 100644 --- a/mobile/lib/features/channels/channel_unread_catch_up.dart +++ b/mobile/lib/features/channels/channel_unread_catch_up.dart @@ -98,6 +98,7 @@ extension _UnreadCatchUp on ChannelsNotifier { // cross-request tombstones: a large batch can evict its own early IDs. final batchDeletedIds = {}; var appliedDeletion = false; + var activityAdvanced = false; for (final event in events) { final channelId = event.channelId; if (channelById.containsKey(channelId) || @@ -139,6 +140,7 @@ extension _UnreadCatchUp on ChannelsNotifier { _unreadDeletedIdsByChannel[channelId]?.contains(event.id) == true) { continue; } + if (_activity.note(channelId, event.createdAt)) activityAdvanced = true; final readAt = readAtByChannel[channelId]; _noteThreadChannel(event, channelId); if (event.pubkey.toLowerCase() == myPk.toLowerCase()) continue; @@ -193,7 +195,10 @@ extension _UnreadCatchUp on ChannelsNotifier { // from here, so a second check would be dead code. // Recorded events also advance `lastMessageAt`, as live events do: a // deadline-unavailable latest-message batch leaves it unset. - if (recordedAtByChannel.isNotEmpty || appliedDeletion) { + // Newer activity also republishes, so the Active filter sees it. + if (recordedAtByChannel.isNotEmpty || + appliedDeletion || + activityAdvanced) { _publishUnreadCatchUp(recordedAtByChannel); } } catch (error) { diff --git a/mobile/lib/features/channels/channels_page.dart b/mobile/lib/features/channels/channels_page.dart index 8cc95543537..055ea371296 100644 --- a/mobile/lib/features/channels/channels_page.dart +++ b/mobile/lib/features/channels/channels_page.dart @@ -27,7 +27,7 @@ import '../../shared/hosted/organization_sign_in.dart'; import 'dart:async'; import 'dart:convert'; import 'dart:io'; -import 'dart:math' show Random, max, min; +import 'dart:math' show max, min; import 'dart:ui'; import 'package:flutter/foundation.dart'; @@ -58,8 +58,6 @@ import '../../shared/theme/theme.dart'; import '../../shared/widgets/avatar_image.dart'; import '../../shared/widgets/bee_refresh_indicator.dart'; import '../../shared/widgets/buzz_loading_indicator.dart'; -import '../../shared/widgets/buzz_toast.dart'; -import '../../shared/widgets/ios_composition_layer.dart'; import '../../shared/widgets/buzz_setting_menu.dart'; import '../../shared/widgets/buzz_titled_sheet_layout.dart'; import '../../shared/widgets/ios_glass_navigation_action.dart'; @@ -110,7 +108,6 @@ import '../../shared/read_state/read_state_provider.dart'; import '../../shared/read_state/read_state_time.dart'; part 'channels_page/body.dart'; -part 'channels_page/activity_recovery.dart'; part 'channels_page/sections.dart'; part 'channels_page/rename_section.dart'; @@ -624,7 +621,6 @@ class ChannelsPage extends HookConsumerWidget { onRefresh: () async { // Restart the reader for Solo chat and project placement. if (soloSliver != null) ref.invalidate(meGroupsProvider); - ref.invalidate(channelFilterActivityProvider); await Future.wait([ ref.read(channelsProvider.notifier).refresh(), if (ref.read(channelFilterProvider) case final manager?) diff --git a/mobile/lib/features/channels/channels_page/activity_recovery.dart b/mobile/lib/features/channels/channels_page/activity_recovery.dart deleted file mode 100644 index 6a9f5ff1a76..00000000000 --- a/mobile/lib/features/channels/channels_page/activity_recovery.dart +++ /dev/null @@ -1,121 +0,0 @@ -part of '../channels_page.dart'; - -/// Observes route visibility without rebuilding the channel inventory. -class _ActivityRecovery extends HookConsumerWidget { - const _ActivityRecovery({required this.activity, required this.child}); - final Map> activity; - final Widget child; - - @override - Widget build(BuildContext context, WidgetRef ref) { - final activityScope = ( - ref.watch(relayConfigProvider.select((config) => config.baseUrl)), - ref.watch(myPubkeyProvider), - ); - final notice = useMemoized(() => ObjectRef(null), [ - activityScope, - ]); - final failures = useMemoized(() => {}, [activityScope]); - final automaticAttempts = useMemoized(() => ObjectRef(0), [activityScope]); - failures.retainAll(activity.keys); - for (final MapEntry(key: id, value: status) in activity.entries) { - if (status.hasError) { - failures.add(id); - } else if (!status.isLoading) { - failures.remove(id); - } - } - final unavailable = failures.isNotEmpty; - if (!unavailable) automaticAttempts.value = 0; - - bool retryFailures() { - var started = false; - for (final id in failures.toList()) { - final provider = channelFilterActivityProvider(id); - if (ref.read(provider).isLoading) continue; - ref.invalidate(provider); - started = true; - } - return started; - } - - // Compact detail routes cover the list within Home's route and mute its - // tickers. On wide layouts the Messages list stays visible beside detail. - final listOnstage = TickerMode.valuesOf(context).enabled; - final routeCurrent = ModalRoute.isCurrentOf(context) ?? true; - final messagesVisible = listOnstage && routeCurrent; - final canRetry = - messagesVisible && - ref.watch(appLifecycleProvider) == AppLifecycleState.resumed && - ref.watch(relaySessionProvider.select((s) => s.status)) == - SessionStatus.connected; - final hasRetryableFailure = failures.any((id) => !activity[id]!.isLoading); - final attempts = automaticAttempts.value; - useEffect(() { - if (!hasRetryableFailure || !canRetry || attempts >= 3) return null; - // Back off over consecutive failed rounds and stagger relay traffic - // across clients. Wait for a failed lookup before scheduling a round. - final delayMs = 10_000 * (1 << attempts); - final spread = delayMs ~/ 5; - final timer = Timer( - Duration(milliseconds: delayMs - spread + Random().nextInt(2 * spread)), - () { - if (retryFailures()) automaticAttempts.value++; - }, - ); - return timer.cancel; - }, [hasRetryableFailure, canRetry, automaticAttempts, attempts]); - - void removeNotice() { - notice.value?.remove(); - notice.value?.dispose(); - notice.value = null; - } - - useEffect(() => removeNotice, [notice]); - useEffect(() { - if (!messagesVisible) { - removeNotice(); - return null; - } - var active = true; - WidgetsBinding.instance.addPostFrameCallback((_) { - if (!active || !context.mounted) return; - if (notice.value != null) { - notice.value!.markNeedsBuild(); - } else if (unavailable) { - late final OverlayEntry entry; - entry = OverlayEntry( - builder: (_) => IosCompositionLayer( - child: Stack( - children: [ - ReconnectCapsule( - visible: failures.isNotEmpty, - message: 'Some activity couldn’t load', - actionLabel: 'Retry', - onHidden: () { - WidgetsBinding.instance.addPostFrameCallback((_) { - if (identical(notice.value, entry) && - failures.isEmpty) { - removeNotice(); - } - }); - }, - onSignIn: () { - if (!context.mounted) return; - retryFailures(); - }, - ), - ], - ), - ), - ); - notice.value = entry; - Overlay.of(context, rootOverlay: true).insert(entry); - } - }); - return () => active = false; - }, [notice, unavailable, messagesVisible]); - return child; - } -} diff --git a/mobile/lib/features/channels/channels_page/body.dart b/mobile/lib/features/channels/channels_page/body.dart index 8d449e7d204..7cd2e47ca14 100644 --- a/mobile/lib/features/channels/channels_page/body.dart +++ b/mobile/lib/features/channels/channels_page/body.dart @@ -570,13 +570,12 @@ class _SliverChannelsList extends HookConsumerWidget { ], ], ); - final content = _ActivityRecovery(activity: activity, child: inventory); - if (orderKey == null) return content; + if (orderKey == null) return inventory; return SectionOrderScope( labels: labels, defaultOrder: keys, storageKey: orderKey, - child: content, + child: inventory, ); } } diff --git a/mobile/lib/features/channels/channels_provider.dart b/mobile/lib/features/channels/channels_provider.dart index 2dc48331b44..6febe74f1af 100644 --- a/mobile/lib/features/channels/channels_provider.dart +++ b/mobile/lib/features/channels/channels_provider.dart @@ -36,6 +36,7 @@ part 'channels_provider_lifecycle.dart'; part 'channel_list_changes.dart'; part 'channels_provider_joins.dart'; part 'channel_thread_follows.dart'; +part 'channel_latest_activity.dart'; const _channelTypeOrder = {'stream': 0, 'forum': 1, 'dm': 2}; const _unreadCatchUpLimit = 1000; @@ -105,12 +106,11 @@ class ChannelsNotifier extends AsyncNotifier> { bool _listNoticesReopened = false; final Set _reopenedLiveChunks = {}; final Map _latestObservedByChannel = {}; - final Map _activityRevisions = {}; + final _activity = _ChannelActivity(); - /// Invalidates verified activity reads for every live message, including - /// self-authored or muted messages, without changing unread or sort state. - int activityRevisionFor(String channelId) => - _activityRevisions[channelId] ?? 0; + /// When anyone last posted in [channelId]; see [_ChannelActivity]. + ({bool known, int? at}) activityFor(String channelId) => + _activity.lookup(channelId); // Per channel, when an earlier unread catch-up in this session was sent, and // the read timestamp and mute state it used. See [_UnreadCatchUp]. @@ -331,7 +331,7 @@ class ChannelsNotifier extends AsyncNotifier> { _refreshCoordinator.retireInFlight(); _clearLiveSubscriptions(); _latestObservedByChannel.clear(); - _activityRevisions.clear(); + _activity.clear(); _observedUnreadEventsByChannel.clear(); _observedUnreadRevisions.touchAll(); _unreadCaughtUpByChannel.clear(); @@ -514,13 +514,19 @@ class ChannelsNotifier extends AsyncNotifier> { for (final c in state.value ?? const []) if (c.isMember) c.id, }; - final latestEventsRequest = _fetchLastMessageEvents(session, [ + final latestRequested = [ for (final entry in visibleMetas) if (memberChannelIds.contains(entry.value.id) && !entry.value.isArchived && !listed.contains(entry.value.id)) entry.value, - ]); + ]; + final latestFailedIds = {}; + final latestEventsRequest = _fetchLastMessageEvents( + session, + latestRequested, + failedChannelIds: latestFailedIds, + ); final (displayNames, latestEvents) = await _fenced(fence, () async { await Future.wait(eagerError: true, [ displayNamesRequest, @@ -586,6 +592,7 @@ class ChannelsNotifier extends AsyncNotifier> { } } } + _activity.noteRead(latestRequested, latestFailedIds, events); final mutedChannelIds = _mutedChannelIds(); for (final event in events ?? const []) { final channelId = event.channelId; @@ -687,34 +694,6 @@ class ChannelsNotifier extends AsyncNotifier> { return channels; } - /// Fetches each channel's independent latest-message window in one HTTP - /// bridge request. The relay preserves NIP-01 per-filter limits while - /// executing the filters with bounded concurrency, avoiding an unbounded - /// burst of websocket REQs on communities with many channels. - Future?> _fetchLastMessageEvents( - RelaySessionNotifier session, - List channels, - ) async { - if (channels.isEmpty) return const []; - - final filters = [ - for (final channel in channels) - NostrFilter( - kinds: EventKind.channelMessageEventKinds, - tags: { - '#h': [channel.id], - }, - limit: channel.channelType == 'dm' ? 1 : 20, - ), - ]; - - return _fetchChannelHistoryBatch( - session, - filters, - operation: 'latest-message query', - ); - } - /// Returns null when the batch is unavailable: it hit a relay deadline. /// Adds to [failedChannelIds] the channels whose fallback filter failed /// another way; the result has none of their events. @@ -914,7 +893,9 @@ class ChannelsNotifier extends AsyncNotifier> { } final updated = List.of(channels); final channel = updated[idx]; - _activityRevisions[channelId] = activityRevisionFor(channelId) + 1; + if (EventKind.channelMessageEventKinds.contains(event.kind)) { + _activity.noteLive(channelId, event.createdAt); + } _noteThreadEvent(event, channel.id, myPk); @@ -995,7 +976,6 @@ class ChannelsNotifier extends AsyncNotifier> { void clearObservedUnreadForChannel(String channelId) { _latestObservedByChannel.remove(channelId); - _activityRevisions.remove(channelId); if (_observedUnreadEventsByChannel.remove(channelId) != null) { _observedUnreadRevisions.touch(channelId); } diff --git a/mobile/lib/features/channels/me/me_page.dart b/mobile/lib/features/channels/me/me_page.dart index 82338885985..bcfe8d1b671 100644 --- a/mobile/lib/features/channels/me/me_page.dart +++ b/mobile/lib/features/channels/me/me_page.dart @@ -370,7 +370,6 @@ class MePage extends HookConsumerWidget { edgeOffset: topSectionHeight, onRefresh: () async { ref.invalidate(meGroupsProvider); - ref.invalidate(channelFilterActivityProvider); await Future.wait([ ref.read(channelsProvider.notifier).refresh(), if (ref.read(soloFilterProvider) case final manager?) diff --git a/mobile/test/features/channels/channel_filter/channel_filter_test.dart b/mobile/test/features/channels/channel_filter/channel_filter_test.dart index 21901580e02..25c119c3b03 100644 --- a/mobile/test/features/channels/channel_filter/channel_filter_test.dart +++ b/mobile/test/features/channels/channel_filter/channel_filter_test.dart @@ -1,11 +1,7 @@ -import 'package:buzz/features/channels/channels_provider.dart'; -import 'package:buzz/features/channels/channel.dart'; -import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'dart:async'; import 'dart:convert'; import 'package:buzz/features/channels/channel_filter/channel_filter_manager.dart'; import 'package:buzz/features/channels/channel_filter/channel_filter_store.dart'; -import 'package:buzz/features/channels/channel_filter/channel_filter_activity.dart'; import 'package:buzz/features/channels/channel_sort/channel_sort_manager.dart'; import 'package:buzz/shared/relay/relay.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -13,81 +9,6 @@ import 'package:nostr/nostr.dart' as nostr; void main() { TestWidgetsFlutterBinding.ensureInitialized(); - test( - 'activity provider reads history and excludes a quiet channel from Active', - () async { - final keys = nostr.Keys.generate(); - final old = DateTime.now().millisecondsSinceEpoch ~/ 1000 - 8 * 86400; - NostrEvent activityEvent(int at) => NostrEvent.fromJson( - nostr.Event.from( - kind: EventKind.streamMessage, - tags: [ - ['h', 'quiet-channel'], - ], - content: 'Earlier activity', - secretKey: nostr.Nip19.decode(payload: keys.nsec).data, - createdAt: at, - verify: false, - ).toMap(), - ); - final session = _ActivitySession(activityEvent(old)); - final container = ProviderContainer( - overrides: [ - channelsProvider.overrideWith(_ActivityChannels.new), - relaySessionProvider.overrideWith(() => session), - ], - ); - addTearDown(container.dispose); - await container.read(channelsProvider.future); - final subscription = container.listen( - channelFilterActivityProvider('quiet-channel'), - (_, _) {}, - ); - addTearDown(subscription.close); - final activity = await container.read( - channelFilterActivityProvider('quiet-channel').future, - ); - expect(session.requests, 1); - expect(activity, old); - expect( - channelMatchesFilter( - mode: ChannelFilterMode.active, - unread: false, - activitySeconds: activity, - activityKnown: true, - nowSeconds: old + 8 * 86400, - windowSeconds: 7 * 86400, - ), - isFalse, - ); - // A relay-accepted future head must stay unknown, not become known empty. - for (final drift in [450, 600, 900]) { - session.event = activityEvent( - DateTime.now().millisecondsSinceEpoch ~/ 1000 + drift, - ); - container.invalidate(channelFilterActivityProvider('quiet-channel')); - await expectLater( - container.read(channelFilterActivityProvider('quiet-channel').future), - throwsFormatException, - ); - final evidence = container.read( - channelFilterActivityProvider('quiet-channel'), - ); - expect(evidence.hasError, isTrue); - expect( - channelMatchesFilter( - mode: ChannelFilterMode.active, - unread: false, - activitySeconds: evidence.asData?.value, - activityKnown: evidence.asData != null, - nowSeconds: old + 8 * 86400, - windowSeconds: 7 * 86400, - ), - isTrue, - ); - } - }, - ); test( 'metadata wins and edits preserve reset tombstones, unknown fields and windows', @@ -414,36 +335,4 @@ void main() { expect(subject.ready, isFalse); }, ); - test('activity accepts verified events and rejects a forged timestamp', () { - final event = signed('activity', 100); - expect(verifiedFilterActivity([event.toJson()]), 100); - expect( - () => verifiedFilterActivity([ - {...event.toJson(), 'created_at': 200}, - ]), - throwsA(anything), - ); - expect(verifiedFilterActivity([]), isNull); - }); -} - -class _ActivityChannels extends ChannelsNotifier { - @override - Future> build() async => []; -} - -class _ActivitySession extends RelaySessionNotifier { - _ActivitySession(this.event); - NostrEvent event; - int requests = 0; - @override - SessionState build() => const SessionState(status: SessionStatus.connected); - @override - Future> fetchHistory( - NostrFilter filter, { - Duration timeout = const Duration(seconds: 8), - }) async { - requests++; - return [event]; - } } diff --git a/mobile/test/features/channels/channels_page_test.dart b/mobile/test/features/channels/channels_page_test.dart index 8839ac34643..998d7210489 100644 --- a/mobile/test/features/channels/channels_page_test.dart +++ b/mobile/test/features/channels/channels_page_test.dart @@ -1,7 +1,6 @@ import 'package:buzz/shared/widgets/ios_glass_navigation_button.dart'; import 'package:buzz/shared/widgets/app_list.dart'; import 'package:buzz/shared/widgets/adaptive_conversation_pane.dart'; -import 'package:buzz/features/channels/channel_filter/channel_filter_activity.dart'; import 'package:buzz/features/channels/channel_filter/channel_filter_provider.dart'; import 'package:buzz/features/channels/channel_filter/channel_filter_manager.dart'; import 'package:buzz/features/channels/channel_filter/channel_filter_store.dart'; diff --git a/mobile/test/features/channels/channels_page_test/filter_feedback_tests.dart b/mobile/test/features/channels/channels_page_test/filter_feedback_tests.dart index 978f10a58f7..775bf92990e 100644 --- a/mobile/test/features/channels/channels_page_test/filter_feedback_tests.dart +++ b/mobile/test/features/channels/channels_page_test/filter_feedback_tests.dart @@ -4,141 +4,6 @@ void _filterFeedbackTests( Widget Function({required List overrides, bool realHome}) buildTestable, ) { - for (final coverMessages in [true, false]) { - testWidgets( - 'activity retry ${coverMessages ? 'pauses while Messages is covered' : 'backs off and leaves manual recovery available'}', - (tester) async { - tester.view.devicePixelRatio = 1; - tester.view.physicalSize = const Size(390, 844); - addTearDown(tester.view.reset); - SharedPreferences.setMockInitialValues({}); - final prefs = await SharedPreferences.getInstance(); - final haptics = []; - final messenger = tester.binding.defaultBinaryMessenger; - messenger.setMockMethodCallHandler(SystemChannels.platform, ( - call, - ) async { - if (call.method == 'HapticFeedback.vibrate' && - call.arguments == 'HapticFeedbackType.heavyImpact') { - haptics.add(call); - } - return null; - }); - addTearDown( - () => - messenger.setMockMethodCallHandler(SystemChannels.platform, null), - ); - final manager = _filterTestManager( - cached: ChannelFilterStore.empty().edit( - 'channels', - ChannelFilterMode.active, - ), - ); - final initialRead = Completer(); - final recovery = Completer(); - var canRecover = false; - final reads = {}; - const ids = ['failed', 'loaded']; - await tester.pumpWidget( - buildTestable( - realHome: coverMessages, - overrides: [ - savedPrefsProvider.overrideWithValue(prefs), - channelsProvider.overrideWith( - () => _FakeNotifier([ - for (final id in ids) - Channel( - id: id, - name: id, - channelType: 'stream', - visibility: 'open', - description: '', - memberCount: 1, - createdBy: 'pk', - createdAt: DateTime(2025), - isMember: true, - ), - ]), - ), - relaySessionProvider.overrideWith( - () => _ReconnectingRelaySession( - initialStatus: SessionStatus.connected, - ), - ), - channelFilterProvider.overrideWithValue(manager), - for (final id in ids) - channelFilterActivityProvider(id).overrideWith((_) async { - final count = reads.update( - id, - (value) => value + 1, - ifAbsent: () => 1, - ); - if (id == 'failed') { - if (count == 1) return initialRead.future; - if (canRecover) return recovery.future; - throw StateError('Activity unavailable'); - } - return DateTime.now().millisecondsSinceEpoch ~/ 1000; - }), - ], - ), - ); - await tester.pumpAndSettle(); - final notice = find.text('Some activity couldn’t load'); - if (coverMessages) { - await tester.tap(find.byTooltip('Search')); - await tester.pumpAndSettle(); - } - initialRead.completeError(StateError('Activity unavailable')); - await tester.pumpAndSettle(); - if (coverMessages) { - await tester.pump(const Duration(seconds: 13)); - expect(notice, findsNothing); - expect(reads['failed'], 1); - await tester.tap(find.byKey(const Key('search-close'))); - await tester.pumpAndSettle(); - expect(notice, findsOneWidget); - - await tester.tap(find.text('loaded')); - await tester.pumpAndSettle(); - await tester.pump(const Duration(seconds: 13)); - expect(notice, findsNothing); - expect(reads['failed'], 1); - await tester.binding.handlePopRoute(); - await tester.pumpAndSettle(); - } else { - for (final (index, minimum, maximum) in [ - (0, 8, 12), - (1, 16, 24), - (2, 32, 48), - ]) { - await tester.pump(Duration(seconds: minimum - 1)); - expect(reads['failed'], index + 1); - await tester.pump(Duration(seconds: maximum - minimum + 1)); - await tester.pumpAndSettle(); - expect(reads['failed'], index + 2); - } - await tester.pump(const Duration(minutes: 2)); - await tester.pumpAndSettle(); - expect(reads['failed'], 4); - } - expect(find.text('failed'), findsOneWidget); - expect(notice, findsOneWidget); - expect(haptics, isEmpty); - canRecover = true; - await tester.tap(find.text('Retry')); - await tester.pumpAndSettle(); - expect(notice, findsOneWidget); - recovery.complete(DateTime.now().millisecondsSinceEpoch ~/ 1000); - await tester.pumpAndSettle(); - expect(reads, {'failed': coverMessages ? 2 : 5, 'loaded': 1}); - expect(notice, findsNothing); - await tester.pumpWidget(const SizedBox()); - manager.dispose(); - }, - ); - } - testWidgets( 'deleting an unfiltered section persists offline without filter publication', (tester) async { diff --git a/mobile/test/features/channels/channels_provider_test.dart b/mobile/test/features/channels/channels_provider_test.dart index 5426b657638..d7c11d1178c 100644 --- a/mobile/test/features/channels/channels_provider_test.dart +++ b/mobile/test/features/channels/channels_provider_test.dart @@ -22,6 +22,7 @@ import 'package:buzz/features/channels/channel_management_provider.dart'; import 'package:buzz/features/channels/channel_mutes/channel_mutes_provider.dart'; import 'package:buzz/features/channels/channel_mutes/channel_mutes_storage.dart'; import 'package:buzz/features/channels/channel_unread.dart'; +import 'package:buzz/features/channels/channel_filter/channel_filter_activity.dart'; import 'package:buzz/features/channels/channels_provider.dart'; import 'package:buzz/features/channels/thread_follows/thread_follows_provider.dart'; import 'package:buzz/features/channels/unread_badge/observed_unread_event.dart'; @@ -2491,48 +2492,111 @@ void main() { }); test( - 'self-authored activity invalidates filter reads without changing unread or sort time', + 'Active filter activity comes from the list reads and live delivery alone', () async { + final now = DateTime.now().millisecondsSinceEpoch ~/ 1000; + NostrEvent message(String id, int at, {String channel = _channelA}) => + NostrEvent( + id: id, + pubkey: myPk, + createdAt: at, + kind: EventKind.streamMessageV2, + tags: [ + ['h', channel], + ], + content: 'my own activity', + sig: 'sig', + ); final session = _FakeRelaySession( - memberships: [_membership(_channelA, myPk)], - metadata: [_meta(id: _channelA, name: 'general')], + memberships: [ + _membership(_channelA, myPk), + _membership(_channelB, myPk), + ], + metadata: [ + _meta(id: _channelA, name: 'general'), + _meta(id: _channelB, name: 'quiet'), + ], + // Self-authored activity counts; a future-dated head does not. + recentMessages: [ + message('mine', now - 100), + message('ahead', now + 900), + ], ); final container = _buildContainer(session: session); addTearDown(container.dispose); await container.read(channelsProvider.future); - final notifier = container.read(channelsProvider.notifier); + await _settle(); + AsyncValue activity(String id) => + container.read(channelFilterActivityProvider(id)); + final listeners = [ + for (final id in [_channelA, _channelB]) + container.listen(channelFilterActivityProvider(id), (_, _) {}), + ]; + addTearDown(() { + for (final listener in listeners) { + listener.close(); + } + }); + final reads = ( + session.historyFilters.length, + session.queryBatches.length, + ); final before = container .read(channelsProvider) .requireValue - .single + .singleWhere((channel) => channel.id == _channelA) .lastMessageAt; - final versions = []; - final listener = container.listen( - channelsProvider.select((_) => notifier.activityRevisionFor(_channelA)), - (_, next) => versions.add(next), + expect(activity(_channelA).value, now - 100); + expect(activity(_channelB), isA>()); + expect(activity(_channelB).value, isNull); + + session.emit(message('live', now - 10)); + await _settle(); + expect(activity(_channelA).value, now - 10); + // Activity leaves unread and sort time alone, and costs no relay reads. + expect( + container + .read(channelsProvider) + .requireValue + .singleWhere((channel) => channel.id == _channelA) + .lastMessageAt, + before, ); - addTearDown(listener.close); - session.emit( - const NostrEvent( - id: 'self-message', - pubkey: myPk, - createdAt: 20, - kind: EventKind.streamMessageV2, - tags: [ - ['h', _channelA], - ], - content: 'my own activity', - sig: 'sig', - ), + expect(( + session.historyFilters.length, + session.queryBatches.length, + ), reads); + + // A reconnect reads only new channels' latest messages; the unread + // catch-up it already sends covers listed ones. + session.recentMessages = [ + ...session.recentMessages, + message('missed', now - 5), + ]; + final batches = session.queryBatches.length; + session.setStatus(SessionStatus.reconnecting); + session.setStatus(SessionStatus.connected); + await _waitUntil( + () => session.queryBatches + .skip(batches) + .any((batch) => batch.every((filter) => filter.since != null)), ); await _settle(); - expect(versions, [1]); - expect(notifier.latestObservedByChannel[_channelA], isNull); + expect(activity(_channelA).value, now - 5); + + // A list read that never answered leaves activity unknown, not empty. + final failed = _FakeRelaySession( + memberships: [_membership(_channelA, myPk)], + metadata: [_meta(id: _channelA, name: 'general')], + )..latestBatchError = RelayException(503, '{"error":"query timed out"}'); + final cold = _buildContainer(session: failed); + addTearDown(cold.dispose); + await cold.read(channelsProvider.future); + await _settle(); expect( - container.read(channelsProvider).requireValue.single.lastMessageAt, - before, + cold.read(channelFilterActivityProvider(_channelA)).isLoading, + isTrue, ); - expect(notifier.activityRevisionFor(_channelB), 0); }, ); diff --git a/mobile/test/features/channels/me/me_page_test.dart b/mobile/test/features/channels/me/me_page_test.dart index 3ce2e269e0e..01340898587 100644 --- a/mobile/test/features/channels/me/me_page_test.dart +++ b/mobile/test/features/channels/me/me_page_test.dart @@ -171,11 +171,12 @@ void main() { channelsProvider.overrideWith(LabeledChannels.new), soloFilterProvider.overrideWithValue(manager), channelFilterActivityProvider('filed').overrideWith( - (ref) async => - DateTime.now() - .subtract(const Duration(days: 8)) - .millisecondsSinceEpoch ~/ - 1000, + (ref) => AsyncData( + DateTime.now() + .subtract(const Duration(days: 8)) + .millisecondsSinceEpoch ~/ + 1000, + ), ), meGroupsProvider.overrideWith( (ref) => Stream.value( diff --git a/mobile/test/features/channels/sidebar_workspaces_journey_test.dart b/mobile/test/features/channels/sidebar_workspaces_journey_test.dart index 7a9ec12f74f..e454d8ed348 100644 --- a/mobile/test/features/channels/sidebar_workspaces_journey_test.dart +++ b/mobile/test/features/channels/sidebar_workspaces_journey_test.dart @@ -50,7 +50,6 @@ void main() { relay.session.debugRelayUrl = 'https://relay.example'; _viewer = relay.pubkey; final now = DateTime.now().millisecondsSinceEpoch ~/ 1000; - var soloActivityReads = 0; // Before desktop's unified record, the legacy Team sections apply. relay.stored.add( relay.event('channel-sections', { @@ -80,12 +79,9 @@ void main() { ), ), channelsProvider.overrideWith(_Channels.new), - channelFilterActivityProvider('solo-scratch').overrideWith((_) async { - if (soloActivityReads++ == 0) { - throw StateError('Activity unavailable'); - } - return now - 8 * 86400; - }), + channelFilterActivityProvider( + 'solo-scratch', + ).overrideWith((_) => AsyncData(now - 8 * 86400)), meGroupsProvider.overrideWith( (ref) => Stream.value( const MeGroups( @@ -323,11 +319,6 @@ void main() { await tester.pumpAndSettle(); await tester.tap(find.byTooltip('Apply changes')); await tester.pumpAndSettle(); - expect(inList('solo-scratch'), findsOneWidget); - expect(find.text('Some activity couldn’t load'), findsOneWidget); - await tester.tap(find.text('Retry')); - await tester.pumpAndSettle(); - expect(find.text('Some activity couldn’t load'), findsNothing); expect(inList('solo-scratch'), findsNothing); expect(inList('solo-plan'), findsOneWidget); await tester.tap(find.byTooltip('Solo options')); From 4ea3a05e93777432320f322bbb416925b82874a0 Mon Sep 17 00:00:00 2001 From: Tom Brow Date: Fri, 20 Nov 2026 13:13:30 -0800 Subject: [PATCH 2/7] fix(mobile): keep Active filter activity correct across reconnects, rebuilds, deletions and clock skew A reconnect catch-up that starts after posts no read has covered asks for the newest one with a limit-1 filter in the same batch, bounded by when a read last covered the channel. A failed catch-up leaves activity unknown until one succeeds. A same-community rebuild keeps activity. A successful latest read replaces the stored time, keeping arrivals newer than the read. A future-dated post counts as now. Signed-off-by: Tom Brow --- .../channels/channel_latest_activity.dart | 123 +++++++++++++-- .../channels/channel_unread_catch_up.dart | 81 ++++++++-- .../features/channels/channels_provider.dart | 5 +- .../channels/channels_provider_test.dart | 141 +++++++++++++++++- 4 files changed, 317 insertions(+), 33 deletions(-) diff --git a/mobile/lib/features/channels/channel_latest_activity.dart b/mobile/lib/features/channels/channel_latest_activity.dart index bc0c10b1587..edb7e650454 100644 --- a/mobile/lib/features/channels/channel_latest_activity.dart +++ b/mobile/lib/features/channels/channel_latest_activity.dart @@ -6,25 +6,85 @@ part of 'channels_provider.dart'; class _ChannelActivity { final Map _latest = {}; final Set _known = {}; + // Channels whose last unread catch-up failed: a post may have arrived while + // disconnected that no read has seen yet. + final Set _catchUpFailed = {}; + // Per latest-message read in flight, the newest arrival in each channel + // since it began. A read that never reports, such as a superseded refresh, + // is dropped once newer reads begin. + final Map> _arrivalsByRead = {}; + final Map _readSentAt = {}; + int _nextRead = 0; + // Per channel, when the last read that covered it (a successful latest read + // or unread catch-up) was sent. Unlike the unread cursor, only [clear] + // resets it, so the catch-up can always ask for posts since then. + final Map _coveredAt = {}; - /// `known` is false until a read or live message covered the channel. - ({bool known, int? at}) lookup(String channelId) => - (known: _known.contains(channelId), at: _latest[channelId]); + /// When the last read that covered [channelId] was sent, if any. + int? coveredAt(String channelId) => _coveredAt[channelId]; + + /// `known` is false until a read or live message covered the channel, and + /// while its last unread catch-up failed. + ({bool known, int? at}) lookup(String channelId) => ( + known: _known.contains(channelId) && !_catchUpFailed.contains(channelId), + at: _latest[channelId], + ); + + /// Starts a latest-message read; pass the result to [noteRead]. + int beginRead() { + final read = _nextRead++; + _arrivalsByRead[read] = {}; + _readSentAt[read] = currentUnixSeconds(); + if (_arrivalsByRead.length > 8) { + final oldest = _arrivalsByRead.keys.first; + _arrivalsByRead.remove(oldest); + _readSentAt.remove(oldest); + } + return read; + } /// Records a latest-message read. A channel it covered is known even when it - /// has no messages; a failed batch or channel stays unknown. + /// has no messages, and its activity becomes the read's newest message or a + /// newer arrival since the read began, so a deleted latest post stops + /// counting. A failed batch or channel stays unknown. void noteRead( + int read, List requested, Set failedIds, List? events, ) { + final arrivals = _arrivalsByRead.remove(read); + final sentAt = _readSentAt.remove(read); if (events == null) return; - for (final channel in requested) { - if (!failedIds.contains(channel.id)) _known.add(channel.id); - } + final newest = {}; for (final event in events) { if (event.channelId case final channelId?) { - note(channelId, event.createdAt); + final at = _clamp(event.createdAt); + newest.update(channelId, (v) => max(v, at), ifAbsent: () => at); + } + } + final covered = {}; + for (final channel in requested) { + if (failedIds.contains(channel.id)) continue; + covered.add(channel.id); + _known.add(channel.id); + if (sentAt != null) _coveredAt[channel.id] = sentAt; + // Without its arrivals (dropped), only raise, as a live event would. + if (arrivals == null) continue; + final read = newest[channel.id]; + final arrived = arrivals[channel.id]; + final at = read == null || arrived == null + ? read ?? arrived + : max(read, arrived); + if (at == null) { + _latest.remove(channel.id); + } else { + _latest[channel.id] = at; + } + } + for (final MapEntry(key: channelId, value: at) in newest.entries) { + if (arrivals == null || !covered.contains(channelId)) { + _raise(channelId, at); } } } @@ -34,20 +94,55 @@ class _ChannelActivity { note(channelId, createdAt); } - /// Returns whether [createdAt] advanced the channel's activity. + /// Records an unread catch-up's outcome per channel. Returns whether that + /// changed which channels are known. + /// [sentAt] is when a catch-up that answered was sent. + bool noteCatchUp( + Iterable channelIds, + Set failedIds, { + int? sentAt, + }) { + var changed = false; + for (final channelId in channelIds) { + if (failedIds.contains(channelId)) { + changed |= _catchUpFailed.add(channelId); + continue; + } + changed |= _catchUpFailed.remove(channelId); + if (sentAt != null) _coveredAt[channelId] = sentAt; + } + return changed; + } + + /// Records a message that arrived live or by catch-up. Returns whether it + /// advanced the channel's activity. bool note(String channelId, int createdAt) { - // A future-dated message would keep the channel active indefinitely. - final now = DateTime.now().millisecondsSinceEpoch ~/ 1000; - if (createdAt > now + 300) return false; + final at = _clamp(createdAt); + for (final arrivals in _arrivalsByRead.values) { + arrivals.update(channelId, (v) => max(v, at), ifAbsent: () => at); + } + return _raise(channelId, at); + } + + bool _raise(String channelId, int at) { final current = _latest[channelId]; - if (current != null && createdAt <= current) return false; - _latest[channelId] = createdAt; + if (current != null && at <= current) return false; + _latest[channelId] = at; return true; } + // The relay accepts posts up to 900s ahead of its clock. Count one as now + // rather than drop it: it is still the channel's newest activity. + static int _clamp(int createdAt) => + min(createdAt, DateTime.now().millisecondsSinceEpoch ~/ 1000); + void clear() { _latest.clear(); _known.clear(); + _catchUpFailed.clear(); + _arrivalsByRead.clear(); + _readSentAt.clear(); + _coveredAt.clear(); } } diff --git a/mobile/lib/features/channels/channel_unread_catch_up.dart b/mobile/lib/features/channels/channel_unread_catch_up.dart index ea3bcab5345..a214dd29743 100644 --- a/mobile/lib/features/channels/channel_unread_catch_up.dart +++ b/mobile/lib/features/channels/channel_unread_catch_up.dart @@ -22,6 +22,7 @@ extension _UnreadCatchUp on ChannelsNotifier { if (!_lifecycleRef.mounted) return; // A catch-up that misses channels is retried, as a list read. var incomplete = false; + var catchUpChannelIds = const {}; try { final myPk = _lifecycleRef.read(myPubkeyProvider); if (myPk == null) return; @@ -36,6 +37,7 @@ extension _UnreadCatchUp on ChannelsNotifier { final channelById = { for (final channel in activeChannels) channel.id: channel, }; + catchUpChannelIds = channelById.keys.toSet(); final readAtByChannel = { for (final channel in activeChannels) channel.id: readState.effectiveTimestamp(channel.id), @@ -45,6 +47,14 @@ extension _UnreadCatchUp on ChannelsNotifier { for (final channel in activeChannels) channel.id: _unreadCaughtUpByChannel[channel.id], }; + final sinceByChannel = { + for (final channel in activeChannels) + channel.id: _unreadCatchUpSince( + channel.id, + readAtByChannel[channel.id], + muted: mutedChannelIds.contains(channel.id), + ), + }; final filters = [ for (final channel in activeChannels) for (final kinds in [ @@ -56,13 +66,23 @@ extension _UnreadCatchUp on ChannelsNotifier { tags: { '#h': [channel.id], }, - since: _unreadCatchUpSince( - channel.id, - readAtByChannel[channel.id], - muted: mutedChannelIds.contains(channel.id), - ), + since: sinceByChannel[channel.id], limit: _unreadCatchUpLimit, ), + for (final channel in activeChannels) + if (_activityGap(channel.id, sinceByChannel[channel.id]!) case ( + :final since, + :final until, + )) + NostrFilter( + kinds: EventKind.channelMessageEventKinds, + tags: { + '#h': [channel.id], + }, + since: since, + until: until, + limit: 1, + ), ]; final failedChannelIds = {}; @@ -72,11 +92,6 @@ extension _UnreadCatchUp on ChannelsNotifier { operation: 'unread catch-up', failedChannelIds: failedChannelIds, ); - // Unavailable: keep the unread state already observed. - if (events == null) { - incomplete = true; - return; - } // The relay round-trip above is the window Jed's probes park in: a newer // refresh, a community switch or an identity switch here means every // write below belongs to a channel list the user has left. @@ -84,6 +99,15 @@ extension _UnreadCatchUp on ChannelsNotifier { _isCatchUpRetired(fence, subscriptionGeneration)) { return; } + // Unavailable: keep the unread state already observed, but not the + // activity, which may have missed a post. + if (events == null) { + incomplete = true; + if (_activity.noteCatchUp(catchUpChannelIds, catchUpChannelIds)) { + _publishUnreadCatchUp(const {}); + } + return; + } _recordUnreadCaughtUp( readAtByChannel, @@ -134,14 +158,22 @@ extension _UnreadCatchUp on ChannelsNotifier { if (channelId == null) continue; final channel = channelById[channelId]; if (channel == null) continue; - windowSizeByChannel.update(channelId, (n) => n + 1, ifAbsent: () => 1); + final readAt = readAtByChannel[channelId]; + // Read posts (the gap filter's among them) say nothing about whether + // the window hides an older unread one. + if (readAt == null || event.createdAt > readAt) { + windowSizeByChannel.update( + channelId, + (n) => n + 1, + ifAbsent: () => 1, + ); + } if (batchDeletedIds.contains(event.id) || _unscopedUnreadDeletedIds.contains(event.id) || _unreadDeletedIdsByChannel[channelId]?.contains(event.id) == true) { continue; } if (_activity.note(channelId, event.createdAt)) activityAdvanced = true; - final readAt = readAtByChannel[channelId]; _noteThreadChannel(event, channelId); if (event.pubkey.toLowerCase() == myPk.toLowerCase()) continue; if (readAt != null && event.createdAt <= readAt) continue; @@ -196,15 +228,26 @@ extension _UnreadCatchUp on ChannelsNotifier { // Recorded events also advance `lastMessageAt`, as live events do: a // deadline-unavailable latest-message batch leaves it unset. // Newer activity also republishes, so the Active filter sees it. + // So does a channel whose activity became known or unknown. + final coverageChanged = _activity.noteCatchUp( + catchUpChannelIds, + failedChannelIds, + sentAt: requestedAt, + ); if (recordedAtByChannel.isNotEmpty || appliedDeletion || - activityAdvanced) { + activityAdvanced || + coverageChanged) { _publishUnreadCatchUp(recordedAtByChannel); } } catch (error) { incomplete = true; if (!_lifecycleRef.mounted) return; debugPrint('[ChannelsNotifier] unread catch-up failed: $error'); + if (!_isCatchUpRetired(fence, subscriptionGeneration) && + _activity.noteCatchUp(catchUpChannelIds, catchUpChannelIds)) { + _publishUnreadCatchUp(const {}); + } } finally { // Optional history can terminate without data. Release this destination // with its retained unread state, but never settle a retired request. @@ -249,6 +292,18 @@ extension _UnreadCatchUp on ChannelsNotifier { return max(afterRead, caughtUp.sentAt - relayCrossDeviceSkewSeconds); } + /// The stretch a catch-up starting at [since] skips since the last read + /// that covered the channel, for the Active filter: a post that arrived + /// while disconnected and was read on another device lies there, as does one + /// before an unread cursor reset. The catch-up asks for its newest post. + /// Null when no read covered the channel yet or nothing is skipped. + ({int since, int until})? _activityGap(String channelId, int since) { + final coveredAt = _activity.coveredAt(channelId); + if (coveredAt == null) return null; + final from = coveredAt - relayCrossDeviceSkewSeconds; + return since > from ? (since: from, until: since - 1) : null; + } + /// Remembers when a catch-up over [readAtByChannel] was sent, and the read /// timestamps and mute state it used, for every channel except /// [failedChannelIds]: their filters failed, so the next catch-up still needs diff --git a/mobile/lib/features/channels/channels_provider.dart b/mobile/lib/features/channels/channels_provider.dart index 6febe74f1af..13524ab7d69 100644 --- a/mobile/lib/features/channels/channels_provider.dart +++ b/mobile/lib/features/channels/channels_provider.dart @@ -239,6 +239,7 @@ class ChannelsNotifier extends AsyncNotifier> { _hasLoaded = false; _hasCachedSnapshot = false; _needsSnapshotRevalidation = false; + _activity.clear(); _memberSnapshotRelayBaseUrl = relayBaseUrl; _memberSnapshotPubkey = pubkey; _memberSnapshotsByChannelId = const {}; @@ -331,7 +332,6 @@ class ChannelsNotifier extends AsyncNotifier> { _refreshCoordinator.retireInFlight(); _clearLiveSubscriptions(); _latestObservedByChannel.clear(); - _activity.clear(); _observedUnreadEventsByChannel.clear(); _observedUnreadRevisions.touchAll(); _unreadCaughtUpByChannel.clear(); @@ -522,6 +522,7 @@ class ChannelsNotifier extends AsyncNotifier> { entry.value, ]; final latestFailedIds = {}; + final latestRead = _activity.beginRead(); final latestEventsRequest = _fetchLastMessageEvents( session, latestRequested, @@ -592,7 +593,7 @@ class ChannelsNotifier extends AsyncNotifier> { } } } - _activity.noteRead(latestRequested, latestFailedIds, events); + _activity.noteRead(latestRead, latestRequested, latestFailedIds, events); final mutedChannelIds = _mutedChannelIds(); for (final event in events ?? const []) { final channelId = event.channelId; diff --git a/mobile/test/features/channels/channels_provider_test.dart b/mobile/test/features/channels/channels_provider_test.dart index d7c11d1178c..4e4176df977 100644 --- a/mobile/test/features/channels/channels_provider_test.dart +++ b/mobile/test/features/channels/channels_provider_test.dart @@ -2516,10 +2516,10 @@ void main() { _meta(id: _channelA, name: 'general'), _meta(id: _channelB, name: 'quiet'), ], - // Self-authored activity counts; a future-dated head does not. + // Self-authored activity counts; a future-dated head counts as now. recentMessages: [ message('mine', now - 100), - message('ahead', now + 900), + message('ahead', now + 900, channel: _channelB), ], ); final container = _buildContainer(session: session); @@ -2547,8 +2547,7 @@ void main() { .singleWhere((channel) => channel.id == _channelA) .lastMessageAt; expect(activity(_channelA).value, now - 100); - expect(activity(_channelB), isA>()); - expect(activity(_channelB).value, isNull); + expect(activity(_channelB).value, inInclusiveRange(now, now + 60)); session.emit(message('live', now - 10)); await _settle(); @@ -2597,9 +2596,135 @@ void main() { cold.read(channelFilterActivityProvider(_channelA)).isLoading, isTrue, ); + + // A covered channel with no messages is known to be empty. + final empty = _buildContainer( + session: _FakeRelaySession( + memberships: [_membership(_channelA, myPk)], + metadata: [_meta(id: _channelA, name: 'general')], + ), + ); + addTearDown(empty.dispose); + await empty.read(channelsProvider.future); + await _settle(); + expect(empty.read(channelsProvider.notifier).activityFor(_channelA), ( + known: true, + at: null, + )); }, ); + test('Active filter activity survives rebuilds, read gaps, failed catch-ups ' + 'and deletions', () async { + final now = DateTime.now().millisecondsSinceEpoch ~/ 1000; + NostrEvent message(String id, int at) => NostrEvent( + id: id, + pubkey: myPk, + createdAt: at, + kind: EventKind.streamMessageV2, + tags: const [ + ['h', _channelA], + ], + content: 'hi', + sig: 'sig', + ); + final old = message('old', now - 10 * 86400); + final session = _FakeRelaySession( + memberships: [_membership(_channelA, myPk)], + metadata: [_meta(id: _channelA, name: 'general')], + recentMessages: [old], + enforceHistoryLimits: true, + ); + final container = _buildContainer( + session: session, + overrides: [readStateProvider.overrideWith(_ActivityReadState.new)], + ); + addTearDown(container.dispose); + await container.read(channelsProvider.future); + final notifier = container.read(channelsProvider.notifier); + await notifier.waitForUnreadCatchUp(); + expect(notifier.activityFor(_channelA), (known: true, at: old.createdAt)); + + // Rebuilding in the same community, as creating a Solo chat does, + // reuses the list without re-reading it; its activity stays. + container.invalidate(channelsProvider); + await container.read(channelsProvider.future); + await notifier.waitForUnreadCatchUp(); + expect(notifier.activityFor(_channelA), (known: true, at: old.createdAt)); + + Future reconnect() async { + final batches = session.queryBatches.length; + session.setStatus(SessionStatus.reconnecting); + session.setStatus(SessionStatus.connected); + await _waitUntil( + () => session.queryBatches + .skip(batches) + .any((batch) => batch.every((filter) => filter.since != null)), + ); + await notifier.waitForUnreadCatchUp(); + await _settle(); + } + + // A post that arrived while disconnected and was read on another + // device is older than the read marker the catch-up starts after. + final offline = message('offline', now - 30); + session.recentMessages = [old, offline]; + (container.read(readStateProvider.notifier) as _ActivityReadState) + .markReadThrough(_channelA, now - 15); + await reconnect(); + expect(notifier.activityFor(_channelA).at, offline.createdAt); + + // A failed catch-up leaves activity unknown until one succeeds. + session.messageBatchError = RelayException( + 503, + '{"error":"query timed out"}', + ); + await reconnect(); + expect(notifier.activityFor(_channelA).known, isFalse); + session.messageBatchError = null; + await notifier.refresh(); + await notifier.waitForUnreadCatchUp(); + expect(notifier.activityFor(_channelA), ( + known: true, + at: offline.createdAt, + )); + + // A rebuild while disconnected resets the unread cursor, so the next + // catch-up starts after the read marker; the post read meanwhile on + // another device still counts. + final missed = message('missed', now - 20); + session.recentMessages = [old, offline, missed]; + session.setStatus(SessionStatus.reconnecting); + container.invalidate(channelsProvider); + final rebuilt = container.read(channelsProvider.future); + (container.read(readStateProvider.notifier) as _ActivityReadState) + .markReadThrough(_channelA, now - 12); + await reconnect(); + await rebuilt; + expect(notifier.activityFor(_channelA), ( + known: true, + at: missed.createdAt, + )); + + // Pull to refresh drops a deleted latest post. + session.recentMessages = [old]; + await notifier.refresh(); + await notifier.waitForUnreadCatchUp(); + expect(notifier.activityFor(_channelA).at, old.createdAt); + + // A post that arrives while that read is out is newer than its answer. + final gate = Completer(); + session.latestBatchGate = gate; + final refreshing = notifier.refresh(); + await _waitUntil(() => session.latestBatchGate == null); + final live = message('live', now - 2); + session.emit(live); + await _settle(); + gate.complete(); + await refreshing; + expect(notifier.activityFor(_channelA).at, live.createdAt); + }); + test('live channel events update channel lastMessageAt', () async { final session = _FakeRelaySession( memberships: [_membership(_channelA, myPk)], @@ -4726,6 +4851,14 @@ class _BridgeDownRelaySession extends _FakeRelaySession { ); } +class _ActivityReadState extends ReadStateNotifier { + @override + ReadStateState build() => const ReadStateState.inert(); + + void markReadThrough(String channelId, int at) => + state = state.copyWithContext(channelId, at); +} + class _FakeAppLifecycleNotifier extends AppLifecycleNotifier { @override AppLifecycleState build() => AppLifecycleState.resumed; From 47663c66a8c4e859269433fd48840529da950c1f Mon Sep 17 00:00:00 2001 From: Tom Brow Date: Fri, 20 Nov 2026 13:39:59 -0800 Subject: [PATCH 3/7] fix(mobile): read Active filter gaps apart from the unread catch-up A failed or slow gap read now leaves only activity unknown instead of discarding the unread catch-up, and a catch-up that fails during setup marks its channels unknown. A covering latest read also clears a failed catch-up mark. Signed-off-by: Tom Brow --- .../channels/channel_latest_activity.dart | 1 + .../channels/channel_unread_catch_up.dart | 84 ++++++++++++++++--- .../channels/channels_provider_test.dart | 50 ++++++++++- 3 files changed, 123 insertions(+), 12 deletions(-) diff --git a/mobile/lib/features/channels/channel_latest_activity.dart b/mobile/lib/features/channels/channel_latest_activity.dart index edb7e650454..f83dcd0db52 100644 --- a/mobile/lib/features/channels/channel_latest_activity.dart +++ b/mobile/lib/features/channels/channel_latest_activity.dart @@ -68,6 +68,7 @@ class _ChannelActivity { if (failedIds.contains(channel.id)) continue; covered.add(channel.id); _known.add(channel.id); + _catchUpFailed.remove(channel.id); if (sentAt != null) _coveredAt[channel.id] = sentAt; // Without its arrivals (dropped), only raise, as a live event would. if (arrivals == null) continue; diff --git a/mobile/lib/features/channels/channel_unread_catch_up.dart b/mobile/lib/features/channels/channel_unread_catch_up.dart index a214dd29743..781294e8df3 100644 --- a/mobile/lib/features/channels/channel_unread_catch_up.dart +++ b/mobile/lib/features/channels/channel_unread_catch_up.dart @@ -24,12 +24,7 @@ extension _UnreadCatchUp on ChannelsNotifier { var incomplete = false; var catchUpChannelIds = const {}; try { - final myPk = _lifecycleRef.read(myPubkeyProvider); - if (myPk == null) return; - - final session = _lifecycleRef.read(relaySessionProvider.notifier); - final mutedChannelIds = _mutedChannelIds(); - final readState = _lifecycleRef.read(readStateProvider); + // Before any read that can throw, so a failed setup marks them unknown. final activeChannels = [ for (final channel in channels) if (channel.isMember && !channel.isArchived) channel, @@ -38,6 +33,12 @@ extension _UnreadCatchUp on ChannelsNotifier { for (final channel in activeChannels) channel.id: channel, }; catchUpChannelIds = channelById.keys.toSet(); + final myPk = _lifecycleRef.read(myPubkeyProvider); + if (myPk == null) return; + + final session = _lifecycleRef.read(relaySessionProvider.notifier); + final mutedChannelIds = _mutedChannelIds(); + final readState = _lifecycleRef.read(readStateProvider); final readAtByChannel = { for (final channel in activeChannels) channel.id: readState.effectiveTimestamp(channel.id), @@ -69,12 +70,14 @@ extension _UnreadCatchUp on ChannelsNotifier { since: sinceByChannel[channel.id], limit: _unreadCatchUpLimit, ), + ]; + final gapFilters = { for (final channel in activeChannels) if (_activityGap(channel.id, sinceByChannel[channel.id]!) case ( :final since, :final until, )) - NostrFilter( + channel.id: NostrFilter( kinds: EventKind.channelMessageEventKinds, tags: { '#h': [channel.id], @@ -83,7 +86,7 @@ extension _UnreadCatchUp on ChannelsNotifier { until: until, limit: 1, ), - ]; + }; final failedChannelIds = {}; final events = await _fetchChannelHistoryBatch( @@ -159,8 +162,8 @@ extension _UnreadCatchUp on ChannelsNotifier { final channel = channelById[channelId]; if (channel == null) continue; final readAt = readAtByChannel[channelId]; - // Read posts (the gap filter's among them) say nothing about whether - // the window hides an older unread one. + // Read posts say nothing about whether the window hides an older + // unread one. if (readAt == null || event.createdAt > readAt) { windowSizeByChannel.update( channelId, @@ -229,8 +232,10 @@ extension _UnreadCatchUp on ChannelsNotifier { // deadline-unavailable latest-message batch leaves it unset. // Newer activity also republishes, so the Active filter sees it. // So does a channel whose activity became known or unknown. + // A channel with a gap to read stays as it was until that read answers. + gapFilters.removeWhere((id, _) => failedChannelIds.contains(id)); final coverageChanged = _activity.noteCatchUp( - catchUpChannelIds, + catchUpChannelIds.difference(gapFilters.keys.toSet()), failedChannelIds, sentAt: requestedAt, ); @@ -240,6 +245,18 @@ extension _UnreadCatchUp on ChannelsNotifier { coverageChanged) { _publishUnreadCatchUp(recordedAtByChannel); } + if (gapFilters.isNotEmpty) { + unawaited( + _catchUpActivityGaps( + session, + gapFilters, + requestedAt, + batchDeletedIds, + fence, + subscriptionGeneration, + ), + ); + } } catch (error) { incomplete = true; if (!_lifecycleRef.mounted) return; @@ -264,6 +281,51 @@ extension _UnreadCatchUp on ChannelsNotifier { } } + /// Reads the newest post in each channel's [_activityGap], after the unread + /// catch-up and apart from it, so its failure only leaves activity unknown. + Future _catchUpActivityGaps( + RelaySessionNotifier session, + Map filtersByChannel, + int sentAt, + Set deletedIds, + _ChannelRefreshFence fence, + int subscriptionGeneration, + ) async { + final channelIds = filtersByChannel.keys.toSet(); + final failedChannelIds = {}; + List? events; + try { + events = await _fetchChannelHistoryBatch( + session, + filtersByChannel.values.toList(), + operation: 'activity gap read', + failedChannelIds: failedChannelIds, + ); + } catch (error) { + debugPrint('[ChannelsNotifier] activity gap read failed: $error'); + } + if (!_lifecycleRef.mounted || + _isCatchUpRetired(fence, subscriptionGeneration)) { + return; + } + var changed = false; + for (final event in events ?? const []) { + final channelId = event.channelId; + if (channelId == null || + !channelIds.contains(channelId) || + deletedIds.contains(event.id) || + _unscopedUnreadDeletedIds.contains(event.id) || + _unreadDeletedIdsByChannel[channelId]?.contains(event.id) == true) { + continue; + } + changed |= _activity.note(channelId, event.createdAt); + } + final failed = events == null ? channelIds : failedChannelIds; + changed |= _activity.noteCatchUp(channelIds, failed, sentAt: sentAt); + if (changed) _publishUnreadCatchUp(const {}); + if (failed.isNotEmpty) _unreadCatchUpRetry.schedule(_retryUnreadCatchUp); + } + /// Where a channel's catch-up starts. /// /// The first catch-up in a session starts after the read timestamp. Every diff --git a/mobile/test/features/channels/channels_provider_test.dart b/mobile/test/features/channels/channels_provider_test.dart index 4e4176df977..fd23237f9dc 100644 --- a/mobile/test/features/channels/channels_provider_test.dart +++ b/mobile/test/features/channels/channels_provider_test.dart @@ -2674,6 +2674,33 @@ void main() { await reconnect(); expect(notifier.activityFor(_channelA).at, offline.createdAt); + // A failed gap read leaves activity unknown but keeps the unread post the + // catch-up returned. + final mention = NostrEvent( + id: 'mention', + pubkey: 'alice', + createdAt: now - 5, + kind: EventKind.streamMessageV2, + tags: const [ + ['h', _channelA], + ['p', myPk], + ], + content: 'hey', + sig: 'sig', + ); + session.recentMessages = [old, offline, mention]; + (container.read(readStateProvider.notifier) as _ActivityReadState) + .markReadThrough(_channelA, now - 8); + session.gapBatchError = RelayException(503, '{"error":"query timed out"}'); + await reconnect(); + await _waitUntil(() => !notifier.activityFor(_channelA).known); + expect( + notifier.observedUnreadEventsByChannel[_channelA]?.keys, + contains(mention.id), + ); + session.gapBatchError = null; + session.recentMessages = [old, offline]; + // A failed catch-up leaves activity unknown until one succeeds. session.messageBatchError = RelayException( 503, @@ -2723,6 +2750,15 @@ void main() { gate.complete(); await refreshing; expect(notifier.activityFor(_channelA).at, live.createdAt); + + // So does a catch-up that fails before it sends anything. + final readState = + container.read(readStateProvider.notifier) as _ActivityReadState; + readState.unavailable = true; + container.invalidate(readStateProvider); + session.setStatus(SessionStatus.reconnecting); + session.setStatus(SessionStatus.connected); + await _waitUntil(() => !notifier.activityFor(_channelA).known); }); test('live channel events update channel lastMessageAt', () async { @@ -4242,6 +4278,8 @@ class _FakeRelaySession extends RelaySessionNotifier { Completer? latestBatchGate; final List> queryBatches = []; Object? messageBatchError; + // Fails batches made only of the catch-up's activity gap reads. + Object? gapBatchError; Object? latestBatchError; Object? messageHistoryError; int messageHistoryCount = 0; @@ -4633,6 +4671,11 @@ class _FakeRelaySession extends RelaySessionNotifier { throw RelayException(400, '{"error":"too many explicit channels"}'); } if (messageBatchError case final error?) throw error; + if (gapBatchError case final error? + when filters.isNotEmpty && + filters.every((f) => f.until != null && f.limit == 1)) { + throw error; + } // The unread catch-up is the only batch that carries `since` on every // filter; the latest-message batch leaves it null. Snapshot the messages at // request time so a parked response reflects the scope that asked for it. @@ -4852,8 +4895,13 @@ class _BridgeDownRelaySession extends _FakeRelaySession { } class _ActivityReadState extends ReadStateNotifier { + bool unavailable = false; + @override - ReadStateState build() => const ReadStateState.inert(); + ReadStateState build() { + if (unavailable) throw StateError('read state unavailable'); + return const ReadStateState.inert(); + } void markReadThrough(String channelId, int at) => state = state.copyWithContext(channelId, at); From ee7fa3c7904678328671228ba86ccc6da4faea1e Mon Sep 17 00:00:00 2001 From: Tom Brow Date: Fri, 20 Nov 2026 13:55:35 -0800 Subject: [PATCH 4/7] Don't retry a failed activity gap read on its own A gap read that kept failing rescheduled the unread retry at the base delay after each successful catch-up reset the backoff, so it restarted a full list read and catch-up every ten seconds. Leave the channel's activity unknown instead; the next list read's catch-up asks again. Signed-off-by: Tom Brow --- mobile/lib/features/channels/channel_unread_catch_up.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mobile/lib/features/channels/channel_unread_catch_up.dart b/mobile/lib/features/channels/channel_unread_catch_up.dart index 781294e8df3..1e59307c04c 100644 --- a/mobile/lib/features/channels/channel_unread_catch_up.dart +++ b/mobile/lib/features/channels/channel_unread_catch_up.dart @@ -283,6 +283,7 @@ extension _UnreadCatchUp on ChannelsNotifier { /// Reads the newest post in each channel's [_activityGap], after the unread /// catch-up and apart from it, so its failure only leaves activity unknown. + /// A failed gap is not retried: the next list read's catch-up reads it again. Future _catchUpActivityGaps( RelaySessionNotifier session, Map filtersByChannel, @@ -323,7 +324,6 @@ extension _UnreadCatchUp on ChannelsNotifier { final failed = events == null ? channelIds : failedChannelIds; changed |= _activity.noteCatchUp(channelIds, failed, sentAt: sentAt); if (changed) _publishUnreadCatchUp(const {}); - if (failed.isNotEmpty) _unreadCatchUpRetry.schedule(_retryUnreadCatchUp); } /// Where a channel's catch-up starts. From 3cbc5935bd0118599464a60b9e920eba1b239c59 Mon Sep 17 00:00:00 2001 From: Tom Brow Date: Fri, 20 Nov 2026 14:42:30 -0800 Subject: [PATCH 5/7] fix(mobile): verify, retry and bound Active filter activity - Count only messages whose signature verifies. A list read picks each channel's newest authentic post, and a channel whose posts all fail stays unknown. - Retry a channel the last list read left without a baseline: background list reads ask for its latest message again. - Re-read a channel's newest post when its latest one is deleted, so the deleted post stops counting. - Read a reconnect's activity gap only for channels an Active section shows, only when it could raise the known activity, and without the per-channel websocket fallback. Other channels keep the gap for a later catch-up and read as unknown meanwhile. Signed-off-by: Tom Brow --- .../channel_filter_activity.dart | 13 +- .../channels/channel_latest_activity.dart | 207 ++++++++++++++---- .../channels/channel_unread_catch_up.dart | 61 ++++-- .../features/channels/channels_provider.dart | 22 +- .../channels/channels_provider_test.dart | 180 ++++++++++----- 5 files changed, 368 insertions(+), 115 deletions(-) diff --git a/mobile/lib/features/channels/channel_filter/channel_filter_activity.dart b/mobile/lib/features/channels/channel_filter/channel_filter_activity.dart index 3b1fc074426..534d0f3806a 100644 --- a/mobile/lib/features/channels/channel_filter/channel_filter_activity.dart +++ b/mobile/lib/features/channels/channel_filter/channel_filter_activity.dart @@ -4,12 +4,23 @@ import '../channels_provider.dart'; /// When anyone last posted in a channel, for the Active filter. Loading until /// the channel list's latest-message read covers the channel; data (null when /// it has no messages) after that. It comes from the list's own reads and -/// live delivery, so it sends no relay requests of its own. +/// live delivery, so it sends no relay requests of its own. While watched, the +/// reconnect catch-up also reads the channel's activity gap. final channelFilterActivityProvider = Provider.autoDispose .family, String>((ref, channelId) { + ref.watch(_activityWatchProvider(channelId)); final notifier = ref.watch(channelsProvider.notifier); final (:known, :at) = ref.watch( channelsProvider.select((_) => notifier.activityFor(channelId)), ); return known ? AsyncData(at) : const AsyncLoading(); }); + +/// Holds the Active section's interest in a channel for as long as anything +/// watches its activity, across the rebuilds each activity change causes. +final _activityWatchProvider = Provider.autoDispose.family(( + ref, + channelId, +) { + ref.onDispose(ref.watch(channelsProvider.notifier).watchActivity(channelId)); +}); diff --git a/mobile/lib/features/channels/channel_latest_activity.dart b/mobile/lib/features/channels/channel_latest_activity.dart index f83dcd0db52..62c653bb2e8 100644 --- a/mobile/lib/features/channels/channel_latest_activity.dart +++ b/mobile/lib/features/channels/channel_latest_activity.dart @@ -4,7 +4,7 @@ part of 'channels_provider.dart'; /// included, as the list's reads and live delivery saw it. Unlike /// [Channel.lastMessageAt], it does not affect unread or sort state. class _ChannelActivity { - final Map _latest = {}; + final Map _latest = {}; final Set _known = {}; // Channels whose last unread catch-up failed: a post may have arrived while // disconnected that no read has seen yet. @@ -12,24 +12,62 @@ class _ChannelActivity { // Per latest-message read in flight, the newest arrival in each channel // since it began. A read that never reports, such as a superseded refresh, // is dropped once newer reads begin. - final Map> _arrivalsByRead = {}; + final Map> _arrivalsByRead = {}; final Map _readSentAt = {}; int _nextRead = 0; + // Reads begun before the last [clear] belong to another scope. + int _firstRead = 0; // Per channel, when the last read that covered it (a successful latest read // or unread catch-up) was sent. Unlike the unread cursor, only [clear] // resets it, so the catch-up can always ask for posts since then. final Map _coveredAt = {}; + // Channels an Active section is showing, with how many listeners each. + final Map _watched = {}; /// When the last read that covered [channelId] was sent, if any. int? coveredAt(String channelId) => _coveredAt[channelId]; + /// The channel's activity time, for deciding whether a read can change it. + int? latestAt(String channelId) => _latest[channelId]?.at; + + /// Whether a latest read or live message ever covered [channelId]. A list + /// read that keeps the list's times still reads the channels without one. + bool hasBaseline(String channelId) => _known.contains(channelId); + + /// Whether an Active section shows [channelId], so reads that only serve + /// the filter are worth sending for it. + bool isWatched(String channelId) => _watched.containsKey(channelId); + + /// Registers an Active section's interest in [channelId]; call the result + /// when it ends. + void Function() watch(String channelId) { + _watched.update(channelId, (n) => n + 1, ifAbsent: () => 1); + var done = false; + return () { + if (done) return; + done = true; + final n = _watched[channelId]! - 1; + n == 0 ? _watched.remove(channelId) : _watched[channelId] = n; + }; + } + /// `known` is false until a read or live message covered the channel, and /// while its last unread catch-up failed. ({bool known, int? at}) lookup(String channelId) => ( known: _known.contains(channelId) && !_catchUpFailed.contains(channelId), - at: _latest[channelId], + at: _latest[channelId]?.at, ); + /// The channels whose activity is one of [eventIds]. Event ids are unique, + /// so a deletion without an `h` tag finds its channel too. + Set headsAmong(Set eventIds) => { + for (final MapEntry(key: id, value: head) in _latest.entries) + if (eventIds.contains(head.id)) id, + }; + + /// Whether [read] began since the last [clear]. + bool isCurrent(int read) => read >= _firstRead; + /// Starts a latest-message read; pass the result to [noteRead]. int beginRead() { final read = _nextRead++; @@ -43,56 +81,72 @@ class _ChannelActivity { return read; } - /// Records a latest-message read. A channel it covered is known even when it - /// has no messages, and its activity becomes the read's newest message or a - /// newer arrival since the read began, so a deleted latest post stops - /// counting. A failed batch or channel stays unknown. + /// Records a latest-message read of [requested]. A channel it covered is + /// known even when it has no messages, and its activity becomes the read's + /// newest authentic message or a newer arrival since the read began, so a + /// deleted latest post stops counting. A failed batch or channel, or one + /// whose messages all fail verification, stays unknown. void noteRead( int read, - List requested, + Iterable requested, Set failedIds, List? events, ) { + if (read < _firstRead) return; final arrivals = _arrivalsByRead.remove(read); final sentAt = _readSentAt.remove(read); if (events == null) return; - final newest = {}; + final byChannel = >{}; for (final event in events) { if (event.channelId case final channelId?) { - final at = _clamp(event.createdAt); - newest.update(channelId, (v) => max(v, at), ifAbsent: () => at); + byChannel.putIfAbsent(channelId, () => []).add(event); + } + } + final newest = {}; + final invalid = {}; + for (final MapEntry(key: channelId, value: posts) in byChannel.entries) { + posts.sort((a, b) => b.createdAt.compareTo(a.createdAt)); + final head = posts.where(_isAuthentic).firstOrNull; + if (head == null) { + invalid.add(channelId); + } else { + newest[channelId] = (at: _clamp(head.createdAt), id: head.id); } } final covered = {}; - for (final channel in requested) { - if (failedIds.contains(channel.id)) continue; - covered.add(channel.id); - _known.add(channel.id); - _catchUpFailed.remove(channel.id); - if (sentAt != null) _coveredAt[channel.id] = sentAt; + for (final channelId in requested) { + if (failedIds.contains(channelId) || invalid.contains(channelId)) { + continue; + } + covered.add(channelId); + _known.add(channelId); + _catchUpFailed.remove(channelId); + if (sentAt != null) _coveredAt[channelId] = sentAt; // Without its arrivals (dropped), only raise, as a live event would. if (arrivals == null) continue; - final read = newest[channel.id]; - final arrived = arrivals[channel.id]; - final at = read == null || arrived == null - ? read ?? arrived - : max(read, arrived); - if (at == null) { - _latest.remove(channel.id); + final head = newest[channelId]; + final arrived = arrivals[channelId]; + final latest = head == null || arrived == null + ? head ?? arrived + : (arrived.at > head.at ? arrived : head); + if (latest == null) { + _latest.remove(channelId); } else { - _latest[channel.id] = at; + _latest[channelId] = latest; } } - for (final MapEntry(key: channelId, value: at) in newest.entries) { + for (final MapEntry(key: channelId, value: head) in newest.entries) { if (arrivals == null || !covered.contains(channelId)) { - _raise(channelId, at); + _raise(channelId, head); } } } - void noteLive(String channelId, int createdAt) { + void noteLive(NostrEvent event) { + final channelId = event.channelId; + if (channelId == null) return; _known.add(channelId); - note(channelId, createdAt); + note(channelId, event); } /// Records an unread catch-up's outcome per channel. Returns whether that @@ -116,22 +170,47 @@ class _ChannelActivity { } /// Records a message that arrived live or by catch-up. Returns whether it - /// advanced the channel's activity. - bool note(String channelId, int createdAt) { - final at = _clamp(createdAt); + /// advanced the channel's activity. Only an authentic message counts. + bool note(String channelId, NostrEvent event) { + final at = _clamp(event.createdAt); + final current = _latest[channelId]?.at; + final raises = current == null || at > current; + final arrives = _arrivalsByRead.values.any( + (arrivals) => (arrivals[channelId]?.at ?? -1) < at, + ); + if (!raises && !arrives) return false; + if (!_isAuthentic(event)) return false; + final head = (at: at, id: event.id); for (final arrivals in _arrivalsByRead.values) { - arrivals.update(channelId, (v) => max(v, at), ifAbsent: () => at); + arrivals.update( + channelId, + (v) => v.at >= at ? v : head, + ifAbsent: () => head, + ); } - return _raise(channelId, at); + return _raise(channelId, head); } - bool _raise(String channelId, int at) { + bool _raise(String channelId, ({int at, String id}) head) { final current = _latest[channelId]; - if (current != null && at <= current) return false; - _latest[channelId] = at; + if (current != null && head.at <= current.at) return false; + _latest[channelId] = head; return true; } + // The activity time decides which rows the filter shows, so it comes only + // from messages with a valid id and signature, as the old per-channel lookup + // required. Only a message that would change the activity is checked, so a + // read checks about one per channel, natively, as read-state sync does. + static bool _isAuthentic(NostrEvent event) { + try { + verifiedNostrEventFromJson(jsonEncode(event.toJson())); + return true; + } catch (_) { + return false; + } + } + // The relay accepts posts up to 900s ahead of its clock. Count one as now // rather than drop it: it is still the channel's newest activity. static int _clamp(int createdAt) => @@ -144,10 +223,64 @@ class _ChannelActivity { _arrivalsByRead.clear(); _readSentAt.clear(); _coveredAt.clear(); + _firstRead = _nextRead; } } extension _ChannelLatestMessages on ChannelsNotifier { + Set _deletionTargets(NostrEvent deletion) => { + for (final tag in deletion.tags) + if (tag.length >= 2 && tag[0] == 'e') tag[1], + }; + + /// Re-reads the newest post of each channel whose activity was one of + /// [deletedIds], so a deleted latest post stops counting. Activity can only + /// rise otherwise. One `limit: 1` filter per such channel; a failed read + /// leaves the channel unknown, which the Active filter shows. + void _reconcileDeletedActivity(Set deletedIds) { + if (deletedIds.isEmpty) return; + final channelIds = _activity.headsAmong(deletedIds); + if (channelIds.isNotEmpty) unawaited(_rereadActivity(channelIds)); + } + + Future _rereadActivity(Set channelIds) async { + final read = _activity.beginRead(); + final failed = {}; + List? events; + try { + events = await _fetchChannelHistoryBatch( + _lifecycleRef.read(relaySessionProvider.notifier), + [ + for (final channelId in channelIds) + NostrFilter( + kinds: EventKind.channelMessageEventKinds, + tags: { + '#h': [channelId], + }, + limit: 1, + ), + ], + operation: 'activity re-read', + failedChannelIds: failed, + optional: true, + ); + } catch (error) { + debugPrint('[ChannelsNotifier] activity re-read failed: $error'); + } + if (!_lifecycleRef.mounted || !_activity.isCurrent(read)) return; + final surviving = events + ?.where( + (event) => + !_unscopedUnreadDeletedIds.contains(event.id) && + _unreadDeletedIdsByChannel[event.channelId]?.contains(event.id) != + true, + ) + .toList(); + _activity.noteRead(read, channelIds, failed, surviving); + if (surviving == null) _activity.noteCatchUp(channelIds, channelIds); + _publishUnreadCatchUp(const {}); + } + /// Fetches each channel's independent latest-message window in one HTTP /// bridge request. The relay preserves NIP-01 per-filter limits while /// executing the filters with bounded concurrency, avoiding an unbounded diff --git a/mobile/lib/features/channels/channel_unread_catch_up.dart b/mobile/lib/features/channels/channel_unread_catch_up.dart index 1e59307c04c..85444b9dcf0 100644 --- a/mobile/lib/features/channels/channel_unread_catch_up.dart +++ b/mobile/lib/features/channels/channel_unread_catch_up.dart @@ -71,22 +71,30 @@ extension _UnreadCatchUp on ChannelsNotifier { limit: _unreadCatchUpLimit, ), ]; - final gapFilters = { - for (final channel in activeChannels) - if (_activityGap(channel.id, sinceByChannel[channel.id]!) case ( - :final since, - :final until, - )) - channel.id: NostrFilter( - kinds: EventKind.channelMessageEventKinds, - tags: { - '#h': [channel.id], - }, - since: since, - until: until, - limit: 1, - ), - }; + // A gap only matters to the Active filter, and only when a post in it + // could be newer than the activity already known. A channel no Active + // section shows keeps its gap for a later catch-up, and reads as + // unknown meanwhile. + final gapFilters = {}; + final deferredGaps = {}; + for (final channel in activeChannels) { + final gap = _activityGap(channel.id, sinceByChannel[channel.id]!); + if (gap == null) continue; + if ((_activity.latestAt(channel.id) ?? -1) >= gap.until) continue; + if (!_activity.isWatched(channel.id)) { + deferredGaps.add(channel.id); + continue; + } + gapFilters[channel.id] = NostrFilter( + kinds: EventKind.channelMessageEventKinds, + tags: { + '#h': [channel.id], + }, + since: gap.since, + until: gap.until, + limit: 1, + ); + } final failedChannelIds = {}; final events = await _fetchChannelHistoryBatch( @@ -176,7 +184,7 @@ extension _UnreadCatchUp on ChannelsNotifier { _unreadDeletedIdsByChannel[channelId]?.contains(event.id) == true) { continue; } - if (_activity.note(channelId, event.createdAt)) activityAdvanced = true; + if (_activity.note(channelId, event)) activityAdvanced = true; _noteThreadChannel(event, channelId); if (event.pubkey.toLowerCase() == myPk.toLowerCase()) continue; if (readAt != null && event.createdAt <= readAt) continue; @@ -234,11 +242,17 @@ extension _UnreadCatchUp on ChannelsNotifier { // So does a channel whose activity became known or unknown. // A channel with a gap to read stays as it was until that read answers. gapFilters.removeWhere((id, _) => failedChannelIds.contains(id)); - final coverageChanged = _activity.noteCatchUp( - catchUpChannelIds.difference(gapFilters.keys.toSet()), - failedChannelIds, - sentAt: requestedAt, - ); + deferredGaps.removeAll(failedChannelIds); + final coverageChanged = + _activity.noteCatchUp( + catchUpChannelIds + .difference(gapFilters.keys.toSet()) + .difference(deferredGaps), + failedChannelIds, + sentAt: requestedAt, + ) | + _activity.noteCatchUp(deferredGaps, deferredGaps); + _reconcileDeletedActivity(batchDeletedIds); if (recordedAtByChannel.isNotEmpty || appliedDeletion || activityAdvanced || @@ -301,6 +315,7 @@ extension _UnreadCatchUp on ChannelsNotifier { filtersByChannel.values.toList(), operation: 'activity gap read', failedChannelIds: failedChannelIds, + optional: true, ); } catch (error) { debugPrint('[ChannelsNotifier] activity gap read failed: $error'); @@ -319,7 +334,7 @@ extension _UnreadCatchUp on ChannelsNotifier { _unreadDeletedIdsByChannel[channelId]?.contains(event.id) == true) { continue; } - changed |= _activity.note(channelId, event.createdAt); + changed |= _activity.note(channelId, event); } final failed = events == null ? channelIds : failedChannelIds; changed |= _activity.noteCatchUp(channelIds, failed, sentAt: sentAt); diff --git a/mobile/lib/features/channels/channels_provider.dart b/mobile/lib/features/channels/channels_provider.dart index 13524ab7d69..211ff1ab5c5 100644 --- a/mobile/lib/features/channels/channels_provider.dart +++ b/mobile/lib/features/channels/channels_provider.dart @@ -6,6 +6,7 @@ import 'dart:math'; import 'package:flutter/foundation.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; +import '../../shared/crypto/event_signing.dart'; import '../../shared/relay/relay.dart'; import '../../shared/channels/channel_list_snapshot.dart'; import '../../shared/channels/open_channel_directory_storage.dart'; @@ -112,6 +113,10 @@ class ChannelsNotifier extends AsyncNotifier> { ({bool known, int? at}) activityFor(String channelId) => _activity.lookup(channelId); + /// Marks [channelId] as shown in an Active section until the result is + /// called, so the reconnect catch-up reads its activity gap. + void Function() watchActivity(String channelId) => _activity.watch(channelId); + // Per channel, when an earlier unread catch-up in this session was sent, and // the read timestamp and mute state it used. See [_UnreadCatchUp]. final Map @@ -512,7 +517,7 @@ class ChannelsNotifier extends AsyncNotifier> { final listed = { if (!fetchLastMessage) for (final c in state.value ?? const []) - if (c.isMember) c.id, + if (c.isMember && _activity.hasBaseline(c.id)) c.id, }; final latestRequested = [ for (final entry in visibleMetas) @@ -593,7 +598,12 @@ class ChannelsNotifier extends AsyncNotifier> { } } } - _activity.noteRead(latestRead, latestRequested, latestFailedIds, events); + _activity.noteRead( + latestRead, + latestRequested.map((c) => c.id), + latestFailedIds, + events, + ); final mutedChannelIds = _mutedChannelIds(); for (final event in events ?? const []) { final channelId = event.channelId; @@ -701,12 +711,14 @@ class ChannelsNotifier extends AsyncNotifier> { /// /// The relay rejects a request whose filters name more than /// [kMaxExplicitChannelValues] channels in aggregate, so larger batches are - /// split into requests within that cap. + /// split into requests within that cap. An [optional] batch has no + /// fallback: any failed request makes it unavailable. Future?> _fetchChannelHistoryBatch( RelaySessionNotifier session, List filters, { required String operation, Set? failedChannelIds, + bool optional = false, }) async { final requests = >[]; var channelValues = 0; @@ -760,6 +772,7 @@ class ChannelsNotifier extends AsyncNotifier> { } } if (fallback.isEmpty) return events; + if (optional) return null; // Fallback starts once every request has settled, so the batch never has // more than the request concurrency in flight. @@ -877,6 +890,7 @@ class ChannelsNotifier extends AsyncNotifier> { return; } if (_applyUnreadDeletion(event)) { + _reconcileDeletedActivity(_deletionTargets(event)); state = state.whenData((channels) => List.of(channels)); return; } @@ -895,7 +909,7 @@ class ChannelsNotifier extends AsyncNotifier> { final updated = List.of(channels); final channel = updated[idx]; if (EventKind.channelMessageEventKinds.contains(event.kind)) { - _activity.noteLive(channelId, event.createdAt); + _activity.noteLive(event); } _noteThreadEvent(event, channel.id, myPk); diff --git a/mobile/test/features/channels/channels_provider_test.dart b/mobile/test/features/channels/channels_provider_test.dart index fd23237f9dc..9804b8a570f 100644 --- a/mobile/test/features/channels/channels_provider_test.dart +++ b/mobile/test/features/channels/channels_provider_test.dart @@ -27,6 +27,7 @@ import 'package:buzz/features/channels/channels_provider.dart'; import 'package:buzz/features/channels/thread_follows/thread_follows_provider.dart'; import 'package:buzz/features/channels/unread_badge/observed_unread_event.dart'; import 'package:buzz/features/channels/unread_badge/unread_badge_provider.dart'; +import 'package:buzz/shared/crypto/event_signing.dart'; import 'package:buzz/shared/read_state/read_state_format.dart'; import 'package:buzz/shared/relay/relay.dart'; import 'package:buzz/shared/read_state/read_state_provider.dart'; @@ -319,19 +320,7 @@ void main() { _meta(id: _channelA, name: 'general'), _meta(id: _channelB, name: 'discoverable'), ], - recentMessages: [ - const NostrEvent( - id: 'm1', - pubkey: 'alice', - createdAt: 30, - kind: EventKind.streamMessageV2, - tags: [ - ['h', _channelA], - ], - content: 'hi', - sig: 'sig', - ), - ], + recentMessages: [_signedPost(30)], ); final container = _buildContainer(session: session); addTearDown(container.dispose); @@ -2496,21 +2485,11 @@ void main() { () async { final now = DateTime.now().millisecondsSinceEpoch ~/ 1000; NostrEvent message(String id, int at, {String channel = _channelA}) => - NostrEvent( - id: id, - pubkey: myPk, - createdAt: at, - kind: EventKind.streamMessageV2, - tags: [ - ['h', channel], - ], - content: 'my own activity', - sig: 'sig', - ); + _signedPost(at, channel: channel, content: id); final session = _FakeRelaySession( memberships: [ - _membership(_channelA, myPk), - _membership(_channelB, myPk), + _membership(_channelA, _activityPk), + _membership(_channelB, _activityPk), ], metadata: [ _meta(id: _channelA, name: 'general'), @@ -2522,7 +2501,10 @@ void main() { message('ahead', now + 900, channel: _channelB), ], ); - final container = _buildContainer(session: session); + final container = _buildContainer( + session: session, + overrides: [_testPubkeyProvider.overrideWith(_ActivityPubkey.new)], + ); addTearDown(container.dispose); await container.read(channelsProvider.future); await _settle(); @@ -2550,6 +2532,13 @@ void main() { expect(activity(_channelB).value, inInclusiveRange(now, now + 60)); session.emit(message('live', now - 10)); + // A message whose signature fails verification is not activity. + session.emit( + NostrEvent.fromJson({ + ...message('forged', now - 1).toJson(), + 'sig': 'f' * 128, + }), + ); await _settle(); expect(activity(_channelA).value, now - 10); // Activity leaves unread and sort time alone, and costs no relay reads. @@ -2597,6 +2586,29 @@ void main() { isTrue, ); + // So does a read whose only post fails verification, and the next + // list read asks for that channel again. + final forged = NostrEvent.fromJson({ + ...message('forged', now - 60).toJson(), + 'sig': 'f' * 128, + }); + final unverified = _FakeRelaySession( + memberships: [_membership(_channelA, myPk)], + metadata: [_meta(id: _channelA, name: 'general')], + recentMessages: [forged], + ); + final retried = _buildContainer(session: unverified); + addTearDown(retried.dispose); + await retried.read(channelsProvider.future); + await _settle(); + final retriedNotifier = retried.read(channelsProvider.notifier); + expect(retriedNotifier.activityFor(_channelA).known, isFalse); + unverified.recentMessages = [message('signed', now - 60)]; + unverified.setStatus(SessionStatus.reconnecting); + unverified.setStatus(SessionStatus.connected); + await _waitUntil(() => retriedNotifier.activityFor(_channelA).known); + expect(retriedNotifier.activityFor(_channelA).at, now - 60); + // A covered channel with no messages is known to be empty. final empty = _buildContainer( session: _FakeRelaySession( @@ -2617,33 +2629,32 @@ void main() { test('Active filter activity survives rebuilds, read gaps, failed catch-ups ' 'and deletions', () async { final now = DateTime.now().millisecondsSinceEpoch ~/ 1000; - NostrEvent message(String id, int at) => NostrEvent( - id: id, - pubkey: myPk, - createdAt: at, - kind: EventKind.streamMessageV2, - tags: const [ - ['h', _channelA], - ], - content: 'hi', - sig: 'sig', - ); + NostrEvent message(String id, int at) => _signedPost(at, content: id); final old = message('old', now - 10 * 86400); final session = _FakeRelaySession( - memberships: [_membership(_channelA, myPk)], + memberships: [_membership(_channelA, _activityPk)], metadata: [_meta(id: _channelA, name: 'general')], recentMessages: [old], enforceHistoryLimits: true, ); final container = _buildContainer( session: session, - overrides: [readStateProvider.overrideWith(_ActivityReadState.new)], + overrides: [ + readStateProvider.overrideWith(_ActivityReadState.new), + _testPubkeyProvider.overrideWith(_ActivityPubkey.new), + ], ); addTearDown(container.dispose); await container.read(channelsProvider.future); final notifier = container.read(channelsProvider.notifier); await notifier.waitForUnreadCatchUp(); expect(notifier.activityFor(_channelA), (known: true, at: old.createdAt)); + // An Active section shows the channel, so its activity gaps are read. + var shown = container.listen( + channelFilterActivityProvider(_channelA), + (_, _) {}, + ); + addTearDown(() => shown.close()); // Rebuilding in the same community, as creating a Solo chat does, // reuses the list without re-reading it; its activity stays. @@ -2676,17 +2687,13 @@ void main() { // A failed gap read leaves activity unknown but keeps the unread post the // catch-up returned. - final mention = NostrEvent( - id: 'mention', - pubkey: 'alice', - createdAt: now - 5, - kind: EventKind.streamMessageV2, - tags: const [ - ['h', _channelA], - ['p', myPk], - ], + final mention = _signedPost( + now - 5, content: 'hey', - sig: 'sig', + key: _aliceKey, + tags: [ + ['p', _activityPk], + ], ); session.recentMessages = [old, offline, mention]; (container.read(readStateProvider.notifier) as _ActivityReadState) @@ -2751,6 +2758,44 @@ void main() { await refreshing; expect(notifier.activityFor(_channelA).at, live.createdAt); + // Deleting the latest post re-reads the channel's newest one. + session.recentMessages = [old]; + session.emit( + NostrEvent.fromJson( + signNostrEvent( + kind: EventKind.deletion, + content: '', + secretKey: _activityKey, + tags: [ + ['h', _channelA], + ['e', live.id], + ], + ).toMap(), + ), + ); + await _waitUntil(() => notifier.activityFor(_channelA).at == old.createdAt); + + // A channel no Active section shows skips its gap read, and reads as + // unknown until a catch-up while it is shown. + shown.close(); + await _settle(); + final hidden = message('hidden', now - 3); + session.recentMessages = [old, hidden]; + (container.read(readStateProvider.notifier) as _ActivityReadState) + .markReadThrough(_channelA, now - 1); + final gapReads = session.queryBatches.where(_isGapBatch).length; + await reconnect(); + expect(session.queryBatches.where(_isGapBatch), hasLength(gapReads)); + expect(notifier.activityFor(_channelA).known, isFalse); + shown = container.listen( + channelFilterActivityProvider(_channelA), + (_, _) {}, + ); + await reconnect(); + await _waitUntil( + () => notifier.activityFor(_channelA).at == hidden.createdAt, + ); + // So does a catch-up that fails before it sends anything. final readState = container.read(readStateProvider.notifier) as _ActivityReadState; @@ -4202,6 +4247,41 @@ final _testPubkeyProvider = NotifierProvider<_TestPubkeyNotifier, String?>( _TestPubkeyNotifier.new, ); +// Active filter activity counts only signed messages. +const _activityKey = + '0000000000000000000000000000000000000000000000000000000000000001'; +const _aliceKey = + '0000000000000000000000000000000000000000000000000000000000000002'; +final _activityPk = derivePublicKeyHex(_activityKey); + +class _ActivityPubkey extends _TestPubkeyNotifier { + @override + String? build() => _activityPk; +} + +NostrEvent _signedPost( + int at, { + String channel = _channelA, + String content = 'hi', + String key = _activityKey, + List> tags = const [], +}) => NostrEvent.fromJson( + signNostrEvent( + kind: EventKind.streamMessageV2, + content: content, + secretKey: key, + createdAt: at, + tags: [ + ['h', channel], + ...tags, + ], + ).toMap(), +); + +bool _isGapBatch(List batch) => + batch.isNotEmpty && + batch.every((filter) => filter.until != null && filter.limit == 1); + /// Drains pending microtasks so provider rebuilds and awaited writes land. Future _settle() async { for (var i = 0; i < 20; i++) { From d4cba5fad9ac673d6e7e50da4c1f4fca98e8fb05 Mon Sep 17 00:00:00 2001 From: Tom Brow Date: Fri, 20 Nov 2026 14:46:59 -0800 Subject: [PATCH 6/7] test(mobile): cover a latest post deleted while offline The reconnect catch-up returns the deletion, and the Active filter's activity drops back to the channel's newest surviving post. Signed-off-by: Tom Brow --- .../channels/channels_provider_test.dart | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/mobile/test/features/channels/channels_provider_test.dart b/mobile/test/features/channels/channels_provider_test.dart index 9804b8a570f..7e16e064dd5 100644 --- a/mobile/test/features/channels/channels_provider_test.dart +++ b/mobile/test/features/channels/channels_provider_test.dart @@ -2759,20 +2759,29 @@ void main() { expect(notifier.activityFor(_channelA).at, live.createdAt); // Deleting the latest post re-reads the channel's newest one. + NostrEvent deletionOf(NostrEvent post) => NostrEvent.fromJson( + signNostrEvent( + kind: EventKind.deletion, + content: '', + secretKey: _activityKey, + tags: [ + ['h', _channelA], + ['e', post.id], + ], + ).toMap(), + ); session.recentMessages = [old]; - session.emit( - NostrEvent.fromJson( - signNostrEvent( - kind: EventKind.deletion, - content: '', - secretKey: _activityKey, - tags: [ - ['h', _channelA], - ['e', live.id], - ], - ).toMap(), - ), + session.emit(deletionOf(live)); + await _waitUntil(() => notifier.activityFor(_channelA).at == old.createdAt); + + // So does a deletion made while offline, which the catch-up returns. + final gone = message('gone', now - 4); + session.emit(gone); + await _waitUntil( + () => notifier.activityFor(_channelA).at == gone.createdAt, ); + session.recentMessages = [old, deletionOf(gone)]; + await reconnect(); await _waitUntil(() => notifier.activityFor(_channelA).at == old.createdAt); // A channel no Active section shows skips its gap read, and reads as From 7d73f30ddc793c75adfc928c460e62ad12805b1f Mon Sep 17 00:00:00 2001 From: Tom Brow Date: Fri, 20 Nov 2026 16:46:59 -0800 Subject: [PATCH 7/7] fix(mobile): forget a deleted latest post instead of re-reading it A deleted latest post now makes the channel's activity unknown, so the Active filter keeps it visible until the next list read re-reads it on resume, reconnect or a list change. This drops the separate limit:1 re-read and its races with newer refreshes. Signed-off-by: Tom Brow --- .../channels/channel_latest_activity.dart | 74 +++++-------------- .../channels/channel_unread_catch_up.dart | 4 +- .../features/channels/channels_provider.dart | 2 +- .../channels/channels_provider_test.dart | 12 ++- 4 files changed, 30 insertions(+), 62 deletions(-) diff --git a/mobile/lib/features/channels/channel_latest_activity.dart b/mobile/lib/features/channels/channel_latest_activity.dart index 62c653bb2e8..fbd8d9e8b3f 100644 --- a/mobile/lib/features/channels/channel_latest_activity.dart +++ b/mobile/lib/features/channels/channel_latest_activity.dart @@ -30,8 +30,9 @@ class _ChannelActivity { /// The channel's activity time, for deciding whether a read can change it. int? latestAt(String channelId) => _latest[channelId]?.at; - /// Whether a latest read or live message ever covered [channelId]. A list - /// read that keeps the list's times still reads the channels without one. + /// Whether a latest read or live message covered [channelId] since its + /// latest post was last deleted, if ever. A list read that keeps the list's + /// times still reads the channels without one. bool hasBaseline(String channelId) => _known.contains(channelId); /// Whether an Active section shows [channelId], so reads that only serve @@ -58,12 +59,21 @@ class _ChannelActivity { at: _latest[channelId]?.at, ); - /// The channels whose activity is one of [eventIds]. Event ids are unique, - /// so a deletion without an `h` tag finds its channel too. - Set headsAmong(Set eventIds) => { - for (final MapEntry(key: id, value: head) in _latest.entries) - if (eventIds.contains(head.id)) id, - }; + /// Forgets the channels whose activity is one of [eventIds], so a deleted + /// latest post stops counting: each reads as unknown until a later list read + /// re-reads it. Event ids are unique, so a deletion without an `h` tag finds + /// its channel too. Returns whether any channel was forgotten. + bool forgetHeads(Set eventIds) { + final channelIds = [ + for (final MapEntry(key: id, value: head) in _latest.entries) + if (eventIds.contains(head.id)) id, + ]; + for (final channelId in channelIds) { + _latest.remove(channelId); + _known.remove(channelId); + } + return channelIds.isNotEmpty; + } /// Whether [read] began since the last [clear]. bool isCurrent(int read) => read >= _firstRead; @@ -233,54 +243,6 @@ extension _ChannelLatestMessages on ChannelsNotifier { if (tag.length >= 2 && tag[0] == 'e') tag[1], }; - /// Re-reads the newest post of each channel whose activity was one of - /// [deletedIds], so a deleted latest post stops counting. Activity can only - /// rise otherwise. One `limit: 1` filter per such channel; a failed read - /// leaves the channel unknown, which the Active filter shows. - void _reconcileDeletedActivity(Set deletedIds) { - if (deletedIds.isEmpty) return; - final channelIds = _activity.headsAmong(deletedIds); - if (channelIds.isNotEmpty) unawaited(_rereadActivity(channelIds)); - } - - Future _rereadActivity(Set channelIds) async { - final read = _activity.beginRead(); - final failed = {}; - List? events; - try { - events = await _fetchChannelHistoryBatch( - _lifecycleRef.read(relaySessionProvider.notifier), - [ - for (final channelId in channelIds) - NostrFilter( - kinds: EventKind.channelMessageEventKinds, - tags: { - '#h': [channelId], - }, - limit: 1, - ), - ], - operation: 'activity re-read', - failedChannelIds: failed, - optional: true, - ); - } catch (error) { - debugPrint('[ChannelsNotifier] activity re-read failed: $error'); - } - if (!_lifecycleRef.mounted || !_activity.isCurrent(read)) return; - final surviving = events - ?.where( - (event) => - !_unscopedUnreadDeletedIds.contains(event.id) && - _unreadDeletedIdsByChannel[event.channelId]?.contains(event.id) != - true, - ) - .toList(); - _activity.noteRead(read, channelIds, failed, surviving); - if (surviving == null) _activity.noteCatchUp(channelIds, channelIds); - _publishUnreadCatchUp(const {}); - } - /// Fetches each channel's independent latest-message window in one HTTP /// bridge request. The relay preserves NIP-01 per-filter limits while /// executing the filters with bounded concurrency, avoiding an unbounded diff --git a/mobile/lib/features/channels/channel_unread_catch_up.dart b/mobile/lib/features/channels/channel_unread_catch_up.dart index 85444b9dcf0..37783ac2853 100644 --- a/mobile/lib/features/channels/channel_unread_catch_up.dart +++ b/mobile/lib/features/channels/channel_unread_catch_up.dart @@ -251,8 +251,8 @@ extension _UnreadCatchUp on ChannelsNotifier { failedChannelIds, sentAt: requestedAt, ) | - _activity.noteCatchUp(deferredGaps, deferredGaps); - _reconcileDeletedActivity(batchDeletedIds); + _activity.noteCatchUp(deferredGaps, deferredGaps) | + _activity.forgetHeads(batchDeletedIds); if (recordedAtByChannel.isNotEmpty || appliedDeletion || activityAdvanced || diff --git a/mobile/lib/features/channels/channels_provider.dart b/mobile/lib/features/channels/channels_provider.dart index 211ff1ab5c5..553dbb67729 100644 --- a/mobile/lib/features/channels/channels_provider.dart +++ b/mobile/lib/features/channels/channels_provider.dart @@ -890,7 +890,7 @@ class ChannelsNotifier extends AsyncNotifier> { return; } if (_applyUnreadDeletion(event)) { - _reconcileDeletedActivity(_deletionTargets(event)); + _activity.forgetHeads(_deletionTargets(event)); state = state.whenData((channels) => List.of(channels)); return; } diff --git a/mobile/test/features/channels/channels_provider_test.dart b/mobile/test/features/channels/channels_provider_test.dart index 7e16e064dd5..e28f6bde639 100644 --- a/mobile/test/features/channels/channels_provider_test.dart +++ b/mobile/test/features/channels/channels_provider_test.dart @@ -2758,7 +2758,8 @@ void main() { await refreshing; expect(notifier.activityFor(_channelA).at, live.createdAt); - // Deleting the latest post re-reads the channel's newest one. + // Deleting the latest post forgets the channel's activity until the next + // list read re-reads it. NostrEvent deletionOf(NostrEvent post) => NostrEvent.fromJson( signNostrEvent( kind: EventKind.deletion, @@ -2772,7 +2773,9 @@ void main() { ); session.recentMessages = [old]; session.emit(deletionOf(live)); - await _waitUntil(() => notifier.activityFor(_channelA).at == old.createdAt); + await _waitUntil(() => !notifier.activityFor(_channelA).known); + await reconnect(); + expect(notifier.activityFor(_channelA), (known: true, at: old.createdAt)); // So does a deletion made while offline, which the catch-up returns. final gone = message('gone', now - 4); @@ -2782,7 +2785,10 @@ void main() { ); session.recentMessages = [old, deletionOf(gone)]; await reconnect(); - await _waitUntil(() => notifier.activityFor(_channelA).at == old.createdAt); + expect(notifier.activityFor(_channelA).known, isFalse); + session.recentMessages = [old]; + await reconnect(); + expect(notifier.activityFor(_channelA), (known: true, at: old.createdAt)); // A channel no Active section shows skips its gap read, and reads as // unknown until a catch-up while it is shown.