From c3264ea581cd19c3a5bf13f75be1565955baaa89 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Wed, 22 Jan 2025 18:08:35 +0530 Subject: [PATCH] [ECO-5193][TM*] Fixed Message.Operation.write method for msgpack, updated ChatRoom public methods --- lib/src/main/java/io/ably/lib/types/Message.java | 16 +++++++++++----- lib/src/test/java/io/ably/lib/chat/ChatRoom.java | 14 ++++++++------ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/src/main/java/io/ably/lib/types/Message.java b/lib/src/main/java/io/ably/lib/types/Message.java index 463a361bb..afdea4bc4 100644 --- a/lib/src/main/java/io/ably/lib/types/Message.java +++ b/lib/src/main/java/io/ably/lib/types/Message.java @@ -98,19 +98,25 @@ public static class Operation { public Map metadata; void write(MessagePacker packer) throws IOException { - packer.packMapHeader(3); - if(clientId != null) { + int fieldCount = 0; + if (clientId != null) fieldCount++; + if (description != null) fieldCount++; + if (metadata != null) fieldCount++; + + packer.packMapHeader(fieldCount); + + if (clientId != null) { packer.packString("clientId"); packer.packString(clientId); } - if(description != null) { + if (description != null) { packer.packString("description"); packer.packString(description); } - if(metadata != null) { + if (metadata != null) { packer.packString("metadata"); packer.packMapHeader(metadata.size()); - for(Map.Entry entry : metadata.entrySet()) { + for (Map.Entry entry : metadata.entrySet()) { packer.packString(entry.getKey()); packer.packString(entry.getValue()); } diff --git a/lib/src/test/java/io/ably/lib/chat/ChatRoom.java b/lib/src/test/java/io/ably/lib/chat/ChatRoom.java index 316c21098..5c784a9c9 100644 --- a/lib/src/test/java/io/ably/lib/chat/ChatRoom.java +++ b/lib/src/test/java/io/ably/lib/chat/ChatRoom.java @@ -7,6 +7,7 @@ import io.ably.lib.http.HttpUtils; import io.ably.lib.rest.AblyRest; import io.ably.lib.types.AblyException; +import io.ably.lib.types.ErrorInfo; import io.ably.lib.types.HttpPaginatedResponse; import io.ably.lib.types.Param; @@ -17,6 +18,7 @@ public class ChatRoom { private final AblyRest ablyRest; private final String roomId; + private final Gson gson = new Gson(); protected ChatRoom(String roomId, AblyRest ablyRest) { this.roomId = roomId; @@ -24,18 +26,18 @@ protected ChatRoom(String roomId, AblyRest ablyRest) { } public JsonElement sendMessage(SendMessageParams params) throws Exception { - return makeAuthorizedRequest("/chat/v2/rooms/" + roomId + "/messages", "POST", new Gson().toJsonTree(params)) - .orElseThrow(() -> new Exception("Failed to send message")); + return makeAuthorizedRequest("/chat/v2/rooms/" + roomId + "/messages", "POST", gson.toJsonTree(params)) + .orElseThrow(() -> AblyException.fromErrorInfo(new ErrorInfo("Failed to send message", 500))); } public JsonElement updateMessage(String serial, UpdateMessageParams params) throws Exception { - return makeAuthorizedRequest("/chat/v2/rooms/" + roomId + "/messages/" + serial, "PUT", new Gson().toJsonTree(params)) - .orElseThrow(() -> new Exception("Failed to update message")); + return makeAuthorizedRequest("/chat/v2/rooms/" + roomId + "/messages/" + serial, "PUT", gson.toJsonTree(params)) + .orElseThrow(() -> AblyException.fromErrorInfo(new ErrorInfo("Failed to update message", 500))); } public JsonElement deleteMessage(String serial, DeleteMessageParams params) throws Exception { - return makeAuthorizedRequest("/chat/v2/rooms/" + roomId + "/messages/" + serial + "/delete", "POST", new Gson().toJsonTree(params)) - .orElseThrow(() -> new Exception("Failed to delete message")); + return makeAuthorizedRequest("/chat/v2/rooms/" + roomId + "/messages/" + serial + "/delete", "POST", gson.toJsonTree(params)) + .orElseThrow(() -> AblyException.fromErrorInfo(new ErrorInfo("Failed to delete message", 500))); } public static class SendMessageParams {