Skip to content

Commit f9af7fb

Browse files
wesbillmanCarlclaude
committed
fix(mobile): open the chat's live subscription a few seconds back
History no longer waits for the live subscription's EOSE, but the relay registers a REQ only after its access checks. A message signed just before the chat opened could commit after the window read and before the subscription existed, and neither would return it. The subscription now replays from 5 s back, the same skew reconnect replay uses; duplicates merge by id. FakeRelay's holdReq models that admission delay. 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 460dc35 commit f9af7fb

3 files changed

Lines changed: 34 additions & 22 deletions

File tree

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ 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+
2632
const _channelLiveEventKinds = [
2733
...EventKind.channelEventKinds,
2834
EventKind.channelThreadSummary,
@@ -197,9 +203,9 @@ class ChannelMessagesNotifier extends Notifier<AsyncValue<List<NostrEvent>>> {
197203
try {
198204
final session = ref.read(relaySessionProvider.notifier);
199205

200-
// The live REQ goes out before history is requested, and its `since`
201-
// replays anything stored from now on, so the two overlap without
202-
// history waiting a round trip for the subscription's EOSE.
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.
203209
unawaited(
204210
session
205211
.subscribe(
@@ -208,7 +214,7 @@ class ChannelMessagesNotifier extends Notifier<AsyncValue<List<NostrEvent>>> {
208214
tags: {
209215
'#h': [channelId],
210216
},
211-
since: _currentUnixSeconds(),
217+
since: _currentUnixSeconds() - _liveOverlapSeconds,
212218
limit: 200,
213219
),
214220
_handleLiveEvent,

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

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

3131
// History must not wait on the live subscription's EOSE.
32-
final eose = relay.holdEose();
32+
final admitted = relay.holdReq();
3333
final container = ProviderContainer(overrides: relay.overrides);
3434
addTearDown(container.dispose);
3535
var messages = container.listen(
@@ -65,8 +65,12 @@ void main() {
6565
);
6666
expect(timeline(), ['first', 'second']);
6767
expect(relay.queries, hasLength(1), reason: 'read the channel window');
68-
eose.complete();
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)]);
71+
admitted.complete();
6972
settle();
73+
expect(timeline(), contains('signed as it opened'));
7074
expect(
7175
relay.reqs.where((req) => (req[1] as String).startsWith('h-')),
7276
isEmpty,

‎mobile/test/helpers/fake_relay.dart‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class FakeRelay {
4040
final _rejections = <({bool Function(NostrEvent) match, String message})>[];
4141
Completer<void>? _okHold;
4242
Completer<void>? _echoHold;
43-
Completer<void>? _eoseHold;
43+
Completer<void>? _reqHold;
4444
Completer<void>? _authHold;
4545
final DateTime Function()? _now;
4646

@@ -98,9 +98,10 @@ class FakeRelay {
9898
/// The relay dispatches after commit, so echo and OK have no fixed order.
9999
Completer<void> holdEcho() => _echoHold = Completer<void>();
100100

101-
/// Withholds stored replay and EOSE for new REQs until the completer
102-
/// completes, as a slow relay would.
103-
Completer<void> holdEose() => _eoseHold = Completer<void>();
101+
/// Holds new REQs until the completer completes, as a relay's access checks
102+
/// would: they read stored events, start live fan-out and reach EOSE only
103+
/// once admitted. Events committed meanwhile reach them only through replay.
104+
Completer<void> holdReq() => _reqHold = Completer<void>();
104105

105106
/// Keeps the next connection unauthenticated until the completer completes.
106107
/// Like the relay, it answers a REQ sent before AUTH with CLOSED.
@@ -151,20 +152,21 @@ class FakeRelay {
151152
}
152153
reqs.add(message);
153154
final filters = message.skip(2).cast<Map<String, dynamic>>().toList();
154-
final history = _query(filters);
155-
if (history == null) return;
156-
socket._live[subId] = filters;
157-
void replay() {
158-
for (final event in history) {
159-
socket._reply(['EVENT', subId, event.toJson()]);
160-
}
161-
socket._reply(['EOSE', subId]);
155+
void admit() {
156+
if (!socket._authenticated) return;
157+
final history = _query(filters);
158+
if (history == null) return;
159+
socket._live[subId] = filters;
160+
scheduleMicrotask(() {
161+
for (final event in history) {
162+
socket._reply(['EVENT', subId, event.toJson()]);
163+
}
164+
socket._reply(['EOSE', subId]);
165+
});
162166
}
163167

164-
final hold = _eoseHold;
165-
hold == null
166-
? scheduleMicrotask(replay)
167-
: hold.future.then((_) => replay());
168+
final hold = _reqHold;
169+
hold == null ? admit() : hold.future.then((_) => admit());
168170
case 'CLOSE':
169171
socket._live.remove(message[1]);
170172
case 'EVENT':

0 commit comments

Comments
 (0)