[TEST] Fight duplicate read markers in main timeline

Let synced null and main markers both update the main marker.
Looks like desktop does the same:
7158a99a1c/src/sync-accumulator.ts (L459-L460)

Change-Id: Ic4bf8b4d947fdd9882a3da053ac2b93747f1f20a
This commit is contained in:
SpiritCroc 2023-02-04 11:34:03 +01:00
parent 3b57c6c5f6
commit cd9a3b9be2
3 changed files with 33 additions and 24 deletions

View File

@ -23,6 +23,7 @@ import io.realm.kotlin.createObject
import org.matrix.android.sdk.api.session.events.model.content.EncryptedEventContent import org.matrix.android.sdk.api.session.events.model.content.EncryptedEventContent
import org.matrix.android.sdk.api.session.events.model.toModel import org.matrix.android.sdk.api.session.events.model.toModel
import org.matrix.android.sdk.api.session.room.model.RoomMemberContent import org.matrix.android.sdk.api.session.room.model.RoomMemberContent
import org.matrix.android.sdk.api.session.room.read.ReadService.Companion.THREAD_ID_MAIN
import org.matrix.android.sdk.internal.crypto.model.SessionInfo import org.matrix.android.sdk.internal.crypto.model.SessionInfo
import org.matrix.android.sdk.internal.database.mapper.asDomain import org.matrix.android.sdk.internal.database.mapper.asDomain
import org.matrix.android.sdk.internal.database.model.ChunkEntity import org.matrix.android.sdk.internal.database.model.ChunkEntity
@ -136,17 +137,21 @@ private fun handleReadReceipts(realm: Realm, roomId: String, eventEntity: EventE
val originServerTs = eventEntity.originServerTs val originServerTs = eventEntity.originServerTs
if (originServerTs != null) { if (originServerTs != null) {
val timestampOfEvent = originServerTs.toDouble() val timestampOfEvent = originServerTs.toDouble()
val readReceiptOfSender = ReadReceiptEntity.getOrCreate(realm, roomId = roomId, userId = senderId, threadId = eventEntity.rootThreadEventId) // null receipts also update the main receipt
// If the synced RR is older, update val receiptDestination = eventEntity.rootThreadEventId ?: THREAD_ID_MAIN
if (timestampOfEvent > readReceiptOfSender.originServerTs) { setOf(receiptDestination, eventEntity.rootThreadEventId).distinct().forEach { rootThreadEventId ->
val previousReceiptsSummary = ReadReceiptsSummaryEntity.where(realm, eventId = readReceiptOfSender.eventId).findFirst() val readReceiptOfSender = ReadReceiptEntity.getOrCreate(realm, roomId = roomId, userId = senderId, threadId = rootThreadEventId)
rrDimber.i{"Handle outdated chunk RR $roomId / $senderId thread ${eventEntity.rootThreadEventId}: event ${readReceiptOfSender.eventId} -> ${eventEntity.eventId}"} // If the synced RR is older, update
readReceiptOfSender.eventId = eventEntity.eventId if (timestampOfEvent > readReceiptOfSender.originServerTs) {
readReceiptOfSender.originServerTs = timestampOfEvent val previousReceiptsSummary = ReadReceiptsSummaryEntity.where(realm, eventId = readReceiptOfSender.eventId).findFirst()
previousReceiptsSummary?.readReceipts?.remove(readReceiptOfSender) rrDimber.i { "Handle outdated chunk RR $roomId / $senderId thread $rootThreadEventId(${eventEntity.rootThreadEventId}): event ${readReceiptOfSender.eventId} -> ${eventEntity.eventId}" }
readReceiptsSummaryEntity.readReceipts.add(readReceiptOfSender) readReceiptOfSender.eventId = eventEntity.eventId
} else { readReceiptOfSender.originServerTs = timestampOfEvent
rrDimber.i{"Handled chunk RR $roomId / $senderId thread ${eventEntity.rootThreadEventId}: keep ${readReceiptOfSender.eventId} (not ${eventEntity.eventId})"} previousReceiptsSummary?.readReceipts?.remove(readReceiptOfSender)
readReceiptsSummaryEntity.readReceipts.add(readReceiptOfSender)
} else {
rrDimber.i { "Handled chunk RR $roomId / $senderId thread $rootThreadEventId(${eventEntity.rootThreadEventId}): keep ${readReceiptOfSender.eventId} (not ${eventEntity.eventId})" }
}
} }
} }
return readReceiptsSummaryEntity return readReceiptsSummaryEntity

View File

@ -20,6 +20,7 @@ import de.spiritcroc.matrixsdk.util.DbgUtil
import de.spiritcroc.matrixsdk.util.Dimber import de.spiritcroc.matrixsdk.util.Dimber
import io.realm.Realm import io.realm.Realm
import org.matrix.android.sdk.api.session.events.model.EventType import org.matrix.android.sdk.api.session.events.model.EventType
import org.matrix.android.sdk.api.session.room.read.ReadService
import org.matrix.android.sdk.internal.database.model.ReadReceiptEntity import org.matrix.android.sdk.internal.database.model.ReadReceiptEntity
import org.matrix.android.sdk.internal.database.model.ReadReceiptsSummaryEntity import org.matrix.android.sdk.internal.database.model.ReadReceiptsSummaryEntity
import org.matrix.android.sdk.internal.database.query.createUnmanaged import org.matrix.android.sdk.internal.database.query.createUnmanaged
@ -145,19 +146,22 @@ internal class ReadReceiptHandler @Inject constructor(
for ((userId, paramsDict) in userIdsDict) { for ((userId, paramsDict) in userIdsDict) {
val ts = paramsDict[TIMESTAMP_KEY] as? Double ?: 0.0 val ts = paramsDict[TIMESTAMP_KEY] as? Double ?: 0.0
val threadId = paramsDict[THREAD_ID_KEY] as String? val syncedThreadId = paramsDict[THREAD_ID_KEY] as String?
val receiptEntity = ReadReceiptEntity.getOrCreate(realm, roomId, userId, threadId) val receiptDestination = syncedThreadId ?: ReadService.THREAD_ID_MAIN
// ensure new ts is superior to the previous one setOf(receiptDestination, syncedThreadId).distinct().forEach { threadId ->
if (ts > receiptEntity.originServerTs) { val receiptEntity = ReadReceiptEntity.getOrCreate(realm, roomId, userId, threadId)
rrDimber.i{"Handle outdated sync RR $roomId / $userId thread $threadId: event ${receiptEntity.eventId} -> $eventId"} // ensure new ts is superior to the previous one
ReadReceiptsSummaryEntity.where(realm, receiptEntity.eventId).findFirst()?.also { if (ts > receiptEntity.originServerTs) {
it.readReceipts.remove(receiptEntity) rrDimber.i { "Handle outdated sync RR $roomId / $userId thread $threadId($syncedThreadId): event ${receiptEntity.eventId} -> $eventId" }
ReadReceiptsSummaryEntity.where(realm, receiptEntity.eventId).findFirst()?.also {
it.readReceipts.remove(receiptEntity)
}
receiptEntity.eventId = eventId
receiptEntity.originServerTs = ts
readReceiptsSummary.readReceipts.add(receiptEntity)
} else {
rrDimber.i { "Handle keep sync RR $roomId / $userId thread $threadId($syncedThreadId): event ${receiptEntity.eventId} (not $eventId)" }
} }
receiptEntity.eventId = eventId
receiptEntity.originServerTs = ts
readReceiptsSummary.readReceipts.add(receiptEntity)
} else {
rrDimber.i{"Handle keep sync RR $roomId / $userId thread $threadId: event ${receiptEntity.eventId} (not $eventId)"}
} }
} }
} }

View File

@ -628,7 +628,7 @@ class TimelineEventController @Inject constructor(
return if (partialState.isFromThreadTimeline()) { return if (partialState.isFromThreadTimeline()) {
this.threadId == partialState.rootThreadEventId this.threadId == partialState.rootThreadEventId
} else { } else {
this.threadId == null || this.threadId == ReadService.THREAD_ID_MAIN this.threadId == ReadService.THREAD_ID_MAIN
} }
} }