Skip to content

Commit 237a39f

Browse files
browsquare-tombstudio
authored
perf(mobile): keep latest-message times on a directory load (#9457)
Opening search loads the channel directory when it is older than 30 seconds. That load reruns the whole channel list read, including each channel's latest messages (up to 20 per channel, 1 per DM), even though the list is already showing and its live subscriptions keep those times current. On an iPhone 12 mini signed in to Block's Builderlab, the latest-message part was about 700 KB of every search open. A directory load now skips the latest-message read when a channel list is already loaded and keeps the loaded times, the same as the background list reads already do. The first load, when no list is showing yet, still reads them. Both reads still fetch latest messages for member channels the list did not already show (for example a DM opened on another device), so those get a time instead of sorting last in Recent; before, background list reads left them without one. If that read hits the relay deadline, the list is marked for a full read and retried with backoff, as a timed-out full read was before. ### Validation New test: with a loaded list, a directory load sends no latest-message requests, adds the directory channel and keeps the member channel's latest-message time. It fails without this change. A second test: a DM first listed by a directory load, holding only our own message (which unread catch-up skips), gets its latest-message time. It fails without the newly-listed read. Its deadline row (the read that first lists the DM times out) checks that the backoff retry dates the DM; it fails if either the revalidation flag or the retry is removed. Full mobile test suite, `flutter analyze` and the file-size check pass. To stay under the 1200-line limit, `_advanceLastMessageAt` moved unchanged into `channel_unread_catch_up.dart`. On the iPhone 12 mini (profile build, signed in to Builderlab), opening search 90 seconds after a cold start and typing "buzz": | | Latest messages | All relay HTTP | | --- | --- | --- | | Before | 704 KB | 3.56 MB | | After | 0 | 2.86 MB | The automated run covered a cold start, the home list, opening search, channel results for the query, and closing search back to the home list with the same order. ### Regressions - A directory load with a loaded list no longer refreshes cached DM display names; the next full list read (pull to refresh, cold start, or a channel action) still does, as for background list reads. - A latest-message time the live subscriptions missed (for example while the socket was down) is not corrected by opening search; the next full list read corrects it. --------- Signed-off-by: Tom Brow <[email protected]> Co-authored-by: Tom Brow <[email protected]> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
1 parent a540e7b commit 237a39f

3 files changed

Lines changed: 199 additions & 73 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,11 @@ extension _UnreadCatchUp on ChannelsNotifier {
268268
}
269269
}
270270
}
271+
272+
Channel _advanceLastMessageAt(Channel channel, int? createdAt) {
273+
if (createdAt == null) return channel;
274+
final at = DateTime.fromMillisecondsSinceEpoch(createdAt * 1000, isUtc: true);
275+
final current = channel.lastMessageAt;
276+
if (current != null && !at.isAfter(current)) return channel;
277+
return channel.copyWith(lastMessageAt: at);
278+
}

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

Lines changed: 73 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -471,14 +471,18 @@ class ChannelsNotifier extends AsyncNotifier<List<Channel>> {
471471
);
472472
// Latest-message filters depend on channel identity/privacy, not names.
473473
// Start them alongside profiles, and observe both errors immediately.
474-
final latestEventsRequest = fetchLastMessage
475-
? _fetchLastMessageEvents(session, [
476-
for (final entry in visibleMetas)
477-
if (memberChannelIds.contains(entry.value.id) &&
478-
!entry.value.isArchived)
479-
entry.value,
480-
])
481-
: Future.value(const <NostrEvent>[]);
474+
final listed = {
475+
if (!fetchLastMessage)
476+
for (final c in state.value ?? const <Channel>[])
477+
if (c.isMember) c.id,
478+
};
479+
final latestEventsRequest = _fetchLastMessageEvents(session, [
480+
for (final entry in visibleMetas)
481+
if (memberChannelIds.contains(entry.value.id) &&
482+
!entry.value.isArchived &&
483+
!listed.contains(entry.value.id))
484+
entry.value,
485+
]);
482486
final (displayNames, latestEvents) = await _fenced(fence, () async {
483487
await Future.wait(eagerError: true, [
484488
displayNamesRequest,
@@ -522,61 +526,57 @@ class ChannelsNotifier extends AsyncNotifier<List<Channel>> {
522526

523527
// Step 3: fetch the most recent message per channel to populate lastMessageAt.
524528
// kind:39000 metadata doesn't carry message timestamps, so channels load with
525-
// lastMessageAt: null. Without this, unread detection and badge computation
526-
// see every channel as having no messages. Skipped on background list
527-
// reads since live subscriptions keep lastMessageAt current after the
528-
// initial load.
529-
if (fetchLastMessage) {
530-
final activeChannels = [
531-
for (final channel in channels)
532-
if (channel.isMember && !channel.isArchived) channel,
533-
];
534-
final channelById = {
535-
for (final channel in activeChannels) channel.id: channel,
536-
};
537-
final events = latestEvents;
538-
final lastMessageMap = <String, int>{};
539-
// An unavailable batch keeps the timestamps already known rather than
540-
// installing channels whose latest message looks absent.
541-
if (events == null) {
542-
for (final channel in state.value ?? const <Channel>[]) {
543-
final at = channel.lastMessageAt;
544-
if (at != null) {
545-
lastMessageMap[channel.id] = at.millisecondsSinceEpoch ~/ 1000;
546-
}
529+
// lastMessageAt: null. Reads that keep the list's times fetch only newly
530+
// listed channels; live delivery keeps the rest current.
531+
final activeChannels = [
532+
for (final channel in channels)
533+
if (channel.isMember && !channel.isArchived) channel,
534+
];
535+
final channelById = {
536+
for (final channel in activeChannels) channel.id: channel,
537+
};
538+
final events = latestEvents;
539+
final lastMessageMap = <String, int>{};
540+
// An unavailable batch keeps the timestamps already known rather than
541+
// installing channels whose latest message looks absent.
542+
if (events == null) {
543+
for (final channel in state.value ?? const <Channel>[]) {
544+
final at = channel.lastMessageAt;
545+
if (at != null) {
546+
lastMessageMap[channel.id] = at.millisecondsSinceEpoch ~/ 1000;
547547
}
548548
}
549-
final mutedChannelIds = _mutedChannelIds();
550-
for (final event in events ?? const <NostrEvent>[]) {
551-
final channelId = event.channelId;
552-
if (channelId == null) continue;
553-
final channel = channelById[channelId];
554-
if (channel == null) continue;
555-
if (!channel.isDm &&
556-
!shouldNotifyForEvent(
557-
event,
558-
myPk,
559-
mutedChannelIds: mutedChannelIds,
560-
channelId: channelId,
561-
)) {
562-
continue;
563-
}
564-
final current = lastMessageMap[channelId];
565-
if (current == null || event.createdAt > current) {
566-
lastMessageMap[channelId] = event.createdAt;
567-
}
549+
}
550+
final mutedChannelIds = _mutedChannelIds();
551+
for (final event in events ?? const <NostrEvent>[]) {
552+
final channelId = event.channelId;
553+
if (channelId == null) continue;
554+
final channel = channelById[channelId];
555+
if (channel == null) continue;
556+
if (!channel.isDm &&
557+
!shouldNotifyForEvent(
558+
event,
559+
myPk,
560+
mutedChannelIds: mutedChannelIds,
561+
channelId: channelId,
562+
)) {
563+
continue;
568564
}
565+
final current = lastMessageMap[channelId];
566+
if (current == null || event.createdAt > current) {
567+
lastMessageMap[channelId] = event.createdAt;
568+
}
569+
}
569570

570-
for (var i = 0; i < channels.length; i++) {
571-
final ts = lastMessageMap[channels[i].id];
572-
if (ts != null) {
573-
channels[i] = channels[i].copyWith(
574-
lastMessageAt: DateTime.fromMillisecondsSinceEpoch(
575-
ts * 1000,
576-
isUtc: true,
577-
),
578-
);
579-
}
571+
for (var i = 0; i < channels.length; i++) {
572+
final ts = lastMessageMap[channels[i].id];
573+
if (ts != null) {
574+
channels[i] = channels[i].copyWith(
575+
lastMessageAt: DateTime.fromMillisecondsSinceEpoch(
576+
ts * 1000,
577+
isUtc: true,
578+
),
579+
);
580580
}
581581
}
582582

@@ -626,12 +626,18 @@ class ChannelsNotifier extends AsyncNotifier<List<Channel>> {
626626
// snapshot as well as the visible list.
627627
for (var i = 0; i < channels.length; i++) {
628628
final previous = prevById[channels[i].id]?.lastMessageAt;
629-
if (previous != null) {
629+
final fetched = channels[i].lastMessageAt;
630+
if (previous != null &&
631+
(fetched == null || previous.isAfter(fetched))) {
630632
channels[i] = channels[i].copyWith(lastMessageAt: previous);
631633
}
632634
}
633635
}
634-
if (fetchLastMessage) _needsSnapshotRevalidation = latestEvents == null;
636+
// A failed targeted batch also leaves new channels for a full read to date.
637+
_needsSnapshotRevalidation =
638+
latestEvents == null ||
639+
(!fetchLastMessage && _needsSnapshotRevalidation);
640+
if (latestEvents == null) _listReadRetry.schedule(_readListIfChanged);
635641
if (hiddenDmIds != null) {
636642
_channelListChangesRead = max(_channelListChangesRead, listChanges);
637643
} else {
@@ -845,17 +851,6 @@ class ChannelsNotifier extends AsyncNotifier<List<Channel>> {
845851
);
846852
}
847853

848-
static Channel _advanceLastMessageAt(Channel channel, int? createdAt) {
849-
if (createdAt == null) return channel;
850-
final at = DateTime.fromMillisecondsSinceEpoch(
851-
createdAt * 1000,
852-
isUtc: true,
853-
);
854-
final current = channel.lastMessageAt;
855-
if (current != null && !at.isAfter(current)) return channel;
856-
return channel.copyWith(lastMessageAt: at);
857-
}
858-
859854
void _handleLiveEvent(NostrEvent event) {
860855
if (_noteChannelChange(event) &&
861856
event.kind == EventKind.nip29EditMetadata) {
@@ -1123,7 +1118,12 @@ class ChannelsNotifier extends AsyncNotifier<List<Channel>> {
11231118
final previousChannels = state.value;
11241119
try {
11251120
state = AsyncData(
1126-
await _fetch(subscribeLive: true, fetchDirectory: true),
1121+
await _fetch(
1122+
subscribeLive: true,
1123+
// Live subscriptions keep a loaded list's latest-message times.
1124+
fetchLastMessage: previousChannels == null,
1125+
fetchDirectory: true,
1126+
),
11271127
);
11281128
} on _StaleChannelRefresh {
11291129
// A community or identity switch retired this request. Its response

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

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,124 @@ void main() {
143143
},
144144
);
145145

146+
test('a directory load keeps latest-message times', () async {
147+
final session = _FakeRelaySession(
148+
memberships: [_membership(_channelA, myPk)],
149+
metadata: [
150+
_meta(id: _channelA, name: 'general'),
151+
_meta(id: _channelB, name: 'discoverable'),
152+
],
153+
recentMessages: [
154+
const NostrEvent(
155+
id: 'm1',
156+
pubkey: 'alice',
157+
createdAt: 30,
158+
kind: EventKind.streamMessageV2,
159+
tags: [
160+
['h', _channelA],
161+
],
162+
content: 'hi',
163+
sig: 'sig',
164+
),
165+
],
166+
);
167+
final container = _buildContainer(session: session);
168+
addTearDown(container.dispose);
169+
bool isLatest(List<NostrFilter> batch) =>
170+
batch.isNotEmpty &&
171+
batch.every(
172+
(filter) =>
173+
filter.since == null &&
174+
filter.tags.containsKey('#h') &&
175+
(filter.limit == 1 || filter.limit == 20),
176+
);
177+
await container.read(channelsProvider.future);
178+
await _settle();
179+
final latestReads = session.queryBatches.where(isLatest).length;
180+
expect(latestReads, isPositive);
181+
182+
await container.read(channelsProvider.notifier).retryDirectory();
183+
await _settle();
184+
185+
expect(session.queryBatches.where(isLatest), hasLength(latestReads));
186+
final channels = container.read(channelsProvider).requireValue;
187+
expect(channels.map((channel) => channel.id), [_channelB, _channelA]);
188+
expect(
189+
channels.firstWhere((channel) => channel.id == _channelA).lastMessageAt,
190+
DateTime.fromMillisecondsSinceEpoch(30 * 1000, isUtc: true),
191+
);
192+
});
193+
194+
for (final deadlineFirst in [false, true]) {
195+
_testWithClock('a directory load dates newly listed DMs'
196+
'${deadlineFirst ? ' after a latest-message deadline' : ''}', (clock) {
197+
NostrEvent message(String channelId, int at, String author) => NostrEvent(
198+
id: '$channelId-$at',
199+
pubkey: author,
200+
createdAt: at,
201+
kind: EventKind.streamMessageV2,
202+
tags: [
203+
['h', channelId],
204+
],
205+
content: 'hello',
206+
sig: 'sig',
207+
);
208+
final session = _FakeRelaySession(
209+
memberships: [_membership(_channelA, myPk)],
210+
metadata: [
211+
_meta(id: _channelA, name: 'Alice', channelType: 'dm'),
212+
_meta(id: _channelB, name: 'Bob', channelType: 'dm'),
213+
],
214+
recentMessages: [message(_channelA, 30, 'alice')],
215+
enforceHistoryLimits: true,
216+
);
217+
final container = _buildContainer(session: session);
218+
container.listen(channelsProvider, (_, _) {});
219+
clock.flushMicrotasks();
220+
DateTime? latest(String id) => container
221+
.read(channelsProvider)
222+
.requireValue
223+
.singleWhere((channel) => channel.id == id)
224+
.lastMessageAt;
225+
226+
// Another device opened DM B with an outgoing message, which unread
227+
// catch-up skips. Search's directory load discovers B first.
228+
session.memberships = [
229+
_membership(_channelA, myPk),
230+
_membership(_channelB, myPk),
231+
];
232+
session.recentMessages = [
233+
message(_channelA, 30, 'alice'),
234+
message(_channelB, 40, myPk),
235+
];
236+
if (deadlineFirst) {
237+
session.latestBatchError = RelayException(
238+
503,
239+
'{"error":"query timed out"}',
240+
);
241+
}
242+
container.read(channelsProvider.notifier).retryDirectory();
243+
clock.flushMicrotasks();
244+
if (deadlineFirst) {
245+
expect(latest(_channelB), isNull);
246+
// The retry after the deadline reads the whole list again.
247+
session.latestBatchError = null;
248+
clock.elapse(const Duration(seconds: 10));
249+
}
250+
251+
expect(
252+
latest(_channelB),
253+
DateTime.fromMillisecondsSinceEpoch(40 * 1000, isUtc: true),
254+
);
255+
expect(
256+
latest(_channelA),
257+
DateTime.fromMillisecondsSinceEpoch(30 * 1000, isUtc: true),
258+
);
259+
container.dispose();
260+
clock.elapse(const Duration(minutes: 10));
261+
});
262+
}
263+
146264
test('a directory load during the first channel load waits for it', () async {
147265
final session = _FakeRelaySession(
148266
memberships: [_membership(_channelA, myPk)],

0 commit comments

Comments
 (0)