Skip to content

Commit 992adf9

Browse files
wesbillmanCarlclaude
committed
fix(mobile): give the shared channel read marks to one community
Channel ids may repeat across communities, so copying the shared channel and catch-up marks into every community could show a second community's channel as read. The first community to open now takes them; marks keyed by event id still seed every community. 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 7bcbc1a commit 992adf9

3 files changed

Lines changed: 84 additions & 22 deletions

File tree

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

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,6 @@ class ReadStateStorage {
9696
}
9797

9898
StoredReadState read(String pubkey, String relayUrl) {
99-
// The shared marks may be another community's, so each community starts
100-
// from them only as local marks. None is publishable, so none reaches its
101-
// relay; another community's IDs simply never match. They stay for the
102-
// other communities to start from.
10399
for (final prefix in const [
104100
_publishableKeyPrefix,
105101
_sourceCreatedAtKeyPrefix,
@@ -108,27 +104,45 @@ class ReadStateStorage {
108104
}
109105
final key = localReadStateKey(pubkey, relayUrl);
110106
return StoredReadState(
111-
contexts: _readContexts(
112-
_prefs.containsKey(key) ? key : _legacyKey(_contextsKeyPrefix, pubkey),
113-
),
107+
contexts: _prefs.containsKey(key)
108+
? _readContexts(key)
109+
: _claimLegacyContexts(pubkey),
114110
publishableContextIds: _readPublishableContextIds(pubkey, relayUrl),
115111
sourceCreatedAt: _readSourceCreatedAt(pubkey, relayUrl),
116112
);
117113
}
118114

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+
119135
void write(
120136
String pubkey,
121137
String relayUrl,
122138
Map<String, int> contexts,
123139
Set<String> publishableContextIds,
124140
Map<String, int> sourceCreatedAt,
125141
) {
126-
final state = <String, String>{};
127-
for (final entry in contexts.entries) {
128-
state[entry.key] = unixSecondsToDateTime(entry.value).toIso8601String();
129-
}
130-
131-
_prefs.setString(localReadStateKey(pubkey, relayUrl), jsonEncode(state));
142+
_prefs.setString(
143+
localReadStateKey(pubkey, relayUrl),
144+
_encodeContexts(contexts),
145+
);
132146
_prefs.setString(
133147
localPublishableContextKey(pubkey, relayUrl),
134148
jsonEncode(publishableContextIds.toList()),
@@ -139,6 +153,11 @@ class ReadStateStorage {
139153
);
140154
}
141155

156+
String _encodeContexts(Map<String, int> contexts) => jsonEncode({
157+
for (final entry in contexts.entries)
158+
entry.key: unixSecondsToDateTime(entry.value).toIso8601String(),
159+
});
160+
142161
Map<String, int> _readContexts(String key) {
143162
final raw = _prefs.getString(key);
144163
if (raw == null || raw.isEmpty) {
@@ -228,6 +247,12 @@ class ReadStateStorage {
228247
}
229248
}
230249

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+
231256
String generateReadStateSlotId() {
232257
final random = Random.secure();
233258
final bytes = List<int>.generate(16, (_) => random.nextInt(256));

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,10 @@ void main() {
311311
SharedPreferences.setMockInitialValues({
312312
'buzz.nip-rs.slot-id:pk': 'a' * 32,
313313
'buzz.channel-read-state.v2:pk':
314-
'{"channel-1":"2026-01-01T00:00:00.000"}',
315-
'buzz.channel-read-state.publishable.v1:pk': '["channel-1"]',
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"]',
316318
});
317319
final prefs = await SharedPreferences.getInstance();
318320
final storage = ReadStateStorage(prefs);
@@ -326,14 +328,15 @@ void main() {
326328
storage.writeSlotId('pk', 'https://other.example', 'b' * 32);
327329
expect(storage.getOrCreateSlotId('pk', _relayUrl), 'a' * 32);
328330

329-
// Every community keeps the shared marks, but only locally: they may be
330-
// another community's, so none is published to its relay.
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.
331334
final mine = storage.read('pk', _relayUrl);
332-
expect(mine.contexts.keys, ['channel-1']);
335+
expect(mine.contexts.keys, {'channel-1', 'thread-activity:root-1'});
333336
expect(mine.publishableContextIds, isEmpty);
334337
storage.write('pk', _relayUrl, {}, {}, {});
335338
final other = storage.read('pk', 'https://other.example');
336-
expect(other.contexts.keys, ['channel-1']);
339+
expect(other.contexts.keys, ['thread-activity:root-1']);
337340
expect(other.publishableContextIds, isEmpty);
338341
expect(storage.read('pk', _relayUrl).contexts, isEmpty);
339342
},

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import 'dart:convert';
2+
13
import 'package:buzz/shared/read_state/read_state_format.dart';
24
import 'package:buzz/shared/read_state/read_state_provider.dart';
5+
import 'package:buzz/shared/read_state/read_state_time.dart';
36
import 'package:buzz/shared/community/community_provider.dart';
47
import 'package:buzz/shared/relay/relay.dart';
58
import 'package:buzz/shared/theme/theme_provider.dart';
@@ -22,10 +25,13 @@ void main() {
2225
late ProviderContainer container;
2326
late _FakeRelayConfig config;
2427

25-
Future<ReadStateNotifier> pumpNotifier() async {
26-
SharedPreferences.setMockInitialValues({});
28+
Future<ReadStateNotifier> pumpNotifier({
29+
Map<String, Object> Function(String pubkey)? stored,
30+
}) async {
31+
final keys = nostr.Keys.generate();
32+
SharedPreferences.setMockInitialValues(stored?.call(keys.public) ?? {});
2733
final prefs = await SharedPreferences.getInstance();
28-
final nsec = nostr.Keys.generate().nsec;
34+
final nsec = keys.nsec;
2935
config = _FakeRelayConfig(nsec);
3036

3137
container = ProviderContainer(
@@ -82,6 +88,34 @@ void main() {
8288
expect(state().effectiveTimestamp(channelId), isNull);
8389
});
8490

91+
test('only the first community takes the shared channel marks', () async {
92+
// Recent, so the saved copy keeps the message mark.
93+
final readAt = DateTime.now().millisecondsSinceEpoch ~/ 1000;
94+
final at = unixSecondsToDateTime(readAt).toIso8601String();
95+
final catchUp = activityContextKey(channelId);
96+
await pumpNotifier(
97+
stored: (pubkey) => {
98+
'buzz.channel-read-state.v2:$pubkey': jsonEncode({
99+
channelId: at,
100+
catchUp: at,
101+
msgKey: at,
102+
}),
103+
},
104+
);
105+
expect(state().effectiveTimestamp(channelId), readAt);
106+
expect(state().effectiveTimestamp(catchUp), readAt);
107+
expect(state().effectiveTimestamp(msgKey), readAt);
108+
109+
// A second community may have a channel with the same id, so it takes
110+
// none of the first's channel marks, only those keyed by event id.
111+
config.switchTo('http://localhost:2');
112+
container.read(readStateProvider);
113+
await settle();
114+
expect(state().effectiveTimestamp(channelId), isNull);
115+
expect(state().effectiveTimestamp(catchUp), isNull);
116+
expect(state().effectiveTimestamp(msgKey), readAt);
117+
});
118+
85119
test('message unread → read → unread round-trips through the real '
86120
'notifier', () async {
87121
final notifier = await pumpNotifier();

0 commit comments

Comments
 (0)