diff --git a/CHANGELOG.md b/CHANGELOG.md index debf9e9e..b7b23997 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,8 @@ - Improve: Backup screen clearer and easier to understand - Improve: Show username above messages in group chats - Fix: Background audio correctly pauses and resumes when viewing videos -- Fix: Multiple bug fixes +- Fix: Sometimes old messages were being received as duplicates +- Fix: Multiple smaller bug fixes ## 0.5.0 diff --git a/lib/src/database/daos/receipts.dao.dart b/lib/src/database/daos/receipts.dao.dart index f79a2281..d15b85e0 100644 --- a/lib/src/database/daos/receipts.dao.dart +++ b/lib/src/database/daos/receipts.dao.dart @@ -72,7 +72,7 @@ class ReceiptsDao extends DatabaseAccessor with _$ReceiptsDaoMixin { await (delete(receivedReceipts)..where( (t) => (t.createdAt.isSmallerThanValue( clock.now().subtract( - const Duration(days: 25), + const Duration(days: 45), ), )), )) diff --git a/lib/src/services/api/messages.api.dart b/lib/src/services/api/messages.api.dart index c46a4c36..9c2aa47d 100644 --- a/lib/src/services/api/messages.api.dart +++ b/lib/src/services/api/messages.api.dart @@ -114,22 +114,6 @@ Future<(Uint8List, Uint8List?)?> _tryToSendCompleteMessageInternal({ // ignore: parameter_assignments receipt = loadedReceipt; - if (receipt.retryCount >= 2) { - // After two retries, change the receiptId. This addresses a bug where the receiver received the message and marked it as received, - // but the app was closed before the message was fully processed. Because the receipt was already stored, subsequent retries were - // detected as duplicates and rejected. - final oldReceiptId = receipt.receiptId; - final updatedReceipt = await twonlyDB.receiptsDao.rotateReceiptId( - oldReceiptId, - ); - if (updatedReceipt != null) { - Log.info( - 'Changed receiptId $oldReceiptId to ${updatedReceipt.receiptId} as retryCount is ${receipt.retryCount}', - ); - receipt = updatedReceipt; - } - } - final contact = await twonlyDB.contactsDao.getContactById( receipt.contactId, ); diff --git a/lib/src/services/api/server_messages.api.dart b/lib/src/services/api/server_messages.api.dart index 77498eb5..cd778309 100644 --- a/lib/src/services/api/server_messages.api.dart +++ b/lib/src/services/api/server_messages.api.dart @@ -141,6 +141,26 @@ Future _handleClient2ClientMessage( } if (await twonlyDB.receiptsDao.isDuplicated(receiptId)) { + Log.info( + '[$receiptId] Message is a duplicate. Sending delivery receipt again.', + ); + try { + final response = Message(type: Message_Type.SENDER_DELIVERY_RECEIPT); + await twonlyDB.receiptsDao.insertReceipt( + ReceiptsCompanion( + receiptId: Value(receiptId), + contactId: Value(fromUserId), + message: Value(response.writeToBuffer()), + contactWillSendsReceipt: const Value(false), + ), + ); + await tryToSendCompleteMessage( + receiptId: receiptId, + blocking: false, + ); + } catch (e) { + Log.warn('[$receiptId] Error handling duplicate receipt ACK: $e'); + } return; }