added read receipts for threads
This commit is contained in:
parent
29d3856ef2
commit
1f8ac06528
@ -18,5 +18,6 @@ package org.matrix.android.sdk.api.session.room.model
|
||||
|
||||
data class ReadReceipt(
|
||||
val roomMember: RoomMemberSummary,
|
||||
val originServerTs: Long
|
||||
val originServerTs: Long,
|
||||
val threadId: String?
|
||||
)
|
||||
|
||||
@ -34,12 +34,12 @@ interface ReadService {
|
||||
/**
|
||||
* Force the read marker to be set on the latest event.
|
||||
*/
|
||||
suspend fun markAsRead(params: MarkAsReadParams = MarkAsReadParams.BOTH)
|
||||
suspend fun markAsRead(params: MarkAsReadParams = MarkAsReadParams.BOTH, mainTimeLineOnly: Boolean)
|
||||
|
||||
/**
|
||||
* Set the read receipt on the event with provided eventId.
|
||||
*/
|
||||
suspend fun setReadReceipt(eventId: String)
|
||||
suspend fun setReadReceipt(eventId: String, threadId: String)
|
||||
|
||||
/**
|
||||
* Set the read marker on the event with provided eventId.
|
||||
@ -62,7 +62,7 @@ interface ReadService {
|
||||
fun getMyReadReceiptLive(): LiveData<Optional<String>>
|
||||
|
||||
/**
|
||||
* Get the eventId where the read receipt for the provided user is.
|
||||
* Get the eventId from the main timeline where the read receipt for the provided user is.
|
||||
* @param userId the id of the user to look for
|
||||
*
|
||||
* @return the eventId where the read receipt for the provided user is attached, or null if not found
|
||||
@ -74,4 +74,8 @@ interface ReadService {
|
||||
* @param eventId the event
|
||||
*/
|
||||
fun getEventReadReceiptsLive(eventId: String): LiveData<List<ReadReceipt>>
|
||||
|
||||
companion object {
|
||||
const val THREAD_ID_MAIN = "main"
|
||||
}
|
||||
}
|
||||
|
||||
@ -133,7 +133,7 @@ private fun handleReadReceipts(realm: Realm, roomId: String, eventEntity: EventE
|
||||
val originServerTs = eventEntity.originServerTs
|
||||
if (originServerTs != null) {
|
||||
val timestampOfEvent = originServerTs.toDouble()
|
||||
val readReceiptOfSender = ReadReceiptEntity.getOrCreate(realm, roomId = roomId, userId = senderId)
|
||||
val readReceiptOfSender = ReadReceiptEntity.getOrCreate(realm, roomId = roomId, userId = senderId, threadId = eventEntity.rootThreadEventId)
|
||||
// If the synced RR is older, update
|
||||
if (timestampOfEvent > readReceiptOfSender.originServerTs) {
|
||||
val previousReceiptsSummary = ReadReceiptsSummaryEntity.where(realm, eventId = readReceiptOfSender.eventId).findFirst()
|
||||
|
||||
@ -22,6 +22,8 @@ import io.realm.RealmQuery
|
||||
import io.realm.Sort
|
||||
import org.matrix.android.sdk.api.session.events.model.UnsignedData
|
||||
import org.matrix.android.sdk.api.session.events.model.isRedacted
|
||||
import org.matrix.android.sdk.api.session.room.model.ReadReceipt
|
||||
import org.matrix.android.sdk.api.session.room.read.ReadService
|
||||
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
|
||||
import org.matrix.android.sdk.api.session.threads.ThreadNotificationState
|
||||
import org.matrix.android.sdk.internal.database.mapper.asDomain
|
||||
@ -273,8 +275,8 @@ internal fun TimelineEventEntity.Companion.isUserMentionedInThread(realm: Realm,
|
||||
/**
|
||||
* Find the read receipt for the current user.
|
||||
*/
|
||||
internal fun findMyReadReceipt(realm: Realm, roomId: String, userId: String): String? =
|
||||
ReadReceiptEntity.where(realm, roomId = roomId, userId = userId)
|
||||
internal fun findMyReadReceipt(realm: Realm, roomId: String, userId: String, threadId: String?): String? =
|
||||
ReadReceiptEntity.where(realm, roomId = roomId, userId = userId, threadId = threadId)
|
||||
.findFirst()
|
||||
?.eventId
|
||||
|
||||
@ -294,7 +296,7 @@ internal fun isUserMentioned(currentUserId: String, timelineEventEntity: Timelin
|
||||
* immediately so we should not display wrong notifications
|
||||
*/
|
||||
internal fun updateNotificationsNew(roomId: String, realm: Realm, currentUserId: String) {
|
||||
val readReceipt = findMyReadReceipt(realm, roomId, currentUserId) ?: return
|
||||
val readReceipt = findMyReadReceipt(realm, roomId, currentUserId, ReadService.THREAD_ID_MAIN) ?: return
|
||||
|
||||
val readReceiptChunk = ChunkEntity
|
||||
.findIncludingEvent(realm, readReceipt) ?: return
|
||||
|
||||
@ -50,7 +50,7 @@ internal class ReadReceiptsSummaryMapper @Inject constructor(
|
||||
.mapNotNull {
|
||||
val roomMember = RoomMemberSummaryEntity.where(realm, roomId = it.roomId, userId = it.userId).findFirst()
|
||||
?: return@mapNotNull null
|
||||
ReadReceipt(roomMember.asDomain(), it.originServerTs.toLong())
|
||||
ReadReceipt(roomMember.asDomain(), it.originServerTs.toLong(), it.threadId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -26,6 +26,7 @@ internal open class ReadReceiptEntity(
|
||||
var eventId: String = "",
|
||||
var roomId: String = "",
|
||||
var userId: String = "",
|
||||
var threadId: String? = null,
|
||||
var originServerTs: Double = 0.0
|
||||
) : RealmObject() {
|
||||
companion object
|
||||
|
||||
@ -20,6 +20,7 @@ import io.realm.RealmObject
|
||||
import io.realm.RealmResults
|
||||
import io.realm.annotations.Index
|
||||
import io.realm.annotations.LinkingObjects
|
||||
import org.matrix.android.sdk.api.session.room.read.ReadService
|
||||
import org.matrix.android.sdk.internal.extensions.assertIsManaged
|
||||
|
||||
internal open class TimelineEventEntity(
|
||||
@ -52,3 +53,7 @@ internal fun TimelineEventEntity.deleteOnCascade(canDeleteRoot: Boolean) {
|
||||
}
|
||||
deleteFromRealm()
|
||||
}
|
||||
|
||||
internal fun TimelineEventEntity.getThreadId(): String {
|
||||
return root?.rootThreadEventId ?: ReadService.THREAD_ID_MAIN
|
||||
}
|
||||
|
||||
@ -23,12 +23,14 @@ import org.matrix.android.sdk.internal.database.model.ChunkEntity
|
||||
import org.matrix.android.sdk.internal.database.model.ReadMarkerEntity
|
||||
import org.matrix.android.sdk.internal.database.model.ReadReceiptEntity
|
||||
import org.matrix.android.sdk.internal.database.model.TimelineEventEntity
|
||||
import org.matrix.android.sdk.internal.database.model.getThreadId
|
||||
|
||||
internal fun isEventRead(
|
||||
realmConfiguration: RealmConfiguration,
|
||||
userId: String?,
|
||||
roomId: String?,
|
||||
eventId: String?
|
||||
eventId: String?,
|
||||
shouldCheckIfReadInEventsThread: Boolean
|
||||
): Boolean {
|
||||
if (userId.isNullOrBlank() || roomId.isNullOrBlank() || eventId.isNullOrBlank()) {
|
||||
return false
|
||||
@ -45,7 +47,8 @@ internal fun isEventRead(
|
||||
eventToCheck.root?.sender == userId -> true
|
||||
// If new event exists and the latest event is from ourselves we can infer the event is read
|
||||
latestEventIsFromSelf(realm, roomId, userId) -> true
|
||||
eventToCheck.isBeforeLatestReadReceipt(realm, roomId, userId) -> true
|
||||
eventToCheck.isBeforeLatestReadReceipt(realm, roomId, userId, null) -> true
|
||||
(shouldCheckIfReadInEventsThread && eventToCheck.isBeforeLatestReadReceipt(realm, roomId, userId, eventToCheck.getThreadId())) -> true
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
@ -54,11 +57,12 @@ internal fun isEventRead(
|
||||
private fun latestEventIsFromSelf(realm: Realm, roomId: String, userId: String) = TimelineEventEntity.latestEvent(realm, roomId, true)
|
||||
?.root?.sender == userId
|
||||
|
||||
private fun TimelineEventEntity.isBeforeLatestReadReceipt(realm: Realm, roomId: String, userId: String): Boolean {
|
||||
return ReadReceiptEntity.where(realm, roomId, userId).findFirst()?.let { readReceipt ->
|
||||
private fun TimelineEventEntity.isBeforeLatestReadReceipt(realm: Realm, roomId: String, userId: String, threadId: String?): Boolean {
|
||||
val isMoreRecent = ReadReceiptEntity.where(realm, roomId, userId, threadId).findFirst()?.let { readReceipt ->
|
||||
val readReceiptEvent = TimelineEventEntity.where(realm, roomId, readReceipt.eventId).findFirst()
|
||||
readReceiptEvent?.isMoreRecentThan(this)
|
||||
} ?: false
|
||||
return isMoreRecent
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -20,12 +20,26 @@ import io.realm.Realm
|
||||
import io.realm.RealmQuery
|
||||
import io.realm.kotlin.createObject
|
||||
import io.realm.kotlin.where
|
||||
import org.matrix.android.sdk.api.session.room.read.ReadService
|
||||
import org.matrix.android.sdk.internal.database.model.ReadReceiptEntity
|
||||
import org.matrix.android.sdk.internal.database.model.ReadReceiptEntityFields
|
||||
|
||||
internal fun ReadReceiptEntity.Companion.where(realm: Realm, roomId: String, userId: String, threadId: String?): RealmQuery<ReadReceiptEntity> {
|
||||
return realm.where<ReadReceiptEntity>()
|
||||
.equalTo(ReadReceiptEntityFields.PRIMARY_KEY, buildPrimaryKey(roomId, userId, threadId))
|
||||
}
|
||||
|
||||
internal fun ReadReceiptEntity.Companion.forMainTimelineWhere(realm: Realm, roomId: String, userId: String): RealmQuery<ReadReceiptEntity> {
|
||||
return realm.where<ReadReceiptEntity>()
|
||||
.equalTo(ReadReceiptEntityFields.PRIMARY_KEY, buildPrimaryKey(roomId, userId, ReadService.THREAD_ID_MAIN))
|
||||
.or()
|
||||
.equalTo(ReadReceiptEntityFields.PRIMARY_KEY, buildPrimaryKey(roomId, userId, null))
|
||||
}
|
||||
|
||||
internal fun ReadReceiptEntity.Companion.where(realm: Realm, roomId: String, userId: String): RealmQuery<ReadReceiptEntity> {
|
||||
return realm.where<ReadReceiptEntity>()
|
||||
.equalTo(ReadReceiptEntityFields.PRIMARY_KEY, buildPrimaryKey(roomId, userId))
|
||||
.equalTo(ReadReceiptEntityFields.USER_ID, userId)
|
||||
.equalTo(ReadReceiptEntityFields.ROOM_ID, roomId)
|
||||
}
|
||||
|
||||
internal fun ReadReceiptEntity.Companion.whereUserId(realm: Realm, userId: String): RealmQuery<ReadReceiptEntity> {
|
||||
@ -38,23 +52,25 @@ internal fun ReadReceiptEntity.Companion.whereRoomId(realm: Realm, roomId: Strin
|
||||
.equalTo(ReadReceiptEntityFields.ROOM_ID, roomId)
|
||||
}
|
||||
|
||||
internal fun ReadReceiptEntity.Companion.createUnmanaged(roomId: String, eventId: String, userId: String, originServerTs: Double): ReadReceiptEntity {
|
||||
internal fun ReadReceiptEntity.Companion.createUnmanaged(roomId: String, eventId: String, userId: String, threadId: String?, originServerTs: Double): ReadReceiptEntity {
|
||||
return ReadReceiptEntity().apply {
|
||||
this.primaryKey = "${roomId}_$userId"
|
||||
this.primaryKey = buildPrimaryKey(roomId, userId, threadId)
|
||||
this.eventId = eventId
|
||||
this.roomId = roomId
|
||||
this.userId = userId
|
||||
this.threadId = threadId
|
||||
this.originServerTs = originServerTs
|
||||
}
|
||||
}
|
||||
|
||||
internal fun ReadReceiptEntity.Companion.getOrCreate(realm: Realm, roomId: String, userId: String): ReadReceiptEntity {
|
||||
return ReadReceiptEntity.where(realm, roomId, userId).findFirst()
|
||||
?: realm.createObject<ReadReceiptEntity>(buildPrimaryKey(roomId, userId))
|
||||
internal fun ReadReceiptEntity.Companion.getOrCreate(realm: Realm, roomId: String, userId: String, threadId: String?): ReadReceiptEntity {
|
||||
return ReadReceiptEntity.where(realm, roomId, userId, threadId).findFirst()
|
||||
?: realm.createObject<ReadReceiptEntity>(buildPrimaryKey(roomId, userId, threadId))
|
||||
.apply {
|
||||
this.roomId = roomId
|
||||
this.userId = userId
|
||||
this.threadId = threadId
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildPrimaryKey(roomId: String, userId: String) = "${roomId}_$userId"
|
||||
private fun buildPrimaryKey(roomId: String, userId: String, threadId: String?) = "${roomId}_${userId}_${threadId ?: "null"}"
|
||||
|
||||
@ -33,6 +33,7 @@ import org.matrix.android.sdk.internal.session.room.membership.RoomMembersRespon
|
||||
import org.matrix.android.sdk.internal.session.room.membership.admin.UserIdAndReason
|
||||
import org.matrix.android.sdk.internal.session.room.membership.joining.InviteBody
|
||||
import org.matrix.android.sdk.internal.session.room.membership.threepid.ThreePidInviteBody
|
||||
import org.matrix.android.sdk.internal.session.room.read.ReadBody
|
||||
import org.matrix.android.sdk.internal.session.room.relation.RelationsResponse
|
||||
import org.matrix.android.sdk.internal.session.room.reporting.ReportContentBody
|
||||
import org.matrix.android.sdk.internal.session.room.send.SendResponse
|
||||
@ -173,7 +174,7 @@ internal interface RoomAPI {
|
||||
@Path("roomId") roomId: String,
|
||||
@Path("receiptType") receiptType: String,
|
||||
@Path("eventId") eventId: String,
|
||||
@Body body: JsonDict = emptyMap()
|
||||
@Body body: ReadBody
|
||||
)
|
||||
|
||||
/**
|
||||
|
||||
@ -22,6 +22,7 @@ import com.zhuinden.monarchy.Monarchy
|
||||
import dagger.assisted.Assisted
|
||||
import dagger.assisted.AssistedFactory
|
||||
import dagger.assisted.AssistedInject
|
||||
import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilitiesService
|
||||
import org.matrix.android.sdk.api.session.room.model.ReadReceipt
|
||||
import org.matrix.android.sdk.api.session.room.read.ReadService
|
||||
import org.matrix.android.sdk.api.util.Optional
|
||||
@ -30,6 +31,7 @@ import org.matrix.android.sdk.internal.database.mapper.ReadReceiptsSummaryMapper
|
||||
import org.matrix.android.sdk.internal.database.model.ReadMarkerEntity
|
||||
import org.matrix.android.sdk.internal.database.model.ReadReceiptEntity
|
||||
import org.matrix.android.sdk.internal.database.model.ReadReceiptsSummaryEntity
|
||||
import org.matrix.android.sdk.internal.database.query.forMainTimelineWhere
|
||||
import org.matrix.android.sdk.internal.database.query.isEventRead
|
||||
import org.matrix.android.sdk.internal.database.query.where
|
||||
import org.matrix.android.sdk.internal.di.SessionDatabase
|
||||
@ -40,7 +42,8 @@ internal class DefaultReadService @AssistedInject constructor(
|
||||
@SessionDatabase private val monarchy: Monarchy,
|
||||
private val setReadMarkersTask: SetReadMarkersTask,
|
||||
private val readReceiptsSummaryMapper: ReadReceiptsSummaryMapper,
|
||||
@UserId private val userId: String
|
||||
@UserId private val userId: String,
|
||||
private val homeServerCapabilitiesService: HomeServerCapabilitiesService,
|
||||
) : ReadService {
|
||||
|
||||
@AssistedFactory
|
||||
@ -48,17 +51,28 @@ internal class DefaultReadService @AssistedInject constructor(
|
||||
fun create(roomId: String): DefaultReadService
|
||||
}
|
||||
|
||||
override suspend fun markAsRead(params: ReadService.MarkAsReadParams) {
|
||||
override suspend fun markAsRead(params: ReadService.MarkAsReadParams, mainTimeLineOnly: Boolean) {
|
||||
val readReceiptThreadId = if (homeServerCapabilitiesService.getHomeServerCapabilities().canUseThreadReadReceiptsAndNotifications) {
|
||||
if (mainTimeLineOnly) ReadService.THREAD_ID_MAIN else null
|
||||
} else {
|
||||
null
|
||||
}
|
||||
val taskParams = SetReadMarkersTask.Params(
|
||||
roomId = roomId,
|
||||
forceReadMarker = params.forceReadMarker(),
|
||||
forceReadReceipt = params.forceReadReceipt()
|
||||
forceReadReceipt = params.forceReadReceipt(),
|
||||
readReceiptThreadId = readReceiptThreadId
|
||||
)
|
||||
setReadMarkersTask.execute(taskParams)
|
||||
}
|
||||
|
||||
override suspend fun setReadReceipt(eventId: String) {
|
||||
val params = SetReadMarkersTask.Params(roomId, fullyReadEventId = null, readReceiptEventId = eventId)
|
||||
override suspend fun setReadReceipt(eventId: String, threadId: String) {
|
||||
val readReceiptThreadId = if (homeServerCapabilitiesService.getHomeServerCapabilities().canUseThreadReadReceiptsAndNotifications) {
|
||||
threadId
|
||||
} else {
|
||||
null
|
||||
}
|
||||
val params = SetReadMarkersTask.Params(roomId, fullyReadEventId = null, readReceiptEventId = eventId, readReceiptThreadId = readReceiptThreadId)
|
||||
setReadMarkersTask.execute(params)
|
||||
}
|
||||
|
||||
@ -68,7 +82,8 @@ internal class DefaultReadService @AssistedInject constructor(
|
||||
}
|
||||
|
||||
override fun isEventRead(eventId: String): Boolean {
|
||||
return isEventRead(monarchy.realmConfiguration, userId, roomId, eventId)
|
||||
val shouldCheckIfReadInEventsThread = homeServerCapabilitiesService.getHomeServerCapabilities().canUseThreadReadReceiptsAndNotifications
|
||||
return isEventRead(monarchy.realmConfiguration, userId, roomId, eventId, shouldCheckIfReadInEventsThread)
|
||||
}
|
||||
|
||||
override fun getReadMarkerLive(): LiveData<Optional<String>> {
|
||||
@ -94,10 +109,11 @@ internal class DefaultReadService @AssistedInject constructor(
|
||||
override fun getUserReadReceipt(userId: String): String? {
|
||||
var eventId: String? = null
|
||||
monarchy.doWithRealm {
|
||||
eventId = ReadReceiptEntity.where(it, roomId = roomId, userId = userId)
|
||||
eventId = ReadReceiptEntity.forMainTimelineWhere(it, roomId = roomId, userId = userId)
|
||||
.findFirst()
|
||||
?.eventId
|
||||
}
|
||||
|
||||
return eventId
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright 2020 The Matrix.org Foundation C.I.C.
|
||||
*
|
||||
* 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 org.matrix.android.sdk.internal.session.room.read
|
||||
|
||||
import com.squareup.moshi.Json
|
||||
import com.squareup.moshi.JsonClass
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
internal data class ReadBody(
|
||||
@Json(name = "thread_id") val threadId: String?,
|
||||
)
|
||||
@ -19,6 +19,7 @@ package org.matrix.android.sdk.internal.session.room.read
|
||||
import com.zhuinden.monarchy.Monarchy
|
||||
import io.realm.Realm
|
||||
import org.matrix.android.sdk.api.session.events.model.LocalEcho
|
||||
import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilitiesService
|
||||
import org.matrix.android.sdk.internal.database.model.RoomSummaryEntity
|
||||
import org.matrix.android.sdk.internal.database.model.TimelineEventEntity
|
||||
import org.matrix.android.sdk.internal.database.query.isEventRead
|
||||
@ -45,8 +46,9 @@ internal interface SetReadMarkersTask : Task<SetReadMarkersTask.Params, Unit> {
|
||||
val roomId: String,
|
||||
val fullyReadEventId: String? = null,
|
||||
val readReceiptEventId: String? = null,
|
||||
val readReceiptThreadId: String? = null,
|
||||
val forceReadReceipt: Boolean = false,
|
||||
val forceReadMarker: Boolean = false
|
||||
val forceReadMarker: Boolean = false,
|
||||
)
|
||||
}
|
||||
|
||||
@ -61,12 +63,14 @@ internal class DefaultSetReadMarkersTask @Inject constructor(
|
||||
@UserId private val userId: String,
|
||||
private val globalErrorReceiver: GlobalErrorReceiver,
|
||||
private val clock: Clock,
|
||||
private val homeServerCapabilitiesService: HomeServerCapabilitiesService,
|
||||
) : SetReadMarkersTask {
|
||||
|
||||
override suspend fun execute(params: SetReadMarkersTask.Params) {
|
||||
val markers = mutableMapOf<String, String>()
|
||||
Timber.v("Execute set read marker with params: $params")
|
||||
val latestSyncedEventId = latestSyncedEventId(params.roomId)
|
||||
val readReceiptThreadId = params.readReceiptThreadId
|
||||
val fullyReadEventId = if (params.forceReadMarker) {
|
||||
latestSyncedEventId
|
||||
} else {
|
||||
@ -77,6 +81,7 @@ internal class DefaultSetReadMarkersTask @Inject constructor(
|
||||
} else {
|
||||
params.readReceiptEventId
|
||||
}
|
||||
|
||||
if (fullyReadEventId != null && !isReadMarkerMoreRecent(monarchy.realmConfiguration, params.roomId, fullyReadEventId)) {
|
||||
if (LocalEcho.isLocalEchoId(fullyReadEventId)) {
|
||||
Timber.w("Can't set read marker for local event $fullyReadEventId")
|
||||
@ -84,18 +89,23 @@ internal class DefaultSetReadMarkersTask @Inject constructor(
|
||||
markers[READ_MARKER] = fullyReadEventId
|
||||
}
|
||||
}
|
||||
|
||||
val shouldCheckIfReadInEventsThread = readReceiptThreadId != null &&
|
||||
homeServerCapabilitiesService.getHomeServerCapabilities().canUseThreadReadReceiptsAndNotifications
|
||||
|
||||
if (readReceiptEventId != null &&
|
||||
!isEventRead(monarchy.realmConfiguration, userId, params.roomId, readReceiptEventId)) {
|
||||
!isEventRead(monarchy.realmConfiguration, userId, params.roomId, readReceiptEventId, shouldCheckIfReadInEventsThread)) {
|
||||
if (LocalEcho.isLocalEchoId(readReceiptEventId)) {
|
||||
Timber.w("Can't set read receipt for local event $readReceiptEventId")
|
||||
} else {
|
||||
markers[READ_RECEIPT] = readReceiptEventId
|
||||
}
|
||||
} else {
|
||||
}
|
||||
|
||||
val shouldUpdateRoomSummary = readReceiptEventId != null && readReceiptEventId == latestSyncedEventId
|
||||
if (markers.isNotEmpty() || shouldUpdateRoomSummary) {
|
||||
updateDatabase(params.roomId, markers, shouldUpdateRoomSummary)
|
||||
updateDatabase(params.roomId, readReceiptThreadId, markers, shouldUpdateRoomSummary)
|
||||
} else {
|
||||
}
|
||||
if (markers.isNotEmpty()) {
|
||||
executeRequest(
|
||||
@ -104,7 +114,8 @@ internal class DefaultSetReadMarkersTask @Inject constructor(
|
||||
) {
|
||||
if (markers[READ_MARKER] == null) {
|
||||
if (readReceiptEventId != null) {
|
||||
roomAPI.sendReceipt(params.roomId, READ_RECEIPT, readReceiptEventId)
|
||||
val readBody = ReadBody(threadId = params.readReceiptThreadId)
|
||||
roomAPI.sendReceipt(params.roomId, READ_RECEIPT, readReceiptEventId, readBody)
|
||||
}
|
||||
} else {
|
||||
// "m.fully_read" value is mandatory to make this call
|
||||
@ -119,7 +130,7 @@ internal class DefaultSetReadMarkersTask @Inject constructor(
|
||||
TimelineEventEntity.latestEvent(realm, roomId = roomId, includesSending = false)?.eventId
|
||||
}
|
||||
|
||||
private suspend fun updateDatabase(roomId: String, markers: Map<String, String>, shouldUpdateRoomSummary: Boolean) {
|
||||
private suspend fun updateDatabase(roomId: String, readReceiptThreadId: String?, markers: Map<String, String>, shouldUpdateRoomSummary: Boolean) {
|
||||
monarchy.awaitTransaction { realm ->
|
||||
val readMarkerId = markers[READ_MARKER]
|
||||
val readReceiptId = markers[READ_RECEIPT]
|
||||
@ -127,7 +138,7 @@ internal class DefaultSetReadMarkersTask @Inject constructor(
|
||||
roomFullyReadHandler.handle(realm, roomId, FullyReadContent(readMarkerId))
|
||||
}
|
||||
if (readReceiptId != null) {
|
||||
val readReceiptContent = ReadReceiptHandler.createContent(userId, readReceiptId, clock.epochMillis())
|
||||
val readReceiptContent = ReadReceiptHandler.createContent(userId, readReceiptId, readReceiptThreadId, clock.epochMillis())
|
||||
readReceiptHandler.handle(realm, roomId, readReceiptContent, false, null)
|
||||
}
|
||||
if (shouldUpdateRoomSummary) {
|
||||
|
||||
@ -24,6 +24,7 @@ import org.matrix.android.sdk.api.extensions.tryOrNull
|
||||
import org.matrix.android.sdk.api.session.events.model.EventType
|
||||
import org.matrix.android.sdk.api.session.events.model.content.EncryptionEventContent
|
||||
import org.matrix.android.sdk.api.session.events.model.toModel
|
||||
import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilitiesService
|
||||
import org.matrix.android.sdk.api.session.room.accountdata.RoomAccountDataTypes
|
||||
import org.matrix.android.sdk.api.session.room.model.Membership
|
||||
import org.matrix.android.sdk.api.session.room.model.PowerLevelsContent
|
||||
@ -75,7 +76,8 @@ internal class RoomSummaryUpdater @Inject constructor(
|
||||
private val roomAvatarResolver: RoomAvatarResolver,
|
||||
private val eventDecryptor: EventDecryptor,
|
||||
private val crossSigningService: DefaultCrossSigningService,
|
||||
private val roomAccountDataDataSource: RoomAccountDataDataSource
|
||||
private val roomAccountDataDataSource: RoomAccountDataDataSource,
|
||||
private val homeServerCapabilitiesService: HomeServerCapabilitiesService,
|
||||
) {
|
||||
|
||||
fun refreshLatestPreviewContent(realm: Realm, roomId: String) {
|
||||
@ -151,9 +153,11 @@ internal class RoomSummaryUpdater @Inject constructor(
|
||||
latestPreviewableEvent.attemptToDecrypt()
|
||||
}
|
||||
|
||||
val shouldCheckIfReadInEventsThread = homeServerCapabilitiesService.getHomeServerCapabilities().canUseThreadReadReceiptsAndNotifications
|
||||
|
||||
roomSummaryEntity.hasUnreadMessages = roomSummaryEntity.notificationCount > 0 ||
|
||||
// avoid this call if we are sure there are unread events
|
||||
latestPreviewableEvent?.let { !isEventRead(realm.configuration, userId, roomId, it.eventId) } ?: false
|
||||
latestPreviewableEvent?.let { !isEventRead(realm.configuration, userId, roomId, it.eventId, shouldCheckIfReadInEventsThread) } ?: false
|
||||
|
||||
roomSummaryEntity.setDisplayName(roomDisplayNameResolver.resolve(realm, roomId))
|
||||
roomSummaryEntity.avatarUrl = roomAvatarResolver.resolve(realm, roomId)
|
||||
|
||||
@ -411,7 +411,6 @@ internal class DefaultTimeline(
|
||||
private fun ensureReadReceiptAreLoaded(realm: Realm) {
|
||||
readReceiptHandler.getContentFromInitSync(roomId)
|
||||
?.also {
|
||||
Timber.w("INIT_SYNC Insert when opening timeline RR for room $roomId")
|
||||
}
|
||||
?.let { readReceiptContent ->
|
||||
realm.executeTransactionAsync {
|
||||
|
||||
@ -33,10 +33,11 @@ import javax.inject.Inject
|
||||
// value : dict key $UserId
|
||||
// value dict key ts
|
||||
// dict value ts value
|
||||
internal typealias ReadReceiptContent = Map<String, Map<String, Map<String, Map<String, Double>>>>
|
||||
internal typealias ReadReceiptContent = Map<String, Map<String, Map<String, Map<String, Any>>>>
|
||||
|
||||
private const val READ_KEY = "m.read"
|
||||
private const val TIMESTAMP_KEY = "ts"
|
||||
private const val THREAD_ID_KEY = "thread_id"
|
||||
|
||||
internal class ReadReceiptHandler @Inject constructor(
|
||||
private val roomSyncEphemeralTemporaryStore: RoomSyncEphemeralTemporaryStore
|
||||
@ -47,14 +48,19 @@ internal class ReadReceiptHandler @Inject constructor(
|
||||
fun createContent(
|
||||
userId: String,
|
||||
eventId: String,
|
||||
threadId: String?,
|
||||
currentTimeMillis: Long
|
||||
): ReadReceiptContent {
|
||||
val userReadReceipt = mutableMapOf<String, Any>(
|
||||
TIMESTAMP_KEY to currentTimeMillis.toDouble(),
|
||||
)
|
||||
threadId?.let {
|
||||
userReadReceipt.put(THREAD_ID_KEY, threadId)
|
||||
}
|
||||
return mapOf(
|
||||
eventId to mapOf(
|
||||
READ_KEY to mapOf(
|
||||
userId to mapOf(
|
||||
TIMESTAMP_KEY to currentTimeMillis.toDouble()
|
||||
)
|
||||
userId to userReadReceipt
|
||||
)
|
||||
)
|
||||
)
|
||||
@ -98,8 +104,9 @@ internal class ReadReceiptHandler @Inject constructor(
|
||||
val readReceiptsSummary = ReadReceiptsSummaryEntity(eventId = eventId, roomId = roomId)
|
||||
|
||||
for ((userId, paramsDict) in userIdsDict) {
|
||||
val ts = paramsDict[TIMESTAMP_KEY] ?: 0.0
|
||||
val receiptEntity = ReadReceiptEntity.createUnmanaged(roomId, eventId, userId, ts)
|
||||
val ts = paramsDict[TIMESTAMP_KEY] as? Double ?: 0.0
|
||||
val threadId = paramsDict[THREAD_ID_KEY] as String?
|
||||
val receiptEntity = ReadReceiptEntity.createUnmanaged(roomId, eventId, userId, threadId, ts)
|
||||
readReceiptsSummary.readReceipts.add(receiptEntity)
|
||||
}
|
||||
readReceiptSummaries.add(readReceiptsSummary)
|
||||
@ -115,7 +122,6 @@ internal class ReadReceiptHandler @Inject constructor(
|
||||
) {
|
||||
// First check if we have data from init sync to handle
|
||||
getContentFromInitSync(roomId)?.let {
|
||||
Timber.w("INIT_SYNC Insert during incremental sync RR for room $roomId")
|
||||
doIncrementalSyncStrategy(realm, roomId, it)
|
||||
aggregator?.ephemeralFilesToDelete?.add(roomId)
|
||||
}
|
||||
@ -132,8 +138,9 @@ internal class ReadReceiptHandler @Inject constructor(
|
||||
}
|
||||
|
||||
for ((userId, paramsDict) in userIdsDict) {
|
||||
val ts = paramsDict[TIMESTAMP_KEY] ?: 0.0
|
||||
val receiptEntity = ReadReceiptEntity.getOrCreate(realm, roomId, userId)
|
||||
val ts = paramsDict[TIMESTAMP_KEY] as? Double ?: 0.0
|
||||
val threadId = paramsDict[THREAD_ID_KEY] as String?
|
||||
val receiptEntity = ReadReceiptEntity.getOrCreate(realm, roomId, userId, threadId)
|
||||
// ensure new ts is superior to the previous one
|
||||
if (ts > receiptEntity.originServerTs) {
|
||||
ReadReceiptsSummaryEntity.where(realm, receiptEntity.eventId).findFirst()?.also {
|
||||
|
||||
@ -216,7 +216,7 @@ class TimelineViewModel @AssistedInject constructor(
|
||||
observePowerLevel()
|
||||
setupPreviewUrlObservers()
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
tryOrNull { room.readService().markAsRead(ReadService.MarkAsReadParams.READ_RECEIPT) }
|
||||
tryOrNull { room.readService().markAsRead(ReadService.MarkAsReadParams.READ_RECEIPT, mainTimeLineOnly = true) }
|
||||
}
|
||||
// Inform the SDK that the room is displayed
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
@ -1096,7 +1096,8 @@ class TimelineViewModel @AssistedInject constructor(
|
||||
}
|
||||
bufferedMostRecentDisplayedEvent.root.eventId?.let { eventId ->
|
||||
session.coroutineScope.launch {
|
||||
tryOrNull { room.readService().setReadReceipt(eventId) }
|
||||
val threadId = initialState.rootThreadEventId ?: ReadService.THREAD_ID_MAIN
|
||||
tryOrNull { room.readService().setReadReceipt(eventId, threadId = threadId) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1114,7 +1115,7 @@ class TimelineViewModel @AssistedInject constructor(
|
||||
if (room == null) return
|
||||
setState { copy(unreadState = UnreadState.HasNoUnread) }
|
||||
viewModelScope.launch {
|
||||
tryOrNull { room.readService().markAsRead(ReadService.MarkAsReadParams.BOTH) }
|
||||
tryOrNull { room.readService().markAsRead(ReadService.MarkAsReadParams.BOTH, mainTimeLineOnly = true) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -72,6 +72,7 @@ import org.matrix.android.sdk.api.session.room.model.RoomSummary
|
||||
import org.matrix.android.sdk.api.session.room.model.message.MessageAudioContent
|
||||
import org.matrix.android.sdk.api.session.room.model.message.MessageImageInfoContent
|
||||
import org.matrix.android.sdk.api.session.room.model.message.MessageVideoContent
|
||||
import org.matrix.android.sdk.api.session.room.read.ReadService
|
||||
import org.matrix.android.sdk.api.session.room.timeline.Timeline
|
||||
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
|
||||
import timber.log.Timber
|
||||
@ -505,8 +506,7 @@ class TimelineEventController @Inject constructor(
|
||||
readReceiptsItem = readReceiptsItemFactory.create(
|
||||
event.eventId,
|
||||
readReceipts,
|
||||
callback,
|
||||
partialState.isFromThreadTimeline()
|
||||
callback
|
||||
),
|
||||
formattedDayModel = formattedDayModel,
|
||||
mergedHeaderModel = mergedHeaderModel
|
||||
@ -549,7 +549,7 @@ class TimelineEventController @Inject constructor(
|
||||
val event = itr.previous()
|
||||
timelineEventsGroups.addOrIgnore(event)
|
||||
val currentReadReceipts = ArrayList(event.readReceipts).filter {
|
||||
it.roomMember.userId != session.myUserId
|
||||
it.roomMember.userId != session.myUserId && it.isVisibleInThisThread()
|
||||
}
|
||||
if (timelineEventVisibilityHelper.shouldShowEvent(
|
||||
timelineEvent = event,
|
||||
@ -567,6 +567,14 @@ class TimelineEventController @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private fun ReadReceipt.isVisibleInThisThread(): Boolean {
|
||||
return if (partialState.isFromThreadTimeline()) {
|
||||
this.threadId == partialState.rootThreadEventId
|
||||
} else {
|
||||
this.threadId == null || this.threadId == ReadService.THREAD_ID_MAIN
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildDaySeparatorItem(originServerTs: Long?): DaySeparatorItem {
|
||||
val formattedDay = dateFormatter.format(originServerTs, DateFormatKind.TIMELINE_DAY_DIVIDER)
|
||||
return DaySeparatorItem_().formattedDay(formattedDay).id(formattedDay)
|
||||
|
||||
@ -30,7 +30,6 @@ class ReadReceiptsItemFactory @Inject constructor(private val avatarRenderer: Av
|
||||
eventId: String,
|
||||
readReceipts: List<ReadReceipt>,
|
||||
callback: TimelineEventController.Callback?,
|
||||
isFromThreadTimeLine: Boolean
|
||||
): ReadReceiptsItem? {
|
||||
if (readReceipts.isEmpty()) {
|
||||
return null
|
||||
@ -45,7 +44,7 @@ class ReadReceiptsItemFactory @Inject constructor(private val avatarRenderer: Av
|
||||
.eventId(eventId)
|
||||
.readReceipts(readReceiptsData)
|
||||
.avatarRenderer(avatarRenderer)
|
||||
.shouldHideReadReceipts(isFromThreadTimeLine)
|
||||
.shouldHideReadReceipts(false)
|
||||
.clickListener {
|
||||
callback?.onReadReceiptsClicked(readReceiptsData)
|
||||
}
|
||||
|
||||
@ -109,7 +109,8 @@ class NotificationBroadcastReceiver : BroadcastReceiver() {
|
||||
val room = session.getRoom(roomId)
|
||||
if (room != null) {
|
||||
session.coroutineScope.launch {
|
||||
tryOrNull { room.readService().markAsRead(ReadService.MarkAsReadParams.READ_RECEIPT) }
|
||||
// TODO: main or null
|
||||
tryOrNull { room.readService().markAsRead(ReadService.MarkAsReadParams.READ_RECEIPT, mainTimeLineOnly = false) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user