[TEST] Restructure RR deduplication logic and avoid threads

We probably should add an extra check to only set main_or_null to events
which actually exist in the main thread

Change-Id: I8db0010e8717b6941917225fcce6646685814665
This commit is contained in:
SpiritCroc 2023-04-02 12:45:36 +02:00
parent 5ef2341bec
commit 76d1c81495
2 changed files with 28 additions and 12 deletions

View File

@ -147,20 +147,28 @@ private fun handleReadReceipts(realm: Realm, roomId: String, eventEntity: EventE
receiptDestinations.forEach { rootThreadEventId -> receiptDestinations.forEach { rootThreadEventId ->
val readReceiptOfSender = ReadReceiptEntity.getOrCreate(realm, roomId = roomId, userId = senderId, threadId = rootThreadEventId) val readReceiptOfSender = ReadReceiptEntity.getOrCreate(realm, roomId = roomId, userId = senderId, threadId = rootThreadEventId)
val shouldForceMon: Boolean val shouldForceMon: Boolean
val shouldSkipMon = if (rootThreadEventId == THREAD_ID_MAIN_OR_NULL) { val shouldSkipMon: Boolean
if (rootThreadEventId == THREAD_ID_MAIN_OR_NULL) {
val previousReceiptsSummary = ReadReceiptsSummaryEntity.where(realm, eventId = readReceiptOfSender.eventId).findFirst() val previousReceiptsSummary = ReadReceiptsSummaryEntity.where(realm, eventId = readReceiptOfSender.eventId).findFirst()
val oldEventTs = previousReceiptsSummary?.let { EventEntity.where(realm, roomId, it.eventId).findFirst()?.originServerTs } val oldEventTs = previousReceiptsSummary?.let { EventEntity.where(realm, roomId, it.eventId).findFirst()?.originServerTs }
val newEventTs = EventEntity.where(realm, roomId, eventEntity.eventId).findFirst()?.originServerTs val newEvent = EventEntity.where(realm, roomId, eventEntity.eventId).findFirst()
val newEventTs = newEvent?.originServerTs
// Avoid setting MAIN_OR_NULL to an event which only exists in the combined null-timeline, but not in the actual main-timeline
if (eventEntity.rootThreadEventId != THREAD_ID_MAIN && newEvent?.rootThreadEventId != null) {
shouldForceMon = false
shouldSkipMon = true
} else {
shouldForceMon = oldEventTs != null && newEventTs != null && oldEventTs < newEventTs shouldForceMon = oldEventTs != null && newEventTs != null && oldEventTs < newEventTs
oldEventTs != null && newEventTs != null && oldEventTs > newEventTs shouldSkipMon = oldEventTs != null && newEventTs != null && oldEventTs > newEventTs
}
} else { } else {
shouldForceMon = false shouldForceMon = false
false shouldSkipMon = false
} }
// If the synced RR is older, update // If the synced RR is older, update
if (shouldForceMon || (timestampOfEvent > readReceiptOfSender.originServerTs && !shouldSkipMon)) { if (shouldForceMon || (timestampOfEvent > readReceiptOfSender.originServerTs && !shouldSkipMon)) {
val previousReceiptsSummary = ReadReceiptsSummaryEntity.where(realm, eventId = readReceiptOfSender.eventId).findFirst() val previousReceiptsSummary = ReadReceiptsSummaryEntity.where(realm, eventId = readReceiptOfSender.eventId).findFirst()
rrDimber.i { "Handle outdated chunk RR $roomId / $senderId thread $rootThreadEventId(${eventEntity.rootThreadEventId}): event ${readReceiptOfSender.eventId} -> ${eventEntity.eventId}" } rrDimber.i { "Handle outdated chunk RR $roomId / $senderId thread $rootThreadEventId(${eventEntity.rootThreadEventId}): event ${readReceiptOfSender.eventId} -> ${eventEntity.eventId} || force $shouldForceMon legacy ${timestampOfEvent > readReceiptOfSender.originServerTs}" }
readReceiptOfSender.eventId = eventEntity.eventId readReceiptOfSender.eventId = eventEntity.eventId
readReceiptOfSender.originServerTs = timestampOfEvent readReceiptOfSender.originServerTs = timestampOfEvent
previousReceiptsSummary?.readReceipts?.remove(readReceiptOfSender) previousReceiptsSummary?.readReceipts?.remove(readReceiptOfSender)

View File

@ -173,18 +173,26 @@ internal class ReadReceiptHandler @Inject constructor(
receiptDestinations.forEach { threadId -> receiptDestinations.forEach { threadId ->
val receiptEntity = ReadReceiptEntity.getOrCreate(realm, roomId, userId, threadId) val receiptEntity = ReadReceiptEntity.getOrCreate(realm, roomId, userId, threadId)
val shouldForceMon: Boolean val shouldForceMon: Boolean
val shouldSkipMon = if (threadId == THREAD_ID_MAIN_OR_NULL) { val shouldSkipMon: Boolean
if (threadId == THREAD_ID_MAIN_OR_NULL) {
val oldEventTs = EventEntity.where(realm, roomId, receiptEntity.eventId).findFirst()?.originServerTs val oldEventTs = EventEntity.where(realm, roomId, receiptEntity.eventId).findFirst()?.originServerTs
val newEventTs = EventEntity.where(realm, roomId, eventId).findFirst()?.originServerTs val newEvent = EventEntity.where(realm, roomId, eventId).findFirst()
val newEventTs = newEvent?.originServerTs
// Avoid setting MAIN_OR_NULL to an event which only exists in the combined null-timeline, but not in the actual main-timeline
if (syncedThreadId != THREAD_ID_MAIN && newEvent?.rootThreadEventId != null) {
shouldForceMon = false
shouldSkipMon = true
} else {
shouldForceMon = oldEventTs != null && newEventTs != null && oldEventTs < newEventTs shouldForceMon = oldEventTs != null && newEventTs != null && oldEventTs < newEventTs
oldEventTs != null && newEventTs != null && oldEventTs > newEventTs shouldSkipMon = oldEventTs != null && newEventTs != null && oldEventTs > newEventTs
}
} else { } else {
shouldForceMon = false shouldForceMon = false
false shouldSkipMon = false
} }
// ensure new ts is superior to the previous one // ensure new ts is superior to the previous one
if (shouldForceMon || (ts > receiptEntity.originServerTs && !shouldSkipMon)) { if (shouldForceMon || (ts > receiptEntity.originServerTs && !shouldSkipMon)) {
rrDimber.i { "Handle outdated sync RR $roomId / $userId thread $threadId($syncedThreadId): event ${receiptEntity.eventId} -> $eventId" } rrDimber.i { "Handle outdated sync RR $roomId / $userId thread $threadId($syncedThreadId): event ${receiptEntity.eventId} -> $eventId || force $shouldForceMon legacy ${ts > receiptEntity.originServerTs}" }
ReadReceiptsSummaryEntity.where(realm, receiptEntity.eventId).findFirst()?.also { ReadReceiptsSummaryEntity.where(realm, receiptEntity.eventId).findFirst()?.also {
it.readReceipts.remove(receiptEntity) it.readReceipts.remove(receiptEntity)
} }