diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/TimelineEventEntityHelper.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/TimelineEventEntityHelper.kt index ea508731b1..8a5d08cd30 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/TimelineEventEntityHelper.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/helper/TimelineEventEntityHelper.kt @@ -19,9 +19,10 @@ package org.matrix.android.sdk.internal.database.helper import io.realm.Realm import org.matrix.android.sdk.internal.database.model.TimelineEventEntity import org.matrix.android.sdk.internal.database.model.TimelineEventEntityFields +import org.matrix.android.sdk.internal.database.query.where internal fun TimelineEventEntity.Companion.nextId(realm: Realm): Long { - val currentIdNum = realm.where(TimelineEventEntity::class.java).max(TimelineEventEntityFields.LOCAL_ID) + val currentIdNum = TimelineEventEntity.where(realm).max(TimelineEventEntityFields.LOCAL_ID) return if (currentIdNum == null) { 1 } else { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/query/TimelineEventEntityQueries.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/query/TimelineEventEntityQueries.kt index 81d5ac835f..215ab34f95 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/query/TimelineEventEntityQueries.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/query/TimelineEventEntityQueries.kt @@ -29,26 +29,35 @@ import org.matrix.android.sdk.internal.database.model.RoomEntity import org.matrix.android.sdk.internal.database.model.TimelineEventEntity import org.matrix.android.sdk.internal.database.model.TimelineEventEntityFields -internal fun TimelineEventEntity.Companion.where(realm: Realm, roomId: String, eventId: String): RealmQuery { - return realm.where() +internal fun TimelineEventEntity.Companion.where(realm: Realm): RealmQuery { + return realm.where() +} + +internal fun TimelineEventEntity.Companion.where(realm: Realm, + roomId: String, + eventId: String): RealmQuery { + return where(realm) .equalTo(TimelineEventEntityFields.ROOM_ID, roomId) .equalTo(TimelineEventEntityFields.EVENT_ID, eventId) } -internal fun TimelineEventEntity.Companion.where(realm: Realm, roomId: String, eventIds: List): RealmQuery { - return realm.where() +internal fun TimelineEventEntity.Companion.where(realm: Realm, + roomId: String, + eventIds: List): RealmQuery { + return where(realm) .equalTo(TimelineEventEntityFields.ROOM_ID, roomId) .`in`(TimelineEventEntityFields.EVENT_ID, eventIds.toTypedArray()) } internal fun TimelineEventEntity.Companion.whereRoomId(realm: Realm, roomId: String): RealmQuery { - return realm.where() + return where(realm) .equalTo(TimelineEventEntityFields.ROOM_ID, roomId) } -internal fun TimelineEventEntity.Companion.findWithSenderMembershipEvent(realm: Realm, senderMembershipEventId: String): List { - return realm.where() +internal fun TimelineEventEntity.Companion.findWithSenderMembershipEvent(realm: Realm, + senderMembershipEventId: String): List { + return where(realm) .equalTo(TimelineEventEntityFields.SENDER_MEMBERSHIP_EVENT_ID, senderMembershipEventId) .findAll() } @@ -110,12 +119,12 @@ internal fun RealmQuery.filterTypes(filterTypes: List.find(eventId: String): TimelineEventEntity? { - return this.where() + return where() .equalTo(TimelineEventEntityFields.EVENT_ID, eventId) .findFirst() } @@ -132,3 +141,14 @@ internal fun RealmQuery.filterSendStates(sendStates: List): RealmResults { + return where(realm) + .`in`(TimelineEventEntityFields.ROOT.SENDER, senderIds.toTypedArray()) + .isNull(TimelineEventEntityFields.ROOT.STATE_KEY) + .findAll() +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TimelineEventDataSource.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TimelineEventDataSource.kt index 638866a46e..8b58d3ca5c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TimelineEventDataSource.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TimelineEventDataSource.kt @@ -19,7 +19,6 @@ package org.matrix.android.sdk.internal.session.room.timeline import androidx.lifecycle.LiveData import com.zhuinden.monarchy.Monarchy import io.realm.Sort -import io.realm.kotlin.where import org.matrix.android.sdk.api.session.events.model.isImageMessage import org.matrix.android.sdk.api.session.events.model.isVideoMessage import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent @@ -29,6 +28,7 @@ import org.matrix.android.sdk.internal.database.mapper.TimelineEventMapper import org.matrix.android.sdk.internal.database.model.TimelineEventEntity import org.matrix.android.sdk.internal.database.model.TimelineEventEntityFields import org.matrix.android.sdk.internal.database.query.where +import org.matrix.android.sdk.internal.database.query.whereRoomId import org.matrix.android.sdk.internal.di.SessionDatabase import org.matrix.android.sdk.internal.task.TaskExecutor import javax.inject.Inject @@ -53,8 +53,7 @@ internal class TimelineEventDataSource @Inject constructor(private val realmSess fun getAttachmentMessages(roomId: String): List { // TODO pretty bad query.. maybe we should denormalize clear type in base? return realmSessionProvider.withRealm { realm -> - realm.where() - .equalTo(TimelineEventEntityFields.ROOM_ID, roomId) + TimelineEventEntity.whereRoomId(realm, roomId) .sort(TimelineEventEntityFields.DISPLAY_INDEX, Sort.ASCENDING) .findAll() ?.mapNotNull { timelineEventMapper.map(it).takeIf { it.root.isImageMessage() || it.root.isVideoMessage() } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/handler/UserAccountDataSyncHandler.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/handler/UserAccountDataSyncHandler.kt index ce827c2311..fa9ba371f7 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/handler/UserAccountDataSyncHandler.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/handler/UserAccountDataSyncHandler.kt @@ -43,10 +43,10 @@ import org.matrix.android.sdk.internal.database.model.PushRulesEntity import org.matrix.android.sdk.internal.database.model.RoomSummaryEntity import org.matrix.android.sdk.internal.database.model.RoomSummaryEntityFields import org.matrix.android.sdk.internal.database.model.TimelineEventEntity -import org.matrix.android.sdk.internal.database.model.TimelineEventEntityFields import org.matrix.android.sdk.internal.database.model.UserAccountDataEntity import org.matrix.android.sdk.internal.database.model.UserAccountDataEntityFields import org.matrix.android.sdk.internal.database.model.deleteOnCascade +import org.matrix.android.sdk.internal.database.query.findAllFrom import org.matrix.android.sdk.internal.database.query.getDirectRooms import org.matrix.android.sdk.internal.database.query.getOrCreate import org.matrix.android.sdk.internal.database.query.where @@ -206,10 +206,7 @@ internal class UserAccountDataSyncHandler @Inject constructor( // See https://spec.matrix.org/latest/client-server-api/#client-behaviour-22 : // "Once ignored, the client will no longer receive events sent by that user, with the exception of state events" // So just delete all non-state events from our local storage. - realm.where(TimelineEventEntity::class.java) - .`in`(TimelineEventEntityFields.ROOT.SENDER, userIds.toTypedArray()) - .isNull(TimelineEventEntityFields.ROOT.STATE_KEY) - .findAll() + TimelineEventEntity.findAllFrom(realm, userIds) .also { Timber.d("Deleting ${it.size} TimelineEventEntity from ignored users") } .forEach { it.deleteOnCascade(true)