Skip to content

Commit

Permalink
[ECO-5193][TM*] Fixed Message.Operation.write method for msgpack, upd…
Browse files Browse the repository at this point in the history
…ated ChatRoom public methods
  • Loading branch information
sacOO7 committed Jan 22, 2025
1 parent 4610d4d commit c3264ea
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
16 changes: 11 additions & 5 deletions lib/src/main/java/io/ably/lib/types/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,25 @@ public static class Operation {
public Map<String, String> 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<String, String> entry : metadata.entrySet()) {
for (Map.Entry<String, String> entry : metadata.entrySet()) {
packer.packString(entry.getKey());
packer.packString(entry.getValue());
}
Expand Down
14 changes: 8 additions & 6 deletions lib/src/test/java/io/ably/lib/chat/ChatRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,25 +18,26 @@
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;
this.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 {
Expand Down

0 comments on commit c3264ea

Please sign in to comment.