From da438657330e2e3bad6e716a37d4a9eaa75ef8d4 Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Wed, 16 Mar 2022 09:27:48 +0100 Subject: [PATCH] [TMP] More ReadMarker debugging Change-Id: I1defffc997864db74e15fe6b06645adeed7b67fe --- .../internal/database/helper/ChunkEntityHelper.kt | 15 ++++++++------- .../sdk/internal/database/query/ReadQueries.kt | 12 ++++++++---- .../session/room/read/SetReadMarkersTask.kt | 9 ++++++++- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/ChunkEntityHelper.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/ChunkEntityHelper.kt index 8bec2a443c..f67da2974e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/ChunkEntityHelper.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/ChunkEntityHelper.kt @@ -16,6 +16,7 @@ package org.matrix.android.sdk.internal.database.helper +import de.spiritcroc.matrixsdk.util.Dimber import io.realm.Realm import io.realm.Sort import io.realm.kotlin.createObject @@ -216,20 +217,20 @@ internal fun ChunkEntity.doesNextChunksVerifyCondition(linkCondition: (ChunkEnti return false } -internal fun ChunkEntity.isMoreRecentThan(chunkToCheck: ChunkEntity): Boolean { - if (this.isLastForward) return true - if (chunkToCheck.isLastForward) return false +internal fun ChunkEntity.isMoreRecentThan(chunkToCheck: ChunkEntity, dimber: Dimber? = null): Boolean { + if (this.isLastForward) return true.also { dimber?.i { "isMoreReacentThan = true (this.isLastForward)" } } + if (chunkToCheck.isLastForward) return false.also { dimber?.i { "isMoreReacentThan = false (ctc.isLastForward)" } } // Check if the chunk to check is linked to this one if (chunkToCheck.doesNextChunksVerifyCondition { it == this }) { - return true + return true.also { dimber?.i { "isMoreReacentThan = true (ctc->this)" } } } if (this.doesNextChunksVerifyCondition { it == chunkToCheck }) { - return false + return false.also { dimber?.i { "isMoreReacentThan = false (this->ctc)" } } } // Otherwise check if this chunk is linked to last forward if (this.doesNextChunksVerifyCondition { it.isLastForward }) { - return true + return true.also { dimber?.i { "isMoreReacentThan = true (this->isLastForward)" } } } // We don't know, so we assume it's false - return false + return false.also { dimber?.i { "isMoreReacentThan = false (fallback)" } } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/query/ReadQueries.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/query/ReadQueries.kt index e3016bd5d9..b12bb99684 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/query/ReadQueries.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/query/ReadQueries.kt @@ -15,6 +15,7 @@ */ package org.matrix.android.sdk.internal.database.query +import de.spiritcroc.matrixsdk.util.Dimber import io.realm.Realm import io.realm.RealmConfiguration import org.matrix.android.sdk.api.session.events.model.LocalEcho @@ -80,9 +81,10 @@ private fun Realm.hasReadReceiptInLatestChunk(latestChunkEntity: ChunkEntity, ro internal fun isReadMarkerMoreRecent(realmConfiguration: RealmConfiguration, roomId: String?, - eventId: String?): Boolean { + eventId: String?, + dimber: Dimber? = null): Boolean { if (roomId.isNullOrBlank() || eventId.isNullOrBlank()) { - return false + return false.also { dimber?.i { "isReadMarkerMoreRecent = false (roomId ${roomId.isNullOrBlank()} || eventId ${eventId.isNullOrBlank()}" } } } return Realm.getInstance(realmConfiguration).use { realm -> val eventToCheck = TimelineEventEntity.where(realm, roomId = roomId, eventId = eventId).findFirst() @@ -93,11 +95,13 @@ internal fun isReadMarkerMoreRecent(realmConfiguration: RealmConfiguration, if (eventToCheckChunk == readMarkerChunk) { val readMarkerIndex = readMarkerEvent?.displayIndex ?: Int.MIN_VALUE val eventToCheckIndex = eventToCheck?.displayIndex ?: Int.MAX_VALUE + dimber?.i { "isReadMarkerMoreRecent = ($eventToCheckIndex <= $readMarkerIndex)" } eventToCheckIndex <= readMarkerIndex } else { - eventToCheckChunk != null && readMarkerChunk?.isMoreRecentThan(eventToCheckChunk) == true + dimber?.i { "isReadMarkerMoreRecent = (non-null ${eventToCheckChunk!=null} && ${readMarkerChunk!=null} && ...)" } + eventToCheckChunk != null && readMarkerChunk?.isMoreRecentThan(eventToCheckChunk, dimber) == true } - } + }.also { dimber?.i { "isReadMarkerMoreRecent result $it" } } } internal fun isMarkedUnread(realmConfiguration: RealmConfiguration, roomId: String?): Boolean { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/read/SetReadMarkersTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/read/SetReadMarkersTask.kt index 7dbb1bb307..0666801c22 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/read/SetReadMarkersTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/read/SetReadMarkersTask.kt @@ -17,6 +17,8 @@ package org.matrix.android.sdk.internal.session.room.read import com.zhuinden.monarchy.Monarchy +import de.spiritcroc.matrixsdk.util.DbgUtil +import de.spiritcroc.matrixsdk.util.Dimber import io.realm.Realm import org.matrix.android.sdk.api.session.events.model.LocalEcho import org.matrix.android.sdk.internal.database.model.RoomSummaryEntity @@ -61,6 +63,8 @@ internal class DefaultSetReadMarkersTask @Inject constructor( private val globalErrorReceiver: GlobalErrorReceiver ) : SetReadMarkersTask { + private val rmDimber = Dimber("ReadMarkerDbg", DbgUtil.DBG_READ_MARKER) + override suspend fun execute(params: SetReadMarkersTask.Params) { val markers = mutableMapOf() Timber.v("Execute set read marker with params: $params") @@ -75,12 +79,15 @@ internal class DefaultSetReadMarkersTask @Inject constructor( } else { params.readReceiptEventId } - if (fullyReadEventId != null && !isReadMarkerMoreRecent(monarchy.realmConfiguration, params.roomId, fullyReadEventId)) { + if (fullyReadEventId != null && !isReadMarkerMoreRecent(monarchy.realmConfiguration, params.roomId, fullyReadEventId, rmDimber)) { + rmDimber.i { "Set to $fullyReadEventId if it's not local..." } if (LocalEcho.isLocalEchoId(fullyReadEventId)) { Timber.w("Can't set read marker for local event $fullyReadEventId") } else { markers[READ_MARKER] = fullyReadEventId } + } else { + rmDimber.i { "Did not set to $fullyReadEventId" } } if (readReceiptEventId != null && !isEventRead(monarchy.realmConfiguration, userId, params.roomId, readReceiptEventId)) {