fix to handle chats without offline messages when connection is lost

avoid NPE:

java.lang.NullPointerException
	at com.nextcloud.talk.chat.data.network.OfflineFirstChatRepository.getCappedMessagesAmountOfChatBlock(OfflineFirstChatRepository.kt:186)
	at com.nextcloud.talk.chat.data.network.OfflineFirstChatRepository.access$getCappedMessagesAmountOfChatBlock(OfflineFirstChatRepository.kt:43)
	at com.nextcloud.talk.chat.data.network.OfflineFirstChatRepository$loadInitialMessages$1.invokeSuspend(OfflineFirstChatRepository.kt:162)

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2024-10-21 15:33:25 +02:00
parent 7e3a4e4a83
commit f817c20b4e
No known key found for this signature in database
GPG Key ID: C793F8B59F43CE7B

View File

@ -159,20 +159,22 @@ class OfflineFirstChatRepository @Inject constructor(
Log.d(TAG, "newestMessageIdFromDb after sync: $newestMessageIdFromDb") Log.d(TAG, "newestMessageIdFromDb after sync: $newestMessageIdFromDb")
} }
val limit = getCappedMessagesAmountOfChatBlock(newestMessageIdFromDb) if (newestMessageIdFromDb.toInt() != 0) {
val limit = getCappedMessagesAmountOfChatBlock(newestMessageIdFromDb)
showMessagesBeforeAndEqual( showMessagesBeforeAndEqual(
internalConversationId, internalConversationId,
newestMessageIdFromDb, newestMessageIdFromDb,
limit limit
) )
// delay is a dirty workaround to make sure messages are added to adapter on initial load before dealing // delay is a dirty workaround to make sure messages are added to adapter on initial load before dealing
// with them (otherwise there is a race condition). // with them (otherwise there is a race condition).
delay(DELAY_TO_ENSURE_MESSAGES_ARE_ADDED) delay(DELAY_TO_ENSURE_MESSAGES_ARE_ADDED)
updateUiForLastCommonRead() updateUiForLastCommonRead()
updateUiForLastReadMessage(newestMessageIdFromDb) updateUiForLastReadMessage(newestMessageIdFromDb)
}
initMessagePolling(newestMessageIdFromDb) initMessagePolling(newestMessageIdFromDb)
} }
@ -180,20 +182,25 @@ class OfflineFirstChatRepository @Inject constructor(
private suspend fun getCappedMessagesAmountOfChatBlock(messageId: Long): Int { private suspend fun getCappedMessagesAmountOfChatBlock(messageId: Long): Int {
val chatBlock = getBlockOfMessage(messageId.toInt()) val chatBlock = getBlockOfMessage(messageId.toInt())
val amountBetween = chatDao.getCountBetweenMessageIds( if (chatBlock != null) {
internalConversationId, val amountBetween = chatDao.getCountBetweenMessageIds(
messageId, internalConversationId,
chatBlock!!.oldestMessageId messageId,
) chatBlock.oldestMessageId
)
Log.d(TAG, "amount of messages between newestMessageId and oldest message of same ChatBlock:$amountBetween") Log.d(TAG, "amount of messages between newestMessageId and oldest message of same ChatBlock:$amountBetween")
val limit = if (amountBetween > DEFAULT_MESSAGES_LIMIT) { val limit = if (amountBetween > DEFAULT_MESSAGES_LIMIT) {
DEFAULT_MESSAGES_LIMIT DEFAULT_MESSAGES_LIMIT
} else {
amountBetween
}
Log.d(TAG, "limit of messages to load for UI (max 100 to ensure performance is okay):$limit")
return limit
} else { } else {
amountBetween Log.e(TAG, "No chat block found. Returning 0 as limit.")
return 0
} }
Log.d(TAG, "limit of messages to load for UI (max 100 to ensure performance is okay):$limit")
return limit
} }
private suspend fun updateUiForLastReadMessage(newestMessageId: Long) { private suspend fun updateUiForLastReadMessage(newestMessageId: Long) {