Skip to content

Commit

Permalink
Added rawData of websocket response to callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
mrajatttt committed Oct 4, 2024
1 parent dacb78b commit 286aa78
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ data class Event(
override var timeStamp: String,
override var contentType: String,
override var id: String,
override var serializedContent: Map<String, Any>? = null
override var serializedContent: String? = null
) : TranscriptItem(id, timeStamp, contentType, serializedContent), EventProtocol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ data class Message(
override var id: String,
override var timeStamp: String,
override var contentType: String,
override var serializedContent: Map<String, Any>? = null,
override var serializedContent: String? = null,
override var participant: String,
override var text: String,
override var displayName: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ data class MessageMetadata(
override var timeStamp: String,
override var contentType: String,
override var id: String,
override var serializedContent: Map<String, Any>? = null
override var serializedContent: String? = null
) : TranscriptItem(id, timeStamp, contentType, serializedContent), MessageMetadataProtocol
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ interface TranscriptItemProtocol {
val id: String
val timeStamp: String
var contentType: String
var serializedContent: Map<String, Any>?
var serializedContent: String?
}

open class TranscriptItem(
override var id: String,
override var timeStamp: String,
override var contentType: String,
override var serializedContent: Map<String, Any>? = null
override var serializedContent: String? = null
) : TranscriptItemProtocol {

internal fun updateId(newId: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.amazon.connect.chat.sdk.network
import android.util.Log
import com.amazon.connect.chat.sdk.model.MessageReceiptType
import com.amazon.connect.chat.sdk.model.MessageReceipts
import com.amazon.connect.chat.sdk.utils.logger.SDKLogger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
Expand Down Expand Up @@ -32,14 +33,12 @@ data class PendingMessageReceipts(
var readReceiptMessageId: String? = null
) {
fun clear() {
Log.d("PendingMessageReceipts", "Clearing pending message receipts.")
deliveredReceiptMessageId = null
readReceiptMessageId = null
}

fun checkAndRemoveDuplicateReceipt() {
if (deliveredReceiptMessageId == readReceiptMessageId) {
Log.d("PendingMessageReceipts", "Duplicate receipt found. Removing delivered receipt for messageId: $deliveredReceiptMessageId")
deliveredReceiptMessageId = null
}
}
Expand All @@ -64,10 +63,8 @@ class MessageReceiptsManagerImpl : MessageReceiptsManager {
messageId: String
): Result<PendingMessageReceipts> = suspendCancellableCoroutine { continuation ->

Log.d("MessageReceiptsManager", "Attempting to send message receipt for messageId: $messageId with event: $event")

if (!shouldSendMessageReceipts) {
Log.d("MessageReceiptsManager", "Sending message receipts is disabled.")
SDKLogger.logger.logDebug { "Sending message receipts is disabled." }
continuation.resume(Result.failure(Exception("Sending message receipts is disabled")))
return@suspendCancellableCoroutine
}
Expand All @@ -76,7 +73,6 @@ class MessageReceiptsManagerImpl : MessageReceiptsManager {

// Cancel the previous job if it's still active
throttleJob?.cancel()
Log.d("MessageReceiptsManager", "Cancelled existing throttle job.")

if (pendingMessageReceipts.readReceiptMessageId == null && pendingMessageReceipts.deliveredReceiptMessageId == null && numPendingDeliveredReceipts == 0) {
return@suspendCancellableCoroutine
Expand All @@ -86,54 +82,44 @@ class MessageReceiptsManagerImpl : MessageReceiptsManager {
throttleJob = CoroutineScope(Dispatchers.Default).launch {
delay((throttleTime * 1000).toLong())
try {
Log.d("MessageReceiptsManager", "Throttling with interval: ${throttleTime * 1000}ms")
pendingMessageReceipts.checkAndRemoveDuplicateReceipt()
Log.d("MessageReceiptsManager", "Resuming continuation with pending receipts.")
continuation.resume(Result.success(pendingMessageReceipts))
} catch (e: Exception) {
Log.e("MessageReceiptsManager", "Error during throttling: ${e.message}", e)
SDKLogger.logger.logError { "Error during throttling: ${e.message}" }
continuation.resumeWithException(e)
}
}
}

override fun invalidateTimer() {
Log.d("MessageReceiptsManager", "Invalidating timer.")
timer?.cancel()
timer = null
}

override fun handleMessageReceipt(event: MessageReceiptType, messageId: String) {
Log.d("MessageReceiptsManager", "Handling message receipt for messageId: $messageId with event: $event")

when (event) {
MessageReceiptType.MESSAGE_DELIVERED -> {
if (deliveredReceiptSet.contains(messageId) || readReceiptSet.contains(messageId)) {
Log.d("MessageReceiptsManager", "Receipt already handled for messageId: $messageId")
return
}
deliveredReceiptSet.add(messageId)
Log.d("MessageReceiptsManager", "Added messageId: $messageId to deliveredReceiptSet")

CoroutineScope(Dispatchers.Default).launch {
Log.d("MessageReceiptsManager", "Scheduling delivery throttle for messageId: $messageId with interval: ${deliveredThrottleTime * 1000}ms")
numPendingDeliveredReceipts++
delay((deliveredThrottleTime * 1000).toLong())
if (readReceiptSet.contains(messageId)) {
Log.d("MessageReceiptsManager", "Read receipt already sent for messageId: $messageId")
SDKLogger.logger.logDebug { "Read receipt already sent for messageId: $messageId" }
} else {
Log.d("MessageReceiptsManager", "Setting delivered receipt to pending for messageId: $messageId")
pendingMessageReceipts.deliveredReceiptMessageId = messageId
}
numPendingDeliveredReceipts--
}
}
MessageReceiptType.MESSAGE_READ -> {
if (readReceiptSet.contains(messageId)) {
Log.d("MessageReceiptsManager", "Read receipt already sent for messageId: $messageId")
return
}
Log.d("MessageReceiptsManager", "Adding messageId: $messageId to readReceiptSet")
readReceiptSet.add(messageId)
pendingMessageReceipts.readReceiptMessageId = messageId
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,22 +259,22 @@ class WebSocketManagerImpl @Inject constructor(
val type = WebSocketMessageType.fromType(typeString)

return when (type) {
WebSocketMessageType.MESSAGE -> handleMessage(jsonObject)
WebSocketMessageType.MESSAGE -> handleMessage(jsonObject, jsonString)
WebSocketMessageType.EVENT -> {
val eventTypeString = jsonObject.optString("ContentType")
when (val eventType = ContentType.fromType(eventTypeString)) {
ContentType.JOINED -> handleParticipantEvent(jsonObject)
ContentType.LEFT -> handleParticipantEvent(jsonObject)
ContentType.TYPING -> handleTyping(jsonObject)
ContentType.ENDED -> handleChatEnded(jsonObject)
ContentType.JOINED -> handleParticipantEvent(jsonObject, jsonString)
ContentType.LEFT -> handleParticipantEvent(jsonObject, jsonString)
ContentType.TYPING -> handleTyping(jsonObject, jsonString)
ContentType.ENDED -> handleChatEnded(jsonObject, jsonString)
else -> {
Log.w("WebSocket", "Unknown event: $eventType")
null
}
}
}
WebSocketMessageType.ATTACHMENT -> handleAttachment(jsonObject)
WebSocketMessageType.MESSAGE_METADATA -> handleMetadata(jsonObject)
WebSocketMessageType.ATTACHMENT -> handleAttachment(jsonObject, jsonString)
WebSocketMessageType.MESSAGE_METADATA -> handleMetadata(jsonObject, jsonString)
else -> {
Log.w("WebSocket", "Unknown websocket message type: $type")
null
Expand Down Expand Up @@ -354,7 +354,7 @@ class WebSocketManagerImpl @Inject constructor(

// --- Helper Methods for websocket data ---

private fun handleMessage(innerJson: JSONObject): TranscriptItem {
private fun handleMessage(innerJson: JSONObject, rawData: String): TranscriptItem {
val participantRole = innerJson.getString("ParticipantRole")
val messageId = innerJson.getString("Id")
val messageText = innerJson.getString("Content")
Expand All @@ -368,12 +368,13 @@ class WebSocketManagerImpl @Inject constructor(
contentType = innerJson.getString("ContentType"),
timeStamp = time,
id = messageId,
displayName = displayName
displayName = displayName,
serializedContent = rawData
)
return message
}

private fun handleParticipantEvent(innerJson: JSONObject): TranscriptItem {
private fun handleParticipantEvent(innerJson: JSONObject, rawData: String): TranscriptItem {
val participantRole = innerJson.getString("ParticipantRole")
val displayName = innerJson.getString("DisplayName")
val time = innerJson.getString("AbsoluteTime")
Expand All @@ -387,11 +388,12 @@ class WebSocketManagerImpl @Inject constructor(
text = innerJson.getString("ContentType"), // TODO: Need to be removed and replaced in UI once callbacks are hooked
contentType = innerJson.getString("ContentType"),
eventDirection = MessageDirection.COMMON,
serializedContent = rawData
)
return event
}

private fun handleTyping(innerJson: JSONObject): TranscriptItem {
private fun handleTyping(innerJson: JSONObject, rawData: String): TranscriptItem {
val participantRole = innerJson.getString("ParticipantRole")
val time = innerJson.getString("AbsoluteTime")
val displayName = innerJson.getString("DisplayName")
Expand All @@ -402,12 +404,13 @@ class WebSocketManagerImpl @Inject constructor(
contentType = innerJson.getString("ContentType"),
id = eventId,
displayName = displayName,
participant = participantRole
participant = participantRole,
serializedContent = rawData,
)
return event
}

private suspend fun handleChatEnded(innerJson: JSONObject): TranscriptItem {
private suspend fun handleChatEnded(innerJson: JSONObject, rawData: String): TranscriptItem {
closeWebSocket("Chat Ended");
isChatActive = false;
this._eventPublisher.emit(ChatEvent.ChatEnded)
Expand All @@ -417,12 +420,13 @@ class WebSocketManagerImpl @Inject constructor(
timeStamp = time,
contentType = innerJson.getString("ContentType"),
id = eventId,
eventDirection = MessageDirection.COMMON
eventDirection = MessageDirection.COMMON,
serializedContent = rawData
)
return event
}

private fun handleMetadata(innerJson: JSONObject): TranscriptItem {
private fun handleMetadata(innerJson: JSONObject, rawData: String): TranscriptItem {
val messageMetadata = innerJson.getJSONObject("MessageMetadata")
val messageId = messageMetadata.getString("MessageId")
val receipts = messageMetadata.optJSONArray("Receipts")
Expand All @@ -442,12 +446,13 @@ class WebSocketManagerImpl @Inject constructor(
eventDirection = MessageDirection.OUTGOING,
timeStamp = time,
id = messageId,
status = status
status = status,
serializedContent = rawData
)
return metadata
}

private fun handleAttachment(innerJson: JSONObject): TranscriptItem? {
private fun handleAttachment(innerJson: JSONObject, rawData: String): TranscriptItem? {
val participantRole = innerJson.getString("ParticipantRole")
val time = innerJson.getString("AbsoluteTime")
val displayName = innerJson.getString("DisplayName")
Expand Down Expand Up @@ -481,7 +486,8 @@ class WebSocketManagerImpl @Inject constructor(
timeStamp = time,
attachmentId = attachmentId,
id = messageId,
displayName = displayName
displayName = displayName,
serializedContent = rawData
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.amazon.connect.chat.sdk.model.Message
import com.amazon.connect.chat.sdk.model.MessageDirection
import com.amazon.connect.chat.sdk.model.MessageMetadata
import com.amazon.connect.chat.sdk.model.MessageStatus
import com.amazon.connect.chat.sdk.model.TranscriptItem
import com.amazonaws.services.connectparticipant.model.Item
import org.json.JSONObject
import java.util.UUID
Expand All @@ -20,11 +19,7 @@ object TranscriptItemUtils {
fun createDummyEndedEvent(): Event {
val isoTime = CommonUtils.getCurrentISOTime()

val serializedContent = mapOf(
"content" to "{\"AbsoluteTime\":\"$isoTime\",\"ContentType\":\"application/vnd.amazonaws.connect.event.chat.ended\",\"Id\":\"chat-ended-event\",\"Type\":\"EVENT\",\"InitialContactId\":\"chat-ended-event-id\"}",
"topic" to "aws/chat",
"contentType" to "application/json"
)
val serializedContent = "{\"AbsoluteTime\":\"$isoTime\",\"ContentType\":\"application/vnd.amazonaws.connect.event.chat.ended\",\"Id\":\"chat-ended-event\",\"Type\":\"EVENT\",\"InitialContactId\":\"chat-ended-event-id\"}"

return Event(
text = null,
Expand Down Expand Up @@ -54,14 +49,14 @@ object TranscriptItemUtils {
attachmentId = attachmentId,
id = randomId,
displayName = displayName,
serializedContent = emptyMap(),
serializedContent = "",
metadata = MessageMetadata(
id = randomId,
status = status,
timeStamp = isoTime,
contentType = contentType,
eventDirection = MessageDirection.OUTGOING,
serializedContent = emptyMap()
serializedContent = ""
)
)
}
Expand Down

0 comments on commit 286aa78

Please sign in to comment.