Sync: avoid deleting root event of CurrentState on gappy sync

This commit is contained in:
ganfra 2022-02-02 18:07:39 +01:00
parent f187216f3c
commit 85af3767b7
3 changed files with 17 additions and 3 deletions

View File

@ -56,7 +56,7 @@ internal class RealmSessionStoreMigration @Inject constructor(
) : RealmMigration { ) : RealmMigration {
companion object { companion object {
const val SESSION_STORE_SCHEMA_VERSION = 21L const val SESSION_STORE_SCHEMA_VERSION = 22L
} }
/** /**
@ -90,6 +90,7 @@ internal class RealmSessionStoreMigration @Inject constructor(
if (oldVersion <= 18) migrateTo19(realm) if (oldVersion <= 18) migrateTo19(realm)
if (oldVersion <= 19) migrateTo20(realm) if (oldVersion <= 19) migrateTo20(realm)
if (oldVersion <= 20) migrateTo21(realm) if (oldVersion <= 20) migrateTo21(realm)
if (oldVersion <= 21) migrateTo22(realm)
} }
private fun migrateTo1(realm: DynamicRealm) { private fun migrateTo1(realm: DynamicRealm) {
@ -445,4 +446,14 @@ internal class RealmSessionStoreMigration @Inject constructor(
} }
} }
} }
private fun migrateTo22(realm: DynamicRealm) {
Timber.d("Step 21 -> 22")
val hasMissingStateEvent = realm.where("CurrentStateEventEntity").isNull(CurrentStateEventEntityFields.ROOT.`$`).findFirst() != null
if (hasMissingStateEvent) {
Timber.v("Has some missing state event, clear session cache")
realm.deleteAll()
}
}
} }

View File

@ -52,6 +52,9 @@ internal fun ChunkEntity.deleteOnCascade(deleteStateEvents: Boolean, canDeleteRo
if (deleteStateEvents) { if (deleteStateEvents) {
stateEvents.deleteAllFromRealm() stateEvents.deleteAllFromRealm()
} }
timelineEvents.clearWith { it.deleteOnCascade(canDeleteRoot) } timelineEvents.clearWith {
val deleteRoot = canDeleteRoot && (it.root?.stateKey == null || deleteStateEvents)
it.deleteOnCascade(deleteRoot)
}
deleteFromRealm() deleteFromRealm()
} }

View File

@ -350,7 +350,7 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle
aggregator: SyncResponsePostTreatmentAggregator): ChunkEntity { aggregator: SyncResponsePostTreatmentAggregator): ChunkEntity {
val lastChunk = ChunkEntity.findLastForwardChunkOfRoom(realm, roomEntity.roomId) val lastChunk = ChunkEntity.findLastForwardChunkOfRoom(realm, roomEntity.roomId)
if (isLimited && lastChunk != null) { if (isLimited && lastChunk != null) {
lastChunk.deleteOnCascade(deleteStateEvents = true, canDeleteRoot = true) lastChunk.deleteOnCascade(deleteStateEvents = false, canDeleteRoot = true)
} }
val chunkEntity = if (!isLimited && lastChunk != null) { val chunkEntity = if (!isLimited && lastChunk != null) {
lastChunk lastChunk