2019-01-28 16:56:23 +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.session.room.read
|
|
|
|
|
|
|
|
import arrow.core.Try
|
2019-01-30 17:39:54 +00:00
|
|
|
import com.zhuinden.monarchy.Monarchy
|
|
|
|
import im.vector.matrix.android.api.auth.data.Credentials
|
|
|
|
import im.vector.matrix.android.internal.database.model.ChunkEntity
|
|
|
|
import im.vector.matrix.android.internal.database.model.EventEntity
|
|
|
|
import im.vector.matrix.android.internal.database.model.ReadReceiptEntity
|
|
|
|
import im.vector.matrix.android.internal.database.model.RoomSummaryEntity
|
2019-07-05 14:07:12 +00:00
|
|
|
import im.vector.matrix.android.internal.database.model.TimelineEventEntity
|
2019-01-30 17:39:54 +00:00
|
|
|
import im.vector.matrix.android.internal.database.query.find
|
|
|
|
import im.vector.matrix.android.internal.database.query.findLastLiveChunkFromRoom
|
|
|
|
import im.vector.matrix.android.internal.database.query.latestEvent
|
|
|
|
import im.vector.matrix.android.internal.database.query.where
|
2019-01-28 16:56:23 +00:00
|
|
|
import im.vector.matrix.android.internal.network.executeRequest
|
|
|
|
import im.vector.matrix.android.internal.session.room.RoomAPI
|
2019-07-02 10:08:44 +00:00
|
|
|
import im.vector.matrix.android.internal.session.room.send.LocalEchoEventFactory
|
2019-01-28 16:56:23 +00:00
|
|
|
import im.vector.matrix.android.internal.task.Task
|
2019-01-30 17:39:54 +00:00
|
|
|
import im.vector.matrix.android.internal.util.tryTransactionAsync
|
2019-07-02 10:08:44 +00:00
|
|
|
import timber.log.Timber
|
2019-06-14 14:32:23 +00:00
|
|
|
import javax.inject.Inject
|
2019-01-28 16:56:23 +00:00
|
|
|
|
2019-01-30 17:39:54 +00:00
|
|
|
internal interface SetReadMarkersTask : Task<SetReadMarkersTask.Params, Unit> {
|
2019-01-28 16:56:23 +00:00
|
|
|
|
|
|
|
data class Params(
|
|
|
|
val roomId: String,
|
|
|
|
val fullyReadEventId: String?,
|
|
|
|
val readReceiptEventId: String?
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
private const val READ_MARKER = "m.fully_read"
|
|
|
|
private const val READ_RECEIPT = "m.read"
|
|
|
|
|
2019-06-14 14:32:23 +00:00
|
|
|
internal class DefaultSetReadMarkersTask @Inject constructor(private val roomAPI: RoomAPI,
|
2019-07-02 10:08:44 +00:00
|
|
|
private val credentials: Credentials,
|
|
|
|
private val monarchy: Monarchy
|
2019-01-28 16:56:23 +00:00
|
|
|
) : SetReadMarkersTask {
|
|
|
|
|
2019-06-04 14:26:37 +00:00
|
|
|
override suspend fun execute(params: SetReadMarkersTask.Params): Try<Unit> {
|
2019-01-28 16:56:23 +00:00
|
|
|
val markers = HashMap<String, String>()
|
2019-07-02 10:08:44 +00:00
|
|
|
if (params.fullyReadEventId != null) {
|
|
|
|
if (LocalEchoEventFactory.isLocalEchoId(params.fullyReadEventId)) {
|
|
|
|
Timber.w("Can't set read marker for local event ${params.fullyReadEventId}")
|
|
|
|
} else {
|
|
|
|
markers[READ_MARKER] = params.fullyReadEventId
|
|
|
|
}
|
2019-01-28 16:56:23 +00:00
|
|
|
}
|
2019-01-31 10:45:11 +00:00
|
|
|
if (params.readReceiptEventId != null
|
2019-03-13 21:30:05 +00:00
|
|
|
&& !isEventRead(params.roomId, params.readReceiptEventId)) {
|
2019-01-31 10:45:11 +00:00
|
|
|
|
2019-07-02 10:08:44 +00:00
|
|
|
if (LocalEchoEventFactory.isLocalEchoId(params.readReceiptEventId)) {
|
|
|
|
Timber.w("Can't set read marker for local event ${params.fullyReadEventId}")
|
|
|
|
} else {
|
|
|
|
updateNotificationCountIfNecessary(params.roomId, params.readReceiptEventId)
|
|
|
|
markers[READ_RECEIPT] = params.readReceiptEventId
|
|
|
|
}
|
2019-01-28 16:56:23 +00:00
|
|
|
}
|
2019-01-30 17:39:54 +00:00
|
|
|
return if (markers.isEmpty()) {
|
|
|
|
Try.just(Unit)
|
|
|
|
} else {
|
|
|
|
executeRequest {
|
|
|
|
apiCall = roomAPI.sendReadMarker(params.roomId, markers)
|
|
|
|
}
|
2019-01-28 16:56:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-30 17:39:54 +00:00
|
|
|
|
|
|
|
private fun updateNotificationCountIfNecessary(roomId: String, eventId: String) {
|
|
|
|
monarchy.tryTransactionAsync { realm ->
|
2019-07-05 14:07:12 +00:00
|
|
|
val isLatestReceived = TimelineEventEntity.latestEvent(realm, roomId)?.eventId == eventId
|
2019-01-30 17:39:54 +00:00
|
|
|
if (isLatestReceived) {
|
|
|
|
val roomSummary = RoomSummaryEntity.where(realm, roomId).findFirst()
|
2019-03-13 21:30:05 +00:00
|
|
|
?: return@tryTransactionAsync
|
2019-01-30 17:39:54 +00:00
|
|
|
roomSummary.notificationCount = 0
|
|
|
|
roomSummary.highlightCount = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun isEventRead(roomId: String, eventId: String): Boolean {
|
|
|
|
var isEventRead = false
|
|
|
|
monarchy.doWithRealm {
|
|
|
|
val readReceipt = ReadReceiptEntity.where(it, roomId, credentials.userId).findFirst()
|
2019-03-13 21:30:05 +00:00
|
|
|
?: return@doWithRealm
|
2019-01-30 17:39:54 +00:00
|
|
|
val liveChunk = ChunkEntity.findLastLiveChunkFromRoom(it, roomId)
|
2019-03-13 21:30:05 +00:00
|
|
|
?: return@doWithRealm
|
2019-07-05 14:07:12 +00:00
|
|
|
val readReceiptIndex = liveChunk.timelineEvents.find(readReceipt.eventId)?.root?.displayIndex
|
2019-03-13 21:30:05 +00:00
|
|
|
?: Int.MIN_VALUE
|
2019-07-05 14:07:12 +00:00
|
|
|
val eventToCheckIndex = liveChunk.timelineEvents.find(eventId)?.root?.displayIndex
|
2019-03-13 21:30:05 +00:00
|
|
|
?: Int.MAX_VALUE
|
|
|
|
isEventRead = eventToCheckIndex <= readReceiptIndex
|
2019-01-30 17:39:54 +00:00
|
|
|
}
|
|
|
|
return isEventRead
|
|
|
|
}
|
|
|
|
|
2019-01-28 16:56:23 +00:00
|
|
|
}
|