Sync : add log and continue when read_receipts fail

This commit is contained in:
ganfra 2019-04-01 17:39:27 +02:00
parent d110dac0a6
commit 04b4f32e16
2 changed files with 28 additions and 11 deletions

View file

@ -18,6 +18,7 @@ package im.vector.matrix.android.internal.session.sync
import im.vector.matrix.android.internal.database.model.ReadReceiptEntity
import io.realm.Realm
import timber.log.Timber
// the receipts dictionnaries
@ -29,26 +30,32 @@ typealias ReadReceiptContent = Map<String, Map<String, Map<String, Map<String, D
internal class ReadReceiptHandler {
fun handle(realm: Realm, roomId: String, content: ReadReceiptContent?): List<ReadReceiptEntity> {
fun handle(realm: Realm, roomId: String, content: ReadReceiptContent?) {
if (content == null) {
return emptyList()
return
}
val readReceipts = content
try {
val readReceipts = mapContentToReadReceiptEntities(roomId, content)
realm.insertOrUpdate(readReceipts)
} catch (exception: Exception) {
Timber.e("Fail to handle read receipt for room $roomId")
}
}
private fun mapContentToReadReceiptEntities(roomId: String, content: ReadReceiptContent): List<ReadReceiptEntity> {
return content
.flatMap { (eventId, receiptDict) ->
receiptDict
.filterKeys { it == "m.read" }
.flatMap { (_, userIdsDict) ->
userIdsDict.map { (userId, paramsDict) ->
val ts = paramsDict.filterKeys { it == "ts" }
.values
.firstOrNull() ?: 0.0
.values
.firstOrNull() ?: 0.0
val primaryKey = roomId + userId
ReadReceiptEntity(primaryKey, userId, eventId, roomId, ts)
}
}
}
realm.insertOrUpdate(readReceipts)
return readReceipts
}
}

View file

@ -32,9 +32,14 @@ import im.vector.matrix.android.internal.database.query.findLastLiveChunkFromRoo
import im.vector.matrix.android.internal.database.query.where
import im.vector.matrix.android.internal.session.room.RoomSummaryUpdater
import im.vector.matrix.android.internal.session.room.timeline.PaginationDirection
import im.vector.matrix.android.internal.session.sync.model.*
import im.vector.matrix.android.internal.session.sync.model.InvitedRoomSync
import im.vector.matrix.android.internal.session.sync.model.RoomSync
import im.vector.matrix.android.internal.session.sync.model.RoomSyncAccountData
import im.vector.matrix.android.internal.session.sync.model.RoomSyncEphemeral
import im.vector.matrix.android.internal.session.sync.model.RoomsSyncResponse
import io.realm.Realm
import io.realm.kotlin.createObject
import timber.log.Timber
internal class RoomSyncHandler(private val monarchy: Monarchy,
private val readReceiptHandler: ReadReceiptHandler,
@ -70,8 +75,10 @@ internal class RoomSyncHandler(private val monarchy: Monarchy,
roomId: String,
roomSync: RoomSync): RoomEntity {
Timber.v("Handle join sync for room $roomId")
val roomEntity = RoomEntity.where(realm, roomId).findFirst()
?: realm.createObject(roomId)
?: realm.createObject(roomId)
if (roomEntity.membership == MyMembership.INVITED) {
roomEntity.chunks.deleteAllFromRealm()
@ -119,6 +126,9 @@ internal class RoomSyncHandler(private val monarchy: Monarchy,
roomId: String,
roomSync:
InvitedRoomSync): RoomEntity {
Timber.v("Handle invited sync for room $roomId")
val roomEntity = RoomEntity()
roomEntity.roomId = roomId
roomEntity.membership = MyMembership.INVITED
@ -164,7 +174,7 @@ internal class RoomSyncHandler(private val monarchy: Monarchy,
ephemeral.events
.filter { it.type == EventType.RECEIPT }
.map { it.content.toModel<ReadReceiptContent>() }
.flatMap { readReceiptHandler.handle(realm, roomId, it) }
.forEach { readReceiptHandler.handle(realm, roomId, it) }
}
private fun handleRoomAccountDataEvents(realm: Realm, roomId: String, accountData: RoomSyncAccountData) {