2018-10-25 15:25:58 +00:00
|
|
|
package im.vector.matrix.android.internal.session.room.timeline
|
|
|
|
|
|
|
|
import android.arch.lifecycle.LiveData
|
|
|
|
import android.arch.paging.LivePagedListBuilder
|
|
|
|
import android.arch.paging.PagedList
|
|
|
|
import com.zhuinden.monarchy.Monarchy
|
2018-11-27 21:42:46 +00:00
|
|
|
import im.vector.matrix.android.api.MatrixCallback
|
2018-10-25 15:25:58 +00:00
|
|
|
import im.vector.matrix.android.api.session.events.interceptor.EnrichedEventInterceptor
|
|
|
|
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
|
2018-11-15 18:00:15 +00:00
|
|
|
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.query.where
|
2018-10-25 15:25:58 +00:00
|
|
|
import im.vector.matrix.android.internal.session.events.interceptor.MessageEventInterceptor
|
2018-11-19 14:47:54 +00:00
|
|
|
import io.realm.Realm
|
|
|
|
import io.realm.RealmQuery
|
2018-10-25 15:25:58 +00:00
|
|
|
|
2018-11-25 15:17:13 +00:00
|
|
|
private const val PAGE_SIZE = 30
|
2018-10-31 15:23:02 +00:00
|
|
|
|
2018-11-08 10:04:40 +00:00
|
|
|
internal class DefaultTimelineHolder(private val roomId: String,
|
2018-11-13 09:50:53 +00:00
|
|
|
private val monarchy: Monarchy,
|
2018-11-27 21:42:46 +00:00
|
|
|
private val boundaryCallback: TimelineBoundaryCallback,
|
|
|
|
private val contextOfEventRequest: GetContextOfEventRequest
|
2018-10-25 15:25:58 +00:00
|
|
|
) : TimelineHolder {
|
|
|
|
|
|
|
|
private val eventInterceptors = ArrayList<EnrichedEventInterceptor>()
|
|
|
|
|
|
|
|
init {
|
2018-11-25 15:17:13 +00:00
|
|
|
boundaryCallback.limit = 30
|
2018-11-14 18:32:42 +00:00
|
|
|
eventInterceptors.add(MessageEventInterceptor(monarchy, roomId))
|
2018-10-25 15:25:58 +00:00
|
|
|
}
|
|
|
|
|
2018-11-19 14:47:54 +00:00
|
|
|
override fun timeline(eventId: String?): LiveData<PagedList<EnrichedEvent>> {
|
2018-11-27 11:06:40 +00:00
|
|
|
if (eventId != null) {
|
2018-11-27 21:42:46 +00:00
|
|
|
fetchEventIfNeeded(eventId)
|
2018-11-27 11:06:40 +00:00
|
|
|
}
|
2018-11-19 14:47:54 +00:00
|
|
|
val realmDataSourceFactory = monarchy.createDataSourceFactory {
|
|
|
|
buildDataSourceFactoryQuery(it, eventId)
|
2018-10-25 15:25:58 +00:00
|
|
|
}
|
|
|
|
val domainSourceFactory = realmDataSourceFactory
|
|
|
|
.map { it.asDomain() }
|
|
|
|
.map { event ->
|
2018-10-31 15:23:02 +00:00
|
|
|
|
2018-10-25 15:25:58 +00:00
|
|
|
val enrichedEvent = EnrichedEvent(event)
|
|
|
|
eventInterceptors
|
|
|
|
.filter {
|
|
|
|
it.canEnrich(enrichedEvent)
|
|
|
|
}.forEach {
|
2018-11-14 18:32:42 +00:00
|
|
|
it.enrich(enrichedEvent)
|
2018-10-25 15:25:58 +00:00
|
|
|
}
|
|
|
|
enrichedEvent
|
|
|
|
}
|
|
|
|
|
|
|
|
val pagedListConfig = PagedList.Config.Builder()
|
|
|
|
.setEnablePlaceholders(false)
|
2018-10-31 15:23:02 +00:00
|
|
|
.setPageSize(PAGE_SIZE)
|
2018-10-25 15:25:58 +00:00
|
|
|
.build()
|
|
|
|
|
|
|
|
val livePagedListBuilder = LivePagedListBuilder(domainSourceFactory, pagedListConfig).setBoundaryCallback(boundaryCallback)
|
|
|
|
return monarchy.findAllPagedWithChanges(realmDataSourceFactory, livePagedListBuilder)
|
|
|
|
}
|
2018-11-19 14:47:54 +00:00
|
|
|
|
2018-11-27 21:42:46 +00:00
|
|
|
private fun fetchEventIfNeeded(eventId: String) {
|
|
|
|
if (!isEventPersisted(eventId)) {
|
|
|
|
contextOfEventRequest.execute(roomId, eventId, object : MatrixCallback<EventContextResponse> {})
|
|
|
|
}
|
|
|
|
}
|
2018-11-27 11:06:40 +00:00
|
|
|
|
2018-11-27 21:42:46 +00:00
|
|
|
private fun isEventPersisted(eventId: String): Boolean {
|
|
|
|
var isEventPersisted = false
|
|
|
|
monarchy.doWithRealm {
|
|
|
|
isEventPersisted = EventEntity.where(it, eventId = eventId).findFirst() != null
|
|
|
|
}
|
|
|
|
return isEventPersisted
|
2018-11-27 11:06:40 +00:00
|
|
|
}
|
|
|
|
|
2018-11-19 14:47:54 +00:00
|
|
|
private fun buildDataSourceFactoryQuery(realm: Realm, eventId: String?): RealmQuery<EventEntity> {
|
|
|
|
val query = if (eventId == null) {
|
|
|
|
EventEntity
|
|
|
|
.where(realm, roomId = roomId)
|
|
|
|
.equalTo("${EventEntityFields.CHUNK}.${ChunkEntityFields.IS_LAST}", true)
|
|
|
|
} else {
|
|
|
|
EventEntity
|
2018-11-27 11:06:40 +00:00
|
|
|
.where(realm, roomId = roomId)
|
2018-11-19 14:47:54 +00:00
|
|
|
.`in`("${EventEntityFields.CHUNK}.${ChunkEntityFields.EVENTS.EVENT_ID}", arrayOf(eventId))
|
|
|
|
}
|
|
|
|
return query.sort(EventEntityFields.DISPLAY_INDEX)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-25 15:25:58 +00:00
|
|
|
}
|