RoomMembers: should fix state events issues

This commit is contained in:
ganfra 2019-07-15 20:26:18 +02:00
parent 9182f2ce4e
commit d469299f42
4 changed files with 11 additions and 15 deletions

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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
}