From 04b4f32e160968e7cd121daaa9cecce11bed73d3 Mon Sep 17 00:00:00 2001 From: ganfra Date: Mon, 1 Apr 2019 17:39:27 +0200 Subject: [PATCH] Sync : add log and continue when read_receipts fail --- .../session/sync/ReadReceiptHandler.kt | 23 ++++++++++++------- .../internal/session/sync/RoomSyncHandler.kt | 16 ++++++++++--- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/ReadReceiptHandler.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/ReadReceiptHandler.kt index 35753ed6..7557f71a 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/ReadReceiptHandler.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/ReadReceiptHandler.kt @@ -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 { + 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 { + 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 } - } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt index 70848eea..56929618 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt @@ -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() } - .flatMap { readReceiptHandler.handle(realm, roomId, it) } + .forEach { readReceiptHandler.handle(realm, roomId, it) } } private fun handleRoomAccountDataEvents(realm: Realm, roomId: String, accountData: RoomSyncAccountData) {