Skip to content

Commit 678617b

Browse files
wesbillmanCarlclaude
committed
fix(mobile): read chat history after the live subscription's EOSE again
Reading history alongside the live REQ can lose a message. The relay registers the subscription only after its access checks, so a message committed between the window read and that registration is in neither, and a timestamp overlap only covers messages signed within it. Back to main's order: subscribe, wait for EOSE, then read. The journey now holds the live REQ, commits a message signed a minute earlier, and expects it in the timeline once the REQ is admitted. Co-authored-by: Carl <32a2e2c9d428ee08902cab75d956da2c1d235a22d4766b0dd4138bf6e2e5db1d@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Wes <[email protected]>
1 parent f9af7fb commit 678617b

2 files changed

Lines changed: 30 additions & 45 deletions

File tree

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

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ const _maxCachedReplies = 2048;
2323
/// such as a profile pushed over the chat, returns without a catch-up fetch.
2424
const _liveDetachDelay = Duration(seconds: 30);
2525

26-
/// How far back the live subscription opens, the same skew reconnect replay
27-
/// uses. The relay admits a REQ only after its access checks, so a message
28-
/// signed just before the chat opened can commit after the window read but
29-
/// before the subscription exists; replay from a little earlier covers it.
30-
const _liveOverlapSeconds = 5;
31-
3226
const _channelLiveEventKinds = [
3327
...EventKind.channelEventKinds,
3428
EventKind.channelThreadSummary,
@@ -203,38 +197,29 @@ class ChannelMessagesNotifier extends Notifier<AsyncValue<List<NostrEvent>>> {
203197
try {
204198
final session = ref.read(relaySessionProvider.notifier);
205199

206-
// History doesn't wait a round trip for the live subscription's EOSE.
207-
// The two overlap because the subscription replays from a few seconds
208-
// back; duplicates merge by id.
209-
unawaited(
210-
session
211-
.subscribe(
212-
NostrFilter(
213-
kinds: _channelLiveEventKinds,
214-
tags: {
215-
'#h': [channelId],
216-
},
217-
since: _currentUnixSeconds() - _liveOverlapSeconds,
218-
limit: 200,
219-
),
220-
_handleLiveEvent,
221-
)
222-
.then(
223-
(unsubscribe) {
224-
if (_isCurrentInit(initVersion)) {
225-
_unsubscribe = unsubscribe;
226-
} else {
227-
unsubscribe();
228-
}
229-
},
230-
onError: (Object error) {
231-
if (!_isCurrentInit(initVersion)) return;
232-
debugPrint(
233-
'[ChannelMessagesNotifier] live subscription failed for $channelId: $error',
234-
);
235-
},
236-
),
237-
);
200+
try {
201+
final unsubscribe = await session.subscribe(
202+
NostrFilter(
203+
kinds: _channelLiveEventKinds,
204+
tags: {
205+
'#h': [channelId],
206+
},
207+
since: _currentUnixSeconds(),
208+
limit: 200,
209+
),
210+
_handleLiveEvent,
211+
);
212+
if (!_isCurrentInit(initVersion)) {
213+
unsubscribe();
214+
return;
215+
}
216+
_unsubscribe = unsubscribe;
217+
} catch (error) {
218+
if (!_isCurrentInit(initVersion)) return;
219+
debugPrint(
220+
'[ChannelMessagesNotifier] live subscription failed for $channelId: $error',
221+
);
222+
}
238223

239224
final historyVersion = ++_threadQuerySerial;
240225
final history = await _fetchNewestHistory(session);

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void main() {
2828
);
2929
relay.seed([message('first', now - 20), message('second', now - 10)]);
3030

31-
// History must not wait on the live subscription's EOSE.
31+
// The relay admits the live REQ only after its access checks.
3232
final admitted = relay.holdReq();
3333
final container = ProviderContainer(overrides: relay.overrides);
3434
addTearDown(container.dispose);
@@ -63,14 +63,14 @@ void main() {
6363
container.read(relaySessionProvider).status,
6464
SessionStatus.connected,
6565
);
66-
expect(timeline(), ['first', 'second']);
67-
expect(relay.queries, hasLength(1), reason: 'read the channel window');
68-
// A message signed just before the chat opened, committed after the
69-
// window read but before the relay admits the live REQ, still arrives.
70-
relay.seed([message('signed as it opened', now - 1)]);
66+
// History waits for the live EOSE, so a message committed before the
67+
// relay admits the REQ is read, however long ago it was signed.
68+
expect(relay.queries, isEmpty);
69+
relay.seed([message('committed as it opened', now - 60)]);
7170
admitted.complete();
7271
settle();
73-
expect(timeline(), contains('signed as it opened'));
72+
expect(timeline(), ['committed as it opened', 'first', 'second']);
73+
expect(relay.queries, hasLength(1), reason: 'read the channel window');
7474
expect(
7575
relay.reqs.where((req) => (req[1] as String).startsWith('h-')),
7676
isEmpty,

0 commit comments

Comments
 (0)