From 568db6bb259c208e56430b832dda91ed8a331de2 Mon Sep 17 00:00:00 2001 From: Michael Liao Date: Thu, 10 Oct 2024 18:53:49 -0700 Subject: [PATCH] Changing sendMessageReceipt return type to be Result --- .../amazon/connect/chat/sdk/ChatSession.kt | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/chat-sdk/src/main/java/com/amazon/connect/chat/sdk/ChatSession.kt b/chat-sdk/src/main/java/com/amazon/connect/chat/sdk/ChatSession.kt index ded86cb..781c979 100644 --- a/chat-sdk/src/main/java/com/amazon/connect/chat/sdk/ChatSession.kt +++ b/chat-sdk/src/main/java/com/amazon/connect/chat/sdk/ChatSession.kt @@ -99,7 +99,7 @@ interface ChatSession { * @param transcriptItem The transcript item. * @param receiptType The type of the receipt. */ - suspend fun sendMessageReceipt(transcriptItem: TranscriptItem, receiptType: MessageReceiptType) + suspend fun sendMessageReceipt(transcriptItem: TranscriptItem, receiptType: MessageReceiptType): Result var onConnectionEstablished: (() -> Unit)? var onConnectionReEstablished: (() -> Unit)? @@ -251,21 +251,26 @@ class ChatSessionImpl @Inject constructor(private val chatService: ChatService) } } - override suspend fun sendMessageReceipt(transcriptItem: TranscriptItem, receiptType: MessageReceiptType) { - withContext(Dispatchers.IO) { - val messageItem = transcriptItem as? Message + override suspend fun sendMessageReceipt(transcriptItem: TranscriptItem, receiptType: MessageReceiptType): Result { + return withContext(Dispatchers.IO) { + val messageItem = transcriptItem as? Message - // Check if the transcript item is a plain text message, is not empty, and is incoming - if (messageItem == null || messageItem.text.isEmpty() || messageItem.participant == "CUSTOMER") { - return@withContext - } + // Check if the transcript item is a plain text message, is not empty, and is incoming + if (messageItem == null || messageItem.text.isEmpty() || messageItem.participant == "CUSTOMER") { + return@withContext Result.failure(IllegalArgumentException("Invalid message item. Cannot send message receipts for outgoing or empty messages.")) + } - // Check if the item already has the read status when sending a read receipt - if (receiptType == MessageReceiptType.MESSAGE_READ && messageItem.metadata?.status == MessageStatus.Read) { - return@withContext - } + // Check if the item already has the read status when sending a read receipt + if (receiptType == MessageReceiptType.MESSAGE_READ && messageItem.metadata?.status == MessageStatus.Read) { + return@withContext Result.success(true) + } - sendReceipt(event = receiptType, messageId = messageItem.id) + val sendReceiptResult = sendReceipt(event = receiptType, messageId = messageItem.id) + if (sendReceiptResult.isSuccess) { + Result.success(true) + } else { + Result.failure(sendReceiptResult.exceptionOrNull() ?: IllegalStateException("sendReceipt call failed")) + } } }