mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
**Category:** new-feature **User Impact:** Mobile now keeps each community’s appearance in sync with desktop, including theme, accent, and system-mode preference. **Problem:** Appearance choices were device-local, so the same account could look different between desktop and mobile. Live sync could also stop after the relay closed a subscription. **Solution:** Store each community’s encrypted appearance preference on its relay using the shared desktop wire contract, restore it from a local identity-scoped cache, and apply replacement events live. Closed subscriptions now recover with guarded backoff and fetch the latest preference so no update is lost during the gap. <details> <summary>File changes</summary> **mobile/lib/app.dart** Connects community appearance state to the authenticated app lifecycle. **mobile/lib/features/settings/accent_picker_page.dart** Aligns mobile accent choices and selection behavior with the shared catalog. **mobile/lib/features/settings/settings_page/appearance_section.dart** Clarifies the active appearance and hides accent controls when the Buzz theme owns its neutral accent. **mobile/lib/features/settings/theme_picker_page.dart** Persists catalog theme choices through the community-scoped provider. **mobile/lib/shared/theme/accent_colors.dart** Matches desktop’s accent catalog and wire values. **mobile/lib/shared/theme/buzz_theme.dart** Keeps Buzz visually neutral without discarding the user’s stored accent for other themes. **mobile/lib/shared/theme/community_theme_preference.dart** Defines and validates the versioned desktop-compatible appearance payload. **mobile/lib/shared/theme/community_theme_provider.dart** Coordinates cache-first appearance loading with account and community changes. **mobile/lib/shared/theme/community_theme_sync.dart** Adds encrypted NIP-78 relay persistence, live replacement handling, deterministic ordering, safe seeding, and resilient subscription recovery. **mobile/lib/shared/theme/theme.dart** Exports the community appearance modules. **mobile/test/features/settings/theme_picker_page_test.dart** Covers the updated settings behavior. **mobile/test/shared/crypto/nip44_interop_test.dart** Proves Dart decrypts a desktop-produced nostr-rs NIP-44 v2 preference. **mobile/test/shared/theme/buzz_theme_test.dart** Covers Buzz’s neutral rendering and stored-accent restoration. **mobile/test/shared/theme/community_theme_preference_test.dart** Covers wire parsing, validation, migration, and future-version handling. **mobile/test/shared/theme/community_theme_sync_test.dart** Covers cache/relay lifecycle, replacement ordering, switching races, absence-only seeding, and closed-subscription recovery. </details> ## Reproduction steps 1. Sign into desktop and mobile with the same account and join the same community relay. 2. On desktop, choose a distinctive non-Buzz theme and accent; mobile should update without a local toggle. 3. Restart mobile and confirm it restores the same appearance. 4. Change the mobile theme and accent and confirm desktop follows. 5. Leave mobile idle or backgrounded through a relay reconnect, then change desktop again; mobile should resubscribe and catch up automatically. 6. Switch communities and confirm each community restores only its own appearance. --------- Signed-off-by: Taylor Ho <taylorkmho@gmail.com> Co-authored-by: npub1223z34hd7vtwc6qj4s7flsxkj644nlre2nthu7lrrmkumhu3xddsrx9r6w <52a228d6edf316ec6812ac3c9fc0d696ab59fc7954d77e7be31eedcddf91335b@buzz.block.builderlab.xyz>
312 lines
10 KiB
Dart
312 lines
10 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
|
|
import 'package:buzz/shared/crypto/nip44.dart';
|
|
import 'package:buzz/shared/relay/relay.dart';
|
|
import 'package:buzz/shared/theme/theme.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:nostr/nostr.dart' as nostr;
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
void main() {
|
|
test('delayed absence seeds the intervening local edit', () async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final keys = nostr.Keys.generate();
|
|
final history = Completer<List<NostrEvent>>();
|
|
final session = _ThemeRelaySession(
|
|
keys.nsec,
|
|
keys.public,
|
|
historyFuture: history.future,
|
|
);
|
|
final storage = CommunityThemeStorage(prefs);
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
communityThemeStorageProvider.overrideWithValue(storage),
|
|
relayConfigProvider.overrideWith(() => _RelayConfig(keys.nsec)),
|
|
relaySessionProvider.overrideWith(() => session),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
container.listen(communityThemeProvider, (_, _) {}, fireImmediately: true);
|
|
|
|
container.read(communityThemeProvider.notifier).setTheme('dracula');
|
|
history.complete([]);
|
|
await _waitUntil(() => session.published != null);
|
|
|
|
expect(container.read(communityThemeProvider).theme, 'dracula');
|
|
expect(
|
|
storage.read(keys.public, 'https://relay.example')?.theme,
|
|
'dracula',
|
|
);
|
|
});
|
|
|
|
test('edit during delayed absence seed wins durable state', () async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final keys = nostr.Keys.generate();
|
|
final history = Completer<List<NostrEvent>>();
|
|
final session = _ThemeRelaySession(
|
|
keys.nsec,
|
|
keys.public,
|
|
historyFuture: history.future,
|
|
);
|
|
final storage = _DelayedThemeStorage(prefs);
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
communityThemeStorageProvider.overrideWithValue(storage),
|
|
relayConfigProvider.overrideWith(() => _RelayConfig(keys.nsec)),
|
|
relaySessionProvider.overrideWith(() => session),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
container.listen(communityThemeProvider, (_, _) {}, fireImmediately: true);
|
|
|
|
history.complete([]);
|
|
await storage.cacheWriteStarted.future;
|
|
container.read(communityThemeProvider.notifier).setTheme('dracula');
|
|
storage.allowCacheWrite.complete();
|
|
storage.allowOutboxWrite.complete();
|
|
await _waitUntil(() => session.published != null);
|
|
|
|
expect(container.read(communityThemeProvider).theme, 'dracula');
|
|
expect(
|
|
storage.read(keys.public, 'https://relay.example')?.theme,
|
|
'dracula',
|
|
);
|
|
final privateHex = nostr.Nip19.decode(payload: keys.nsec).data;
|
|
final key = getConversationKey(privateHex, keys.public);
|
|
expect(
|
|
jsonDecode(nip44Decrypt(key, session.published!.content))['theme'],
|
|
'dracula',
|
|
);
|
|
});
|
|
|
|
test(
|
|
'local edit stays authoritative before persistence through exact ack',
|
|
() async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final keys = nostr.Keys.generate();
|
|
final session = _ThemeRelaySession(keys.nsec, keys.public);
|
|
final storage = _DelayedThemeStorage(prefs);
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
communityThemeStorageProvider.overrideWithValue(storage),
|
|
relayConfigProvider.overrideWith(() => _RelayConfig(keys.nsec)),
|
|
relaySessionProvider.overrideWith(() => session),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
|
|
final subscription = container.listen(
|
|
communityThemeProvider,
|
|
(_, _) {},
|
|
fireImmediately: true,
|
|
);
|
|
addTearDown(subscription.close);
|
|
await session.subscribed.future;
|
|
|
|
final notifier = container.read(communityThemeProvider.notifier);
|
|
notifier.setTheme('dracula');
|
|
const local = CommunityThemePreference(
|
|
theme: 'dracula',
|
|
accent: '#3b82f6',
|
|
followSystem: true,
|
|
);
|
|
expect(container.read(communityThemeProvider), local);
|
|
|
|
session.emit(session.remoteEvent(theme: 'houston', id: 'remote-z'));
|
|
expect(container.read(communityThemeProvider), local);
|
|
|
|
storage.allowCacheWrite.complete();
|
|
await storage.outboxWriteStarted.future;
|
|
session.emit(session.remoteEvent(theme: 'solarized', id: 'remote-a'));
|
|
expect(container.read(communityThemeProvider), local);
|
|
|
|
storage.allowOutboxWrite.complete();
|
|
await _waitUntil(() => session.published != null);
|
|
expect(container.read(communityThemeProvider), local);
|
|
|
|
session.emit(session.published!);
|
|
await _pumpEventQueue();
|
|
expect(container.read(communityThemeProvider), local);
|
|
expect(storage.readOutbox(keys.public, 'https://relay.example'), isNull);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'provider rebuild preserves delayed local edit and publishes on replacement manager',
|
|
() async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final keys = nostr.Keys.generate();
|
|
final session = _ThemeRelaySession(keys.nsec, keys.public);
|
|
final storage = _DelayedThemeStorage(prefs);
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
communityThemeStorageProvider.overrideWithValue(storage),
|
|
relayConfigProvider.overrideWith(() => _RelayConfig(keys.nsec)),
|
|
relaySessionProvider.overrideWith(() => session),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
final subscription = container.listen(
|
|
communityThemeProvider,
|
|
(_, _) {},
|
|
fireImmediately: true,
|
|
);
|
|
addTearDown(subscription.close);
|
|
await session.subscribed.future;
|
|
|
|
container.read(communityThemeProvider.notifier).setTheme('dracula');
|
|
expect(container.read(communityThemeProvider).theme, 'dracula');
|
|
await storage.cacheWriteStarted.future;
|
|
|
|
session.setStatus(SessionStatus.reconnecting);
|
|
await _pumpEventQueue();
|
|
expect(container.read(communityThemeProvider).theme, 'dracula');
|
|
session.setStatus(SessionStatus.connected);
|
|
await _waitUntil(() => session.subscribeCalls == 2);
|
|
expect(container.read(communityThemeProvider).theme, 'dracula');
|
|
|
|
storage.allowCacheWrite.complete();
|
|
storage.allowOutboxWrite.complete();
|
|
await _waitUntil(() => session.published != null);
|
|
|
|
final privateHex = nostr.Nip19.decode(payload: keys.nsec).data;
|
|
final key = getConversationKey(privateHex, keys.public);
|
|
expect(
|
|
jsonDecode(nip44Decrypt(key, session.published!.content))['theme'],
|
|
'dracula',
|
|
);
|
|
expect(container.read(communityThemeProvider).theme, 'dracula');
|
|
},
|
|
);
|
|
}
|
|
|
|
class _DelayedThemeStorage extends CommunityThemeStorage {
|
|
_DelayedThemeStorage(super.prefs);
|
|
|
|
final allowCacheWrite = Completer<void>();
|
|
final allowOutboxWrite = Completer<void>();
|
|
final cacheWriteStarted = Completer<void>();
|
|
final outboxWriteStarted = Completer<void>();
|
|
|
|
@override
|
|
Future<bool> write(
|
|
String pubkey,
|
|
String relayUrl,
|
|
CommunityThemePreference preference,
|
|
) async {
|
|
if (!cacheWriteStarted.isCompleted) cacheWriteStarted.complete();
|
|
await allowCacheWrite.future;
|
|
return super.write(pubkey, relayUrl, preference);
|
|
}
|
|
|
|
@override
|
|
Future<bool> writeOutbox(
|
|
String pubkey,
|
|
String relayUrl,
|
|
CommunityThemePreference preference,
|
|
) async {
|
|
if (!outboxWriteStarted.isCompleted) outboxWriteStarted.complete();
|
|
await allowOutboxWrite.future;
|
|
return super.writeOutbox(pubkey, relayUrl, preference);
|
|
}
|
|
}
|
|
|
|
class _RelayConfig extends RelayConfigNotifier {
|
|
_RelayConfig(this.nsec);
|
|
|
|
final String nsec;
|
|
|
|
@override
|
|
RelayConfig build() =>
|
|
RelayConfig(baseUrl: 'https://relay.example', nsec: nsec);
|
|
}
|
|
|
|
class _ThemeRelaySession extends RelaySessionNotifier {
|
|
_ThemeRelaySession(this.nsec, this.pubkey, {this.historyFuture});
|
|
|
|
final String nsec;
|
|
final String pubkey;
|
|
final Future<List<NostrEvent>>? historyFuture;
|
|
final subscribed = Completer<void>();
|
|
int subscribeCalls = 0;
|
|
void Function(NostrEvent)? _listener;
|
|
NostrEvent? published;
|
|
|
|
@override
|
|
SessionState build() => const SessionState(status: SessionStatus.connected);
|
|
|
|
@override
|
|
Future<List<NostrEvent>> fetchHistory(
|
|
NostrFilter filter, {
|
|
Duration timeout = const Duration(seconds: 8),
|
|
}) async => historyFuture ?? [remoteEvent(theme: 'buzz', id: 'initial')];
|
|
|
|
@override
|
|
Future<void Function()> subscribe(
|
|
NostrFilter filter,
|
|
void Function(NostrEvent) onEvent, {
|
|
void Function(String message)? onClosed,
|
|
}) async {
|
|
subscribeCalls++;
|
|
_listener = onEvent;
|
|
if (!subscribed.isCompleted) subscribed.complete();
|
|
return () => _listener = null;
|
|
}
|
|
|
|
@override
|
|
Future<NostrEvent> publish(
|
|
NostrEvent event, {
|
|
Duration timeout = const Duration(seconds: 8),
|
|
}) async {
|
|
published = event;
|
|
return event;
|
|
}
|
|
|
|
void emit(NostrEvent event) => _listener?.call(event);
|
|
|
|
void setStatus(SessionStatus status) {
|
|
state = SessionState(status: status);
|
|
}
|
|
|
|
NostrEvent remoteEvent({required String theme, required String id}) {
|
|
final privateHex = nostr.Nip19.decode(payload: nsec).data;
|
|
final key = getConversationKey(privateHex, pubkey);
|
|
final preference = CommunityThemePreference(
|
|
theme: theme,
|
|
accent: '#3b82f6',
|
|
followSystem: true,
|
|
);
|
|
return NostrEvent(
|
|
id: id,
|
|
pubkey: pubkey,
|
|
createdAt: 1,
|
|
kind: 30078,
|
|
tags: const [
|
|
['d', communityThemeDTag],
|
|
['t', communityThemeDTag],
|
|
],
|
|
content: nip44Encrypt(key, jsonEncode(preference.toJson())),
|
|
sig: 'sig',
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> _pumpEventQueue() async {
|
|
await Future<void>.delayed(Duration.zero);
|
|
await Future<void>.delayed(Duration.zero);
|
|
}
|
|
|
|
Future<void> _waitUntil(bool Function() condition) async {
|
|
final deadline = DateTime.now().add(const Duration(seconds: 3));
|
|
while (!condition()) {
|
|
if (DateTime.now().isAfter(deadline)) fail('condition not met');
|
|
await Future<void>.delayed(const Duration(milliseconds: 5));
|
|
}
|
|
}
|