2019-07-05 14:07:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 New Vector Ltd
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package im.vector.matrix.android.internal.database.query
|
|
|
|
|
2019-08-07 08:54:54 +00:00
|
|
|
import im.vector.matrix.android.api.session.room.send.SendState
|
2019-08-02 09:35:27 +00:00
|
|
|
import im.vector.matrix.android.internal.database.model.*
|
2019-07-05 14:07:12 +00:00
|
|
|
import im.vector.matrix.android.internal.database.model.EventEntity.LinkFilterMode.*
|
2019-08-07 08:54:54 +00:00
|
|
|
import io.realm.*
|
2019-07-05 14:07:12 +00:00
|
|
|
import io.realm.kotlin.where
|
|
|
|
|
|
|
|
internal fun TimelineEventEntity.Companion.where(realm: Realm, eventId: String): RealmQuery<TimelineEventEntity> {
|
2019-07-09 13:33:31 +00:00
|
|
|
return realm.where<TimelineEventEntity>()
|
|
|
|
.equalTo(TimelineEventEntityFields.EVENT_ID, eventId)
|
2019-07-05 14:07:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal fun TimelineEventEntity.Companion.where(realm: Realm, eventIds: List<String>): RealmQuery<TimelineEventEntity> {
|
2019-07-09 13:33:31 +00:00
|
|
|
return realm.where<TimelineEventEntity>()
|
|
|
|
.`in`(TimelineEventEntityFields.EVENT_ID, eventIds.toTypedArray())
|
2019-07-05 14:07:12 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 15:00:13 +00:00
|
|
|
internal fun TimelineEventEntity.Companion.where(realm: Realm,
|
2019-07-05 17:07:33 +00:00
|
|
|
roomId: String? = null,
|
|
|
|
type: String? = null,
|
|
|
|
linkFilterMode: EventEntity.LinkFilterMode = LINKED_ONLY): RealmQuery<TimelineEventEntity> {
|
2019-07-05 15:00:13 +00:00
|
|
|
val query = realm.where<TimelineEventEntity>()
|
|
|
|
if (roomId != null) {
|
|
|
|
query.equalTo(TimelineEventEntityFields.ROOM_ID, roomId)
|
|
|
|
}
|
|
|
|
if (type != null) {
|
|
|
|
query.equalTo(TimelineEventEntityFields.ROOT.TYPE, type)
|
|
|
|
}
|
|
|
|
return when (linkFilterMode) {
|
|
|
|
LINKED_ONLY -> query.equalTo(TimelineEventEntityFields.ROOT.IS_UNLINKED, false)
|
|
|
|
UNLINKED_ONLY -> query.equalTo(TimelineEventEntityFields.ROOT.IS_UNLINKED, true)
|
|
|
|
BOTH -> query
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 13:32:24 +00:00
|
|
|
internal fun TimelineEventEntity.Companion.findWithSenderMembershipEvent(realm: Realm, senderMembershipEventId: String): List<TimelineEventEntity> {
|
|
|
|
return realm.where<TimelineEventEntity>()
|
|
|
|
.equalTo(TimelineEventEntityFields.SENDER_MEMBERSHIP_EVENT.EVENT_ID, senderMembershipEventId)
|
|
|
|
.findAll()
|
|
|
|
}
|
|
|
|
|
2019-07-05 14:07:12 +00:00
|
|
|
|
|
|
|
internal fun TimelineEventEntity.Companion.latestEvent(realm: Realm,
|
|
|
|
roomId: String,
|
2019-07-22 16:58:55 +00:00
|
|
|
includesSending: Boolean,
|
2019-07-05 14:07:12 +00:00
|
|
|
includedTypes: List<String> = emptyList(),
|
|
|
|
excludedTypes: List<String> = emptyList()): TimelineEventEntity? {
|
|
|
|
|
|
|
|
val roomEntity = RoomEntity.where(realm, roomId).findFirst() ?: return null
|
2019-07-22 16:58:55 +00:00
|
|
|
val eventList = if (includesSending && roomEntity.sendingTimelineEvents.isNotEmpty()) {
|
2019-07-05 14:07:12 +00:00
|
|
|
roomEntity.sendingTimelineEvents
|
|
|
|
} else {
|
|
|
|
ChunkEntity.findLastLiveChunkFromRoom(realm, roomId)?.timelineEvents
|
|
|
|
}
|
|
|
|
val query = eventList?.where()
|
|
|
|
if (includedTypes.isNotEmpty()) {
|
2019-07-05 15:00:13 +00:00
|
|
|
query?.`in`(TimelineEventEntityFields.ROOT.TYPE, includedTypes.toTypedArray())
|
2019-07-05 14:07:12 +00:00
|
|
|
} else if (excludedTypes.isNotEmpty()) {
|
2019-07-05 15:00:13 +00:00
|
|
|
query?.not()?.`in`(TimelineEventEntityFields.ROOT.TYPE, excludedTypes.toTypedArray())
|
2019-07-05 14:07:12 +00:00
|
|
|
}
|
|
|
|
return query
|
|
|
|
?.sort(TimelineEventEntityFields.ROOT.DISPLAY_INDEX, Sort.DESCENDING)
|
|
|
|
?.findFirst()
|
|
|
|
}
|
|
|
|
|
2019-07-05 17:07:33 +00:00
|
|
|
internal fun RealmQuery<TimelineEventEntity>.next(from: Int? = null, strict: Boolean = true): TimelineEventEntity? {
|
|
|
|
if (from != null) {
|
|
|
|
if (strict) {
|
|
|
|
this.greaterThan(TimelineEventEntityFields.ROOT.STATE_INDEX, from)
|
|
|
|
} else {
|
|
|
|
this.greaterThanOrEqualTo(TimelineEventEntityFields.ROOT.STATE_INDEX, from)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this
|
|
|
|
.sort(TimelineEventEntityFields.ROOT.STATE_INDEX, Sort.ASCENDING)
|
|
|
|
.findFirst()
|
|
|
|
}
|
|
|
|
|
|
|
|
internal fun RealmQuery<TimelineEventEntity>.prev(since: Int? = null, strict: Boolean = false): TimelineEventEntity? {
|
|
|
|
if (since != null) {
|
|
|
|
if (strict) {
|
|
|
|
this.lessThan(TimelineEventEntityFields.ROOT.STATE_INDEX, since)
|
|
|
|
} else {
|
|
|
|
this.lessThanOrEqualTo(TimelineEventEntityFields.ROOT.STATE_INDEX, since)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this
|
|
|
|
.sort(TimelineEventEntityFields.ROOT.STATE_INDEX, Sort.DESCENDING)
|
|
|
|
.findFirst()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-05 14:07:12 +00:00
|
|
|
internal fun RealmList<TimelineEventEntity>.find(eventId: String): TimelineEventEntity? {
|
2019-07-09 13:33:31 +00:00
|
|
|
return this.where()
|
|
|
|
.equalTo(TimelineEventEntityFields.ROOT.EVENT_ID, eventId)
|
|
|
|
.findFirst()
|
2019-07-05 14:07:12 +00:00
|
|
|
}
|
2019-08-07 08:54:54 +00:00
|
|
|
|
|
|
|
internal fun TimelineEventEntity.Companion.findAllInRoomWithSendStates(realm: Realm,
|
|
|
|
roomId: String,
|
|
|
|
sendStates: List<SendState>)
|
|
|
|
: RealmResults<TimelineEventEntity> {
|
|
|
|
|
|
|
|
val sendStatesStr = sendStates.map { it.name }.toTypedArray()
|
|
|
|
return realm.where<TimelineEventEntity>()
|
|
|
|
.equalTo(TimelineEventEntityFields.ROOM_ID, roomId)
|
|
|
|
.`in`(TimelineEventEntityFields.ROOT.SEND_STATE_STR,sendStatesStr)
|
|
|
|
.findAll()
|
|
|
|
}
|