Get Event from cache without using a `Room`

This commit is contained in:
Benoit Marty 2023-02-07 14:52:12 +01:00
parent e7f3cf6d57
commit 4226296990
4 changed files with 35 additions and 5 deletions

View File

@ -28,4 +28,12 @@ interface EventService {
roomId: String, roomId: String,
eventId: String eventId: String
): Event ): Event
/**
* Get an Event from cache. Return null if not found.
*/
fun getEventFromCache(
roomId: String,
eventId: String
): Event?
} }

View File

@ -47,6 +47,12 @@ internal fun EventEntity.Companion.where(realm: Realm, eventId: String): RealmQu
.equalTo(EventEntityFields.EVENT_ID, eventId) .equalTo(EventEntityFields.EVENT_ID, eventId)
} }
internal fun EventEntity.Companion.where(realm: Realm, roomId: String, eventId: String): RealmQuery<EventEntity> {
return realm.where<EventEntity>()
.equalTo(EventEntityFields.ROOM_ID, roomId)
.equalTo(EventEntityFields.EVENT_ID, eventId)
}
internal fun EventEntity.Companion.whereRoomId(realm: Realm, roomId: String): RealmQuery<EventEntity> { internal fun EventEntity.Companion.whereRoomId(realm: Realm, roomId: String): RealmQuery<EventEntity> {
return realm.where<EventEntity>() return realm.where<EventEntity>()
.equalTo(EventEntityFields.ROOM_ID, roomId) .equalTo(EventEntityFields.ROOM_ID, roomId)

View File

@ -18,13 +18,18 @@ package org.matrix.android.sdk.internal.session.events
import org.matrix.android.sdk.api.session.events.EventService import org.matrix.android.sdk.api.session.events.EventService
import org.matrix.android.sdk.api.session.events.model.Event import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.internal.database.RealmSessionProvider
import org.matrix.android.sdk.internal.database.mapper.asDomain
import org.matrix.android.sdk.internal.database.model.EventEntity
import org.matrix.android.sdk.internal.database.query.where
import org.matrix.android.sdk.internal.session.call.CallEventProcessor import org.matrix.android.sdk.internal.session.call.CallEventProcessor
import org.matrix.android.sdk.internal.session.room.timeline.GetEventTask import org.matrix.android.sdk.internal.session.room.timeline.GetEventTask
import javax.inject.Inject import javax.inject.Inject
internal class DefaultEventService @Inject constructor( internal class DefaultEventService @Inject constructor(
private val getEventTask: GetEventTask, private val getEventTask: GetEventTask,
private val callEventProcessor: CallEventProcessor private val callEventProcessor: CallEventProcessor,
private val realmSessionProvider: RealmSessionProvider,
) : EventService { ) : EventService {
override suspend fun getEvent(roomId: String, eventId: String): Event { override suspend fun getEvent(roomId: String, eventId: String): Event {
@ -36,4 +41,17 @@ internal class DefaultEventService @Inject constructor(
return event return event
} }
override fun getEventFromCache(roomId: String, eventId: String): Event? {
return realmSessionProvider.withRealm { realm ->
EventEntity.where(
realm = realm,
roomId = roomId,
eventId = eventId
)
.findFirst()
?.asDomain()
}
}
} }

View File

@ -19,15 +19,13 @@ package im.vector.app.features.home.room.detail.timeline.factory
import im.vector.app.core.epoxy.TimelineEmptyItem import im.vector.app.core.epoxy.TimelineEmptyItem
import im.vector.app.core.epoxy.TimelineEmptyItem_ import im.vector.app.core.epoxy.TimelineEmptyItem_
import im.vector.app.core.epoxy.VectorEpoxyModel import im.vector.app.core.epoxy.VectorEpoxyModel
import im.vector.app.core.extensions.isVoiceBroadcast
import im.vector.app.features.analytics.DecryptionFailureTracker import im.vector.app.features.analytics.DecryptionFailureTracker
import im.vector.app.features.home.room.detail.timeline.helper.TimelineEventVisibilityHelper import im.vector.app.features.home.room.detail.timeline.helper.TimelineEventVisibilityHelper
import im.vector.app.features.voicebroadcast.VoiceBroadcastConstants import im.vector.app.features.voicebroadcast.VoiceBroadcastConstants
import im.vector.app.features.voicebroadcast.model.isVoiceBroadcast
import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.events.model.EventType import org.matrix.android.sdk.api.session.events.model.EventType
import org.matrix.android.sdk.api.session.events.model.RelationType import org.matrix.android.sdk.api.session.events.model.RelationType
import org.matrix.android.sdk.api.session.getRoom
import org.matrix.android.sdk.api.session.room.getTimelineEvent
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
import org.matrix.android.sdk.api.session.room.timeline.getRelationContent import org.matrix.android.sdk.api.session.room.timeline.getRelationContent
import timber.log.Timber import timber.log.Timber
@ -143,7 +141,7 @@ class TimelineItemFactory @Inject constructor(
event.root.isRedacted() -> messageItemFactory.create(params) event.root.isRedacted() -> messageItemFactory.create(params)
relationContent?.type == RelationType.REFERENCE -> { relationContent?.type == RelationType.REFERENCE -> {
// Hide the decryption error for VoiceBroadcast chunks // Hide the decryption error for VoiceBroadcast chunks
val relatedEvent = relationContent.eventId?.let { session.getRoom(event.roomId)?.getTimelineEvent(it) } val relatedEvent = relationContent.eventId?.let { session.eventService().getEventFromCache(event.roomId, it) }
if (relatedEvent?.isVoiceBroadcast() != true) encryptedItemFactory.create(params) else null if (relatedEvent?.isVoiceBroadcast() != true) encryptedItemFactory.create(params) else null
} }
else -> encryptedItemFactory.create(params) else -> encryptedItemFactory.create(params)