Continue reworking sync/timeline events handling

This commit is contained in:
ganfra 2020-01-25 18:59:45 +01:00
parent 1d8b81bb04
commit 3a89a30056
5 changed files with 84 additions and 56 deletions

View File

@ -19,7 +19,13 @@ package im.vector.matrix.android.internal.database.helper
import im.vector.matrix.android.api.session.events.model.Event
import im.vector.matrix.android.api.session.events.model.toModel
import im.vector.matrix.android.api.session.room.model.RoomMemberContent
import im.vector.matrix.android.internal.database.model.*
import im.vector.matrix.android.internal.database.model.ChunkEntity
import im.vector.matrix.android.internal.database.model.EventAnnotationsSummaryEntity
import im.vector.matrix.android.internal.database.model.EventEntity
import im.vector.matrix.android.internal.database.model.ReadReceiptEntity
import im.vector.matrix.android.internal.database.model.ReadReceiptsSummaryEntity
import im.vector.matrix.android.internal.database.model.TimelineEventEntity
import im.vector.matrix.android.internal.database.model.TimelineEventEntityFields
import im.vector.matrix.android.internal.database.query.getOrCreate
import im.vector.matrix.android.internal.database.query.where
import im.vector.matrix.android.internal.extensions.assertIsManaged
@ -37,15 +43,7 @@ internal fun ChunkEntity.addTimelineEvent(roomId: String,
direction: PaginationDirection,
roomMemberEvent: Event?): TimelineEventEntity {
var currentDisplayIndex = lastDisplayIndex(direction, 0)
if (direction == PaginationDirection.FORWARDS) {
currentDisplayIndex += 1
forwardsDisplayIndex = currentDisplayIndex
} else {
currentDisplayIndex -= 1
backwardsDisplayIndex = currentDisplayIndex
}
val displayIndex = nextDisplayIndex(direction)
val localId = TimelineEventEntity.nextId(realm)
val eventId = eventEntity.eventId
val senderId = eventEntity.sender ?: ""
@ -73,12 +71,12 @@ internal fun ChunkEntity.addTimelineEvent(roomId: String,
val timelineEventEntity = TimelineEventEntity().also {
it.localId = localId
it.root = realm.copyToRealm(eventEntity)
it.root = eventEntity
it.eventId = eventId
it.roomId = roomId
it.annotations = EventAnnotationsSummaryEntity.where(realm, eventId).findFirst()
it.readReceipts = readReceiptsSummaryEntity
it.displayIndex = currentDisplayIndex
it.displayIndex = displayIndex
}
if (roomMemberEvent != null) {
val roomMemberContent = roomMemberEvent.content.toModel<RoomMemberContent>()
@ -91,9 +89,13 @@ internal fun ChunkEntity.addTimelineEvent(roomId: String,
return timelineEventEntity
}
internal fun ChunkEntity.lastDisplayIndex(direction: PaginationDirection, defaultValue: Int = 0): Int {
internal fun ChunkEntity.nextDisplayIndex(direction: PaginationDirection): Int {
return when (direction) {
PaginationDirection.FORWARDS -> forwardsDisplayIndex
PaginationDirection.BACKWARDS -> backwardsDisplayIndex
} ?: defaultValue
PaginationDirection.FORWARDS -> {
(timelineEvents.where().max(TimelineEventEntityFields.DISPLAY_INDEX)?.toInt() ?: 0) + 1
}
PaginationDirection.BACKWARDS -> {
(timelineEvents.where().min(TimelineEventEntityFields.DISPLAY_INDEX)?.toInt() ?: 0) - 1
}
}
}

View File

@ -22,34 +22,35 @@ import io.realm.annotations.RealmModule
* Realm module for Session
*/
@RealmModule(library = true,
classes = [
ChunkEntity::class,
EventEntity::class,
TimelineEventEntity::class,
FilterEntity::class,
GroupEntity::class,
GroupSummaryEntity::class,
ReadReceiptEntity::class,
RoomEntity::class,
RoomSummaryEntity::class,
RoomTagEntity::class,
SyncEntity::class,
UserEntity::class,
IgnoredUserEntity::class,
BreadcrumbsEntity::class,
EventAnnotationsSummaryEntity::class,
ReactionAggregatedSummaryEntity::class,
EditAggregatedSummaryEntity::class,
PushRulesEntity::class,
PushRuleEntity::class,
PushConditionEntity::class,
PusherEntity::class,
PusherDataEntity::class,
ReadReceiptsSummaryEntity::class,
ReadMarkerEntity::class,
UserDraftsEntity::class,
DraftEntity::class,
HomeServerCapabilitiesEntity::class,
RoomMemberSummaryEntity::class
])
classes = [
ChunkEntity::class,
EventEntity::class,
TimelineEventEntity::class,
FilterEntity::class,
GroupEntity::class,
GroupSummaryEntity::class,
ReadReceiptEntity::class,
RoomEntity::class,
RoomSummaryEntity::class,
RoomTagEntity::class,
SyncEntity::class,
UserEntity::class,
IgnoredUserEntity::class,
BreadcrumbsEntity::class,
EventAnnotationsSummaryEntity::class,
ReactionAggregatedSummaryEntity::class,
EditAggregatedSummaryEntity::class,
PushRulesEntity::class,
PushRuleEntity::class,
PushConditionEntity::class,
PusherEntity::class,
PusherDataEntity::class,
ReadReceiptsSummaryEntity::class,
ReadMarkerEntity::class,
UserDraftsEntity::class,
DraftEntity::class,
HomeServerCapabilitiesEntity::class,
RoomMemberSummaryEntity::class,
CurrentStateEventEntity::class
])
internal class SessionRealmModule

View File

@ -73,7 +73,9 @@ internal class DefaultLoadRoomMembersTask @Inject constructor(
if (roomMemberEvent.eventId == null || roomMemberEvent.stateKey == null) {
continue
}
val eventEntity = roomMemberEvent.toEntity(roomId, SendState.SYNCED)
val eventEntity = roomMemberEvent.toEntity(roomId, SendState.SYNCED).let {
realm.copyToRealmOrUpdate(it)
}
CurrentStateEventEntity.getOrCreate(realm, roomId, roomMemberEvent.stateKey, roomMemberEvent.type).apply {
eventId = roomMemberEvent.eventId
root = eventEntity

View File

@ -20,11 +20,13 @@ import com.zhuinden.monarchy.Monarchy
import im.vector.matrix.android.api.session.events.model.Event
import im.vector.matrix.android.api.session.events.model.EventType
import im.vector.matrix.android.api.session.room.send.SendState
import im.vector.matrix.android.internal.database.helper.*
import im.vector.matrix.android.internal.database.helper.addOrUpdate
import im.vector.matrix.android.internal.database.helper.addTimelineEvent
import im.vector.matrix.android.internal.database.mapper.asDomain
import im.vector.matrix.android.internal.database.mapper.toEntity
import im.vector.matrix.android.internal.database.model.ChunkEntity
import im.vector.matrix.android.internal.database.model.CurrentStateEventEntity
import im.vector.matrix.android.internal.database.model.EventEntity
import im.vector.matrix.android.internal.database.model.RoomEntity
import im.vector.matrix.android.internal.database.query.create
import im.vector.matrix.android.internal.database.query.find
@ -152,18 +154,35 @@ internal class TokenChunkEventPersistor @Inject constructor(private val monarchy
} else {
receivedChunk.events.asReversed()
}
val stateEvents = receivedChunk.stateEvents
for (stateEvent in stateEvents) {
if (stateEvent.type == EventType.STATE_ROOM_MEMBER && stateEvent.stateKey != null && !stateEvent.isRedacted()) {
roomMemberEventsByUser[stateEvent.stateKey] = stateEvent
}
}
val eventEntities = ArrayList<EventEntity>(eventList.size)
for (event in eventList) {
if (event.eventId == null || event.senderId == null) {
continue
}
val eventEntity = event.toEntity(roomId, SendState.SYNCED)
if (event.type == EventType.STATE_ROOM_MEMBER && event.stateKey != null) {
val eventEntity = event.toEntity(roomId, SendState.SYNCED).also {
realm.copyToRealmOrUpdate(it)
}
if(direction == PaginationDirection.FORWARDS){
eventEntities.add(eventEntity)
}else {
eventEntities.add(0, eventEntity)
}
if (event.type == EventType.STATE_ROOM_MEMBER && event.stateKey != null && !event.isRedacted()) {
roomMemberEventsByUser[event.stateKey] = event
}
val roomMemberEvent = roomMemberEventsByUser.getOrPut(event.senderId) {
CurrentStateEventEntity.getOrNull(realm, roomId, event.senderId, EventType.STATE_ROOM_MEMBER)?.root?.asDomain()
}
for (eventEntity in eventEntities) {
val senderId = eventEntity.sender ?: continue
val roomMemberEvent = roomMemberEventsByUser.getOrPut(senderId) {
CurrentStateEventEntity.getOrNull(realm, roomId, senderId, EventType.STATE_ROOM_MEMBER)?.root?.asDomain()
}
currentChunk.addTimelineEvent(roomId, eventEntity, PaginationDirection.FORWARDS, roomMemberEvent)
currentChunk.addTimelineEvent(roomId, eventEntity, direction, roomMemberEvent)
}
roomEntity.addOrUpdate(currentChunk)
}

View File

@ -126,7 +126,9 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle
if (event.eventId == null || event.stateKey == null) {
continue
}
val eventEntity = event.toEntity(roomId, SendState.SYNCED)
val eventEntity = event.toEntity(roomId, SendState.SYNCED).let {
realm.copyToRealmOrUpdate(it)
}
CurrentStateEventEntity.getOrCreate(realm, roomId, event.stateKey, event.type).apply {
eventId = event.eventId
root = eventEntity
@ -215,8 +217,10 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle
continue
}
eventIds.add(event.eventId)
val eventEntity = event.toEntity(roomId, SendState.SYNCED)
if (event.isStateEvent() && event.stateKey != null) {
val eventEntity = event.toEntity(roomId, SendState.SYNCED).let {
realm.copyToRealmOrUpdate(it)
}
if (event.isStateEvent() && event.stateKey != null && !event.isRedacted()) {
CurrentStateEventEntity.getOrCreate(realm, roomId, event.stateKey, event.type).apply {
eventId = event.eventId
root = eventEntity