From 95545c66359c445171291ebd81fa5248d41803e4 Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Mon, 14 Apr 2025 14:49:15 +0200 Subject: [PATCH] Avoid Npe in sendChatMessage as chatDao.getTempMessageForConversation could have returned null Signed-off-by: Marcel Hibbe --- .../data/network/OfflineFirstChatRepository.kt | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) 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 84f740e64..1fc1f9235 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 @@ -42,6 +42,7 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.map import kotlinx.coroutines.isActive @@ -847,13 +848,17 @@ class OfflineFirstChatRepository @Inject constructor( .catch { e -> Log.e(TAG, "Error when sending message", e) - val failedMessage = chatDao.getTempMessageForConversation(internalConversationId, referenceId).first() - failedMessage.sendingFailed = true - chatDao.updateChatMessage(failedMessage) - - val failedMessageModel = failedMessage.asModel() - _updateMessageFlow.emit(failedMessageModel) + val failedMessage = chatDao.getTempMessageForConversation( + internalConversationId, + referenceId + ).firstOrNull() + failedMessage?.let { + it.sendingFailed = true + chatDao.updateChatMessage(it) + val failedMessageModel = it.asModel() + _updateMessageFlow.emit(failedMessageModel) + } emit(Result.failure(e)) } }