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..534d0f3806a 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,26 @@ -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. 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)), ); - 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(); + }); + +/// 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 new file mode 100644 index 00000000000..fbd8d9e8b3f --- /dev/null +++ b/mobile/lib/features/channels/channel_latest_activity.dart @@ -0,0 +1,275 @@ +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 = {}; + // 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; + // 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 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 + /// 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, + ); + + /// 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; + + /// 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 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, + 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 byChannel = >{}; + for (final event in events) { + if (event.channelId case final channelId?) { + 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 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 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[channelId] = latest; + } + } + for (final MapEntry(key: channelId, value: head) in newest.entries) { + if (arrivals == null || !covered.contains(channelId)) { + _raise(channelId, head); + } + } + } + + void noteLive(NostrEvent event) { + final channelId = event.channelId; + if (channelId == null) return; + _known.add(channelId); + note(channelId, event); + } + + /// 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. 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) => v.at >= at ? v : head, + ifAbsent: () => head, + ); + } + return _raise(channelId, head); + } + + bool _raise(String channelId, ({int at, String id}) head) { + final current = _latest[channelId]; + 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) => + min(createdAt, DateTime.now().millisecondsSinceEpoch ~/ 1000); + + void clear() { + _latest.clear(); + _known.clear(); + _catchUpFailed.clear(); + _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], + }; + + /// 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..37783ac2853 100644 --- a/mobile/lib/features/channels/channel_unread_catch_up.dart +++ b/mobile/lib/features/channels/channel_unread_catch_up.dart @@ -22,13 +22,9 @@ 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; - - 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, @@ -36,6 +32,13 @@ extension _UnreadCatchUp on ChannelsNotifier { final channelById = { 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), @@ -45,6 +48,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,14 +67,34 @@ 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, ), ]; + // 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( @@ -72,11 +103,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 +110,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, @@ -98,6 +133,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) || @@ -133,13 +169,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 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; } - final readAt = readAtByChannel[channelId]; + if (_activity.note(channelId, event)) activityAdvanced = true; _noteThreadChannel(event, channelId); if (event.pubkey.toLowerCase() == myPk.toLowerCase()) continue; if (readAt != null && event.createdAt <= readAt) continue; @@ -193,13 +238,47 @@ 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. + // 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)); + deferredGaps.removeAll(failedChannelIds); + final coverageChanged = + _activity.noteCatchUp( + catchUpChannelIds + .difference(gapFilters.keys.toSet()) + .difference(deferredGaps), + failedChannelIds, + sentAt: requestedAt, + ) | + _activity.noteCatchUp(deferredGaps, deferredGaps) | + _activity.forgetHeads(batchDeletedIds); + if (recordedAtByChannel.isNotEmpty || + appliedDeletion || + activityAdvanced || + coverageChanged) { _publishUnreadCatchUp(recordedAtByChannel); } + if (gapFilters.isNotEmpty) { + unawaited( + _catchUpActivityGaps( + session, + gapFilters, + requestedAt, + batchDeletedIds, + fence, + subscriptionGeneration, + ), + ); + } } 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. @@ -216,6 +295,52 @@ 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, + 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, + optional: true, + ); + } 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); + } + final failed = events == null ? channelIds : failedChannelIds; + changed |= _activity.noteCatchUp(channelIds, failed, sentAt: sentAt); + if (changed) _publishUnreadCatchUp(const {}); + } + /// Where a channel's catch-up starts. /// /// The first catch-up in a session starts after the read timestamp. Every @@ -244,6 +369,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_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 3d62779d9f0..2ec43f79d27 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'; @@ -38,6 +39,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; @@ -107,12 +109,15 @@ 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); + + /// 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]. @@ -241,6 +246,7 @@ class ChannelsNotifier extends AsyncNotifier> { _hasLoaded = false; _hasCachedSnapshot = false; _needsSnapshotRevalidation = false; + _activity.clear(); _memberSnapshotRelayBaseUrl = relayBaseUrl; _memberSnapshotPubkey = pubkey; _memberSnapshotsByChannelId = const {}; @@ -333,7 +339,6 @@ class ChannelsNotifier extends AsyncNotifier> { _refreshCoordinator.retireInFlight(); _clearLiveSubscriptions(); _latestObservedByChannel.clear(); - _activityRevisions.clear(); _observedUnreadEventsByChannel.clear(); _observedUnreadRevisions.touchAll(); _unreadCaughtUpByChannel.clear(); @@ -514,15 +519,22 @@ 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 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 latestRead = _activity.beginRead(); + final latestEventsRequest = _fetchLastMessageEvents( + session, + latestRequested, + failedChannelIds: latestFailedIds, + ); final (displayNames, latestEvents) = await _fenced(fence, () async { await Future.wait(eagerError: true, [ displayNamesRequest, @@ -588,6 +600,12 @@ class ChannelsNotifier extends AsyncNotifier> { } } } + _activity.noteRead( + latestRead, + latestRequested.map((c) => c.id), + latestFailedIds, + events, + ); final mutedChannelIds = _mutedChannelIds(); for (final event in events ?? const []) { final channelId = event.channelId; @@ -689,46 +707,20 @@ 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. /// /// 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; @@ -782,6 +774,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. @@ -899,6 +892,7 @@ class ChannelsNotifier extends AsyncNotifier> { return; } if (_applyUnreadDeletion(event)) { + _activity.forgetHeads(_deletionTargets(event)); state = state.whenData((channels) => List.of(channels)); return; } @@ -917,7 +911,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(event); + } _noteThreadEvent(event, channel.id, myPk); @@ -998,7 +994,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 6549f74733d..3ada604adb8 100644 --- a/mobile/test/features/channels/channels_provider_test.dart +++ b/mobile/test/features/channels/channels_provider_test.dart @@ -25,6 +25,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/saved_channel_page.dart'; import 'package:buzz/features/channels/thread_follows/thread_follows_provider.dart'; @@ -322,19 +323,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); @@ -2495,51 +2484,346 @@ 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}) => + _signedPost(at, channel: channel, content: id); final session = _FakeRelaySession( - memberships: [_membership(_channelA, myPk)], - metadata: [_meta(id: _channelA, name: 'general')], + memberships: [ + _membership(_channelA, _activityPk), + _membership(_channelB, _activityPk), + ], + metadata: [ + _meta(id: _channelA, name: 'general'), + _meta(id: _channelB, name: 'quiet'), + ], + // Self-authored activity counts; a future-dated head counts as now. + recentMessages: [ + message('mine', now - 100), + message('ahead', now + 900, channel: _channelB), + ], + ); + final container = _buildContainer( + session: session, + overrides: [_testPubkeyProvider.overrideWith(_ActivityPubkey.new)], ); - 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), - ); - addTearDown(listener.close); + expect(activity(_channelA).value, now - 100); + 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( - const NostrEvent( - id: 'self-message', - pubkey: myPk, - createdAt: 20, - kind: EventKind.streamMessageV2, - tags: [ - ['h', _channelA], - ], - content: 'my own activity', - sig: 'sig', - ), + NostrEvent.fromJson({ + ...message('forged', now - 1).toJson(), + 'sig': 'f' * 128, + }), ); await _settle(); - expect(versions, [1]); - expect(notifier.latestObservedByChannel[_channelA], isNull); + expect(activity(_channelA).value, now - 10); + // Activity leaves unread and sort time alone, and costs no relay reads. expect( - container.read(channelsProvider).requireValue.single.lastMessageAt, + container + .read(channelsProvider) + .requireValue + .singleWhere((channel) => channel.id == _channelA) + .lastMessageAt, before, ); - expect(notifier.activityRevisionFor(_channelB), 0); + 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(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( + cold.read(channelFilterActivityProvider(_channelA)).isLoading, + 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( + 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) => _signedPost(at, content: id); + final old = message('old', now - 10 * 86400); + final session = _FakeRelaySession( + memberships: [_membership(_channelA, _activityPk)], + metadata: [_meta(id: _channelA, name: 'general')], + recentMessages: [old], + enforceHistoryLimits: true, + ); + final container = _buildContainer( + session: session, + 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. + 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 gap read leaves activity unknown but keeps the unread post the + // catch-up returned. + final mention = _signedPost( + now - 5, + content: 'hey', + key: _aliceKey, + tags: [ + ['p', _activityPk], + ], + ); + 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, + '{"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); + + // 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, + content: '', + secretKey: _activityKey, + tags: [ + ['h', _channelA], + ['e', post.id], + ], + ).toMap(), + ); + session.recentMessages = [old]; + session.emit(deletionOf(live)); + 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); + session.emit(gone); + await _waitUntil( + () => notifier.activityFor(_channelA).at == gone.createdAt, + ); + session.recentMessages = [old, deletionOf(gone)]; + await reconnect(); + 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. + 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; + 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 { final session = _FakeRelaySession( memberships: [_membership(_channelA, myPk)], @@ -3981,6 +4265,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++) { @@ -4057,6 +4376,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; @@ -4448,6 +4769,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. @@ -4666,6 +4992,19 @@ class _BridgeDownRelaySession extends _FakeRelaySession { ); } +class _ActivityReadState extends ReadStateNotifier { + bool unavailable = false; + + @override + ReadStateState build() { + if (unavailable) throw StateError('read state unavailable'); + return const ReadStateState.inert(); + } + + void markReadThrough(String channelId, int at) => + state = state.copyWithContext(channelId, at); +} + class _FakeAppLifecycleNotifier extends AppLifecycleNotifier { @override AppLifecycleState build() => AppLifecycleState.resumed; 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'));