feat(chat): app-wide inbox listener so messages arrive without opening the chat

Reception only ran inside an open ChatScreen for a known peer, so a first
message from a new peer was invisible: nothing in local storage -> nothing
in the inbox list -> you never opened the chat -> you never subscribed.

Add InboxService: one long-lived NIP-17 inbox subscription for the whole
app (foreground), persisting every incoming message to MessageStore and
firing a 'changes' signal the inbox list live-reloads on. Reconnects when
the network returns; degrades to nothing offline. Started from main when a
social identity exists.

Make MessageStore.append idempotent (dedup by sender+timestamp+text) and
serialized behind a write lock — the global listener and an open chat's own
subscription now write the same conversation concurrently and relays
re-deliver stored gift wraps on every resubscribe. Tests for both.

Known trade-offs (follow-ups): foreground-only (no push yet); each of
InboxService/ChatScreen/MarketScreen opens its own RelayPool (a shared
connection is a later optimization).
This commit is contained in:
vjrj 2026-07-10 16:53:03 +02:00
parent e852b569ce
commit 73ee98206f
8 changed files with 280 additions and 4 deletions

View file

@ -9,6 +9,7 @@ import 'data/variety_repository.dart';
import 'i18n/strings.g.dart';
import 'services/auto_backup_service.dart';
import 'services/coarse_location.dart';
import 'services/inbox_service.dart';
import 'services/message_store.dart';
import 'services/offer_outbox.dart';
import 'services/onboarding_store.dart';
@ -47,6 +48,7 @@ class TaneApp extends StatelessWidget {
this.messageStore,
this.profileStore,
this.profileCache,
this.inbox,
this.showIntro = false,
this.autoBackup,
super.key,
@ -61,6 +63,7 @@ class TaneApp extends StatelessWidget {
messageStore,
profileStore,
profileCache,
inbox,
);
final VarietyRepository repository;
@ -86,6 +89,9 @@ class TaneApp extends StatelessWidget {
/// Optional cache of peers' published display names.
final ProfileCache? profileCache;
/// App-wide inbox listener; drives the messages list's live refresh.
final InboxService? inbox;
final bool showIntro;
/// Drives silent periodic backups off the app lifecycle. Null in widget tests
@ -104,6 +110,7 @@ class TaneApp extends StatelessWidget {
MessageStore? messageStore,
ProfileStore? profileStore,
ProfileCache? profileCache,
InboxService? inbox,
) {
return GoRouter(
initialLocation: showIntro ? '/intro' : '/',
@ -131,6 +138,7 @@ class TaneApp extends StatelessWidget {
social: social,
settings: socialSettings,
profileCache: profileCache,
inbox: inbox,
),
),
if (social != null && socialSettings != null && profileStore != null)