mirror of
https://github.com/block/buzz.git
synced 2026-10-27 04:30:52 +00:00
## Summary Returning to a previously visited community shows its saved channel list immediately, including offline, while membership and unread state refresh in the background. Initial revalidation failures keep the list visible and retry every 60 seconds without overlapping background attempts. After successful history revalidation, ordinary reconnects use metadata-only refreshes. A reconnect arriving during a background request queues one follow-up attempt; timer ticks never queue overlapping work. History deadlines retain the full-revalidation retry until history succeeds. Snapshots are disposable, versioned and scoped by canonical relay URL and signing identity, capped at 2 MB / 2,000 member channels. They include hidden-DM IDs and prefer fresher in-memory state. Community removal and identity replacement retire outstanding writes; re-adding unchanged credentials preserves the current cache and writer. Startup sweeps orphan snapshots without waiting for disk acknowledgement; community mutations also sweep orphan snapshots, including data restored without secure community records. Cache-delete failures do not block sign-out or strand push revocation; deletion is attempted before journaling and retried by orphan cleanup. Successfully persisted unchanged snapshots skip disk writes; failed writes remain retryable. The cold path reuses fetched membership rosters, overlaps profile/latest-message requests, shares verified DM profiles with avatars, and fetches the own profile once per authenticated connection. Community connection work overlaps the switching animation; avatar hydration no longer gates landing. Full channel refreshes also revalidate cached DM participant profiles so relay-side name changes reach labels and avatars; metadata-only reconnects and background refreshes reuse admitted profiles. Me-chat and invite starter recovery use an explicit fresh membership fetch that propagates errors while leaving the cached list visible. Cached layout/read preferences permit immediate landing; newer cross-device preferences may update ordering or badges after reveal. Integrated with main’s #8490 idle/resume changes: hidden chats detach live subscriptions, background membership polling stays quiet, and each resume reconciles once. Chat history follows main’s live-admission-first ordering to avoid missing messages committed during relay admission; cached community landing remains immediate. ## Validation - `just mobile-install mobile-check mobile-test`: clean analysis; 3,778 tests passed, 19 skipped; four checks for builds without push configuration passed. File-size and diff checks passed. - Provider/storage journeys cover offline startup, failed initial revalidation recovering live delivery, bounded retries, lightweight reconnects, scope isolation, default-port URLs, padded/mismatched stored credentials, credential replacement, backup-restored orphan data, removal races, failed platform deletes and failed writes. - Follow-up regressions cover interrupted native Me-chat lookup, invite failure propagation, retry after an initial list failure, history deadlines, queued reconnects, escaped-path keys, unchanged credential re-adds and non-blocking startup cleanup. Reverting each of the eight behavior/caller/cleanup-order fixes fails its corresponding regression. - DM-name regression changes only the relay profile, verifies a full refresh updates the channel label and shared profile cache, and confirms ordinary reconnects avoid another profile request. The original cache-only implementation fails this regression. - Original signed-in iPhone 18 Pro debug simulator measurements: Buzz's 413 channels went from 6.26–6.30 s to approximately 1.5 s on returning; all three warmed communities switched in 1.47–1.54 s. Real socket pause restored the cached list offline and retained it on reconnect. These are debug simulator observations, not release-device benchmarks. - Integration with current main also passes the real-session resume/background/AUTH and live-admission/detach journeys. - Follow-up failure paths were exercised with deterministic provider/storage tests and widget journeys; the running simulator was not modified during this follow-up. Human acceptance remains pending; this PR stays draft. Try switching among previously visited communities, opening a channel, restarting and repeating. First visits and oversized snapshots require network loading. Cached metadata can be stale until refresh; it lives in ordinary preferences without additional application-level encryption. The snapshot contains no message bodies or credentials. Persistent disk-deletion failures can leave metadata until a later cleanup succeeds. --------- Signed-off-by: Star Lord <b89298dbe87c6b3fd8a425b535d9d161f23c88d555a47925ae0b04dfe02b201e@buzz.block.builderlab.xyz> Co-authored-by: Star Lord <b89298dbe87c6b3fd8a425b535d9d161f23c88d555a47925ae0b04dfe02b201e@buzz.block.builderlab.xyz> Co-authored-by: Rocket <0c41329ade659e01801116549bc617351689892a382bc864a1186dc9191293a7@buzz.block.builderlab.xyz> Co-authored-by: Groot <f4c08302591916fb4b80926bf7e18d22eef8aa0c877e4e841acc46405a45700b@buzz.block.builderlab.xyz> Co-authored-by: Mantis <530af7635ef7d0e1a5189d234338db88cb4ef1ede7119b4ebb4fb05c3eafe3b1@buzz.block.builderlab.xyz>
188 lines
7.1 KiB
Dart
188 lines
7.1 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import '../relay/relay_provider.dart' show RelayConfig;
|
|
import '../theme/community_theme_preference.dart';
|
|
import '../theme/theme_provider.dart';
|
|
|
|
const _keyPrefix = 'buzz.channel-list.v1';
|
|
const _version = 1;
|
|
|
|
/// Snapshots beyond these bounds are not stored; the list loads from the relay.
|
|
const maxChannelListSnapshotChannels = 2000;
|
|
const maxChannelListSnapshotBytes = 2 * 1024 * 1024;
|
|
|
|
/// A disposable, relay-derived channel list for one relay and identity.
|
|
class ChannelListSnapshot {
|
|
const ChannelListSnapshot({
|
|
required this.channels,
|
|
required this.hiddenDmIds,
|
|
});
|
|
|
|
/// Member channel JSON in the shape of `Channel.toJson`.
|
|
final List<Map<String, dynamic>> channels;
|
|
final Set<String> hiddenDmIds;
|
|
}
|
|
|
|
/// Permission to write one scope's snapshot, captured before a network fetch.
|
|
///
|
|
/// [ChannelListSnapshotStorage.remove] retires every token captured before
|
|
/// it, so a fetch in flight during removal cannot recreate the snapshot.
|
|
class ChannelListSnapshotToken {
|
|
const ChannelListSnapshotToken._(this._key, this._epoch);
|
|
|
|
final String _key;
|
|
final int _epoch;
|
|
}
|
|
|
|
/// Stores the last authoritative channel list per relay and identity.
|
|
///
|
|
/// The data is a cache derived from relay events: unreadable, unsupported or
|
|
/// oversized data is discarded rather than migrated.
|
|
class ChannelListSnapshotStorage {
|
|
ChannelListSnapshotStorage(this._prefs);
|
|
|
|
final SharedPreferences _prefs;
|
|
|
|
/// Retirement epochs shared by every storage over the same preferences.
|
|
static final _epochsByPrefs = Expando<Map<String, int>>();
|
|
static final _savedValuesByPrefs = Expando<Map<String, String>>();
|
|
|
|
Map<String, String> get _savedValues => _savedValuesByPrefs[_prefs] ??= {};
|
|
|
|
Map<String, int> get _epochs => _epochsByPrefs[_prefs] ??= {};
|
|
|
|
/// The scope key. `wss://`/`ws://` origins fold to `https://`/`http://`
|
|
/// like `RelayConfig.baseUrl`, so both onboarding paths share a snapshot.
|
|
static String key(String pubkey, String relayUrl) {
|
|
final relay = normalizeCommunityRelayUrl(
|
|
RelayConfig(baseUrl: normalizeCommunityRelayUrl(relayUrl)).baseUrl,
|
|
);
|
|
return '$_keyPrefix:${pubkey.toLowerCase()}:${Uri.encodeComponent(relay)}';
|
|
}
|
|
|
|
/// Returns the scope's snapshot, or null when absent or unusable.
|
|
ChannelListSnapshot? read(String pubkey, String relayUrl) {
|
|
final key = ChannelListSnapshotStorage.key(pubkey, relayUrl);
|
|
final raw = _prefs.getString(key);
|
|
if (raw == null) return null;
|
|
try {
|
|
// UTF-16 length is at most the UTF-8 length, so this skips encoding
|
|
// for the oversized case before measuring the exact bound.
|
|
if (raw.length > maxChannelListSnapshotBytes ||
|
|
utf8.encode(raw).length > maxChannelListSnapshotBytes) {
|
|
throw const FormatException('size');
|
|
}
|
|
final json = jsonDecode(raw) as Map<String, dynamic>;
|
|
if (json['v'] != _version) throw const FormatException('version');
|
|
final channels = json['channels'] as List<dynamic>;
|
|
if (channels.length > maxChannelListSnapshotChannels) {
|
|
throw const FormatException('count');
|
|
}
|
|
return ChannelListSnapshot(
|
|
channels: [
|
|
for (final channel in channels) channel as Map<String, dynamic>,
|
|
],
|
|
hiddenDmIds: {
|
|
for (final id in json['hidden_dm_ids'] as List<dynamic>) id as String,
|
|
},
|
|
);
|
|
} catch (_) {
|
|
// Unusable cache: drop it without retiring in-flight writers.
|
|
_savedValues.remove(key);
|
|
_prefs.remove(key).ignore();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// Captures write permission for the scope before its fetch starts.
|
|
ChannelListSnapshotToken capture(String pubkey, String relayUrl) {
|
|
final key = ChannelListSnapshotStorage.key(pubkey, relayUrl);
|
|
return ChannelListSnapshotToken._(key, _epochs[key] ?? 0);
|
|
}
|
|
|
|
/// Replaces the scope's snapshot. Returns false when [token] was retired,
|
|
/// the snapshot exceeds the size bounds, or storage rejected the write.
|
|
Future<bool> write(
|
|
ChannelListSnapshotToken token,
|
|
ChannelListSnapshot snapshot,
|
|
) async {
|
|
if ((_epochs[token._key] ?? 0) != token._epoch) return false;
|
|
final raw = snapshot.channels.length > maxChannelListSnapshotChannels
|
|
? null
|
|
: jsonEncode({
|
|
'v': _version,
|
|
'channels': snapshot.channels,
|
|
'hidden_dm_ids': snapshot.hiddenDmIds.toList()..sort(),
|
|
});
|
|
if (raw == null || utf8.encode(raw).length > maxChannelListSnapshotBytes) {
|
|
// An older, smaller snapshot no longer describes this list.
|
|
_savedValues.remove(token._key);
|
|
await _prefs.remove(token._key);
|
|
return false;
|
|
}
|
|
if (_savedValues[token._key] == raw &&
|
|
_prefs.getString(token._key) == raw) {
|
|
return true;
|
|
}
|
|
// The token check and SharedPreferences' synchronous in-memory update run
|
|
// without an intervening await, so a later remove always wins.
|
|
// Failed platform writes still change SharedPreferences' memory cache.
|
|
// Only skip values known to have been successfully persisted.
|
|
_savedValues.remove(token._key);
|
|
final saved = await _prefs.setString(token._key, raw);
|
|
if (saved && (_epochs[token._key] ?? 0) == token._epoch) {
|
|
_savedValues[token._key] = raw;
|
|
}
|
|
return saved;
|
|
}
|
|
|
|
/// Deletes the scope's snapshot and retires its outstanding tokens.
|
|
///
|
|
/// Throws on storage failure. Community sign-out treats this as best-effort;
|
|
/// orphan cleanup retries the remaining disk entry.
|
|
Future<void> remove(String pubkey, String relayUrl) async {
|
|
await _removeKey(ChannelListSnapshotStorage.key(pubkey, relayUrl));
|
|
}
|
|
|
|
/// Retires and deletes snapshots without a stored community. Failed deletes
|
|
/// remain on disk for retry at the next startup or community mutation.
|
|
Future<void> removeOrphans(Set<String> retainedKeys) async {
|
|
final keys = {..._prefs.getKeys(), ..._epochs.keys};
|
|
// Start every deletion before yielding: later community additions must not
|
|
// have a new writer retired by an older cleanup's delayed next iteration.
|
|
await Future.wait([
|
|
for (final key in keys)
|
|
if (key.startsWith('$_keyPrefix:') && !retainedKeys.contains(key))
|
|
_removeKey(key).catchError((Object error) {
|
|
debugPrint('[ChannelListSnapshotStorage] cleanup failed: $error');
|
|
}),
|
|
]);
|
|
}
|
|
|
|
Future<void> _removeKey(String key) async {
|
|
_savedValues.remove(key);
|
|
_epochs[key] = (_epochs[key] ?? 0) + 1;
|
|
if (!await _prefs.remove(key)) {
|
|
throw StateError('Channel list snapshot could not be removed.');
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The app's snapshot storage, or null where preferences were never provided
|
|
/// (isolated tests). Null means there is no snapshot to read, write or remove.
|
|
final channelListSnapshotStorageProvider =
|
|
Provider<ChannelListSnapshotStorage?>((ref) {
|
|
final SharedPreferences prefs;
|
|
try {
|
|
prefs = ref.watch(savedPrefsProvider);
|
|
} catch (_) {
|
|
// The unprovided override throws, wrapped by Riverpod.
|
|
return null;
|
|
}
|
|
return ChannelListSnapshotStorage(prefs);
|
|
});
|