Skip to content

Commit 1d32fcd

Browse files
committed
Keep mobile relay sessions stable during push lease bookkeeping
Signed-off-by: Tom Brow <[email protected]>
1 parent 8953cbf commit 1d32fcd

2 files changed

Lines changed: 92 additions & 6 deletions

File tree

‎mobile/lib/shared/relay/relay_provider.dart‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,16 @@ class Env {
8282
class RelayConfigNotifier extends Notifier<RelayConfig> {
8383
@override
8484
RelayConfig build() {
85-
// Watch the active community so that when it changes (community switch),
86-
// the config rebuilds, triggering the full provider cascade.
87-
final activeAsync = ref.watch(activeCommunityProvider);
88-
final active = activeAsync.value;
89-
if (active != null) {
90-
return RelayConfig(baseUrl: active.relayUrl, nsec: active.nsec);
85+
// Push lease bookkeeping updates the community while using its live
86+
// session. Only connection-context changes may restart that session.
87+
final context = ref.watch(
88+
activeCommunityProvider.select((value) {
89+
final active = value.value;
90+
return (id: active?.id, relayUrl: active?.relayUrl, nsec: active?.nsec);
91+
}),
92+
);
93+
if (context.relayUrl != null) {
94+
return RelayConfig(baseUrl: context.relayUrl!, nsec: context.nsec);
9195
}
9296

9397
// Fallback to compile-time env config (dev mode).
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import 'package:buzz/shared/community/community.dart';
2+
import 'package:buzz/shared/community/community_provider.dart';
3+
import 'package:buzz/shared/community/community_storage.dart';
4+
import 'package:buzz/shared/relay/relay_provider.dart';
5+
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:hooks_riverpod/hooks_riverpod.dart';
7+
8+
import 'community_storage_test.dart';
9+
10+
void main() {
11+
test(
12+
'reserving a push lease does not notify relay config listeners',
13+
() async {
14+
final storage = CommunityStorage(secure: FakeSecureStorage());
15+
final community = Community.create(
16+
name: 'Tom Town',
17+
relayUrl: 'https://relay.example.com',
18+
).copyWith(pushNotificationsEnabled: true);
19+
await storage.save(community);
20+
await storage.saveActiveId(community.id);
21+
final container = ProviderContainer(
22+
overrides: [
23+
communityStorageProvider.overrideWithValue(storage),
24+
communitySnapshotWriterProvider.overrideWithValue((_) async {}),
25+
],
26+
);
27+
addTearDown(container.dispose);
28+
await container.read(activeCommunityProvider.future);
29+
final changes = <RelayConfig>[];
30+
container.listen(relayConfigProvider, (_, next) => changes.add(next));
31+
await container
32+
.read(communityListProvider.notifier)
33+
.reservePushLeaseGeneration(community.id);
34+
await container.read(activeCommunityProvider.future);
35+
await container.pump();
36+
expect(
37+
changes,
38+
isEmpty,
39+
reason: 'Push metadata must not reset the connected relay session',
40+
);
41+
},
42+
);
43+
for (final change in ['community', 'relay', 'identity']) {
44+
test('$change change notifies relay config listeners', () async {
45+
final storage = CommunityStorage(secure: FakeSecureStorage());
46+
final original = Community.create(
47+
name: 'Original',
48+
relayUrl: 'https://relay.example.com',
49+
nsec: 'first',
50+
);
51+
await storage.save(original);
52+
await storage.saveActiveId(original.id);
53+
final container = ProviderContainer(
54+
overrides: [
55+
communityStorageProvider.overrideWithValue(storage),
56+
communitySnapshotWriterProvider.overrideWithValue((_) async {}),
57+
],
58+
);
59+
addTearDown(container.dispose);
60+
await container.read(activeCommunityProvider.future);
61+
final changes = <RelayConfig>[];
62+
container.listen(relayConfigProvider, (_, next) => changes.add(next));
63+
final updated = switch (change) {
64+
'community' => Community.create(
65+
name: 'Other',
66+
relayUrl: original.relayUrl,
67+
nsec: original.nsec,
68+
),
69+
'relay' => original.copyWith(relayUrl: 'https://other.example.com'),
70+
_ => original.copyWith(nsec: 'second'),
71+
};
72+
await storage.save(updated);
73+
await storage.saveActiveId(updated.id);
74+
container.invalidate(communityListProvider);
75+
await container.read(activeCommunityProvider.future);
76+
await container.pump();
77+
expect(changes, hasLength(1));
78+
expect(changes.single.baseUrl, updated.relayUrl);
79+
expect(changes.single.nsec, updated.nsec);
80+
});
81+
}
82+
}

0 commit comments

Comments
 (0)