mirror of
https://github.com/element-hq/element-android
synced 2024-11-23 01:45:36 +03:00
Clean a bit code and reorganize files
This commit is contained in:
parent
e942b54b56
commit
d0a241bd2d
18 changed files with 67 additions and 43 deletions
Binary file not shown.
|
@ -70,6 +70,7 @@ dependencies {
|
|||
|
||||
// Database
|
||||
implementation 'com.github.Zhuinden:realm-monarchy:0.5.1'
|
||||
kapt 'dk.ilios:realmfieldnameshelper:1.1.1'
|
||||
|
||||
// Paging
|
||||
implementation "android.arch.paging:runtime:1.0.1"
|
||||
|
|
|
@ -8,4 +8,6 @@ open class ReadReceiptEntity(@PrimaryKey var primaryKey: String = "",
|
|||
var eventId: String = "",
|
||||
var roomId: String = "",
|
||||
var originServerTs: Double = 0.0
|
||||
) : RealmObject()
|
||||
) : RealmObject() {
|
||||
companion object
|
||||
}
|
||||
|
|
|
@ -2,15 +2,18 @@ package im.vector.matrix.android.internal.database.query
|
|||
|
||||
import im.vector.matrix.android.internal.database.DBConstants
|
||||
import im.vector.matrix.android.internal.database.model.ChunkEntity
|
||||
import im.vector.matrix.android.internal.database.model.ChunkEntityFields
|
||||
import im.vector.matrix.android.internal.database.model.RoomEntityFields
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmQuery
|
||||
import io.realm.RealmResults
|
||||
import io.realm.kotlin.where
|
||||
|
||||
fun ChunkEntity.Companion.where(realm: Realm, roomId: String): RealmQuery<ChunkEntity> {
|
||||
return realm.where(ChunkEntity::class.java)
|
||||
.equalTo("room.roomId", roomId)
|
||||
.notEqualTo("prevToken", DBConstants.STATE_EVENTS_CHUNK_TOKEN)
|
||||
.notEqualTo("nextToken", DBConstants.STATE_EVENTS_CHUNK_TOKEN)
|
||||
return realm.where<ChunkEntity>()
|
||||
.equalTo("${ChunkEntityFields.ROOM}.${RoomEntityFields.ROOM_ID}", roomId)
|
||||
.notEqualTo(ChunkEntityFields.PREV_TOKEN, DBConstants.STATE_EVENTS_CHUNK_TOKEN)
|
||||
.notEqualTo(ChunkEntityFields.NEXT_TOKEN, DBConstants.STATE_EVENTS_CHUNK_TOKEN)
|
||||
}
|
||||
|
||||
fun ChunkEntity.Companion.findWithPrevToken(realm: Realm, roomId: String, prevToken: String?): ChunkEntity? {
|
||||
|
@ -19,7 +22,7 @@ fun ChunkEntity.Companion.findWithPrevToken(realm: Realm, roomId: String, prevTo
|
|||
}
|
||||
return where(realm, roomId)
|
||||
.and()
|
||||
.equalTo("prevToken", prevToken)
|
||||
.equalTo(ChunkEntityFields.PREV_TOKEN, prevToken)
|
||||
.findFirst()
|
||||
}
|
||||
|
||||
|
@ -29,22 +32,22 @@ fun ChunkEntity.Companion.findWithNextToken(realm: Realm, roomId: String, nextTo
|
|||
}
|
||||
return where(realm, roomId)
|
||||
.and()
|
||||
.equalTo("nextToken", nextToken)
|
||||
.equalTo(ChunkEntityFields.NEXT_TOKEN, nextToken)
|
||||
.findFirst()
|
||||
}
|
||||
|
||||
fun ChunkEntity.Companion.findLastLiveChunkFromRoom(realm: Realm, roomId: String): ChunkEntity? {
|
||||
return where(realm, roomId)
|
||||
.and()
|
||||
.isNull("nextToken")
|
||||
.isNull(ChunkEntityFields.NEXT_TOKEN)
|
||||
.findAll()
|
||||
.last(null)
|
||||
}
|
||||
|
||||
fun ChunkEntity.Companion.findAllIncludingEvents(realm: Realm, eventIds: List<String>): RealmResults<ChunkEntity> {
|
||||
return realm.where(ChunkEntity::class.java)
|
||||
.`in`("events.eventId", eventIds.toTypedArray())
|
||||
.notEqualTo("prevToken", DBConstants.STATE_EVENTS_CHUNK_TOKEN)
|
||||
.notEqualTo("nextToken", DBConstants.STATE_EVENTS_CHUNK_TOKEN)
|
||||
return realm.where<ChunkEntity>()
|
||||
.`in`(ChunkEntityFields.EVENTS.EVENT_ID, eventIds.toTypedArray())
|
||||
.notEqualTo(ChunkEntityFields.PREV_TOKEN, DBConstants.STATE_EVENTS_CHUNK_TOKEN)
|
||||
.notEqualTo(ChunkEntityFields.NEXT_TOKEN, DBConstants.STATE_EVENTS_CHUNK_TOKEN)
|
||||
.findAll()
|
||||
}
|
|
@ -3,49 +3,53 @@ package im.vector.matrix.android.internal.database.query
|
|||
import im.vector.matrix.android.api.session.events.model.EventType
|
||||
import im.vector.matrix.android.api.session.room.model.RoomMember
|
||||
import im.vector.matrix.android.internal.database.mapper.asDomain
|
||||
import im.vector.matrix.android.internal.database.model.ChunkEntityFields
|
||||
import im.vector.matrix.android.internal.database.model.EventEntity
|
||||
import im.vector.matrix.android.internal.database.model.EventEntityFields
|
||||
import im.vector.matrix.android.internal.database.model.RoomEntityFields
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmList
|
||||
import io.realm.RealmQuery
|
||||
import io.realm.Sort
|
||||
import io.realm.kotlin.where
|
||||
|
||||
fun EventEntity.Companion.where(realm: Realm, eventId: String): RealmQuery<EventEntity> {
|
||||
return realm.where(EventEntity::class.java)
|
||||
.equalTo("eventId", eventId)
|
||||
return realm.where<EventEntity>()
|
||||
.equalTo(EventEntityFields.EVENT_ID, eventId)
|
||||
}
|
||||
|
||||
fun EventEntity.Companion.where(realm: Realm, roomId: String, type: String? = null): RealmQuery<EventEntity> {
|
||||
val query = realm.where(EventEntity::class.java)
|
||||
.equalTo("chunk.room.roomId", roomId)
|
||||
val query = realm.where<EventEntity>()
|
||||
.equalTo("${EventEntityFields.CHUNK}.${ChunkEntityFields.ROOM}.${RoomEntityFields.ROOM_ID}", roomId)
|
||||
if (type != null) {
|
||||
query.equalTo("type", type)
|
||||
query.equalTo(EventEntityFields.TYPE, type)
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
fun EventEntity.Companion.stateEvents(realm: Realm, roomId: String): RealmQuery<EventEntity> {
|
||||
return realm.where(EventEntity::class.java)
|
||||
.equalTo("chunk.room.roomId", roomId)
|
||||
.isNotNull("stateKey")
|
||||
return realm.where<EventEntity>()
|
||||
.equalTo("${EventEntityFields.CHUNK}.${ChunkEntityFields.ROOM}.${RoomEntityFields.ROOM_ID}", roomId)
|
||||
.isNotNull(EventEntityFields.STATE_KEY)
|
||||
}
|
||||
|
||||
fun RealmQuery<EventEntity>.last(from: Long? = null): EventEntity? {
|
||||
if (from != null) {
|
||||
this.lessThanOrEqualTo("originServerTs", from)
|
||||
this.lessThanOrEqualTo(EventEntityFields.ORIGIN_SERVER_TS, from)
|
||||
}
|
||||
return this
|
||||
.sort("originServerTs", Sort.DESCENDING)
|
||||
.sort(EventEntityFields.ORIGIN_SERVER_TS, Sort.DESCENDING)
|
||||
.findFirst()
|
||||
}
|
||||
|
||||
fun RealmList<EventEntity>.fastContains(eventEntity: EventEntity): Boolean {
|
||||
return this.where().equalTo("eventId", eventEntity.eventId).findFirst() != null
|
||||
return this.where().equalTo(EventEntityFields.EVENT_ID, eventEntity.eventId).findFirst() != null
|
||||
}
|
||||
|
||||
fun EventEntity.Companion.findAllRoomMembers(realm: Realm, roomId: String): Map<String, RoomMember> {
|
||||
return EventEntity
|
||||
.where(realm, roomId, EventType.STATE_ROOM_MEMBER)
|
||||
.sort("originServerTs")
|
||||
.sort(EventEntityFields.ORIGIN_SERVER_TS)
|
||||
.findAll()
|
||||
.map { it.asDomain() }
|
||||
.associateBy { it.stateKey!! }
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package im.vector.matrix.android.internal.database.query
|
||||
|
||||
import im.vector.matrix.android.internal.database.model.ReadReceiptEntity
|
||||
import im.vector.matrix.android.internal.database.model.ReadReceiptEntityFields
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmQuery
|
||||
import io.realm.kotlin.where
|
||||
|
||||
fun ReadReceiptEntity.Companion.where(realm: Realm, roomId: String, userId: String): RealmQuery<ReadReceiptEntity> {
|
||||
return realm.where<ReadReceiptEntity>()
|
||||
.equalTo(ReadReceiptEntityFields.ROOM_ID, roomId)
|
||||
.equalTo(ReadReceiptEntityFields.USER_ID, userId)
|
||||
}
|
|
@ -2,17 +2,19 @@ package im.vector.matrix.android.internal.database.query
|
|||
|
||||
import im.vector.matrix.android.api.session.room.model.MyMembership
|
||||
import im.vector.matrix.android.internal.database.model.RoomEntity
|
||||
import im.vector.matrix.android.internal.database.model.RoomEntityFields
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmQuery
|
||||
import io.realm.kotlin.where
|
||||
|
||||
fun RoomEntity.Companion.where(realm: Realm, roomId: String): RealmQuery<RoomEntity> {
|
||||
return realm.where<RoomEntity>(RoomEntity::class.java).equalTo("roomId", roomId)
|
||||
return realm.where<RoomEntity>().equalTo(RoomEntityFields.ROOM_ID, roomId)
|
||||
}
|
||||
|
||||
fun RoomEntity.Companion.where(realm: Realm, membership: MyMembership? = null): RealmQuery<RoomEntity> {
|
||||
val query = realm.where(RoomEntity::class.java)
|
||||
val query = realm.where<RoomEntity>()
|
||||
if (membership != null) {
|
||||
query.equalTo("membership", membership.name)
|
||||
query.equalTo(RoomEntityFields.MEMBERSHIP_STR, membership.name)
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
package im.vector.matrix.android.internal.database.query
|
||||
|
||||
import im.vector.matrix.android.internal.database.model.RoomSummaryEntity
|
||||
import im.vector.matrix.android.internal.database.model.RoomSummaryEntityFields
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmQuery
|
||||
import io.realm.kotlin.where
|
||||
|
||||
fun RoomSummaryEntity.Companion.where(realm: Realm, roomId: String? = null): RealmQuery<RoomSummaryEntity> {
|
||||
val query = realm.where(RoomSummaryEntity::class.java)
|
||||
val query = realm.where<RoomSummaryEntity>()
|
||||
if (roomId != null) {
|
||||
query.equalTo("roomId", roomId)
|
||||
query.equalTo(RoomSummaryEntityFields.ROOM_ID, roomId)
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import im.vector.matrix.android.api.session.events.model.EnrichedEvent
|
|||
import im.vector.matrix.android.api.session.events.model.EventType
|
||||
import im.vector.matrix.android.internal.database.mapper.asDomain
|
||||
import im.vector.matrix.android.internal.database.model.EventEntity
|
||||
import im.vector.matrix.android.internal.database.model.EventEntityFields
|
||||
import im.vector.matrix.android.internal.database.query.last
|
||||
import im.vector.matrix.android.internal.database.query.where
|
||||
|
||||
|
@ -19,7 +20,7 @@ class MessageEventInterceptor(val monarchy: Monarchy) : EnrichedEventInterceptor
|
|||
monarchy.doWithRealm { realm ->
|
||||
val roomMember = EventEntity
|
||||
.where(realm, roomId, EventType.STATE_ROOM_MEMBER)
|
||||
.equalTo("stateKey", event.root.sender)
|
||||
.equalTo(EventEntityFields.STATE_KEY, event.root.sender)
|
||||
.last(from = event.root.originServerTs)
|
||||
?.asDomain()
|
||||
event.enrichWith(roomMember)
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package im.vector.matrix.android.internal.session.room
|
||||
|
||||
import im.vector.matrix.android.internal.network.NetworkConstants
|
||||
import im.vector.matrix.android.internal.session.room.model.RoomMembersResponse
|
||||
import im.vector.matrix.android.internal.session.room.model.TokenChunkEvent
|
||||
import im.vector.matrix.android.internal.session.room.members.RoomMembersResponse
|
||||
import im.vector.matrix.android.internal.session.room.timeline.TokenChunkEvent
|
||||
import kotlinx.coroutines.Deferred
|
||||
import retrofit2.Response
|
||||
import retrofit2.http.GET
|
||||
|
|
|
@ -14,7 +14,6 @@ import im.vector.matrix.android.internal.database.query.findAllRoomMembers
|
|||
import im.vector.matrix.android.internal.database.query.where
|
||||
import im.vector.matrix.android.internal.network.executeRequest
|
||||
import im.vector.matrix.android.internal.session.room.RoomAPI
|
||||
import im.vector.matrix.android.internal.session.room.model.RoomMembersResponse
|
||||
import im.vector.matrix.android.internal.session.sync.StateEventsChunkHandler
|
||||
import im.vector.matrix.android.internal.util.CancelableCoroutine
|
||||
import im.vector.matrix.android.internal.util.MatrixCoroutineDispatchers
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package im.vector.matrix.android.internal.session.room.model
|
||||
package im.vector.matrix.android.internal.session.room.members
|
||||
|
||||
import com.squareup.moshi.Json
|
||||
import com.squareup.moshi.JsonClass
|
|
@ -9,6 +9,7 @@ import im.vector.matrix.android.api.session.events.model.EnrichedEvent
|
|||
import im.vector.matrix.android.api.session.room.TimelineHolder
|
||||
import im.vector.matrix.android.internal.database.mapper.asDomain
|
||||
import im.vector.matrix.android.internal.database.model.ChunkEntity
|
||||
import im.vector.matrix.android.internal.database.model.EventEntityFields
|
||||
import im.vector.matrix.android.internal.database.query.where
|
||||
import im.vector.matrix.android.internal.session.events.interceptor.MessageEventInterceptor
|
||||
import io.realm.Sort
|
||||
|
@ -30,7 +31,7 @@ class DefaultTimelineHolder(private val roomId: String,
|
|||
.findAll()
|
||||
.last(null)
|
||||
?.let {
|
||||
it.events.where().sort("originServerTs", Sort.DESCENDING)
|
||||
it.events.where().sort(EventEntityFields.ORIGIN_SERVER_TS, Sort.DESCENDING)
|
||||
}
|
||||
}
|
||||
val domainSourceFactory = realmDataSourceFactory
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package im.vector.matrix.android.internal.session.room.model
|
||||
package im.vector.matrix.android.internal.session.room.timeline
|
||||
|
||||
enum class PaginationDirection(val value: String) {
|
||||
/**
|
|
@ -18,8 +18,6 @@ import im.vector.matrix.android.internal.database.query.where
|
|||
import im.vector.matrix.android.internal.legacy.util.FilterUtil
|
||||
import im.vector.matrix.android.internal.network.executeRequest
|
||||
import im.vector.matrix.android.internal.session.room.RoomAPI
|
||||
import im.vector.matrix.android.internal.session.room.model.PaginationDirection
|
||||
import im.vector.matrix.android.internal.session.room.model.TokenChunkEvent
|
||||
import im.vector.matrix.android.internal.session.sync.StateEventsChunkHandler
|
||||
import im.vector.matrix.android.internal.util.CancelableCoroutine
|
||||
import im.vector.matrix.android.internal.util.MatrixCoroutineDispatchers
|
||||
|
|
|
@ -7,8 +7,6 @@ import im.vector.matrix.android.api.failure.Failure
|
|||
import im.vector.matrix.android.api.session.events.model.EnrichedEvent
|
||||
import im.vector.matrix.android.internal.database.model.ChunkEntity
|
||||
import im.vector.matrix.android.internal.database.query.findAllIncludingEvents
|
||||
import im.vector.matrix.android.internal.session.room.model.PaginationDirection
|
||||
import im.vector.matrix.android.internal.session.room.model.TokenChunkEvent
|
||||
import im.vector.matrix.android.internal.util.PagingRequestHelper
|
||||
import java.util.*
|
||||
import java.util.concurrent.Executor
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package im.vector.matrix.android.internal.session.room.model
|
||||
package im.vector.matrix.android.internal.session.room.timeline
|
||||
|
||||
import com.squareup.moshi.Json
|
||||
import com.squareup.moshi.JsonClass
|
|
@ -24,8 +24,8 @@ class ReadReceiptHandler {
|
|||
.flatMap { (_, userIdsDict) ->
|
||||
userIdsDict.map { (userId, paramsDict) ->
|
||||
val ts = paramsDict.filterKeys { it == "ts" }
|
||||
.values
|
||||
.firstOrNull() ?: 0.0
|
||||
.values
|
||||
.firstOrNull() ?: 0.0
|
||||
val primaryKey = roomId + userId
|
||||
ReadReceiptEntity(primaryKey, userId, eventId, roomId, ts)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue