From d469299f426584df9e7e39b8a525e04b67c81cdd Mon Sep 17 00:00:00 2001 From: ganfra Date: Mon, 15 Jul 2019 20:26:18 +0200 Subject: [PATCH] RoomMembers: should fix state events issues --- .../room/membership/LoadRoomMembersTask.kt | 2 +- .../room/timeline/TokenChunkEventPersistor.kt | 4 ++-- .../internal/session/sync/RoomSyncHandler.kt | 18 +++++++----------- .../internal/session/user/UserEntityFactory.kt | 2 +- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/LoadRoomMembersTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/LoadRoomMembersTask.kt index 3364a8e684..a30906053d 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/LoadRoomMembersTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/LoadRoomMembersTask.kt @@ -74,7 +74,7 @@ internal class DefaultLoadRoomMembersTask @Inject constructor(private val roomAP for (roomMemberEvent in response.roomMemberEvents) { roomEntity.addStateEvent(roomMemberEvent) - UserEntityFactory.create(roomMemberEvent)?.also { + UserEntityFactory.createOrNull(roomMemberEvent)?.also { realm.insertOrUpdate(it) } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TokenChunkEventPersistor.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TokenChunkEventPersistor.kt index e9a34699e9..fb8b62716f 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TokenChunkEventPersistor.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TokenChunkEventPersistor.kt @@ -153,7 +153,7 @@ internal class TokenChunkEventPersistor @Inject constructor(private val monarchy for (event in receivedChunk.events) { event.eventId?.also { eventIds.add(it) } currentChunk.add(roomId, event, direction, isUnlinked = currentChunk.isUnlinked()) - UserEntityFactory.create(event)?.also { + UserEntityFactory.createOrNull(event)?.also { realm.insertOrUpdate(it) } } @@ -174,7 +174,7 @@ internal class TokenChunkEventPersistor @Inject constructor(private val monarchy roomEntity.addOrUpdate(currentChunk) for (stateEvent in receivedChunk.stateEvents) { roomEntity.addStateEvent(stateEvent, isUnlinked = currentChunk.isUnlinked()) - UserEntityFactory.create(stateEvent)?.also { + UserEntityFactory.createOrNull(stateEvent)?.also { realm.insertOrUpdate(it) } } 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 a50e7ca861..215321bd42 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 @@ -26,6 +26,7 @@ import im.vector.matrix.android.api.session.room.model.tag.RoomTagContent import im.vector.matrix.android.internal.crypto.CryptoManager import im.vector.matrix.android.internal.database.helper.* import im.vector.matrix.android.internal.database.model.ChunkEntity +import im.vector.matrix.android.internal.database.model.EventEntityFields import im.vector.matrix.android.internal.database.model.RoomEntity import im.vector.matrix.android.internal.database.model.UserEntity import im.vector.matrix.android.internal.database.query.find @@ -124,34 +125,29 @@ internal class RoomSyncHandler @Inject constructor(private val monarchy: Monarch } roomEntity.membership = Membership.JOIN - val lastChunk = ChunkEntity.findLastLiveChunkFromRoom(realm, roomId) - val isInitialSync = lastChunk == null - val lastStateIndex = lastChunk?.lastStateIndex(PaginationDirection.FORWARDS) ?: 0 - val numberOfStateEvents = roomSync.state?.events?.size ?: 0 - val stateIndexOffset = lastStateIndex + numberOfStateEvents - // State event if (roomSync.state != null && roomSync.state.events.isNotEmpty()) { - val untimelinedStateIndex = if (isInitialSync) Int.MIN_VALUE else stateIndexOffset + val minStateIndex = roomEntity.untimelinedStateEvents.where().min(EventEntityFields.STATE_INDEX)?.toInt() + ?: Int.MIN_VALUE + val untimelinedStateIndex = minStateIndex + 1 roomSync.state.events.forEach { event -> roomEntity.addStateEvent(event, filterDuplicates = true, stateIndex = untimelinedStateIndex) // Give info to crypto module cryptoManager.onStateEvent(roomId, event) - UserEntityFactory.create(event)?.also { + UserEntityFactory.createOrNull(event)?.also { realm.insertOrUpdate(it) } } } if (roomSync.timeline != null && roomSync.timeline.events.isNotEmpty()) { - val timelineStateOffset = if (isInitialSync || roomSync.timeline.limited.not()) 0 else stateIndexOffset val chunkEntity = handleTimelineEvents( realm, roomEntity, roomSync.timeline.events, roomSync.timeline.prevToken, roomSync.timeline.limited, - timelineStateOffset + 0 ) roomEntity.addOrUpdate(chunkEntity) } @@ -227,7 +223,7 @@ internal class RoomSyncHandler @Inject constructor(private val monarchy: Monarch Timber.v("Can't find corresponding local echo for tx:$it") } } - UserEntityFactory.create(event)?.also { + UserEntityFactory.createOrNull(event)?.also { realm.insertOrUpdate(it) } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/UserEntityFactory.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/UserEntityFactory.kt index 45bbca7829..188c7d84bf 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/UserEntityFactory.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/UserEntityFactory.kt @@ -24,7 +24,7 @@ import im.vector.matrix.android.internal.database.model.UserEntity internal object UserEntityFactory { - fun create(event: Event): UserEntity? { + fun createOrNull(event: Event): UserEntity? { if (event.type != EventType.STATE_ROOM_MEMBER) { return null }