Skip to content

Commit

Permalink
[ECO-5196] Updated Message.kt
Browse files Browse the repository at this point in the history
1. Marked message headers as non-nullable to enforce library level defaults
2. Removed redudunt copy method, refactored update method signature
3. Removed filesuppress for notImplemented methods across files
  • Loading branch information
sacOO7 committed Feb 25, 2025
1 parent fbac777 commit 99a0fa2
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 37 deletions.
6 changes: 3 additions & 3 deletions chat-android/src/main/java/com/ably/chat/ChatApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal class ChatApi(
text = messageJsonObject.requireString(MessageProperty.Text),
createdAt = messageJsonObject.requireLong(MessageProperty.CreatedAt),
metadata = messageJsonObject.getAsJsonObject(MessageProperty.Metadata),
headers = messageJsonObject.get(MessageProperty.Headers)?.toMap(),
headers = messageJsonObject.get(MessageProperty.Headers)?.toMap() ?: mapOf(),
action = action,
version = messageJsonObject.requireString(MessageProperty.Version),
timestamp = messageJsonObject.requireLong(MessageProperty.Timestamp),
Expand Down Expand Up @@ -75,7 +75,7 @@ internal class ChatApi(
text = params.text,
createdAt = createdAt,
metadata = params.metadata ?: MessageMetadata(),
headers = params.headers,
headers = params.headers ?: mapOf(),
action = MessageAction.MESSAGE_CREATE,
version = serial,
timestamp = createdAt,
Expand Down Expand Up @@ -105,7 +105,7 @@ internal class ChatApi(
text = params.message.text,
createdAt = message.createdAt,
metadata = params.message.metadata ?: MessageMetadata(),
headers = params.message.headers,
headers = params.message.headers ?: mapOf(),
action = MessageAction.MESSAGE_UPDATE,
version = version,
timestamp = timestamp,
Expand Down
2 changes: 0 additions & 2 deletions chat-android/src/main/java/com/ably/chat/ChatClient.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:Suppress("NotImplementedDeclaration")

package com.ably.chat

import io.ably.lib.realtime.AblyRealtime
Expand Down
17 changes: 1 addition & 16 deletions chat-android/src/main/java/com/ably/chat/Message.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ data class Message(
* Do not use the headers for authoritative information. There is no server-side
* validation. When reading the headers treat them like user input.
*/
val headers: MessageHeaders?,
val headers: MessageHeaders,

/**
* The latest action of the message. This can be used to determine if the message was created, updated, or deleted.
Expand All @@ -97,21 +97,6 @@ data class Message(
val operation: Message.Operation? = null,
)

fun com.ably.chat.Message.copy(text: String, metadata: MessageMetadata? = null, headers: MessageHeaders? = null): com.ably.chat.Message =
Message(
serial = this.serial,
clientId = this.clientId,
roomId = this.roomId,
text = text,
createdAt = this.createdAt,
metadata = metadata ?: this.metadata,
headers = headers ?: this.headers,
action = this.action,
version = this.version,
timestamp = this.timestamp,
operation = this.operation,
)

internal fun buildMessageOperation(jsonObject: JsonObject?): Message.Operation? {
if (jsonObject == null) {
return null
Expand Down
14 changes: 6 additions & 8 deletions chat-android/src/main/java/com/ably/chat/Messages.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:Suppress("StringLiteralDuplication")

package com.ably.chat

import com.ably.chat.OrderBy.NewestFirst
Expand Down Expand Up @@ -78,12 +76,12 @@ interface Messages : EmitsDiscontinuities {
* The original message is not modified.
* Spec: CHA-M8
*
* @param messageCopy The updated copy of the message created using the `message.copy` method.
* @param updatedMessage The updated copy of the message created using the `message.copy` method.
* @param operationDescription Optional description for the update action.
* @param operationMetadata Optional metadata for the update action.
* @returns updated message.
*/
suspend fun update(messageCopy: Message, operationDescription: String? = null, operationMetadata: OperationMetadata? = null): Message
suspend fun update(updatedMessage: Message, operationDescription: String? = null, operationMetadata: OperationMetadata? = null): Message

/**
* Delete a message in the chat room.
Expand Down Expand Up @@ -364,7 +362,7 @@ internal class DefaultMessages(
serial = pubSubMessage.serial,
text = data.text,
metadata = data.metadata,
headers = pubSubMessage.extras.asJsonObject().get("headers")?.toMap(),
headers = pubSubMessage.extras.asJsonObject().get("headers")?.toMap() ?: mapOf(),
action = pubSubMessage.action,
version = pubSubMessage.version,
timestamp = pubSubMessage.timestamp,
Expand Down Expand Up @@ -407,13 +405,13 @@ internal class DefaultMessages(
)

override suspend fun update(
messageCopy: Message,
updatedMessage: Message,
operationDescription: String?,
operationMetadata: OperationMetadata?,
): Message = chatApi.updateMessage(
messageCopy,
updatedMessage,
UpdateMessageParams(
message = SendMessageParams(messageCopy.text, messageCopy.metadata, messageCopy.headers),
message = SendMessageParams(updatedMessage.text, updatedMessage.metadata, updatedMessage.headers),
description = operationDescription,
metadata = operationMetadata,
),
Expand Down
2 changes: 1 addition & 1 deletion chat-android/src/main/java/com/ably/chat/Occupancy.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@file:Suppress("StringLiteralDuplication", "NotImplementedDeclaration")
@file:Suppress("StringLiteralDuplication")

package com.ably.chat

Expand Down
2 changes: 1 addition & 1 deletion chat-android/src/main/java/com/ably/chat/Presence.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@file:Suppress("StringLiteralDuplication", "NotImplementedDeclaration")
@file:Suppress("StringLiteralDuplication")

package com.ably.chat

Expand Down
2 changes: 1 addition & 1 deletion chat-android/src/main/java/com/ably/chat/RoomReactions.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@file:Suppress("StringLiteralDuplication", "NotImplementedDeclaration")
@file:Suppress("StringLiteralDuplication")

package com.ably.chat

Expand Down
2 changes: 1 addition & 1 deletion chat-android/src/main/java/com/ably/chat/Typing.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@file:Suppress("StringLiteralDuplication", "NotImplementedDeclaration")
@file:Suppress("StringLiteralDuplication")

package com.ably.chat

Expand Down
4 changes: 2 additions & 2 deletions chat-android/src/test/java/com/ably/chat/ChatApiTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ChatApiTest {
text = "hello",
createdAt = 1_000_000L,
metadata = MessageMetadata(),
headers = null,
headers = mapOf(),
action = MessageAction.MESSAGE_CREATE,
version = "timeserial",
timestamp = 1_000_000L,
Expand Down Expand Up @@ -106,7 +106,7 @@ class ChatApiTest {
clientId = "clientId",
text = "hello",
createdAt = 1_000_000L,
headers = null,
headers = mapOf(),
metadata = MessageMetadata(),
action = MessageAction.MESSAGE_CREATE,
version = "timeserial",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.ably.chat.MessageMetadata
import com.ably.chat.RoomOptions
import com.ably.chat.RoomStatus
import com.ably.chat.assertWaiter
import com.ably.chat.copy
import io.ably.lib.types.MessageAction
import java.util.UUID
import kotlinx.coroutines.CompletableDeferred
Expand Down Expand Up @@ -168,7 +167,7 @@ class MessagesIntegrationTest {
val opDescription = "Updating message"
val opMetadata = mapOf("operation" to "update")

val messageCopy = sentMessage.copy(updatedText, updatedMetadata, headers)
val messageCopy = sentMessage.copy(text = updatedText, metadata = updatedMetadata, headers = headers)
val updatedMessage = room.messages.update(
messageCopy,
opDescription,
Expand Down

0 comments on commit 99a0fa2

Please sign in to comment.