54 lines
1.5 KiB
Dart
54 lines
1.5 KiB
Dart
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
|
|
|
class MessagePersistence {
|
|
static Future<void> replace(
|
|
Database db,
|
|
Map<String, dynamic> row, {
|
|
String? removeId,
|
|
}) => db.transaction((txn) async {
|
|
await txn.insert(
|
|
'messages',
|
|
row,
|
|
conflictAlgorithm: ConflictAlgorithm.replace,
|
|
);
|
|
if (removeId != null && removeId != row['id']) {
|
|
await txn.delete(
|
|
'messages',
|
|
where: 'account_id = ? AND chat_id = ? AND id = ?',
|
|
whereArgs: [row['account_id'], row['chat_id'], removeId],
|
|
);
|
|
}
|
|
});
|
|
|
|
/// Updates only an item still queued by this account. Deletion wins over a
|
|
/// late network response, including a retry or a successful server receipt.
|
|
static Future<bool> replacePending(
|
|
Database db,
|
|
Map<String, dynamic> row, {
|
|
required String pendingId,
|
|
}) async {
|
|
return db.transaction((txn) async {
|
|
final args = [row['account_id'], row['chat_id'], pendingId];
|
|
const where =
|
|
"account_id = ? AND chat_id = ? AND id = ? "
|
|
"AND status = 'pending' AND deleted = 0";
|
|
final existing = await txn.query(
|
|
'messages',
|
|
columns: ['id'],
|
|
where: where,
|
|
whereArgs: args,
|
|
);
|
|
if (existing.isEmpty) return false;
|
|
await txn.insert(
|
|
'messages',
|
|
row,
|
|
conflictAlgorithm: ConflictAlgorithm.replace,
|
|
);
|
|
if (row['id'] != pendingId) {
|
|
await txn.delete('messages', where: where, whereArgs: args);
|
|
}
|
|
return true;
|
|
});
|
|
}
|
|
}
|