Skip to content

Commit

Permalink
Changing sendMessageReceipt return type to be Result<Boolean>
Browse files Browse the repository at this point in the history
  • Loading branch information
mliao95 committed Oct 11, 2024
1 parent 4f639d5 commit 568db6b
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions chat-sdk/src/main/java/com/amazon/connect/chat/sdk/ChatSession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean>

var onConnectionEstablished: (() -> Unit)?
var onConnectionReEstablished: (() -> Unit)?
Expand Down Expand Up @@ -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<Boolean> {
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<Boolean>(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"))
}
}
}

Expand Down

0 comments on commit 568db6b

Please sign in to comment.