feat(chat): usable 1:1 chat — bottom-anchored, Drift-backed, dated
- Anchor the message list to the bottom (`reverse: true`) so new messages stay in view instead of landing below the fold. - Move chat history from the OS keystore (O(n²) JSON blob, silent 200-msg cap) to a separate encrypted Drift/SQLCipher DB (`ChatDatabase`): indexed append, uncapped history, dedup as a unique-key invariant. It's an ephemeral per-device cache, isolated from the inventory schema, its migrations, and its sync. No data migration (pre-release). - Add day separators (Today/Yesterday/locale date) and a per-bubble time, all via ICU (12/24h per locale; Localizations locale maps Asturian → Spanish for intl date symbols). - Add peer avatars (deterministic colour from the pubkey + name initial), surface send failures that were previously silent, and make bubble text selectable (addresses, links). - New i18n keys in en/es/pt/ast; tests for grouping, formatting, avatars, scroll anchoring, storage and send errors. Docs: docs/design/chat-storage.md + open-decisions.md.
This commit is contained in:
parent
171daabce3
commit
6cff0d0b11
27 changed files with 1793 additions and 264 deletions
|
|
@ -46,7 +46,7 @@ void main() {
|
|||
late InboxService inbox;
|
||||
|
||||
setUp(() async {
|
||||
store = MessageStore(InMemorySecretStore());
|
||||
store = MessageStore(newTestChatDatabase());
|
||||
inbox = InboxService(
|
||||
connection: await offlineConnection(),
|
||||
selfPubkey: 'me',
|
||||
|
|
|
|||
|
|
@ -1,18 +1,24 @@
|
|||
import 'package:commons_core/commons_core.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:tane/db/chat_database.dart';
|
||||
import 'package:tane/services/message_store.dart';
|
||||
|
||||
import '../support/test_support.dart';
|
||||
|
||||
void main() {
|
||||
late ChatDatabase db;
|
||||
late MessageStore store;
|
||||
setUp(() => store = MessageStore(InMemorySecretStore()));
|
||||
setUp(() {
|
||||
db = newTestChatDatabase();
|
||||
store = MessageStore(db);
|
||||
});
|
||||
tearDown(() => db.close());
|
||||
|
||||
PrivateMessage msg(String from, String text, int atMs) =>
|
||||
PrivateMessage(
|
||||
fromPubkey: from,
|
||||
text: text,
|
||||
at: DateTime.fromMillisecondsSinceEpoch(atMs));
|
||||
PrivateMessage msg(String from, String text, int atMs) => PrivateMessage(
|
||||
fromPubkey: from,
|
||||
text: text,
|
||||
at: DateTime.fromMillisecondsSinceEpoch(atMs),
|
||||
);
|
||||
|
||||
test('history is empty for an unknown peer', () async {
|
||||
expect(await store.history('peer'), isEmpty);
|
||||
|
|
@ -34,53 +40,62 @@ void main() {
|
|||
expect((await store.history('b')).single.text, 'toB');
|
||||
});
|
||||
|
||||
test('conversations lists peers newest-first with their last message',
|
||||
() async {
|
||||
await store.append('peerA', msg('me', 'hi A', 1000));
|
||||
await store.append('peerB', msg('peerB', 'yo B', 3000));
|
||||
await store.append('peerA', msg('peerA', 'back A', 2000));
|
||||
test(
|
||||
'conversations lists peers newest-first with their last message',
|
||||
() async {
|
||||
await store.append('peerA', msg('me', 'hi A', 1000));
|
||||
await store.append('peerB', msg('peerB', 'yo B', 3000));
|
||||
await store.append('peerA', msg('peerA', 'back A', 2000));
|
||||
|
||||
final convos = await store.conversations();
|
||||
expect(convos.map((c) => c.peerPubkey), ['peerB', 'peerA']); // 3000 > 2000
|
||||
expect(convos.first.lastText, 'yo B');
|
||||
expect(convos.last.lastText, 'back A');
|
||||
});
|
||||
final convos = await store.conversations();
|
||||
expect(convos.map((c) => c.peerPubkey), [
|
||||
'peerB',
|
||||
'peerA',
|
||||
]); // 3000 > 2000
|
||||
expect(convos.first.lastText, 'yo B');
|
||||
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));
|
||||
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));
|
||||
});
|
||||
// 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('per-identity scope isolates conversations (0 = legacy keys)', () async {
|
||||
final secret = InMemorySecretStore();
|
||||
final acct0 = MessageStore(secret); // legacy / account 0
|
||||
final acct1 = MessageStore(secret, accountScope: 'acct1');
|
||||
test(
|
||||
'per-identity scope isolates conversations (0 = legacy scope)',
|
||||
() async {
|
||||
final acct0 = MessageStore(db); // legacy / account 0
|
||||
final acct1 = MessageStore(db, accountScope: 'acct1');
|
||||
|
||||
await acct0.append('peer', msg('peer', 'for identity 0', 1000));
|
||||
await acct1.append('peer', msg('peer', 'for identity 1', 2000));
|
||||
await acct0.append('peer', msg('peer', 'for identity 0', 1000));
|
||||
await acct1.append('peer', msg('peer', 'for identity 1', 2000));
|
||||
|
||||
// Same peer, but each identity sees only its own conversation.
|
||||
expect((await acct0.history('peer')).single.text, 'for identity 0');
|
||||
expect((await acct1.history('peer')).single.text, 'for identity 1');
|
||||
expect((await acct0.conversations()).single.lastText, 'for identity 0');
|
||||
expect((await acct1.conversations()).single.lastText, 'for identity 1');
|
||||
});
|
||||
// Same peer, but each identity sees only its own conversation.
|
||||
expect((await acct0.history('peer')).single.text, 'for identity 0');
|
||||
expect((await acct1.history('peer')).single.text, 'for identity 1');
|
||||
expect((await acct0.conversations()).single.lastText, 'for identity 0');
|
||||
expect((await acct1.conversations()).single.lastText, 'for identity 1');
|
||||
},
|
||||
);
|
||||
|
||||
test('history is capped to the most recent 200', () async {
|
||||
test('history is uncapped: nothing is silently dropped', () async {
|
||||
for (var i = 0; i < 210; i++) {
|
||||
await store.append('peer', msg('me', 'm$i', i));
|
||||
}
|
||||
final history = await store.history('peer');
|
||||
expect(history, hasLength(200));
|
||||
expect(history.first.text, 'm10'); // oldest 10 dropped
|
||||
expect(history, hasLength(210));
|
||||
expect(history.first.text, 'm0'); // the oldest survives
|
||||
expect(history.last.text, 'm209');
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ void main() {
|
|||
late UnreadService unread;
|
||||
|
||||
setUp(() {
|
||||
store = MessageStore(InMemorySecretStore());
|
||||
store = MessageStore(newTestChatDatabase());
|
||||
unread = UnreadService(store, InMemorySecretStore());
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue