From 9aa0d381162cf9b6d780e2f39b7302ac0e3abf9e Mon Sep 17 00:00:00 2001 From: Valere Date: Tue, 23 Jun 2020 09:25:25 +0200 Subject: [PATCH 1/2] Pagination do not set ageLocalTs --- .../session/room/timeline/TokenChunkEventPersistor.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 1756c948b0..6d26aed601 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 @@ -207,13 +207,15 @@ internal class TokenChunkEventPersistor @Inject constructor(@SessionDatabase pri roomMemberContentsByUser[stateEvent.stateKey] = stateEvent.content.toModel() } } + val now = System.currentTimeMillis() val eventIds = ArrayList(eventList.size) for (event in eventList) { if (event.eventId == null || event.senderId == null) { continue } + val ageLocalTs = event.unsignedData?.age?.let { now - it } eventIds.add(event.eventId) - val eventEntity = event.toEntity(roomId, SendState.SYNCED).copyToRealmOrIgnore(realm) + val eventEntity = event.toEntity(roomId, SendState.SYNCED, ageLocalTs).copyToRealmOrIgnore(realm) if (event.type == EventType.STATE_ROOM_MEMBER && event.stateKey != null) { val contentToUse = if (direction == PaginationDirection.BACKWARDS) { event.prevContent From a04a0e71753f4bb5ff74f7fee6b3d7f0e8bfc20f Mon Sep 17 00:00:00 2001 From: Valere Date: Fri, 26 Jun 2020 09:19:31 +0200 Subject: [PATCH 2/2] Code review --- .../internal/database/mapper/EventMapper.kt | 2 +- .../room/membership/LoadRoomMembersTask.kt | 5 ++-- .../session/room/send/LocalEchoRepository.kt | 2 +- .../room/timeline/TokenChunkEventPersistor.kt | 6 ++-- .../internal/session/sync/RoomSyncHandler.kt | 30 +++++++++++-------- 5 files changed, 27 insertions(+), 18 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/EventMapper.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/EventMapper.kt index e28de760fd..cd646c8703 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/EventMapper.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/EventMapper.kt @@ -93,7 +93,7 @@ internal fun EventEntity.asDomain(): Event { return EventMapper.map(this) } -internal fun Event.toEntity(roomId: String, sendState: SendState, ageLocalTs: Long? = null): EventEntity { +internal fun Event.toEntity(roomId: String, sendState: SendState, ageLocalTs: Long?): EventEntity { return EventMapper.map(this, roomId).apply { this.sendState = sendState this.ageLocalTs = ageLocalTs 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 fa4a17ba34..ce4b31b89a 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 @@ -70,12 +70,13 @@ internal class DefaultLoadRoomMembersTask @Inject constructor( // We ignore all the already known members val roomEntity = RoomEntity.where(realm, roomId).findFirst() ?: realm.createObject(roomId) - + val now = System.currentTimeMillis() for (roomMemberEvent in response.roomMemberEvents) { if (roomMemberEvent.eventId == null || roomMemberEvent.stateKey == null) { continue } - val eventEntity = roomMemberEvent.toEntity(roomId, SendState.SYNCED).copyToRealmOrIgnore(realm) + val ageLocalTs = roomMemberEvent.unsignedData?.age?.let { now - it } + val eventEntity = roomMemberEvent.toEntity(roomId, SendState.SYNCED, ageLocalTs).copyToRealmOrIgnore(realm) CurrentStateEventEntity.getOrCreate(realm, roomId, roomMemberEvent.stateKey, roomMemberEvent.type).apply { eventId = roomMemberEvent.eventId root = eventEntity diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoRepository.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoRepository.kt index fbfa95b4a7..fb47c16943 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoRepository.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoRepository.kt @@ -55,7 +55,7 @@ internal class LocalEchoRepository @Inject constructor(@SessionDatabase private throw IllegalStateException("You should have set an eventId for your event") } val timelineEventEntity = Realm.getInstance(monarchy.realmConfiguration).use { realm -> - val eventEntity = event.toEntity(roomId, SendState.UNSENT) + val eventEntity = event.toEntity(roomId, SendState.UNSENT, System.currentTimeMillis()) val roomMemberHelper = RoomMemberHelper(realm, roomId) val myUser = roomMemberHelper.getLastRoomMember(senderId) val localId = TimelineEventEntity.nextId(realm) 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 6d26aed601..609ebe16fc 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 @@ -200,14 +200,16 @@ internal class TokenChunkEventPersistor @Inject constructor(@SessionDatabase pri val eventList = receivedChunk.events val stateEvents = receivedChunk.stateEvents + val now = System.currentTimeMillis() + for (stateEvent in stateEvents) { - val stateEventEntity = stateEvent.toEntity(roomId, SendState.SYNCED).copyToRealmOrIgnore(realm) + val ageLocalTs = stateEvent.unsignedData?.age?.let { now - it } + val stateEventEntity = stateEvent.toEntity(roomId, SendState.SYNCED, ageLocalTs).copyToRealmOrIgnore(realm) currentChunk.addStateEvent(roomId, stateEventEntity, direction) if (stateEvent.type == EventType.STATE_ROOM_MEMBER && stateEvent.stateKey != null) { roomMemberContentsByUser[stateEvent.stateKey] = stateEvent.content.toModel() } } - val now = System.currentTimeMillis() val eventIds = ArrayList(eventList.size) for (event in eventList) { if (event.eventId == null || event.senderId == null) { 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 b2fdf6045d..e778e3455e 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 @@ -102,12 +102,12 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle } is HandlingStrategy.INVITED -> handlingStrategy.data.mapWithProgress(reporter, R.string.initial_sync_start_importing_account_invited_rooms, 0.1f) { - handleInvitedRoom(realm, it.key, it.value) + handleInvitedRoom(realm, it.key, it.value, syncLocalTimeStampMillis) } is HandlingStrategy.LEFT -> { handlingStrategy.data.mapWithProgress(reporter, R.string.initial_sync_start_importing_account_left_rooms, 0.3f) { - handleLeftRoom(realm, it.key, it.value) + handleLeftRoom(realm, it.key, it.value, syncLocalTimeStampMillis) } } } @@ -143,7 +143,8 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle if (event.eventId == null || event.stateKey == null) { continue } - val eventEntity = event.toEntity(roomId, SendState.SYNCED).copyToRealmOrIgnore(realm) + val ageLocalTs = event.unsignedData?.age?.let { syncLocalTimestampMillis - it } + val eventEntity = event.toEntity(roomId, SendState.SYNCED, ageLocalTs).copyToRealmOrIgnore(realm) CurrentStateEventEntity.getOrCreate(realm, roomId, event.stateKey, event.type).apply { eventId = event.eventId root = eventEntity @@ -185,21 +186,23 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle private fun handleInvitedRoom(realm: Realm, roomId: String, - roomSync: InvitedRoomSync): RoomEntity { + roomSync: InvitedRoomSync, + syncLocalTimestampMillis: Long): RoomEntity { Timber.v("Handle invited sync for room $roomId") val roomEntity = RoomEntity.where(realm, roomId).findFirst() ?: realm.createObject(roomId) roomEntity.membership = Membership.INVITE if (roomSync.inviteState != null && roomSync.inviteState.events.isNotEmpty()) { - roomSync.inviteState.events.forEach { - if (it.stateKey == null) { + roomSync.inviteState.events.forEach { event -> + if (event.stateKey == null) { return@forEach } - val eventEntity = it.toEntity(roomId, SendState.SYNCED).copyToRealmOrIgnore(realm) - CurrentStateEventEntity.getOrCreate(realm, roomId, it.stateKey, it.type).apply { + val ageLocalTs = event.unsignedData?.age?.let { syncLocalTimestampMillis - it } + val eventEntity = event.toEntity(roomId, SendState.SYNCED, ageLocalTs).copyToRealmOrIgnore(realm) + CurrentStateEventEntity.getOrCreate(realm, roomId, event.stateKey, event.type).apply { eventId = eventEntity.eventId root = eventEntity } - roomMemberEventHandler.handle(realm, roomId, it) + roomMemberEventHandler.handle(realm, roomId, event) } } val inviterEvent = roomSync.inviteState?.events?.lastOrNull { @@ -211,13 +214,15 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle private fun handleLeftRoom(realm: Realm, roomId: String, - roomSync: RoomSync): RoomEntity { + roomSync: RoomSync, + syncLocalTimestampMillis: Long): RoomEntity { val roomEntity = RoomEntity.where(realm, roomId).findFirst() ?: realm.createObject(roomId) for (event in roomSync.state?.events.orEmpty()) { if (event.eventId == null || event.stateKey == null) { continue } - val eventEntity = event.toEntity(roomId, SendState.SYNCED).copyToRealmOrIgnore(realm) + val ageLocalTs = event.unsignedData?.age?.let { syncLocalTimestampMillis - it } + val eventEntity = event.toEntity(roomId, SendState.SYNCED, ageLocalTs).copyToRealmOrIgnore(realm) CurrentStateEventEntity.getOrCreate(realm, roomId, event.stateKey, event.type).apply { eventId = event.eventId root = eventEntity @@ -228,7 +233,8 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle if (event.eventId == null || event.senderId == null) { continue } - val eventEntity = event.toEntity(roomId, SendState.SYNCED).copyToRealmOrIgnore(realm) + val ageLocalTs = event.unsignedData?.age?.let { syncLocalTimestampMillis - it } + val eventEntity = event.toEntity(roomId, SendState.SYNCED, ageLocalTs).copyToRealmOrIgnore(realm) if (event.stateKey != null) { CurrentStateEventEntity.getOrCreate(realm, roomId, event.stateKey, event.type).apply { eventId = event.eventId