read : use the new apis

This commit is contained in:
ganfra 2024-02-14 11:09:46 +01:00
parent 9100cc81ea
commit 7e0e0f9ec4
7 changed files with 45 additions and 36 deletions

View file

@ -159,10 +159,10 @@ class MessagesPresenter @AssistedInject constructor(
} }
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
// Mark the room as read on entering but don't send read receipts // Remove the unread flag on entering but don't send read receipts
// as those will be handled by the timeline. // as those will be handled by the timeline.
withContext(dispatchers.io) { withContext(dispatchers.io) {
room.markAsRead(null) room.setUnreadFlag(isUnread = false)
} }
} }

View file

@ -232,12 +232,17 @@ class TimelinePresenter @AssistedInject constructor(
lastReadReceiptId: MutableState<EventId?>, lastReadReceiptId: MutableState<EventId?>,
readReceiptType: ReceiptType, readReceiptType: ReceiptType,
) = launch(dispatchers.computation) { ) = launch(dispatchers.computation) {
// Get last valid EventId seen by the user, as the first index might refer to a Virtual item // If we are at the bottom of timeline, we mark the room as read.
val eventId = getLastEventIdBeforeOrAt(firstVisibleIndex, timelineItems) if (firstVisibleIndex == 0) {
if (eventId != null && firstVisibleIndex <= lastReadReceiptIndex.value && eventId != lastReadReceiptId.value) { room.markAsRead(receiptType = readReceiptType)
lastReadReceiptIndex.value = firstVisibleIndex } else {
lastReadReceiptId.value = eventId // Get last valid EventId seen by the user, as the first index might refer to a Virtual item
timeline.sendReadReceipt(eventId = eventId, receiptType = readReceiptType) val eventId = getLastEventIdBeforeOrAt(firstVisibleIndex, timelineItems)
if (eventId != null && firstVisibleIndex <= lastReadReceiptIndex.value && eventId != lastReadReceiptId.value) {
lastReadReceiptIndex.value = firstVisibleIndex
lastReadReceiptId.value = eventId
timeline.sendReadReceipt(eventId = eventId, receiptType = readReceiptType)
}
} }
} }

View file

@ -145,15 +145,20 @@ class RoomListPresenter @Inject constructor(
is RoomListEvents.HideContextMenu -> contextMenu = RoomListState.ContextMenu.Hidden is RoomListEvents.HideContextMenu -> contextMenu = RoomListState.ContextMenu.Hidden
is RoomListEvents.LeaveRoom -> leaveRoomState.eventSink(LeaveRoomEvent.ShowConfirmation(event.roomId)) is RoomListEvents.LeaveRoom -> leaveRoomState.eventSink(LeaveRoomEvent.ShowConfirmation(event.roomId))
is RoomListEvents.MarkAsRead -> coroutineScope.launch { is RoomListEvents.MarkAsRead -> coroutineScope.launch {
val receiptType = if (sessionPreferencesStore.isSendPublicReadReceiptsEnabled().first()) { client.getRoom(event.roomId)?.use { room ->
ReceiptType.READ room.setUnreadFlag(isUnread = false)
} else { val receiptType = if (sessionPreferencesStore.isSendPublicReadReceiptsEnabled().first()) {
ReceiptType.READ_PRIVATE ReceiptType.READ
} else {
ReceiptType.READ_PRIVATE
}
room.markAsRead(receiptType)
} }
client.getRoom(event.roomId)?.markAsRead(receiptType)
} }
is RoomListEvents.MarkAsUnread -> coroutineScope.launch { is RoomListEvents.MarkAsUnread -> coroutineScope.launch {
client.getRoom(event.roomId)?.markAsUnread() client.getRoom(event.roomId)?.use { room ->
room.setUnreadFlag(isUnread = true)
}
} }
} }
} }

View file

@ -487,18 +487,18 @@ class RoomListPresenterTests {
}.test { }.test {
val initialState = awaitItem() val initialState = awaitItem()
assertThat(room.markAsReadCalls).isEmpty() assertThat(room.markAsReadCalls).isEmpty()
assertThat(room.markAsUnreadReadCallCount).isEqualTo(0) assertThat(room.setUnreadFlagCalls).isEmpty()
initialState.eventSink.invoke(RoomListEvents.MarkAsRead(A_ROOM_ID)) initialState.eventSink.invoke(RoomListEvents.MarkAsRead(A_ROOM_ID))
assertThat(room.markAsReadCalls).isEqualTo(listOf(ReceiptType.READ)) assertThat(room.markAsReadCalls).isEqualTo(listOf(ReceiptType.READ))
assertThat(room.markAsUnreadReadCallCount).isEqualTo(0) assertThat(room.setUnreadFlagCalls).isEqualTo(listOf(false))
initialState.eventSink.invoke(RoomListEvents.MarkAsUnread(A_ROOM_ID)) initialState.eventSink.invoke(RoomListEvents.MarkAsUnread(A_ROOM_ID))
assertThat(room.markAsReadCalls).isEqualTo(listOf(ReceiptType.READ)) assertThat(room.markAsReadCalls).isEqualTo(listOf(ReceiptType.READ))
assertThat(room.markAsUnreadReadCallCount).isEqualTo(1) assertThat(room.setUnreadFlagCalls).isEqualTo(listOf(false, true))
// Test again with private read receipts // Test again with private read receipts
sessionPreferencesStore.setSendPublicReadReceipts(false) sessionPreferencesStore.setSendPublicReadReceipts(false)
initialState.eventSink.invoke(RoomListEvents.MarkAsRead(A_ROOM_ID)) initialState.eventSink.invoke(RoomListEvents.MarkAsRead(A_ROOM_ID))
assertThat(room.markAsReadCalls).isEqualTo(listOf(ReceiptType.READ, ReceiptType.READ_PRIVATE)) assertThat(room.markAsReadCalls).isEqualTo(listOf(ReceiptType.READ, ReceiptType.READ_PRIVATE))
assertThat(room.markAsUnreadReadCallCount).isEqualTo(1) assertThat(room.setUnreadFlagCalls).isEqualTo(listOf(false, true, false))
cancelAndIgnoreRemainingEvents() cancelAndIgnoreRemainingEvents()
scope.cancel() scope.cancel()
} }

View file

@ -153,15 +153,17 @@ interface MatrixRoom : Closeable {
suspend fun reportContent(eventId: EventId, reason: String, blockUserId: UserId?): Result<Unit> suspend fun reportContent(eventId: EventId, reason: String, blockUserId: UserId?): Result<Unit>
/** /**
* Reverts a previously set unread flag, and eventually send a Read Receipt. * Mark the room as read by trying to attach an unthreaded read receipt to the latest room event.
* @param receiptType The type of receipt to send. If null, no Read Receipt will be sent. * @param receiptType The type of receipt to send.
*/ */
suspend fun markAsRead(receiptType: ReceiptType?): Result<Unit> suspend fun markAsRead(receiptType: ReceiptType): Result<Unit>
/** /**
* Sets a flag on the room to indicate that the user has explicitly marked it as unread. * Sets a flag on the room to indicate that the user has explicitly marked it as unread, or reverts the flag.
* @param isUnread true to mark the room as unread, false to remove the flag.
*
*/ */
suspend fun markAsUnread(): Result<Unit> suspend fun setUnreadFlag(isUnread: Boolean): Result<Unit>
/** /**
* Share a location message in the room. * Share a location message in the room.

View file

@ -442,19 +442,15 @@ class RustMatrixRoom(
} }
} }
override suspend fun markAsRead(receiptType: ReceiptType?): Result<Unit> = withContext(roomDispatcher) { override suspend fun markAsRead(receiptType: ReceiptType): Result<Unit> = withContext(roomDispatcher) {
runCatching { runCatching {
if (receiptType != null) { innerRoom.markAsRead(receiptType.toRustReceiptType())
innerRoom.markAsReadAndSendReadReceipt(receiptType.toRustReceiptType())
} else {
innerRoom.markAsRead()
}
} }
} }
override suspend fun markAsUnread(): Result<Unit> = withContext(roomDispatcher) { override suspend fun setUnreadFlag(isUnread: Boolean): Result<Unit> = withContext(roomDispatcher) {
runCatching { runCatching {
innerRoom.markAsUnread() innerRoom.setUnreadFlag(isUnread)
} }
} }

View file

@ -378,17 +378,18 @@ class FakeMatrixRoom(
return reportContentResult return reportContentResult
} }
val markAsReadCalls = mutableListOf<ReceiptType?>() val markAsReadCalls = mutableListOf<ReceiptType>()
override suspend fun markAsRead(receiptType: ReceiptType?): Result<Unit> {
override suspend fun markAsRead(receiptType: ReceiptType): Result<Unit> {
markAsReadCalls.add(receiptType) markAsReadCalls.add(receiptType)
return Result.success(Unit) return Result.success(Unit)
} }
var markAsUnreadReadCallCount = 0 var setUnreadFlagCalls = mutableListOf<Boolean>()
private set private set
override suspend fun markAsUnread(): Result<Unit> { override suspend fun setUnreadFlag(isUnread: Boolean): Result<Unit> {
markAsUnreadReadCallCount++ setUnreadFlagCalls.add(isUnread)
return Result.success(Unit) return Result.success(Unit)
} }