|
| 1 | +package io.ably.lib.chat; |
| 2 | + |
| 3 | +import com.google.gson.JsonElement; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import io.ably.lib.rest.AblyRest; |
| 6 | + |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +public class ChatRoom extends ChatAPIClient{ |
| 10 | + private final String roomId; |
| 11 | + |
| 12 | + private ChatRoom(AblyRest ablyRest, String roomId) { |
| 13 | + super(ablyRest); |
| 14 | + this.roomId = roomId; |
| 15 | + } |
| 16 | + |
| 17 | + public JsonElement sendMessage(SendMessageParams params) throws Exception { |
| 18 | + return makeAuthorizedRequest("/chat/v2/rooms/" + roomId + "/messages", "POST", params.toJsonObject()) |
| 19 | + .orElseThrow(() -> new Exception("Failed to send message")); |
| 20 | + } |
| 21 | + |
| 22 | + public JsonElement updateMessage(String serial, UpdateMessageParams params) throws Exception { |
| 23 | + return makeAuthorizedRequest("/chat/v2/rooms/" + roomId + "/messages/" + serial, "PUT", params.toJsonObject()) |
| 24 | + .orElseThrow(() -> new Exception("Failed to update message")); |
| 25 | + } |
| 26 | + |
| 27 | + public JsonElement deleteMessage(String serial, DeleteMessageParams params) throws Exception { |
| 28 | + return makeAuthorizedRequest("/chat/v2/rooms/" + roomId + "/messages/" + serial + "/delete", "POST", params.toJsonObject()) |
| 29 | + .orElseThrow(() -> new Exception("Failed to delete message")); |
| 30 | + } |
| 31 | + |
| 32 | + public static class SendMessageParams { |
| 33 | + private final String text; |
| 34 | + private final JsonElement metadata; |
| 35 | + private final Map<String, String> headers; |
| 36 | + |
| 37 | + public SendMessageParams(String text, JsonElement metadata, Map<String, String> headers) { |
| 38 | + this.text = text; |
| 39 | + this.metadata = metadata; |
| 40 | + this.headers = headers; |
| 41 | + } |
| 42 | + |
| 43 | + private JsonObject toJsonObject() { |
| 44 | + JsonObject jsonObject = new JsonObject(); |
| 45 | + jsonObject.addProperty("text", text); |
| 46 | + // (CHA-M3b) |
| 47 | + if (headers != null) { |
| 48 | + JsonObject headersJson = new JsonObject(); |
| 49 | + headers.forEach(headersJson::addProperty); |
| 50 | + jsonObject.add("headers", headersJson); |
| 51 | + } |
| 52 | + // (CHA-M3b) |
| 53 | + if (metadata != null) { |
| 54 | + jsonObject.add("metadata", metadata); |
| 55 | + } |
| 56 | + return jsonObject; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public static class UpdateMessageParams { |
| 61 | + private final SendMessageParams message; |
| 62 | + private final String description; |
| 63 | + private final JsonElement metadata; |
| 64 | + |
| 65 | + public UpdateMessageParams(SendMessageParams message, String description, JsonElement metadata) { |
| 66 | + this.message = message; |
| 67 | + this.description = description; |
| 68 | + this.metadata = metadata; |
| 69 | + } |
| 70 | + |
| 71 | + private JsonObject toJsonObject() { |
| 72 | + JsonObject jsonObject = new JsonObject(); |
| 73 | + jsonObject.add("message", message.toJsonObject()); |
| 74 | + if (description != null) { |
| 75 | + jsonObject.addProperty("description", description); |
| 76 | + } |
| 77 | + if (metadata != null) { |
| 78 | + jsonObject.add("metadata", metadata); |
| 79 | + } |
| 80 | + return jsonObject; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public static class DeleteMessageParams { |
| 85 | + private final String description; |
| 86 | + private final JsonElement metadata; |
| 87 | + |
| 88 | + public DeleteMessageParams(String description, JsonElement metadata) { |
| 89 | + this.description = description; |
| 90 | + this.metadata = metadata; |
| 91 | + } |
| 92 | + |
| 93 | + private JsonObject toJsonObject() { |
| 94 | + JsonObject jsonObject = new JsonObject(); |
| 95 | + if (description != null) { |
| 96 | + jsonObject.addProperty("description", description); |
| 97 | + } |
| 98 | + if (metadata != null) { |
| 99 | + jsonObject.add("metadata", metadata); |
| 100 | + } |
| 101 | + return jsonObject; |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments