import 'dart:convert'; import 'dart:typed_data'; import '../models/message.dart'; import '../models/translation_support.dart'; import '../helpers/smaz.dart'; import '../utils/app_logger.dart'; import 'legacy_key_migration.dart'; import 'history_archive.dart'; import 'history_store.dart'; class MessageStore { static const String _keyPrefix = 'messages_'; String publicKeyHex = ''; set setPublicKeyHex(String value) => publicKeyHex = value.length > 10 ? value.substring(0, 10) : ''; String get keyFor => '$_keyPrefix$publicKeyHex'; /// Upper bound on persisted messages per conversation. The oldest entries /// beyond it are dropped on save. static const int maxStoredMessages = 1000; static final HistoryArchive _archive = HistoryArchive( mergeKey: _storedMergeKey, maxEntries: maxStoredMessages, ); Future saveMessages(String contactKeyHex, List messages) { if (publicKeyHex.isEmpty) { appLogger.warn('Public key hex is not set. Cannot save messages.'); return Future.value(); } final key = '$keyFor$contactKeyHex'; final scope = publicKeyHex; return _archive.save( key, [for (final message in messages) jsonEncode(_messageToJson(message))], readStored: (store) => _readStored(store, key, '$_keyPrefix$contactKeyHex', scope), ); } /// The stored prefix stays in the database, rather than another JSON cache. void setArchivedCount(String contactKeyHex, int count) => _archive.setArchivedCount('$keyFor$contactKeyHex', count); void archive(String contactKeyHex, List messages) => _archive.archive( '$keyFor$contactKeyHex', messages.map((message) => jsonEncode(_messageToJson(message))), ); int? archivedCount(String contactKeyHex) => _archive.archivedCount('$keyFor$contactKeyHex'); void forgetArchive([String? contactKeyHex]) => _archive.forget(contactKeyHex == null ? null : '$keyFor$contactKeyHex'); static String _storedMergeKey(Map json) { final messageId = json['messageId']; if (messageId is String && messageId.isNotEmpty) return 'id:$messageId'; return 'fallback:${json['timestamp']}:${json['isOutgoing']}:${json['text']}'; } Future _readStored( HistoryStore store, String key, String oldKey, String scope, ) async { final raw = await store.getString(key); if ((raw == null || raw.isEmpty) && canMigrateLegacyKeys(scope)) { final legacy = await store.getString(oldKey); if (legacy != null && legacy.isNotEmpty) { if (await store.setString(key, legacy)) await store.remove(oldKey); } return legacy; } return raw; } Future<({String key, int count})> _storedHistory(String contactKeyHex) async { final store = HistoryStore.instance; final scope = publicKeyHex; final key = '$keyFor$contactKeyHex'; var count = await store.entryCount(key); if (count > 0) return (key: key, count: count); final raw = await store.getString(key); if ((raw == null || raw.isEmpty) && canMigrateLegacyKeys(scope)) { final oldKey = '$_keyPrefix$contactKeyHex'; final legacy = await store.getString(oldKey); if (legacy != null && legacy.isNotEmpty) { if (await store.setString(key, legacy)) { await store.remove(oldKey); count = await store.entryCount(key); } else { throw StateError('Could not migrate stored message history'); } } } if (raw == null && count == 0) { final fallback = '$_keyPrefix$scope'; final fallbackCount = await store.entryCount(fallback); if (fallbackCount > 0) { final fallbackJson = await store.getString(fallback); if (fallbackJson == null || !await store.setString(key, fallbackJson)) { throw StateError('Could not migrate fallback message history'); } return (key: key, count: fallbackCount); } } return (key: key, count: count); } Future> loadMessages( String contactKeyHex, { int offset = 0, int? limit, }) async { if (publicKeyHex.isEmpty) return []; try { final history = await _storedHistory(contactKeyHex); final entries = await HistoryStore.instance.readEntries( history.key, offset: offset, limit: limit, ); return _decodeMessages(entries); } catch (error) { appLogger.warn( 'Stored messages for $contactKeyHex are unreadable: $error', ); return []; } } /// Reads a page backwards using physical stored-row offsets. Malformed /// records are skipped without making earlier valid history unreachable. Future<({List messages, int start, int end, int total})> loadMessagePage(String contactKeyHex, {int? before, int limit = 50}) async { RangeError.checkNotNegative(limit, 'limit'); if (publicKeyHex.isEmpty) { return (messages: [], start: 0, end: 0, total: 0); } try { final history = await _storedHistory(contactKeyHex); return await _archive.readPage( history.key, history.count, _decodeMessages, before: before, limit: limit, ); } on FormatException catch (error) { appLogger.warn( 'Stored messages for $contactKeyHex are unreadable: $error', ); return (messages: [], start: 0, end: 0, total: 0); } } List _decodeMessages(List entries) { final messages = []; for (final entry in entries) { try { messages.add( _messageFromJson(jsonDecode(entry) as Map), ); } catch (error) { appLogger.warn('Skipping malformed stored message: $error'); } } return messages; } Future updateBookmarkFlag( String contactKeyHex, String messageId, bool isBookmarked, ) { if (publicKeyHex.isEmpty) return Future.value(); return _archive.updateBookmarkFlag( '$keyFor$contactKeyHex', messageId, isBookmarked, ); } Future clearMessages(String contactKeyHex) { if (publicKeyHex.isEmpty) return Future.value(); return _archive.clear(HistoryStore.instance, '$keyFor$contactKeyHex'); } Map _messageToJson(Message msg) { return { 'senderKey': base64Encode(msg.senderKey), 'text': msg.text, 'timestamp': msg.timestamp.millisecondsSinceEpoch, 'receivedAt': msg.receivedAt.millisecondsSinceEpoch, 'isOutgoing': msg.isOutgoing, 'isBookmarked': msg.isBookmarked, 'isCli': msg.isCli, 'isSmazCompressed': msg.isSmazCompressed, 'status': msg.status.index, 'messageId': msg.messageId, 'originalText': msg.originalText, 'translatedText': msg.translatedText, 'translatedLanguageCode': msg.translatedLanguageCode, 'translationStatus': msg.translationStatus.value, 'translationModelId': msg.translationModelId, 'retryCount': msg.retryCount, 'estimatedTimeoutMs': msg.estimatedTimeoutMs, 'expectedAckHash': msg.expectedAckHash, 'sentAt': msg.sentAt?.millisecondsSinceEpoch, 'deliveredAt': msg.deliveredAt?.millisecondsSinceEpoch, 'tripTimeMs': msg.tripTimeMs, 'pathLength': msg.pathLength, 'pathBytes': msg.pathBytes.isNotEmpty ? base64Encode(msg.pathBytes) : null, 'reactions': msg.reactions, 'reactionStatuses': msg.reactionStatuses.map( (key, value) => MapEntry(key, value.index), ), 'fourByteRoomContactKey': base64Encode(msg.fourByteRoomContactKey), // `text` is stored decoded; legacy records without this flag may still // hold a Smaz payload. 'plainText': true, }; } Message _messageFromJson(Map json) { final rawText = json['text'] as String; final isCli = json['isCli'] as bool? ?? false; final smazDecoded = (isCli || json['plainText'] == true) ? null : Smaz.tryDecodePrefixed(rawText); final decodedText = smazDecoded ?? rawText; final isSmazCompressed = json['isSmazCompressed'] as bool? ?? (smazDecoded != null); final rawPathLength = json['pathLength'] as int?; final rawPathBytes = json['pathBytes'] != null ? Uint8List.fromList(base64Decode(json['pathBytes'] as String)) : Uint8List(0); int? decodedPathLength = rawPathLength; Uint8List decodedPathBytes = rawPathBytes; if (rawPathLength != null) { if (rawPathLength == 0xFF || rawPathLength < 0) { decodedPathLength = -1; decodedPathBytes = Uint8List(0); } else if (rawPathLength >= 64) { final mode = (rawPathLength & 0xC0) >> 6; final hopCount = rawPathLength & 0x3F; final width = mode + 1; final byteLen = hopCount * width; decodedPathLength = hopCount; if (byteLen <= rawPathBytes.length) { decodedPathBytes = rawPathBytes.sublist(0, byteLen); } else { decodedPathBytes = Uint8List(0); } } else if (rawPathLength == 0) { decodedPathBytes = Uint8List(0); } } return Message( senderKey: Uint8List.fromList(base64Decode(json['senderKey'] as String)), text: decodedText, timestamp: DateTime.fromMillisecondsSinceEpoch(json['timestamp'] as int), receivedAt: legacyAwareReceivedAt(json), isOutgoing: json['isOutgoing'] as bool? ?? false, isBookmarked: json['isBookmarked'] as bool? ?? false, isCli: isCli, isSmazCompressed: isSmazCompressed, status: _statusAt(json['status']) ?? MessageStatus.failed, messageId: json['messageId'] as String?, originalText: json['originalText'] as String?, translatedText: json['translatedText'] as String?, translatedLanguageCode: json['translatedLanguageCode'] as String?, translationStatus: parseMessageTranslationStatus( json['translationStatus'], ), translationModelId: json['translationModelId'] as String?, retryCount: json['retryCount'] as int? ?? 0, estimatedTimeoutMs: json['estimatedTimeoutMs'] as int?, expectedAckHash: json['expectedAckHash'] as int? ?? 0, sentAt: json['sentAt'] != null ? DateTime.fromMillisecondsSinceEpoch(json['sentAt'] as int) : null, deliveredAt: json['deliveredAt'] != null ? DateTime.fromMillisecondsSinceEpoch(json['deliveredAt'] as int) : null, tripTimeMs: json['tripTimeMs'] as int?, pathLength: decodedPathLength, pathBytes: decodedPathBytes, reactions: (json['reactions'] as Map?)?.map( (key, value) => MapEntry( key, (value is int) ? List.filled(value, null) : List.from(value), ), ) ?? {}, reactionStatuses: { for (final entry in (json['reactionStatuses'] as Map? ?? const {}) .entries) entry.key: ?_statusAt(entry.value), }, fourByteRoomContactKey: json['fourByteRoomContactKey'] != null ? Uint8List.fromList( base64Decode(json['fourByteRoomContactKey'] as String), ) : null, ); } /// Out-of-range or non-int values yield null; `elementAtOrNull` throws on /// negative indexes. static MessageStatus? _statusAt(Object? value) { if (value is! int || value < 0 || value >= MessageStatus.values.length) { return null; } return MessageStatus.values[value]; } }