From 088657168fca8757dab07de7b28bf3433229366d Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Thu, 19 May 2022 22:32:57 +0200 Subject: [PATCH] Avoid missing messages when trying to add event to chunk twice Change-Id: I98ae8e7a5254e6f93fd2a38498b7f8e173ccd565 --- .../room/timeline/TokenChunkEventPersistor.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TokenChunkEventPersistor.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TokenChunkEventPersistor.kt index d412fb8703..6a6dac51a2 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TokenChunkEventPersistor.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TokenChunkEventPersistor.kt @@ -213,6 +213,20 @@ internal class TokenChunkEventPersistor @Inject constructor( // If it exists, we want to stop here, just link the prevChunk val existingChunk = existingTimelineEvent?.chunk?.firstOrNull() if (existingChunk != null) { + if (existingChunk == currentChunk) { + Timber.w("Avoid double insertion of event $eventId, shouldn't happen in an ideal world | " + + "direction: $direction.value" + + "room: $roomId" + + "chunk: ${existingChunk.identifier()}" + + "eventId: $eventId" + + "caughtByOldCheck ${((if (direction == PaginationDirection.BACKWARDS) currentChunk.nextChunk else currentChunk.prevChunk) == existingChunk)}" + + "caughtByOldBackwardCheck ${(currentChunk.nextChunk == existingChunk)}" + + "caughtByOldForwardCheck ${(currentChunk.prevChunk == existingChunk)}" + ) + // No idea why this happens, but if it does, we don't want to throw away all the other events + // (or even link chunks to themselves) + return@forEach + } val alreadyLinkedNext = currentChunk.doesNextChunksVerifyCondition { it == existingChunk } val alreadyLinkedPrev = currentChunk.doesPrevChunksVerifyCondition { it == existingChunk } if (alreadyLinkedNext || alreadyLinkedPrev) {