mirror of
https://github.com/block/buzz.git
synced 2026-11-25 18:52:52 +00:00
## Summary One identity can be paired with several communities, but mobile keyed several caches by pubkey only. Mobile keeps a single `ProviderScope` and doesn't remount on a community switch, so one community's data showed up in another, and some of it was published to the other community's relay. - **Channel groups**: the second community showed the first community's groups. - **Read marks** (contexts, publishable set, NIP-RS client and slot IDs): the first community's context IDs were merged into, and published to, the second community's read-state slot. - **Inbox done/unread rows and thread follows**: one shared 500-entry cap, so activity in one community evicted another's entries. - **Channel stars and mutes**: union-merged across communities and published to every relay. Every one of these is now keyed by pubkey + normalized community relay, the same way channel sort already is. **Switching communities.** The relay session rebuilds for the new community before the previous community's managers are retired. Each manager (groups, read state, stars, mutes) now checks that the session still serves its own community before it publishes, merges a fetch, or handles a live event. Previously, a pending publish flushed on dispose, or an in-flight fetch, could cross into the next community. **Old pubkey-only entries.** Channel IDs can repeat across communities, so nothing tells us which community an old channel-keyed entry belongs to: - **Groups, stars, mutes and read marks** are dropped on upgrade, and each community restores its own from its relay. Any change that never reached a relay is lost, and a read mark the relay doesn't hold shows as unread once. That's an accepted tradeoff: such changes would be very old by now. - **Inbox, thread follows, and read-state slot and client IDs** seed every community, and the old entry is kept. The inbox and follows entries are event IDs, which only ever match their own community. The slot ID keeps each community publishing to the slot it already has on its relay. This PR also removes the **Pulse** feed (`features/pulse/` and the Nostr filters only it used). Nothing in the app navigates to it anymore, and its providers were among the community-unscoped caches. ### Related issue None found. ### Testing - Each affected store has a regression test through the real provider that switches A → B → A. For groups, read marks, stars and mutes, the test sets a value in A, switches to B while A's change is still pending, and checks two cases: B's data arriving live and B answering A's in-flight fetch. It asserts that nothing sent to B contains A's IDs, B shows only its own value, and A comes back unchanged. Each guard was removed in turn to confirm a test fails without it, except two that the current switch order never reaches: the groups merge guard and the read-state live-event guard. - Upgrade tests cover each kind of old entry: dropped for groups, stars, mutes and read marks, seeded for inbox, follows and slot IDs. - `flutter analyze` is clean, and the full mobile suite passes in the pre-push hook at `89ebfcc12`. - Not yet exercised in the app. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Wes <wesbillman@users.noreply.github.com> Co-authored-by: Carl <32a2e2c9d428ee08902cab75d956da2c1d235a22d4766b0dd4138bf6e2e5db1d@buzz.block.builderlab.xyz> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
561 lines
17 KiB
Dart
561 lines
17 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'dart:math';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:nostr/nostr.dart' as nostr;
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
import '../../../shared/crypto/nip44.dart';
|
|
import '../../../shared/relay/relay.dart';
|
|
import '../../../shared/read_state/read_state_time.dart';
|
|
import 'channel_sections_storage.dart';
|
|
|
|
const _uuid = Uuid();
|
|
|
|
class ChannelSectionsCrypto {
|
|
final Uint8List _conversationKey;
|
|
|
|
ChannelSectionsCrypto(String nsec, String pubkey)
|
|
: _conversationKey = _deriveKey(nsec, pubkey);
|
|
|
|
static Uint8List _deriveKey(String nsec, String pubkey) {
|
|
final privkeyHex = nostr.Nip19.decode(payload: nsec).data;
|
|
return getConversationKey(privkeyHex, pubkey);
|
|
}
|
|
|
|
String encrypt(String plaintext) => nip44Encrypt(_conversationKey, plaintext);
|
|
|
|
String decrypt(String ciphertext) =>
|
|
nip44Decrypt(_conversationKey, ciphertext);
|
|
}
|
|
|
|
class ChannelSectionsManager {
|
|
final String pubkey;
|
|
final String relayUrl;
|
|
final ChannelSectionsStorage _storage;
|
|
final ChannelSectionsCrypto _crypto;
|
|
final RelaySessionNotifier? _relaySession;
|
|
final SignedEventRelay? _signedEventRelay;
|
|
final bool _remoteEnabled;
|
|
final VoidCallback _onChanged;
|
|
|
|
ChannelSectionStore _store;
|
|
ChannelSectionStore? _lastPublishedStore;
|
|
Timer? _publishDebounce;
|
|
int _lastRemoteCreatedAt = 0;
|
|
String? _lastRemoteEventId;
|
|
void Function()? _unsubscribe;
|
|
bool _disposed = false;
|
|
|
|
/// Base delay for the startup-sync retry backoff. Overridable in tests.
|
|
final Duration _startupRetryBaseDelay;
|
|
Timer? _startupRetryTimer;
|
|
int _startupRetryAttempt = 0;
|
|
bool _startupFetchSucceeded = false;
|
|
Future<void>? _syncInFlight;
|
|
bool _syncAgain = false;
|
|
int _subscriptionGeneration = 0;
|
|
int _localRevision = 0;
|
|
bool _publishPending = false;
|
|
|
|
ChannelSectionsManager({
|
|
required this.pubkey,
|
|
required this.relayUrl,
|
|
required SharedPreferences prefs,
|
|
required ChannelSectionsCrypto crypto,
|
|
required RelaySessionNotifier? relaySession,
|
|
required SignedEventRelay? signedEventRelay,
|
|
required bool remoteEnabled,
|
|
required VoidCallback onChanged,
|
|
@visibleForTesting
|
|
Duration startupRetryBaseDelay = const Duration(seconds: 2),
|
|
}) : _storage = ChannelSectionsStorage(prefs),
|
|
_crypto = crypto,
|
|
_relaySession = relaySession,
|
|
_signedEventRelay = signedEventRelay,
|
|
_remoteEnabled = remoteEnabled,
|
|
_onChanged = onChanged,
|
|
_startupRetryBaseDelay = startupRetryBaseDelay,
|
|
_store = ChannelSectionsStorage(prefs).read(pubkey, relayUrl);
|
|
|
|
ChannelSectionStore get store => _store;
|
|
|
|
Future<void> initialize() async {
|
|
if (_disposed) return;
|
|
|
|
if (!_remoteEnabled || _relaySession == null) {
|
|
_onChanged();
|
|
return;
|
|
}
|
|
|
|
await _syncWithRelay();
|
|
_onChanged();
|
|
}
|
|
|
|
/// Re-reads the retained head once, e.g. on app foreground resume, to catch
|
|
/// an EVENT a healthy socket never delivered. Skipped while a local edit is
|
|
/// pending and discarded if one lands before the read returns.
|
|
void refreshFromRelay() {
|
|
if (_disposed || !_remoteEnabled || _publishPending) return;
|
|
final revision = _localRevision;
|
|
unawaited(
|
|
_fetchAndMerge(
|
|
isCurrent: () => revision == _localRevision && !_publishPending,
|
|
),
|
|
);
|
|
}
|
|
|
|
/// One startup-sync attempt: fetch the remote blob, then start the live
|
|
/// subscription. Either step can lose a transient race on cold start (the
|
|
/// relay rate-limits the burst of per-channel subscriptions and rejects
|
|
/// with `rate-limited: quota exceeded`) — retry with backoff instead of
|
|
/// silently giving up, which left desktop-created groups invisible until
|
|
/// an unrelated refetch.
|
|
///
|
|
/// Retries are intentionally unbounded for the manager's lifetime: this
|
|
/// sync must eventually land for groups to appear at all, and at the 30s
|
|
/// delay ceiling a persistent retry is cheap. Do not "fix" this into a
|
|
/// bounded loop — giving up permanently is the exact bug this replaces.
|
|
Future<void> _syncWithRelay() {
|
|
if (_disposed) return Future.value();
|
|
final inFlight = _syncInFlight;
|
|
if (inFlight != null) {
|
|
_syncAgain = true;
|
|
return inFlight;
|
|
}
|
|
|
|
final sync = _runSyncWithRelay();
|
|
_syncInFlight = sync;
|
|
return sync.whenComplete(() {
|
|
_syncInFlight = null;
|
|
if (_disposed || !_syncAgain) return;
|
|
_syncAgain = false;
|
|
unawaited(_syncWithRelay());
|
|
});
|
|
}
|
|
|
|
Future<void> _runSyncWithRelay() async {
|
|
if (!_startupFetchSucceeded) {
|
|
final fetched = await _fetchAndMerge();
|
|
if (_disposed) return;
|
|
_startupFetchSucceeded = fetched;
|
|
}
|
|
|
|
final subscribed = _unsubscribe != null || await _startLiveSubscription();
|
|
if (_disposed) return;
|
|
|
|
if (!_startupFetchSucceeded || !subscribed) {
|
|
_scheduleStartupRetry();
|
|
} else {
|
|
// Fully recovered — later transient failures (e.g. a late relay
|
|
// CLOSED) start backing off from the base delay again instead of the
|
|
// ceiling the cold start climbed to.
|
|
_startupRetryAttempt = 0;
|
|
}
|
|
}
|
|
|
|
void _scheduleStartupRetry() {
|
|
if (_disposed) return;
|
|
_startupRetryTimer?.cancel();
|
|
// The inner shift cap is overflow protection, not the delay policy: the
|
|
// consecutive-failure counter is unbounded, and an unchecked `<<`
|
|
// past 62 wraps negative, which would make the Timer fire immediately in
|
|
// a hot loop. At the default 2s base the outer 30s clamp is what callers
|
|
// actually observe (2s, 4s, …, 30s); the shift cap only bites for the
|
|
// tiny injected bases used in tests.
|
|
final delayMs = min(
|
|
_startupRetryBaseDelay.inMilliseconds << min(_startupRetryAttempt, 5),
|
|
30000,
|
|
);
|
|
_startupRetryAttempt++;
|
|
debugPrint(
|
|
'[ChannelSectionsManager] startup sync incomplete; '
|
|
'retrying in ${delayMs}ms (attempt $_startupRetryAttempt)',
|
|
);
|
|
_startupRetryTimer = Timer(Duration(milliseconds: delayMs), () {
|
|
_startupRetryTimer = null;
|
|
unawaited(
|
|
_syncWithRelay().then((_) {
|
|
if (!_disposed) _onChanged();
|
|
}),
|
|
);
|
|
});
|
|
}
|
|
|
|
void dispose({bool flushPending = true}) {
|
|
if (_disposed) return;
|
|
_disposed = true;
|
|
_subscriptionGeneration++;
|
|
_syncAgain = false;
|
|
|
|
_startupRetryTimer?.cancel();
|
|
_startupRetryTimer = null;
|
|
|
|
final hadPending = _publishDebounce != null;
|
|
_publishDebounce?.cancel();
|
|
_publishDebounce = null;
|
|
|
|
if (flushPending && hadPending && _remoteEnabled) {
|
|
unawaited(_publish(allowDisposed: true));
|
|
}
|
|
|
|
_unsubscribe?.call();
|
|
_unsubscribe = null;
|
|
}
|
|
|
|
void createSection(String name) {
|
|
if (_disposed) return;
|
|
final maxOrder = _store.sections.fold<int>(
|
|
-1,
|
|
(max, s) => s.order > max ? s.order : max,
|
|
);
|
|
final section = ChannelSection(
|
|
id: _uuid.v4(),
|
|
name: name.trim(),
|
|
order: maxOrder + 1,
|
|
);
|
|
_store = ChannelSectionStore(
|
|
sections: [..._store.sections, section],
|
|
assignments: _store.assignments,
|
|
);
|
|
_persist();
|
|
markDirty();
|
|
}
|
|
|
|
void renameSection(String sectionId, String newName) {
|
|
if (_disposed) return;
|
|
_store = ChannelSectionStore(
|
|
sections: [
|
|
for (final s in _store.sections)
|
|
if (s.id == sectionId)
|
|
ChannelSection(
|
|
id: s.id,
|
|
name: newName.trim(),
|
|
icon: s.icon,
|
|
order: s.order,
|
|
)
|
|
else
|
|
s,
|
|
],
|
|
assignments: _store.assignments,
|
|
);
|
|
_persist();
|
|
markDirty();
|
|
}
|
|
|
|
void deleteSection(String sectionId) {
|
|
if (_disposed) return;
|
|
final updatedAssignments = Map<String, String>.from(_store.assignments)
|
|
..removeWhere((_, sid) => sid == sectionId);
|
|
_store = ChannelSectionStore(
|
|
sections: [
|
|
for (final s in _store.sections)
|
|
if (s.id != sectionId) s,
|
|
],
|
|
assignments: updatedAssignments,
|
|
);
|
|
_persist();
|
|
markDirty();
|
|
}
|
|
|
|
void moveSectionUp(String sectionId) {
|
|
if (_disposed) return;
|
|
final sorted = _sortedSections();
|
|
final idx = sorted.indexWhere((s) => s.id == sectionId);
|
|
if (idx <= 0) return;
|
|
_swapOrders(sorted, idx, idx - 1);
|
|
markDirty();
|
|
}
|
|
|
|
void moveSectionDown(String sectionId) {
|
|
if (_disposed) return;
|
|
final sorted = _sortedSections();
|
|
final idx = sorted.indexWhere((s) => s.id == sectionId);
|
|
if (idx < 0 || idx >= sorted.length - 1) return;
|
|
_swapOrders(sorted, idx, idx + 1);
|
|
markDirty();
|
|
}
|
|
|
|
void assignChannel(String channelId, String sectionId) {
|
|
if (_disposed) return;
|
|
final updated = Map<String, String>.from(_store.assignments)
|
|
..[channelId] = sectionId;
|
|
_store = ChannelSectionStore(
|
|
sections: _store.sections,
|
|
assignments: updated,
|
|
);
|
|
_persist();
|
|
markDirty();
|
|
}
|
|
|
|
void unassignChannel(String channelId) {
|
|
if (_disposed) return;
|
|
final updated = Map<String, String>.from(_store.assignments)
|
|
..remove(channelId);
|
|
_store = ChannelSectionStore(
|
|
sections: _store.sections,
|
|
assignments: updated,
|
|
);
|
|
_persist();
|
|
markDirty();
|
|
}
|
|
|
|
void markDirty() {
|
|
if (!_remoteEnabled || _disposed) return;
|
|
_localRevision++;
|
|
_publishPending = true;
|
|
_publishDebounce?.cancel();
|
|
_publishDebounce = Timer(const Duration(seconds: 5), () {
|
|
_publishDebounce = null;
|
|
unawaited(
|
|
_publish().whenComplete(
|
|
() => _publishPending = _publishDebounce != null,
|
|
),
|
|
);
|
|
});
|
|
}
|
|
|
|
/// Returns whether the fetch reached the relay (regardless of whether a
|
|
/// remote blob exists).
|
|
Future<bool> _fetchAndMerge({
|
|
bool allowDisposed = false,
|
|
bool Function()? isCurrent,
|
|
}) async {
|
|
if (_relaySession == null) return false;
|
|
try {
|
|
final events = await _relaySession.fetchHistory(
|
|
NostrFilter(
|
|
kinds: const [EventKind.readState],
|
|
authors: [pubkey],
|
|
tags: const {
|
|
'#d': ['channel-sections'],
|
|
},
|
|
limit: 1,
|
|
),
|
|
);
|
|
if (_disposed && !allowDisposed) return false;
|
|
if (isCurrent != null && !isCurrent()) return false;
|
|
_mergeEvents(events);
|
|
_persist();
|
|
if (!_disposed) _onChanged();
|
|
return true;
|
|
} catch (error) {
|
|
debugPrint('[ChannelSectionsManager] fetch failed: $error');
|
|
// Local state remains usable when relay is unavailable.
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// Returns whether the live subscription was established.
|
|
Future<bool> _startLiveSubscription() async {
|
|
if (_relaySession == null || _disposed) return false;
|
|
final generation = ++_subscriptionGeneration;
|
|
try {
|
|
final unsubscribe = await _relaySession.subscribe(
|
|
NostrFilter(
|
|
kinds: const [EventKind.readState],
|
|
authors: [pubkey],
|
|
tags: const {
|
|
'#d': ['channel-sections'],
|
|
},
|
|
limit: 1,
|
|
),
|
|
_handleIncomingEvent,
|
|
onClosed: (message) => _handleSubscriptionClosed(generation, message),
|
|
);
|
|
if (_disposed || generation != _subscriptionGeneration) {
|
|
unsubscribe();
|
|
return false;
|
|
}
|
|
_unsubscribe = unsubscribe;
|
|
return true;
|
|
} catch (error) {
|
|
debugPrint('[ChannelSectionsManager] live subscription failed: $error');
|
|
// Non-fatal — local state and history still work; retried by the
|
|
// startup-sync backoff.
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// A relay `CLOSED` can arrive after `subscribe()` already reported
|
|
/// success: the 500ms readiness wait times out silently under load, and
|
|
/// the rate-limit rejection lands later. Without this handler the manager
|
|
/// would keep a dead subscription and never retry — the exact
|
|
/// load-correlated cold-start failure this retry exists for.
|
|
void _handleSubscriptionClosed(int generation, String message) {
|
|
if (_disposed || generation != _subscriptionGeneration) return;
|
|
debugPrint(
|
|
'[ChannelSectionsManager] live subscription closed by relay: $message',
|
|
);
|
|
_unsubscribe = null;
|
|
_subscriptionGeneration++;
|
|
_scheduleStartupRetry();
|
|
}
|
|
|
|
void _mergeEvents(List<NostrEvent> events) {
|
|
for (final event in events) {
|
|
if (event.pubkey != pubkey) continue;
|
|
_mergeEvent(event);
|
|
}
|
|
}
|
|
|
|
void _mergeEvent(NostrEvent event) {
|
|
// Another community's groups must never reach this community's store.
|
|
if (!_servesThisCommunity) return;
|
|
// Only process channel-sections d-tag events.
|
|
final dTag = event.getTagValue('d');
|
|
if (dTag != 'channel-sections') return;
|
|
|
|
try {
|
|
final plaintext = _crypto.decrypt(event.content);
|
|
final parsed = jsonDecode(plaintext);
|
|
if (parsed is! Map<String, dynamic>) return;
|
|
|
|
final incoming = ChannelSectionStore.fromJson(parsed);
|
|
|
|
if (_isAfterCursor(event.createdAt, event.id)) {
|
|
_lastRemoteCreatedAt = event.createdAt;
|
|
_lastRemoteEventId = event.id;
|
|
_store = incoming;
|
|
_persist();
|
|
// A pending debounce stays armed: its republish re-converges an OK
|
|
// that later wins the cursor after this head replaced the store.
|
|
}
|
|
} catch (_) {
|
|
// Decryption failure or parse error — keep existing state.
|
|
}
|
|
}
|
|
|
|
/// Last-write-wins. Relay retains `ORDER BY created_at DESC, id ASC`: at
|
|
/// equal second the lower event ID wins.
|
|
bool _isAfterCursor(int createdAt, String id) =>
|
|
createdAt > _lastRemoteCreatedAt ||
|
|
(createdAt == _lastRemoteCreatedAt &&
|
|
id.compareTo(_lastRemoteEventId ?? '') < 0);
|
|
|
|
void _handleIncomingEvent(NostrEvent event) {
|
|
if (_disposed) return;
|
|
_mergeEvent(event);
|
|
if (!_disposed) _onChanged();
|
|
}
|
|
|
|
bool _isIdenticalToLastPublished() {
|
|
final last = _lastPublishedStore;
|
|
if (last == null) return false;
|
|
if (last.sections.length != _store.sections.length) return false;
|
|
if (last.assignments.length != _store.assignments.length) return false;
|
|
for (var i = 0; i < _store.sections.length; i++) {
|
|
final a = last.sections[i];
|
|
final b = _store.sections[i];
|
|
if (a.id != b.id ||
|
|
a.name != b.name ||
|
|
a.icon != b.icon ||
|
|
a.order != b.order) {
|
|
return false;
|
|
}
|
|
}
|
|
for (final key in _store.assignments.keys) {
|
|
if (last.assignments[key] != _store.assignments[key]) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
Future<void> _publish({bool allowDisposed = false}) async {
|
|
if ((!allowDisposed && _disposed) ||
|
|
!_remoteEnabled ||
|
|
_signedEventRelay == null ||
|
|
!_servesThisCommunity) {
|
|
return;
|
|
}
|
|
|
|
// Read-before-write: merge remote state before publishing
|
|
await _fetchAndMerge(allowDisposed: allowDisposed);
|
|
|
|
// No-op suppression: skip if nothing changed
|
|
if (_isIdenticalToLastPublished() || !_servesThisCommunity) return;
|
|
|
|
final submitted = _store;
|
|
final revision = _localRevision;
|
|
try {
|
|
final payload = jsonEncode(submitted.toJson());
|
|
final ciphertext = _crypto.encrypt(payload);
|
|
final createdAt = max(currentUnixSeconds(), _lastRemoteCreatedAt + 1);
|
|
|
|
String? signedId;
|
|
await _signedEventRelay.submit(
|
|
kind: EventKind.readState,
|
|
content: ciphertext,
|
|
tags: [
|
|
['d', 'channel-sections'],
|
|
['t', 'channel-sections'],
|
|
],
|
|
createdAt: createdAt,
|
|
onSigned: (event) => signedId = event.id,
|
|
);
|
|
|
|
// Keep the cursor a coherent (created_at, id) pair so an OK that beats
|
|
// its own echo cannot make later same-second heads compare against a
|
|
// stale ID. A newer head learned during the await stays.
|
|
if (signedId != null && _isAfterCursor(createdAt, signedId!)) {
|
|
_lastRemoteCreatedAt = createdAt;
|
|
_lastRemoteEventId = signedId;
|
|
// A same-second loser merged during the await must not strand the
|
|
// device on content the relay did not keep.
|
|
if (revision == _localRevision && !identical(_store, submitted)) {
|
|
_store = submitted;
|
|
_persist();
|
|
if (!_disposed) _onChanged();
|
|
}
|
|
}
|
|
_lastPublishedStore = ChannelSectionStore(
|
|
sections: List.of(submitted.sections),
|
|
assignments: Map.of(submitted.assignments),
|
|
);
|
|
} catch (error) {
|
|
debugPrint('[ChannelSectionsManager] publish failed: $error');
|
|
}
|
|
}
|
|
|
|
/// Switching community rebuilds the session in place before this manager
|
|
/// is retired, so in between it must neither take in nor publish data.
|
|
bool get _servesThisCommunity {
|
|
final served = _relaySession?.relayUrl;
|
|
return served == null || served == relayUrl;
|
|
}
|
|
|
|
/// A retired manager never writes the prefs key its successor now owns.
|
|
void _persist() {
|
|
if (_disposed) return;
|
|
_storage.write(pubkey, relayUrl, _store);
|
|
}
|
|
|
|
List<ChannelSection> _sortedSections() {
|
|
final sorted = _store.sections.toList()
|
|
..sort((a, b) => a.order.compareTo(b.order));
|
|
return sorted;
|
|
}
|
|
|
|
void _swapOrders(List<ChannelSection> sorted, int indexA, int indexB) {
|
|
final orderA = sorted[indexA].order;
|
|
final orderB = sorted[indexB].order;
|
|
final idA = sorted[indexA].id;
|
|
final idB = sorted[indexB].id;
|
|
|
|
_store = ChannelSectionStore(
|
|
sections: [
|
|
for (final s in _store.sections)
|
|
if (s.id == idA)
|
|
ChannelSection(id: s.id, name: s.name, icon: s.icon, order: orderB)
|
|
else if (s.id == idB)
|
|
ChannelSection(id: s.id, name: s.name, icon: s.icon, order: orderA)
|
|
else
|
|
s,
|
|
],
|
|
assignments: _store.assignments,
|
|
);
|
|
_persist();
|
|
}
|
|
}
|