feat(block2): persist chat history (messages survive leaving the chat)

Messages were in-memory only — gone on leaving the conversation.
- MessageStore: keystore-backed (no plaintext), a capped per-peer JSON list
  (last 200). Simple; the encrypted Drift DB is the eventual home at scale.
- MessagesCubit: loads saved history on start (subscribes FIRST, then loads, so
  a message arriving during the load isn't dropped) and persists every sent and
  received message. Wired via DI + TaneApp(messageStore).

Tests (plain 'test', no hang risk): MessageStore round-trip/per-peer/cap, and a
cubit test that reopens a fresh cubit and sees the saved conversation. Analyzer
clean; run 'flutter test' locally to confirm.
This commit is contained in:
vjrj 2026-07-10 12:20:25 +02:00
parent 6d8c6cf7e1
commit 7bf80d2031
8 changed files with 186 additions and 22 deletions

View file

@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../i18n/strings.g.dart';
import '../services/message_store.dart';
import '../services/social_service.dart';
import '../services/social_settings.dart';
import '../state/messages_cubit.dart';
@ -18,6 +19,7 @@ class ChatScreen extends StatefulWidget {
required this.social,
required this.settings,
required this.peerPubkey,
this.messageStore,
super.key,
});
@ -25,6 +27,9 @@ class ChatScreen extends StatefulWidget {
final SocialSettings settings;
final String peerPubkey;
/// Optional persistence for chat history (keystore-backed); null in tests.
final MessageStore? messageStore;
@override
State<ChatScreen> createState() => _ChatScreenState();
}
@ -59,9 +64,12 @@ class _ChatScreenState extends State<ChatScreen> {
return;
}
final self = widget.social.publicKeyHex;
final messages = MessagesCubit(session?.messages,
peerPubkey: widget.peerPubkey, selfPubkey: self)
..start();
final messages = MessagesCubit(
session?.messages,
peerPubkey: widget.peerPubkey,
selfPubkey: self,
store: widget.messageStore,
)..start();
final trust = TrustCubit(session?.trust,
peerPubkey: widget.peerPubkey, selfPubkey: self);
unawaited(trust.load());