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:
parent
e852b569ce
commit
73ee98206f
8 changed files with 280 additions and 4 deletions
55
apps/app_seeds/test/services/inbox_service_test.dart
Normal file
55
apps/app_seeds/test/services/inbox_service_test.dart
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import 'package:commons_core/commons_core.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:tane/services/inbox_service.dart';
|
||||
import 'package:tane/services/message_store.dart';
|
||||
import 'package:tane/services/social_service.dart';
|
||||
import 'package:tane/services/social_settings.dart';
|
||||
|
||||
import '../support/test_support.dart';
|
||||
|
||||
/// The app-wide inbox listener persists incoming messages and announces changes,
|
||||
/// so the inbox list refreshes even when the specific chat isn't open. Driven
|
||||
/// through the [InboxService.ingest] seam so no relay/network is involved.
|
||||
void main() {
|
||||
const seedHex =
|
||||
'000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f';
|
||||
|
||||
late MessageStore store;
|
||||
late InboxService inbox;
|
||||
|
||||
setUp(() async {
|
||||
store = MessageStore(InMemorySecretStore());
|
||||
inbox = InboxService(
|
||||
social: await SocialService.fromRootSeedHex(seedHex),
|
||||
settings: SocialSettings(InMemorySecretStore()),
|
||||
store: store,
|
||||
);
|
||||
});
|
||||
|
||||
tearDown(() => inbox.stop());
|
||||
|
||||
PrivateMessage msg(String from, String text, int atMs) => PrivateMessage(
|
||||
fromPubkey: from,
|
||||
text: text,
|
||||
at: DateTime.fromMillisecondsSinceEpoch(atMs));
|
||||
|
||||
test('an incoming message is persisted into its conversation', () async {
|
||||
await inbox.ingest(msg('alice', 'got seeds?', 1000));
|
||||
final convos = await store.conversations();
|
||||
expect(convos.single.peerPubkey, 'alice');
|
||||
expect(convos.single.lastText, 'got seeds?');
|
||||
});
|
||||
|
||||
test('a new message announces a change; a duplicate stays silent', () async {
|
||||
final changes = <void>[];
|
||||
final sub = inbox.changes.listen(changes.add);
|
||||
|
||||
await inbox.ingest(msg('alice', 'hola', 1000));
|
||||
await inbox.ingest(msg('alice', 'hola', 1000)); // relay re-delivery
|
||||
await Future<void>.delayed(Duration.zero); // let the broadcast flush
|
||||
|
||||
expect(changes, hasLength(1)); // only the first, new one fired
|
||||
expect(await store.history('alice'), hasLength(1));
|
||||
await sub.cancel();
|
||||
});
|
||||
}
|
||||
|
|
@ -46,6 +46,19 @@ void main() {
|
|||
expect(convos.last.lastText, 'back A');
|
||||
});
|
||||
|
||||
test('append is idempotent: a re-delivered message is not duplicated',
|
||||
() async {
|
||||
// Same sender + timestamp + text = the same gift wrap redelivered by a
|
||||
// relay on resubscribe. It must not pile up.
|
||||
expect(await store.append('peer', msg('peer', 'hola', 1000)), isTrue);
|
||||
expect(await store.append('peer', msg('peer', 'hola', 1000)), isFalse);
|
||||
expect(await store.history('peer'), hasLength(1));
|
||||
|
||||
// A genuinely different message (later timestamp) still lands.
|
||||
expect(await store.append('peer', msg('peer', 'hola', 2000)), isTrue);
|
||||
expect(await store.history('peer'), hasLength(2));
|
||||
});
|
||||
|
||||
test('history is capped to the most recent 200', () async {
|
||||
for (var i = 0; i < 210; i++) {
|
||||
await store.append('peer', msg('me', 'm$i', i));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue