Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<HashMap<String, Int>>()
verifyBlocking(network, times(2)) { pullChatMessages(any(), any(), fieldMapCaptor.capture()) }
assertEquals(1, fieldMapCaptor.secondValue["lookIntoFuture"])
assertEquals(0, fieldMapCaptor.secondValue["lastKnownMessageId"])

val blockCaptor = argumentCaptor<ChatBlockEntity>()
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<ChatBlockEntity>()
verifyBlocking(chatBlocksDao) { upsertChatBlock(blockCaptor.capture()) }
assertEquals(51L, blockCaptor.firstValue.newestMessageId)
}

@Test
fun `fetchNewMessages keeps notifications only while the chat is paused`() =
runTest {
Expand Down
Loading