Skip to content

Commit f4d4177

Browse files
committed
refactor(chat): rename closeBacklog to tryCloseBacklog
closeBacklog can fall back to fetching the newest messages instead of actually closing the gap once MAX_BACKLOG_ROUNDS is exceeded, leaving the remaining range genuinely open for later. The name promised full closure with no such caveat. Rename it and its OfflineFirstChatRepository wrapper (closeBacklogFromNewestOfflineMessage) to make the best-effort nature explicit, and update call sites, comments, and test names to match. Assisted-by: Claude Sonnet 5 <noreply@anthropic.com> Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
1 parent e3ba086 commit f4d4177

3 files changed

Lines changed: 15 additions & 14 deletions

File tree

app/src/main/java/com/nextcloud/talk/chat/data/network/ChatMessageSyncer.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ class ChatMessageSyncer @Inject constructor(
308308
chatBlocksDao.getNewestMessageIdFromChatBlocks(target.internalConversationId, target.threadId)
309309

310310
val outcome = if (newestMessageIdFromDb > 0) {
311-
closeBacklog(
311+
tryCloseBacklog(
312312
target = target,
313313
fromMessageId = newestMessageIdFromDb,
314314
limit = limit,
@@ -344,7 +344,8 @@ class ChatMessageSyncer @Inject constructor(
344344
}
345345

346346
/**
347-
* Fetches the messages newer than [fromMessageId] until the backlog is fully closed.
347+
* Tries to fetch the messages newer than [fromMessageId] until the backlog is fully closed —
348+
* there is no guarantee it will be: see the [MAX_BACKLOG_ROUNDS] fallback below.
348349
*
349350
* A single fetch is capped by [limit], so one request only narrows a backlog larger than
350351
* that — and on chat-relay servers the remaining gap would become permanent as soon as a
@@ -355,8 +356,8 @@ class ChatMessageSyncer @Inject constructor(
355356
* stays visible in the block structure (closable by scrolling up) rather than a block
356357
* claiming ranges that were never fetched.
357358
*/
358-
@Suppress("LongParameterList")
359-
suspend fun closeBacklog(
359+
@Suppress("LongParameterList", "LongMethod")
360+
suspend fun tryCloseBacklog(
360361
target: SyncTarget,
361362
fromMessageId: Long,
362363
limit: Int = DEFAULT_MESSAGES_LIMIT,

app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class OfflineFirstChatRepository @Inject constructor(
193193
Log.d(TAG, "weLikelyOnlyHaveASmallBacklog:$weLikelyOnlyHaveASmallBacklog")
194194

195195
if (weLikelyOnlyHaveASmallBacklog) {
196-
closeBacklogFromNewestOfflineMessage(newestMessageIdFromDb)
196+
tryCloseBacklogFromNewestOfflineMessage(newestMessageIdFromDb)
197197
} else {
198198
fetchNewestMessagesForInitialLoad(
199199
withNetworkParams,
@@ -206,10 +206,10 @@ class OfflineFirstChatRepository @Inject constructor(
206206
/**
207207
* Tries to close the backlog since the newest offline message.
208208
*/
209-
private suspend fun closeBacklogFromNewestOfflineMessage(newestMessageIdFromDb: Long) {
210-
Log.d(TAG, "Closing the backlog from the newest offline message for initial loading")
209+
private suspend fun tryCloseBacklogFromNewestOfflineMessage(newestMessageIdFromDb: Long) {
210+
Log.d(TAG, "Try to close the backlog from the newest offline message for initial loading")
211211

212-
syncer.closeBacklog(
212+
syncer.tryCloseBacklog(
213213
target = syncTarget,
214214
fromMessageId = newestMessageIdFromDb,
215215
lastCommonRead = newXChatLastCommonRead,
@@ -338,10 +338,10 @@ class OfflineFirstChatRepository @Inject constructor(
338338

339339
val lastHttpSyncedMessageId = syncer.lastHttpSyncedMessageId(internalConversationId, threadId) ?: 0L
340340

341-
// closeBacklog loops until the backlog is fully closed, so a backlog larger than the
341+
// tryCloseBacklog loops until the backlog is fully closed, so a backlog larger than the
342342
// request limit is caught up within one insurance cycle instead of narrowing it by one
343343
// page every cycle.
344-
val outcome = syncer.closeBacklog(
344+
val outcome = syncer.tryCloseBacklog(
345345
target = syncTarget,
346346
fromMessageId = lastHttpSyncedMessageId,
347347
limit = 200,

app/src/test/java/com/nextcloud/talk/chat/data/network/ChatMessageSyncerTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ class ChatMessageSyncerTest {
293293
}
294294

295295
@Test
296-
fun `closeBacklog loops until the server returns fewer messages than the limit`() =
296+
fun `tryCloseBacklog loops until the server returns fewer messages than the limit`() =
297297
runTest {
298298
val existingBlock = block(oldest = 10, newest = 42)
299299
whenever(chatBlocksDao.getChatBlocksContainingMessageId(eq(INTERNAL_CONVERSATION_ID), eq(null), any()))
@@ -306,7 +306,7 @@ class ChatMessageSyncerTest {
306306
Response.success(overall(message(45)))
307307
)
308308

309-
val outcome = syncer.closeBacklog(target(), fromMessageId = 42, limit = 2)
309+
val outcome = syncer.tryCloseBacklog(target(), fromMessageId = 42, limit = 2)
310310

311311
assertTrue(outcome.persistedNewMessages)
312312
assertEquals(3, outcome.persistedMessageCount)
@@ -320,7 +320,7 @@ class ChatMessageSyncerTest {
320320
}
321321

322322
@Test
323-
fun `closeBacklog falls back to the newest messages when the backlog persists`() =
323+
fun `tryCloseBacklog falls back to the newest messages when the backlog persists`() =
324324
runTest {
325325
val existingBlock = block(oldest = 10, newest = 42)
326326
whenever(chatBlocksDao.getChatBlocksContainingMessageId(eq(INTERNAL_CONVERSATION_ID), eq(null), any()))
@@ -337,7 +337,7 @@ class ChatMessageSyncerTest {
337337
Response.success(overall(message(100)))
338338
)
339339

340-
val outcome = syncer.closeBacklog(target(), fromMessageId = 42, limit = 1)
340+
val outcome = syncer.tryCloseBacklog(target(), fromMessageId = 42, limit = 1)
341341

342342
// the reported range is the fallback's own — it must not be merged with the backlog
343343
// rounds' range, since the fallback lands in a separate, disconnected chat block

0 commit comments

Comments
 (0)