diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/PendingRetryReceiptCache.kt b/app/src/main/java/org/thoughtcrime/securesms/database/PendingRetryReceiptCache.kt index 7ac9a56d40..65b6f7dc57 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/PendingRetryReceiptCache.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/PendingRetryReceiptCache.kt @@ -23,10 +23,15 @@ class PendingRetryReceiptCache @VisibleForTesting constructor( fun insert(author: RecipientId, authorDevice: Int, sentTimestamp: Long, receivedTimestamp: Long, threadId: Long) { if (!FeatureFlags.retryReceipts()) return ensurePopulated() - + val model: PendingRetryReceiptModel = database.insert(author, authorDevice, sentTimestamp, receivedTimestamp, threadId) synchronized(pendingRetries) { - val model: PendingRetryReceiptModel = database.insert(author, authorDevice, sentTimestamp, receivedTimestamp, threadId) - pendingRetries[RemoteMessageId(author, sentTimestamp)] = model + val key = RemoteMessageId(author, sentTimestamp) + val existing: PendingRetryReceiptModel? = pendingRetries[key] + + // We rely on db unique constraint and auto-incrementing ids for conflict resolution here. + if (existing == null || existing.id < model.id) { + pendingRetries[key] = model + } } } @@ -54,8 +59,8 @@ class PendingRetryReceiptCache @VisibleForTesting constructor( synchronized(pendingRetries) { pendingRetries.remove(RemoteMessageId(model.author, model.sentTimestamp)) - database.delete(model) } + database.delete(model) } fun clear() {