From 4226296990ab2fb939dff77779ab543140cf45e2 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 7 Feb 2023 14:52:12 +0100 Subject: [PATCH] Get Event from cache without using a `Room` --- .../sdk/api/session/events/EventService.kt | 8 ++++++++ .../database/query/EventEntityQueries.kt | 6 ++++++ .../session/events/DefaultEventService.kt | 20 ++++++++++++++++++- .../timeline/factory/TimelineItemFactory.kt | 6 ++---- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/events/EventService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/events/EventService.kt index 7f275bf952..11ef3f0d2f 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/events/EventService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/events/EventService.kt @@ -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? } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/query/EventEntityQueries.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/query/EventEntityQueries.kt index 4805c36f8c..75232f01f1 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/query/EventEntityQueries.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/query/EventEntityQueries.kt @@ -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 { + return realm.where() + .equalTo(EventEntityFields.ROOM_ID, roomId) + .equalTo(EventEntityFields.EVENT_ID, eventId) +} + internal fun EventEntity.Companion.whereRoomId(realm: Realm, roomId: String): RealmQuery { return realm.where() .equalTo(EventEntityFields.ROOM_ID, roomId) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/events/DefaultEventService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/events/DefaultEventService.kt index 51d305f441..50ea588dff 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/events/DefaultEventService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/events/DefaultEventService.kt @@ -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() + + } + } } diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/TimelineItemFactory.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/TimelineItemFactory.kt index d44713f404..84b71ceedf 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/TimelineItemFactory.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/TimelineItemFactory.kt @@ -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)