From 410956705100530be1cb9d67e5e9d973b8e5601e Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Wed, 18 Nov 2026 10:24:42 +0100 Subject: [PATCH] fix(chat): show the first message of an empty conversation A conversation without any messages, such as a new classified one where all messages expired, has no chat block after the initial load. With the high-performance backend the first message then never showed: the signaling message had no block to extend, and the insurance requests were skipped because no HTTP-synced anchor existed yet. An empty answer to the initial newest-messages fetch now anchors the insurance requests at 0. A signaling message that arrives while no chat block exists is fetched via HTTP, which creates the block. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marcel Hibbe --- .../chat/data/network/ChatMessageSyncer.kt | 19 ++++++++ .../network/OfflineFirstChatRepository.kt | 13 ++++- .../data/network/ChatMessageSyncerTest.kt | 47 +++++++++++++++++++ .../network/OfflineFirstChatRepositoryTest.kt | 37 +++++++++++++++ 4 files changed, 114 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/chat/data/network/ChatMessageSyncer.kt b/app/src/main/java/com/nextcloud/talk/chat/data/network/ChatMessageSyncer.kt index 7490a665af2..cdff0811290 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/data/network/ChatMessageSyncer.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/data/network/ChatMessageSyncer.kt @@ -808,6 +808,8 @@ class ChatMessageSyncer @Inject constructor( // the server confirmed there is nothing newer than the queried message, so // the queried message is a valid HTTP-synced anchor recordHttpSyncedMessageId(target, queriedMessageId.toLong()) + } else { + recordAnchorIfConversationEmpty(target, queriedMessageId, lookIntoFuture) } NOTHING_SYNCED } @@ -893,11 +895,28 @@ class ChatMessageSyncer @Inject constructor( // an empty response to a lookIntoFuture request confirms the queried message as // the newest one, so it is a valid HTTP-synced anchor recordHttpSyncedMessageId(target, queriedMessageId.toLong()) + } else { + recordAnchorIfConversationEmpty(target, queriedMessageId, lookIntoFuture) } NOTHING_SYNCED } } + /** + * A newest-messages fetch (no lastKnownMessageId, not looking into the future) that returns + * nothing confirms the conversation has no messages at all — e.g. a classified conversation, + * which starts without the usual "conversation created" system message. No chat block is + * created in that case, so without an anchor the insurance requests would skip forever and a + * first message would never become visible (with HPB there is no long polling to fetch it). + * Anchoring at 0 lets them fetch from the very beginning, which is cheap for an empty room. + */ + private fun recordAnchorIfConversationEmpty(target: SyncTarget, queriedMessageId: Int?, lookIntoFuture: Boolean) { + if (!lookIntoFuture && queriedMessageId == null) { + Log.d(TAG, "Conversation ${target.internalConversationId} has no messages, anchoring HTTP sync at 0") + recordHttpSyncedMessageId(target, 0L) + } + } + @Suppress("LongParameterList") private suspend fun updateMessagesData( target: SyncTarget, diff --git a/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt b/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt index 10c8111aeee..d88426cfdb3 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt @@ -1098,13 +1098,22 @@ class OfflineFirstChatRepository @Inject constructor( return } + // Without any chat block (e.g. the first message of an empty conversation) there is nothing + // the signaling messages could extend, and messages outside of a chat block are not shown. + // Fetch them via HTTP instead, which creates the chat block. + val latestChatBlock = chatBlocksDao.getLatestChatBlock(internalConversationId, threadId).first() + if (latestChatBlock == null) { + Log.d(TAG, "onSignalingChatMessageReceived without chat block, fetching new messages") + fetchNewMessages() + return + } + persistChatMessagesAndHandleSystemMessages(chatMessages, emitOnIncoming = true) // we assume that the signaling messages are on top of the latest chatblock and include them inside it. // If for whatever reason the assumption was not correct and there would be messages in between, the // insurance request should fix this by adding the missing messages and updating the chatblocks. - val latestChatBlock = chatBlocksDao.getLatestChatBlock(internalConversationId, threadId) - latestChatBlock.first()?.apply { + latestChatBlock.apply { newestMessageId = chatMessages.maxOf { it.id } chatBlocksDao.upsertChatBlock(this) } diff --git a/app/src/test/java/com/nextcloud/talk/chat/data/network/ChatMessageSyncerTest.kt b/app/src/test/java/com/nextcloud/talk/chat/data/network/ChatMessageSyncerTest.kt index 1a55e8ce7ad..612f8da0d89 100644 --- a/app/src/test/java/com/nextcloud/talk/chat/data/network/ChatMessageSyncerTest.kt +++ b/app/src/test/java/com/nextcloud/talk/chat/data/network/ChatMessageSyncerTest.kt @@ -667,6 +667,53 @@ class ChatMessageSyncerTest { assertEquals(42L, syncer.lastHttpSyncedMessageId(INTERNAL_CONVERSATION_ID, null)) } + @Test + fun `an empty newest-messages response anchors the insurance requests at 0`() = + runTest { + wheneverBlocking { network.pullChatMessages(any(), any(), any()) } + .thenReturn(Response.success(overall())) + + syncer.initialCatchUp(target()) + + assertEquals(0L, syncer.lastHttpSyncedMessageId(INTERNAL_CONVERSATION_ID, null)) + } + + @Test + fun `a not modified newest-messages response anchors the insurance requests at 0`() = + runTest { + val rawResponse = okhttp3.Response.Builder() + .request(Request.Builder().url(CHAT_URL).build()) + .protocol(Protocol.HTTP_1_1) + .code(HTTP_NOT_MODIFIED) + .message("Not Modified") + .build() + wheneverBlocking { network.pullChatMessages(any(), any(), any()) } + .thenReturn(Response.error("".toResponseBody(), rawResponse)) + + syncer.initialCatchUp(target()) + + assertEquals(0L, syncer.lastHttpSyncedMessageId(INTERNAL_CONVERSATION_ID, null)) + } + + @Test + fun `an empty response for older history does not anchor the insurance requests`() = + runTest { + whenever(chatBlocksDao.getChatBlocksContainingMessageId(INTERNAL_CONVERSATION_ID, null, 42L)) + .thenReturn(flowOf(emptyList())) + wheneverBlocking { network.pullChatMessages(any(), any(), any()) } + .thenReturn(Response.success(overall())) + + val fieldMap = syncer.buildFieldMap( + lookIntoFuture = false, + timeout = 0, + includeLastKnown = false, + lastKnown = 42 + ) + syncer.pullAndPersistMessages(target(), fieldMap) + + assertNull(syncer.lastHttpSyncedMessageId(INTERNAL_CONVERSATION_ID, null)) + } + @Test fun `pullUntilVisibleMessage returns after the first round when it already has a visible message`() = runTest { diff --git a/app/src/test/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepositoryTest.kt b/app/src/test/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepositoryTest.kt index 93ccfbff719..93ac0552b5b 100644 --- a/app/src/test/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepositoryTest.kt +++ b/app/src/test/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepositoryTest.kt @@ -501,6 +501,43 @@ class OfflineFirstChatRepositoryTest { verifyBlocking(network, never()) { pullChatMessages(any(), any(), any()) } } + @Test + fun `the first message of an empty conversation is fetched into a chat block when it arrives via signaling`() = + runTest { + // an empty conversation (e.g. a classified one): the initial load finds no messages, so + // no chat block exists that a signaling message could extend + givenLatestBlock(null) + repository.updateConversation(conversation(lastReadMessage = 0, unreadMessages = 0)) + wheneverBlocking { network.pullChatMessages(any(), any(), any()) } + .thenReturn(Response.success(overall()), Response.success(overall(message(1)))) + repository.loadInitialMessages(Bundle()) + + repository.onSignalingChatMessageReceived(listOf(message(1))) + + val fieldMapCaptor = argumentCaptor>() + verifyBlocking(network, times(2)) { pullChatMessages(any(), any(), fieldMapCaptor.capture()) } + assertEquals(1, fieldMapCaptor.secondValue["lookIntoFuture"]) + assertEquals(0, fieldMapCaptor.secondValue["lastKnownMessageId"]) + + val blockCaptor = argumentCaptor() + verifyBlocking(chatBlocksDao) { upsertAndMergeConnectedChatBlocks(blockCaptor.capture()) } + assertEquals(1L, blockCaptor.firstValue.oldestMessageId) + assertEquals(1L, blockCaptor.firstValue.newestMessageId) + } + + @Test + fun `a signaling message extends the latest chat block without a request`() = + runTest { + givenLatestBlock(block(oldest = 10, newest = 50)) + + repository.onSignalingChatMessageReceived(listOf(message(51))) + + verifyBlocking(network, never()) { pullChatMessages(any(), any(), any()) } + val blockCaptor = argumentCaptor() + verifyBlocking(chatBlocksDao) { upsertChatBlock(blockCaptor.capture()) } + assertEquals(51L, blockCaptor.firstValue.newestMessageId) + } + @Test fun `fetchNewMessages keeps notifications only while the chat is paused`() = runTest {