174 lines
5.8 KiB
Dart
174 lines
5.8 KiB
Dart
import '../../../core/servers/chat/chat_identity.dart';
|
|
import 'dart:convert';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import '../../../core/servers/server_scope.dart';
|
|
|
|
/// One conversation's persisted composer draft, keyed by session id.
|
|
///
|
|
/// The draft is stored exactly as typed (untrimmed); only the wire copy is
|
|
/// trimmed. Attachments are intentionally absent: a file handle cannot be
|
|
/// serialised, and the web build keeps them memory-only too.
|
|
abstract interface class ChatDraftStore {
|
|
/// The saved draft, or an empty string when there is none or the store is
|
|
/// unavailable. Implementations may throw; the controller treats that as
|
|
/// "nothing saved" and keeps its in-memory draft.
|
|
Future<String> read(String sessionId);
|
|
|
|
/// Stores [text] exactly. A blank draft removes the entry so the store does
|
|
/// not grow without bound. Returns false when nothing was stored.
|
|
Future<bool> write(String sessionId, String text);
|
|
|
|
/// The messages sent from this device that the server has not yet accepted,
|
|
/// as stored JSON intents in send order. Empty when there are none.
|
|
Future<List<Object?>> readUnsent(String sessionId);
|
|
|
|
/// Replaces the stored unsent intents; an empty list removes the entry.
|
|
/// Returns false when nothing was stored.
|
|
Future<bool> writeUnsent(String sessionId, List<Object?> intents);
|
|
}
|
|
|
|
/// The app's per-session draft keys. Independent of the legacy web/Android
|
|
/// storage: no old draft is imported or written under an old key.
|
|
const String chatDraftKeyPrefix = 'pipelines.next.chatDraft.v1.';
|
|
|
|
/// The per-session key for messages still awaiting the server's acceptance.
|
|
const String chatUnsentKeyPrefix = 'pipelines.next.chatUnsent.v1.';
|
|
|
|
/// `shared_preferences`-backed draft storage.
|
|
///
|
|
/// Reads are tolerant and a write reports failure instead of throwing, so the
|
|
/// composer can keep the in-memory draft and say plainly that it was not saved.
|
|
class SharedPreferencesChatDraftStore implements ChatDraftStore {
|
|
const SharedPreferencesChatDraftStore({this.serverId = ''});
|
|
final String serverId;
|
|
String _native(String sessionId) {
|
|
final parsed = parseFederatedChatId(sessionId);
|
|
return parsed != null && serverStorageScope(parsed.server) == serverId
|
|
? parsed.native
|
|
: sessionId;
|
|
}
|
|
|
|
String _key(String sessionId) =>
|
|
scopedServerKey('$chatDraftKeyPrefix${_native(sessionId)}', serverId);
|
|
|
|
Future<SharedPreferences?> _preferences() async {
|
|
try {
|
|
return await SharedPreferences.getInstance();
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<String> read(String sessionId) async {
|
|
final preferences = await _preferences();
|
|
if (preferences == null) return '';
|
|
try {
|
|
return preferences.getString(_key(sessionId)) ?? '';
|
|
} catch (_) {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<bool> write(String sessionId, String text) async {
|
|
final preferences = await _preferences();
|
|
if (preferences == null) return false;
|
|
try {
|
|
final key = _key(sessionId);
|
|
if (text.isEmpty) return await preferences.remove(key);
|
|
return await preferences.setString(key, text);
|
|
} catch (_) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
String _unsentKey(String sessionId) =>
|
|
scopedServerKey('$chatUnsentKeyPrefix${_native(sessionId)}', serverId);
|
|
|
|
@override
|
|
Future<List<Object?>> readUnsent(String sessionId) async {
|
|
final preferences = await _preferences();
|
|
if (preferences == null) return const [];
|
|
try {
|
|
final stored = preferences.getString(_unsentKey(sessionId));
|
|
if (stored == null || stored.isEmpty) return const [];
|
|
final decoded = jsonDecode(stored);
|
|
return decoded is List ? decoded : const [];
|
|
} catch (_) {
|
|
return const [];
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<bool> writeUnsent(String sessionId, List<Object?> intents) async {
|
|
final preferences = await _preferences();
|
|
if (preferences == null) return false;
|
|
try {
|
|
final key = _unsentKey(sessionId);
|
|
if (intents.isEmpty) return await preferences.remove(key);
|
|
return await preferences.setString(key, jsonEncode(intents));
|
|
} catch (_) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// An in-memory store for tests and previews. It never touches the platform
|
|
/// store, so no production preference can be mutated by a fixture.
|
|
class MemoryChatDraftStore implements ChatDraftStore {
|
|
MemoryChatDraftStore({
|
|
Map<String, String>? drafts,
|
|
Map<String, List<Object?>>? unsent,
|
|
}) : _drafts = {...?drafts},
|
|
_unsent = {...?unsent};
|
|
|
|
final Map<String, String> _drafts;
|
|
final Map<String, List<Object?>> _unsent;
|
|
|
|
/// A snapshot of the stored unsent intents per session.
|
|
Map<String, List<Object?>> get unsentEntries => Map.unmodifiable(_unsent);
|
|
bool failReads = false;
|
|
bool failWrites = false;
|
|
|
|
/// A snapshot of the stored entries, so a test can tell a removed key from an
|
|
/// empty string.
|
|
Map<String, String> get entries => Map.unmodifiable(_drafts);
|
|
|
|
@override
|
|
Future<String> read(String sessionId) async {
|
|
if (failReads) throw StateError('Draft storage is unavailable.');
|
|
return _drafts[sessionId] ?? '';
|
|
}
|
|
|
|
@override
|
|
Future<bool> write(String sessionId, String text) async {
|
|
if (failWrites) return false;
|
|
if (text.isEmpty) {
|
|
_drafts.remove(sessionId);
|
|
} else {
|
|
_drafts[sessionId] = text;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
@override
|
|
Future<List<Object?>> readUnsent(String sessionId) async {
|
|
if (failReads) throw StateError('Draft storage is unavailable.');
|
|
// A round trip through JSON, exactly as the device store keeps it.
|
|
return jsonDecode(jsonEncode(_unsent[sessionId] ?? const [])) as List;
|
|
}
|
|
|
|
@override
|
|
Future<bool> writeUnsent(String sessionId, List<Object?> intents) async {
|
|
if (failWrites) return false;
|
|
if (intents.isEmpty) {
|
|
_unsent.remove(sessionId);
|
|
} else {
|
|
_unsent[sessionId] = jsonDecode(jsonEncode(intents)) as List;
|
|
}
|
|
return true;
|
|
}
|
|
}
|