Skip to content

Commit 89ebfcc

Browse files
wesbillmanCarlclaude
committed
fix(mobile): start each community fresh instead of claiming identity-wide caches
Stars, mutes and read marks saved per identity by earlier versions may belong to any of the identity's communities, since channel ids repeat across them. Drop them on upgrade, as sections already do, and let each community restore its own state from its relay. 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 6c898ce commit 89ebfcc

7 files changed

Lines changed: 63 additions & 125 deletions

File tree

‎mobile/lib/features/channels/channel_mutes/channel_mutes_storage.dart‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,10 @@ class ChannelMutesStorage {
7474
ChannelMutesStorage(this._prefs);
7575

7676
ChannelMuteStore read(String pubkey, String relayUrl) {
77-
final key = channelMutesKey(pubkey, relayUrl);
78-
// The first community to open claims the shared entry, which may hold
79-
// changes not yet published. Its channel ids only match that community's
80-
// channels, and the relay copy it merges with is already shared the same way.
81-
final legacyKey = legacyChannelMutesKey(pubkey);
82-
final legacy = _prefs.getString(legacyKey);
83-
if (legacy != null) {
84-
_prefs.remove(legacyKey);
85-
if (!_prefs.containsKey(key)) _prefs.setString(key, legacy);
86-
}
87-
88-
final raw = _prefs.getString(key);
77+
// The shared entry may hold another community's channels, and the relay
78+
// copy is authoritative, so drop it rather than let one community claim it.
79+
_prefs.remove(legacyChannelMutesKey(pubkey));
80+
final raw = _prefs.getString(channelMutesKey(pubkey, relayUrl));
8981
if (raw == null || raw.isEmpty) {
9082
return const ChannelMuteStore();
9183
}

‎mobile/lib/features/channels/channel_stars/channel_stars_storage.dart‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,10 @@ class ChannelStarsStorage {
7474
ChannelStarsStorage(this._prefs);
7575

7676
ChannelStarStore read(String pubkey, String relayUrl) {
77-
final key = channelStarsKey(pubkey, relayUrl);
78-
// The first community to open claims the shared entry, which may hold
79-
// changes not yet published. Its channel ids only match that community's
80-
// channels, and the relay copy it merges with is already shared the same way.
81-
final legacyKey = legacyChannelStarsKey(pubkey);
82-
final legacy = _prefs.getString(legacyKey);
83-
if (legacy != null) {
84-
_prefs.remove(legacyKey);
85-
if (!_prefs.containsKey(key)) _prefs.setString(key, legacy);
86-
}
87-
88-
final raw = _prefs.getString(key);
77+
// The shared entry may hold another community's channels, and the relay
78+
// copy is authoritative, so drop it rather than let one community claim it.
79+
_prefs.remove(legacyChannelStarsKey(pubkey));
80+
final raw = _prefs.getString(channelStarsKey(pubkey, relayUrl));
8981
if (raw == null || raw.isEmpty) {
9082
return const ChannelStarStore();
9183
}

‎mobile/lib/shared/read_state/read_state_storage.dart‎

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -96,53 +96,35 @@ class ReadStateStorage {
9696
}
9797

9898
StoredReadState read(String pubkey, String relayUrl) {
99+
// The shared marks may be another community's, and the relay keeps the
100+
// published ones, so drop them rather than let one community claim them.
99101
for (final prefix in const [
102+
_contextsKeyPrefix,
100103
_publishableKeyPrefix,
101104
_sourceCreatedAtKeyPrefix,
102105
]) {
103106
_prefs.remove(_legacyKey(prefix, pubkey));
104107
}
105-
final key = localReadStateKey(pubkey, relayUrl);
106108
return StoredReadState(
107-
contexts: _prefs.containsKey(key)
108-
? _readContexts(key)
109-
: _claimLegacyContexts(pubkey),
109+
contexts: _readContexts(pubkey, relayUrl),
110110
publishableContextIds: _readPublishableContextIds(pubkey, relayUrl),
111111
sourceCreatedAt: _readSourceCreatedAt(pubkey, relayUrl),
112112
);
113113
}
114114

115-
/// The shared marks come over as local marks only: none is publishable, so
116-
/// none reaches a community's relay. Channel ids may repeat across
117-
/// communities, so the first community to open claims the channel marks.
118-
/// Marks keyed by event id only ever match their own community, so they
119-
/// stay for the others to start from.
120-
Map<String, int> _claimLegacyContexts(String pubkey) {
121-
final legacyKey = _legacyKey(_contextsKeyPrefix, pubkey);
122-
final contexts = _readContexts(legacyKey);
123-
final shared = {
124-
for (final entry in contexts.entries)
125-
if (_isEventContext(entry.key)) entry.key: entry.value,
126-
};
127-
if (shared.isEmpty) {
128-
_prefs.remove(legacyKey);
129-
} else {
130-
_prefs.setString(legacyKey, _encodeContexts(shared));
131-
}
132-
return contexts;
133-
}
134-
135115
void write(
136116
String pubkey,
137117
String relayUrl,
138118
Map<String, int> contexts,
139119
Set<String> publishableContextIds,
140120
Map<String, int> sourceCreatedAt,
141121
) {
142-
_prefs.setString(
143-
localReadStateKey(pubkey, relayUrl),
144-
_encodeContexts(contexts),
145-
);
122+
final state = <String, String>{};
123+
for (final entry in contexts.entries) {
124+
state[entry.key] = unixSecondsToDateTime(entry.value).toIso8601String();
125+
}
126+
127+
_prefs.setString(localReadStateKey(pubkey, relayUrl), jsonEncode(state));
146128
_prefs.setString(
147129
localPublishableContextKey(pubkey, relayUrl),
148130
jsonEncode(publishableContextIds.toList()),
@@ -153,13 +135,8 @@ class ReadStateStorage {
153135
);
154136
}
155137

156-
String _encodeContexts(Map<String, int> contexts) => jsonEncode({
157-
for (final entry in contexts.entries)
158-
entry.key: unixSecondsToDateTime(entry.value).toIso8601String(),
159-
});
160-
161-
Map<String, int> _readContexts(String key) {
162-
final raw = _prefs.getString(key);
138+
Map<String, int> _readContexts(String pubkey, String relayUrl) {
139+
final raw = _prefs.getString(localReadStateKey(pubkey, relayUrl));
163140
if (raw == null || raw.isEmpty) {
164141
return {};
165142
}
@@ -247,12 +224,6 @@ class ReadStateStorage {
247224
}
248225
}
249226

250-
/// Message, thread and thread catch-up marks, keyed by event id.
251-
bool _isEventContext(String contextId) =>
252-
contextId.startsWith(msgContextPrefix) ||
253-
contextId.startsWith(threadContextPrefix) ||
254-
contextId.startsWith('thread-activity:');
255-
256227
String generateReadStateSlotId() {
257228
final random = Random.secure();
258229
final bytes = List<int>.generate(16, (_) => random.nextInt(256));

‎mobile/test/features/channels/channel_stars/channel_stars_storage_test.dart‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import 'package:shared_preferences/shared_preferences.dart';
77
/// Tests for [ChannelStarStore] parsing and [mergeStores] last-writer-wins.
88
void main() {
99
test(
10-
'the first community to open claims the shared stars and mutes',
10+
'identity-wide stars and mutes from before relay scoping are dropped',
1111
() async {
1212
SharedPreferences.setMockInitialValues({
1313
legacyChannelStarsKey(
@@ -17,17 +17,12 @@ void main() {
1717
'{"version":1,"channels":{"chan-a":{"muted":true,"updatedAt":1}}}',
1818
});
1919
final prefs = await SharedPreferences.getInstance();
20-
const a = 'https://relay-a.example';
21-
const b = 'https://relay-b.example';
22-
final stars = ChannelStarsStorage(prefs);
23-
final mutes = ChannelMutesStorage(prefs);
20+
const relayUrl = 'https://relay.example';
2421

25-
expect(stars.read('pk', a).channels.keys, ['chan-a']);
26-
expect(mutes.read('pk', a).channels.keys, ['chan-a']);
27-
expect(stars.read('pk', b).channels, isEmpty);
28-
expect(mutes.read('pk', b).channels, isEmpty);
29-
expect(stars.read('pk', a).channels.keys, ['chan-a']);
30-
expect(mutes.read('pk', a).channels.keys, ['chan-a']);
22+
expect(ChannelStarsStorage(prefs).read('pk', relayUrl).channels, isEmpty);
23+
expect(ChannelMutesStorage(prefs).read('pk', relayUrl).channels, isEmpty);
24+
expect(prefs.containsKey(legacyChannelStarsKey('pk')), isFalse);
25+
expect(prefs.containsKey(legacyChannelMutesKey('pk')), isFalse);
3126
},
3227
);
3328

‎mobile/test/features/channels/read_state/read_state_manager_test.dart‎

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -305,42 +305,35 @@ void main() {
305305
);
306306
});
307307

308-
test(
309-
'each community keeps its own slot and marks, seeded from the shared ones',
310-
() async {
311-
SharedPreferences.setMockInitialValues({
312-
'buzz.nip-rs.slot-id:pk': 'a' * 32,
313-
'buzz.channel-read-state.v2:pk':
314-
'{"channel-1":"2026-01-01T00:00:00.000",'
315-
'"thread-activity:root-1":"2026-01-01T00:00:00.000"}',
316-
'buzz.channel-read-state.publishable.v1:pk':
317-
'["channel-1","thread-activity:root-1"]',
318-
});
319-
final prefs = await SharedPreferences.getInstance();
320-
final storage = ReadStateStorage(prefs);
308+
test('each community keeps its own slot, seeded from the shared one, '
309+
'and none takes the shared marks', () async {
310+
SharedPreferences.setMockInitialValues({
311+
'buzz.nip-rs.slot-id:pk': 'a' * 32,
312+
'buzz.channel-read-state.v2:pk':
313+
'{"channel-1":"2026-01-01T00:00:00.000",'
314+
'"thread-activity:root-1":"2026-01-01T00:00:00.000"}',
315+
'buzz.channel-read-state.publishable.v1:pk':
316+
'["channel-1","thread-activity:root-1"]',
317+
});
318+
final prefs = await SharedPreferences.getInstance();
319+
final storage = ReadStateStorage(prefs);
321320

322-
// Both communities keep publishing to the slot they already have.
323-
expect(storage.getOrCreateSlotId('pk', _relayUrl), 'a' * 32);
324-
expect(
325-
storage.getOrCreateSlotId('pk', 'https://other.example'),
326-
'a' * 32,
327-
);
328-
storage.writeSlotId('pk', 'https://other.example', 'b' * 32);
329-
expect(storage.getOrCreateSlotId('pk', _relayUrl), 'a' * 32);
330-
331-
// The shared marks come over only locally: they may be another
332-
// community's, so none is published to its relay. Channel ids may
333-
// repeat across communities, so only the first takes channel marks.
334-
final mine = storage.read('pk', _relayUrl);
335-
expect(mine.contexts.keys, {'channel-1', 'thread-activity:root-1'});
336-
expect(mine.publishableContextIds, isEmpty);
337-
storage.write('pk', _relayUrl, {}, {}, {});
338-
final other = storage.read('pk', 'https://other.example');
339-
expect(other.contexts.keys, ['thread-activity:root-1']);
340-
expect(other.publishableContextIds, isEmpty);
341-
expect(storage.read('pk', _relayUrl).contexts, isEmpty);
342-
},
343-
);
321+
// Both communities keep publishing to the slot they already have.
322+
expect(storage.getOrCreateSlotId('pk', _relayUrl), 'a' * 32);
323+
expect(storage.getOrCreateSlotId('pk', 'https://other.example'), 'a' * 32);
324+
storage.writeSlotId('pk', 'https://other.example', 'b' * 32);
325+
expect(storage.getOrCreateSlotId('pk', _relayUrl), 'a' * 32);
326+
327+
// The shared marks may be another community's, and the relay keeps
328+
// the published ones, so neither community takes them.
329+
expect(storage.read('pk', _relayUrl).contexts, isEmpty);
330+
expect(storage.read('pk', 'https://other.example').contexts, isEmpty);
331+
expect(prefs.containsKey('buzz.channel-read-state.v2:pk'), isFalse);
332+
expect(
333+
prefs.containsKey('buzz.channel-read-state.publishable.v1:pk'),
334+
isFalse,
335+
);
336+
});
344337

345338
test('carries its own override keys and takes none from others', () async {
346339
SharedPreferences.setMockInitialValues({});

‎mobile/test/features/channels/read_state/read_state_provider_test.dart‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,32 +69,24 @@ void main() {
6969
// Recent, so the saved copy keeps message marks.
7070
final readAt = DateTime.now().millisecondsSinceEpoch ~/ 1000;
7171
final at = unixSecondsToDateTime(readAt).toIso8601String();
72-
final catchUp = activityContextKey(channelId);
73-
// Upgrading from the identity-wide marks: the first community to open
74-
// takes them all, kept locally.
72+
// Upgrading from the identity-wide marks: they may be another
73+
// community's, and the relay keeps the published ones, so none is taken.
7574
final notifier = await pumpNotifier(
7675
stored: (pubkey) => {
7776
'buzz.channel-read-state.v2:$pubkey': jsonEncode({
7877
channelId: at,
79-
catchUp: at,
8078
otherMsgKey: at,
8179
}),
8280
},
8381
);
84-
expect(state().effectiveTimestamp(channelId), readAt);
85-
expect(state().effectiveTimestamp(catchUp), readAt);
82+
expect(state().contexts, isEmpty);
8683
notifier.markContextRead(msgKey, readAt);
8784

88-
// A second community may have a channel with the same id, so it takes
89-
// only the shared marks keyed by event id, and none of the first
90-
// community's own, which it would otherwise publish to its relay.
85+
// Each community keeps its own marks, even for a channel with the same id.
9186
config.switchTo('http://localhost:2');
9287
container.read(readStateProvider);
9388
await settle();
94-
expect(state().effectiveTimestamp(channelId), isNull);
95-
expect(state().effectiveTimestamp(catchUp), isNull);
96-
expect(state().effectiveTimestamp(otherMsgKey), readAt);
97-
expect(state().effectiveTimestamp(msgKey), isNull);
89+
expect(state().contexts, isEmpty);
9890
container
9991
.read(readStateProvider.notifier)
10092
.markContextRead(channelId, readAt + 5);
@@ -103,7 +95,7 @@ void main() {
10395
container.read(readStateProvider);
10496
await settle();
10597
expect(state().effectiveTimestamp(msgKey), readAt);
106-
expect(state().effectiveTimestamp(channelId), readAt);
98+
expect(state().effectiveTimestamp(channelId), isNull);
10799
});
108100

109101
test('message unread → read → unread round-trips through the real '

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ class FlakyPrefs extends Fake implements SharedPreferences {
180180
@override
181181
String? getString(String key) => _inner.getString(key);
182182

183+
@override
184+
Future<bool> remove(String key) => _inner.remove(key);
185+
183186
@override
184187
Future<bool> setString(String key, String value) async {
185188
if (failures == 0) return _inner.setString(key, value);

0 commit comments

Comments
 (0)