From c214e5daf199cf22d8fc6665258229b26639e42b Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Sun, 22 May 2022 12:22:20 +0200 Subject: [PATCH] Avoid missing timestamps due to missing previewable event Just use a non-previewable event instead. Improves sorting of the room list: don't drop rooms to the bottom because of too much un-previewable activity. Change-Id: Ib1ad8050caae85a3e7c8a686c2fa63a2924db890 --- .../sdk/api/session/room/model/RoomSummary.kt | 1 + .../database/mapper/RoomSummaryMapper.kt | 1 + .../room/summary/RoomSummaryUpdater.kt | 23 +++++++++++++++---- .../home/room/list/RoomSummaryItemFactory.kt | 2 ++ 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomSummary.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomSummary.kt index e9b2f8cdcd..c07bbe6e00 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomSummary.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/RoomSummary.kt @@ -53,6 +53,7 @@ data class RoomSummary( val hasUnreadMessages: Boolean = false, val hasUnreadContentMessages: Boolean = false, val hasUnreadOriginalContentMessages: Boolean = false, + val lastActivityTime: Long? = null, val unreadCount: Int? = null, val markedUnread: Boolean = false, val aggregatedUnreadCount: Int = 0, diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/mapper/RoomSummaryMapper.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/mapper/RoomSummaryMapper.kt index 134d4c2dc8..48433e7174 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/mapper/RoomSummaryMapper.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/mapper/RoomSummaryMapper.kt @@ -73,6 +73,7 @@ internal class RoomSummaryMapper @Inject constructor(private val timelineEventMa hasUnreadMessages = roomSummaryEntity.hasUnreadMessages, hasUnreadContentMessages = roomSummaryEntity.hasUnreadContentMessages, hasUnreadOriginalContentMessages = roomSummaryEntity.hasUnreadOriginalContentMessages, + lastActivityTime = roomSummaryEntity.lastActivityTime, markedUnread = roomSummaryEntity.markedUnread, tags = tags, typingUsers = typingUsers, diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/summary/RoomSummaryUpdater.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/summary/RoomSummaryUpdater.kt index b32bf673dd..be46015037 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/summary/RoomSummaryUpdater.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/summary/RoomSummaryUpdater.kt @@ -56,6 +56,7 @@ import org.matrix.android.sdk.internal.database.query.findAllInRoomWithSendState import org.matrix.android.sdk.internal.database.query.getOrCreate import org.matrix.android.sdk.internal.database.query.getOrNull import org.matrix.android.sdk.internal.database.query.isEventRead +import org.matrix.android.sdk.internal.database.query.latestEvent import org.matrix.android.sdk.internal.database.query.where import org.matrix.android.sdk.internal.di.UserId import org.matrix.android.sdk.internal.extensions.clearWith @@ -153,12 +154,24 @@ internal class RoomSummaryUpdater @Inject constructor( val lastActivityFromEvent = scLatestPreviewableEvent?.root?.originServerTs if (lastActivityFromEvent != null) { roomSummaryEntity.lastActivityTime = lastActivityFromEvent - attemptToDecryptLatestPreviewables( - roomSummaryEntity.latestPreviewableEvent, - roomSummaryEntity.latestPreviewableContentEvent, - roomSummaryEntity.latestPreviewableOriginalContentEvent - ) + } else if (latestPreviewableEvent != scLatestPreviewableEvent) { + // Try using a less aggressive previewable filter for last activity, so we avoid null timestamps, which would just drop the room to the bottom + roomSummaryEntity.lastActivityTime = latestPreviewableEvent?.root?.originServerTs } + // If we still did not find a timestamp for the last activity: + // Any (non-previewable) event is still better for sorting than just dropping the room to the bottom in the list + if (roomSummaryEntity.lastActivityTime == null) { + roomSummaryEntity.lastActivityTime = TimelineEventEntity.latestEvent( + realm = realm, + roomId = roomId, + includesSending = true + )?.root?.originServerTs + } + attemptToDecryptLatestPreviewables( + roomSummaryEntity.latestPreviewableEvent, + roomSummaryEntity.latestPreviewableContentEvent, + roomSummaryEntity.latestPreviewableOriginalContentEvent + ) val roomSummaryUnreadCount = roomSummaryEntity.unreadCount if (roomSummaryUnreadCount != null /* && preferences.prioritizeUnreadCountsOverRoomPreviewsForUnreadCalculation() */) { diff --git a/vector/src/main/java/im/vector/app/features/home/room/list/RoomSummaryItemFactory.kt b/vector/src/main/java/im/vector/app/features/home/room/list/RoomSummaryItemFactory.kt index 5cd3cf156c..fee4a3b562 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/list/RoomSummaryItemFactory.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/list/RoomSummaryItemFactory.kt @@ -117,6 +117,8 @@ class RoomSummaryItemFactory @Inject constructor(private val displayableEventFor if (latestEvent != null) { latestFormattedEvent = displayableEventFormatter.format(latestEvent, roomSummary.isDirect, roomSummary.isDirect.not()) latestEventTime = dateFormatter.format(latestEvent.root.originServerTs, DateFormatKind.ROOM_LIST) + } else if (roomSummary.lastActivityTime != null) { + latestEventTime = dateFormatter.format(roomSummary.lastActivityTime, DateFormatKind.ROOM_LIST) } val typingMessage = typingHelper.getTypingMessage(roomSummary.typingUsers) return RoomSummaryItem_()