diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/helper/RoomEntityHelper.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/helper/RoomEntityHelper.kt index 7cb6124c0b..1458b6090f 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/helper/RoomEntityHelper.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/helper/RoomEntityHelper.kt @@ -54,12 +54,10 @@ internal fun RoomEntity.addStateEvents(stateEvents: List, } } -internal fun RoomEntity.addSendingEvent(event: Event, - stateIndex: Int) { +internal fun RoomEntity.addSendingEvent(event: Event) { assertIsManaged() val eventEntity = event.toEntity(roomId).apply { this.sendState = SendState.UNSENT - this.stateIndex = stateIndex } sendingTimelineEvents.add(0, eventEntity) } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/SenderRoomMemberExtractor.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/SenderRoomMemberExtractor.kt index 209e5f3831..f7a0a9a68a 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/SenderRoomMemberExtractor.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/SenderRoomMemberExtractor.kt @@ -19,6 +19,7 @@ package im.vector.matrix.android.internal.session.room.membership import im.vector.matrix.android.api.session.events.model.EventType import im.vector.matrix.android.api.session.events.model.toModel import im.vector.matrix.android.api.session.room.model.RoomMember +import im.vector.matrix.android.api.session.room.send.SendState import im.vector.matrix.android.internal.database.mapper.ContentMapper import im.vector.matrix.android.internal.database.model.ChunkEntity import im.vector.matrix.android.internal.database.model.EventEntity @@ -31,7 +32,6 @@ import im.vector.matrix.android.internal.database.query.where import io.realm.Realm import io.realm.RealmList import io.realm.RealmQuery -import timber.log.Timber import javax.inject.Inject internal class SenderRoomMemberExtractor @Inject constructor(private val roomId: String) { @@ -41,16 +41,21 @@ internal class SenderRoomMemberExtractor @Inject constructor(private val roomId: // If the event is unlinked we want to fetch unlinked state events val unlinked = event.isUnlinked val roomEntity = RoomEntity.where(realm, roomId = roomId).findFirst() ?: return null - val chunkEntity = ChunkEntity.findIncludingEvent(realm, event.eventId) - val content = when { - chunkEntity == null -> null - event.stateIndex <= 0 -> baseQuery(chunkEntity.events, sender, unlinked).next(from = event.stateIndex)?.prevContent - else -> baseQuery(chunkEntity.events, sender, unlinked).prev(since = event.stateIndex)?.content - } - val fallbackContent = content - ?: baseQuery(roomEntity.untimelinedStateEvents, sender, unlinked).prev(since = event.stateIndex)?.content - return ContentMapper.map(fallbackContent).toModel() + // When not synced, we should grab the live RoomMember event + return if (event.sendState != SendState.SYNCED) { + RoomMembers(realm, roomId).get(sender) + } else { + val chunkEntity = ChunkEntity.findIncludingEvent(realm, event.eventId) + val content = when { + chunkEntity == null -> null + event.stateIndex <= 0 -> baseQuery(chunkEntity.events, sender, unlinked).next(from = event.stateIndex)?.prevContent + else -> baseQuery(chunkEntity.events, sender, unlinked).prev(since = event.stateIndex)?.content + } + val fallbackContent = content + ?: baseQuery(roomEntity.untimelinedStateEvents, sender, unlinked).prev(since = event.stateIndex)?.content + ContentMapper.map(fallbackContent).toModel() + } } private fun baseQuery(list: RealmList, diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/DefaultRelationService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/DefaultRelationService.kt index 42f7f0ca66..8d93c8b05a 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/DefaultRelationService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/DefaultRelationService.kt @@ -176,10 +176,7 @@ internal class DefaultRelationService @Inject constructor(private val context: C monarchy.tryTransactionAsync { realm -> val roomEntity = RoomEntity.where(realm, roomId = roomId).findFirst() ?: return@tryTransactionAsync - val liveChunk = ChunkEntity.findLastLiveChunkFromRoom(realm, roomId = roomId) - ?: return@tryTransactionAsync - - roomEntity.addSendingEvent(event, liveChunk.forwardsStateIndex ?: 0) + roomEntity.addSendingEvent(event) } } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoEventFactory.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoEventFactory.kt index dbdfe124be..1160cc5ee2 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoEventFactory.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoEventFactory.kt @@ -33,7 +33,6 @@ import im.vector.matrix.android.internal.database.model.ChunkEntity import im.vector.matrix.android.internal.database.model.RoomEntity import im.vector.matrix.android.internal.database.query.findLastLiveChunkFromRoom import im.vector.matrix.android.internal.database.query.where -import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.content.ThumbnailExtractor import im.vector.matrix.android.internal.util.StringProvider import im.vector.matrix.android.internal.util.tryTransactionAsync @@ -346,10 +345,7 @@ internal class LocalEchoEventFactory @Inject constructor(private val credentials monarchy.tryTransactionAsync { realm -> val roomEntity = RoomEntity.where(realm, roomId = event.roomId!!).findFirst() ?: return@tryTransactionAsync - val liveChunk = ChunkEntity.findLastLiveChunkFromRoom(realm, roomId = event.roomId) - ?: return@tryTransactionAsync - - roomEntity.addSendingEvent(event, liveChunk.forwardsStateIndex ?: 0) + roomEntity.addSendingEvent(event) } }