Skip to content

Commit 3fcb1fb

Browse files
authored
Merge pull request #6513 from nextcloud/bugfix/noid/fixPlausibleLastReadMessageGuard
fix(chat): treat a newest known message id of 0 as unknown
2 parents 08c08a4 + f70125e commit 3fcb1fb

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

app/src/main/java/com/nextcloud/talk/chat/viewmodels/ChatViewModel.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff 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 {

app/src/test/java/com/nextcloud/talk/chat/viewmodels/ChatViewModelTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)