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,
eventId: String
): 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)
}
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> {
return realm.where<EventEntity>()
.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.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.room.timeline.GetEventTask
import javax.inject.Inject
internal class DefaultEventService @Inject constructor(
private val getEventTask: GetEventTask,
private val callEventProcessor: CallEventProcessor
private val callEventProcessor: CallEventProcessor,
private val realmSessionProvider: RealmSessionProvider,
) : EventService {
override suspend fun getEvent(roomId: String, eventId: String): Event {
@ -36,4 +41,17 @@ internal class DefaultEventService @Inject constructor(
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.VectorEpoxyModel
import im.vector.app.core.extensions.isVoiceBroadcast
import im.vector.app.features.analytics.DecryptionFailureTracker
import im.vector.app.features.home.room.detail.timeline.helper.TimelineEventVisibilityHelper
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.events.model.EventType
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.getRelationContent
import timber.log.Timber
@ -143,7 +141,7 @@ class TimelineItemFactory @Inject constructor(
event.root.isRedacted() -> messageItemFactory.create(params)
relationContent?.type == RelationType.REFERENCE -> {
// 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
}
else -> encryptedItemFactory.create(params)