File tree Expand file tree Collapse file tree
main/java/com/nextcloud/talk/chat/viewmodels
test/java/com/nextcloud/talk/chat/viewmodels Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2426,8 +2426,16 @@ class ChatViewModel @AssistedInject constructor(
24262426
24272427 private const val PLAUSIBLE_MESSAGE_ID_BUFFER = 10_000L
24282428
2429- fun isPlausibleLastReadMessageId (messageId : Int , newestKnownRealMessageId : Long? ): Boolean =
2430- newestKnownRealMessageId == null || messageId <= newestKnownRealMessageId + PLAUSIBLE_MESSAGE_ID_BUFFER
2429+ /* *
2430+ * A real server-assigned message id is always positive, so a null, zero or negative
2431+ * [newestKnownRealMessageId] is never a trustworthy ceiling to judge [messageId] against -
2432+ * e.g. a federated conversation's cached lastMessage can carry an id of 0 when it was
2433+ * never populated. Without a trustworthy ceiling, [messageId] is accepted unchecked here.
2434+ */
2435+ fun isPlausibleLastReadMessageId (messageId : Int , newestKnownRealMessageId : Long? ): Boolean {
2436+ val trustworthyCeiling = newestKnownRealMessageId?.takeIf { it > 0 } ? : return true
2437+ return messageId <= trustworthyCeiling + PLAUSIBLE_MESSAGE_ID_BUFFER
2438+ }
24312439 }
24322440
24332441 sealed class OutOfOfficeUIState {
Original file line number Diff line number Diff line change @@ -61,6 +61,13 @@ class ChatViewModelTest {
6161 )
6262 }
6363
64+ @Test
65+ fun `isPlausibleLastReadMessageId treats a newest known id of 0 as unknown` () {
66+ // A federated conversation's cached lastMessage can carry an id of 0 - a real message id
67+ // is never 0, so this must not be treated as a real ceiling near the start of the room.
68+ assertTrue(ChatViewModel .isPlausibleLastReadMessageId(messageId = 136556 , newestKnownRealMessageId = 0L ))
69+ }
70+
6471 // The unread marker latch: the marker position must only be derived from the visible window
6572 // when the window provably reaches back to the unread boundary — otherwise a window of
6673 // only-unread messages (e.g. after a capped fetch of the newest messages) would place the
You can’t perform that action at this time.
0 commit comments