|
| 1 | +package io.getstream.chat.java.models; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.*; |
| 4 | +import io.getstream.chat.java.exceptions.StreamException; |
| 5 | +import io.getstream.chat.java.models.Message.Attachment; |
| 6 | +import io.getstream.chat.java.models.Message.MessageRequestObject; |
| 7 | +import io.getstream.chat.java.models.User.UserRequestObject; |
| 8 | +import io.getstream.chat.java.models.framework.StreamRequest; |
| 9 | +import io.getstream.chat.java.models.framework.StreamResponseObject; |
| 10 | +import io.getstream.chat.java.services.DraftService; |
| 11 | +import io.getstream.chat.java.services.framework.Client; |
| 12 | +import java.util.Date; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import lombok.*; |
| 16 | +import org.jetbrains.annotations.NotNull; |
| 17 | +import org.jetbrains.annotations.Nullable; |
| 18 | +import retrofit2.Call; |
| 19 | + |
| 20 | +/** Represents draft message functionality in Stream Chat. */ |
| 21 | +@Data |
| 22 | +public class Draft { |
| 23 | + /** A draft message. */ |
| 24 | + @Data |
| 25 | + @NoArgsConstructor |
| 26 | + public static class DraftMessage { |
| 27 | + @Nullable |
| 28 | + @JsonProperty("id") |
| 29 | + private String id; |
| 30 | + |
| 31 | + @Nullable |
| 32 | + @JsonProperty("text") |
| 33 | + private String text; |
| 34 | + |
| 35 | + @Nullable |
| 36 | + @JsonProperty("html") |
| 37 | + private String html; |
| 38 | + |
| 39 | + @Nullable |
| 40 | + @JsonProperty("mml") |
| 41 | + private String mml; |
| 42 | + |
| 43 | + @Nullable |
| 44 | + @JsonProperty("parent_id") |
| 45 | + private String parentId; |
| 46 | + |
| 47 | + @Nullable |
| 48 | + @JsonProperty("show_in_channel") |
| 49 | + private Boolean showInChannel; |
| 50 | + |
| 51 | + @Nullable |
| 52 | + @JsonProperty("attachments") |
| 53 | + private List<Attachment> attachments; |
| 54 | + |
| 55 | + @Nullable |
| 56 | + @JsonProperty("mentioned_users") |
| 57 | + private List<User> mentionedUsers; |
| 58 | + |
| 59 | + @Nullable |
| 60 | + @JsonProperty("custom") |
| 61 | + private Map<String, Object> custom; |
| 62 | + |
| 63 | + @Nullable |
| 64 | + @JsonProperty("quoted_message_id") |
| 65 | + private String quotedMessageId; |
| 66 | + |
| 67 | + @Nullable |
| 68 | + @JsonProperty("type") |
| 69 | + private String type; |
| 70 | + |
| 71 | + @Nullable |
| 72 | + @JsonProperty("silent") |
| 73 | + private Boolean silent; |
| 74 | + |
| 75 | + @Nullable |
| 76 | + @JsonProperty("poll_id") |
| 77 | + private String pollId; |
| 78 | + } |
| 79 | + |
| 80 | + /** A draft object containing the message and metadata. */ |
| 81 | + @Data |
| 82 | + @NoArgsConstructor |
| 83 | + public static class DraftObject { |
| 84 | + @NotNull |
| 85 | + @JsonProperty("channel_cid") |
| 86 | + private String channelCid; |
| 87 | + |
| 88 | + @NotNull |
| 89 | + @JsonProperty("created_at") |
| 90 | + private Date createdAt; |
| 91 | + |
| 92 | + @NotNull |
| 93 | + @JsonProperty("message") |
| 94 | + private DraftMessage message; |
| 95 | + |
| 96 | + @Nullable |
| 97 | + @JsonProperty("channel") |
| 98 | + private Channel channel; |
| 99 | + |
| 100 | + @Nullable |
| 101 | + @JsonProperty("parent_id") |
| 102 | + private String parentId; |
| 103 | + |
| 104 | + @Nullable |
| 105 | + @JsonProperty("parent_message") |
| 106 | + private Message parentMessage; |
| 107 | + |
| 108 | + @Nullable |
| 109 | + @JsonProperty("quoted_message") |
| 110 | + private Message quotedMessage; |
| 111 | + } |
| 112 | + |
| 113 | + /** Response for draft creation. */ |
| 114 | + @Data |
| 115 | + @NoArgsConstructor |
| 116 | + @EqualsAndHashCode(callSuper = true) |
| 117 | + public static class CreateDraftResponse extends StreamResponseObject { |
| 118 | + @NotNull |
| 119 | + @JsonProperty("draft") |
| 120 | + private DraftObject draft; |
| 121 | + } |
| 122 | + |
| 123 | + /** Response for getting a draft. */ |
| 124 | + @Data |
| 125 | + @NoArgsConstructor |
| 126 | + @EqualsAndHashCode(callSuper = true) |
| 127 | + public static class GetDraftResponse extends StreamResponseObject { |
| 128 | + @NotNull |
| 129 | + @JsonProperty("draft") |
| 130 | + private DraftObject draft; |
| 131 | + } |
| 132 | + |
| 133 | + /** Response for querying drafts. */ |
| 134 | + @Data |
| 135 | + @NoArgsConstructor |
| 136 | + @EqualsAndHashCode(callSuper = true) |
| 137 | + public static class QueryDraftsResponse extends StreamResponseObject { |
| 138 | + @NotNull |
| 139 | + @JsonProperty("drafts") |
| 140 | + private List<DraftObject> drafts; |
| 141 | + |
| 142 | + @Nullable |
| 143 | + @JsonProperty("next") |
| 144 | + private String next; |
| 145 | + } |
| 146 | + |
| 147 | + /** Request data for creating a draft. */ |
| 148 | + @Builder( |
| 149 | + builderClassName = "CreateDraftRequest", |
| 150 | + builderMethodName = "", |
| 151 | + buildMethodName = "internalBuild") |
| 152 | + public static class CreateDraftRequestData { |
| 153 | + @NotNull |
| 154 | + @JsonProperty("message") |
| 155 | + private MessageRequestObject message; |
| 156 | + |
| 157 | + @Nullable |
| 158 | + @JsonProperty("user_id") |
| 159 | + private String userId; |
| 160 | + |
| 161 | + @Nullable |
| 162 | + @JsonProperty("user") |
| 163 | + private UserRequestObject user; |
| 164 | + |
| 165 | + public static class CreateDraftRequest extends StreamRequest<CreateDraftResponse> { |
| 166 | + @NotNull private String channelType; |
| 167 | + @NotNull private String channelId; |
| 168 | + |
| 169 | + private CreateDraftRequest(@NotNull String channelType, @NotNull String channelId) { |
| 170 | + this.channelType = channelType; |
| 171 | + this.channelId = channelId; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + protected Call<CreateDraftResponse> generateCall(Client client) throws StreamException { |
| 176 | + return client |
| 177 | + .create(DraftService.class) |
| 178 | + .createDraft(this.channelType, this.channelId, this.internalBuild()); |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + /** Request for deleting a draft. */ |
| 184 | + public static class DeleteDraftRequest extends StreamRequest<StreamResponseObject> { |
| 185 | + @NotNull private String channelType; |
| 186 | + @NotNull private String channelId; |
| 187 | + @Nullable private String userId; |
| 188 | + @Nullable private String parentId; |
| 189 | + |
| 190 | + private DeleteDraftRequest(@NotNull String channelType, @NotNull String channelId) { |
| 191 | + this.channelType = channelType; |
| 192 | + this.channelId = channelId; |
| 193 | + } |
| 194 | + |
| 195 | + @NotNull |
| 196 | + public DeleteDraftRequest userId(@NotNull String userId) { |
| 197 | + this.userId = userId; |
| 198 | + return this; |
| 199 | + } |
| 200 | + |
| 201 | + @NotNull |
| 202 | + public DeleteDraftRequest parentId(@Nullable String parentId) { |
| 203 | + this.parentId = parentId; |
| 204 | + return this; |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + protected Call<StreamResponseObject> generateCall(Client client) throws StreamException { |
| 209 | + return client |
| 210 | + .create(DraftService.class) |
| 211 | + .deleteDraft(this.channelType, this.channelId, this.userId, this.parentId); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + /** Request for getting a draft. */ |
| 216 | + public static class GetDraftRequest extends StreamRequest<GetDraftResponse> { |
| 217 | + @NotNull private String channelType; |
| 218 | + @NotNull private String channelId; |
| 219 | + @Nullable private String userId; |
| 220 | + @Nullable private String parentId; |
| 221 | + |
| 222 | + private GetDraftRequest(@NotNull String channelType, @NotNull String channelId) { |
| 223 | + this.channelType = channelType; |
| 224 | + this.channelId = channelId; |
| 225 | + } |
| 226 | + |
| 227 | + @NotNull |
| 228 | + public GetDraftRequest userId(@NotNull String userId) { |
| 229 | + this.userId = userId; |
| 230 | + return this; |
| 231 | + } |
| 232 | + |
| 233 | + @NotNull |
| 234 | + public GetDraftRequest parentId(@Nullable String parentId) { |
| 235 | + this.parentId = parentId; |
| 236 | + return this; |
| 237 | + } |
| 238 | + |
| 239 | + @Override |
| 240 | + protected Call<GetDraftResponse> generateCall(Client client) throws StreamException { |
| 241 | + return client |
| 242 | + .create(DraftService.class) |
| 243 | + .getDraft(this.channelType, this.channelId, this.userId, this.parentId); |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + /** Request data for querying drafts. */ |
| 248 | + @Builder( |
| 249 | + builderClassName = "QueryDraftsRequest", |
| 250 | + builderMethodName = "", |
| 251 | + buildMethodName = "internalBuild") |
| 252 | + public static class QueryDraftsRequestData { |
| 253 | + @Nullable |
| 254 | + @JsonProperty("filter") |
| 255 | + private Map<String, Object> filter; |
| 256 | + |
| 257 | + @Singular |
| 258 | + @Nullable |
| 259 | + @JsonProperty("sort") |
| 260 | + private List<Sort> sorts; |
| 261 | + |
| 262 | + @NotNull |
| 263 | + @JsonProperty("user_id") |
| 264 | + private String userId; |
| 265 | + |
| 266 | + @Nullable |
| 267 | + @JsonProperty("limit") |
| 268 | + private Integer limit; |
| 269 | + |
| 270 | + @Nullable |
| 271 | + @JsonProperty("next") |
| 272 | + private String next; |
| 273 | + |
| 274 | + @Nullable |
| 275 | + @JsonProperty("prev") |
| 276 | + private String prev; |
| 277 | + |
| 278 | + public static class QueryDraftsRequest extends StreamRequest<QueryDraftsResponse> { |
| 279 | + public QueryDraftsRequest() {} |
| 280 | + |
| 281 | + @Override |
| 282 | + protected Call<QueryDraftsResponse> generateCall(Client client) throws StreamException { |
| 283 | + return client.create(DraftService.class).queryDrafts(this.internalBuild()); |
| 284 | + } |
| 285 | + } |
| 286 | + } |
| 287 | + |
| 288 | + /** |
| 289 | + * Creates a draft message in a channel |
| 290 | + * |
| 291 | + * @param type the channel type |
| 292 | + * @param id the channel id |
| 293 | + * @return the created request |
| 294 | + */ |
| 295 | + @NotNull |
| 296 | + public static CreateDraftRequestData.CreateDraftRequest createDraft( |
| 297 | + @NotNull String type, @NotNull String id) { |
| 298 | + return new CreateDraftRequestData.CreateDraftRequest(type, id); |
| 299 | + } |
| 300 | + |
| 301 | + /** |
| 302 | + * Deletes a draft message from a channel |
| 303 | + * |
| 304 | + * @param type the channel type |
| 305 | + * @param id the channel id |
| 306 | + * @return the created request |
| 307 | + */ |
| 308 | + @NotNull |
| 309 | + public static DeleteDraftRequest deleteDraft(@NotNull String type, @NotNull String id) { |
| 310 | + return new DeleteDraftRequest(type, id); |
| 311 | + } |
| 312 | + |
| 313 | + /** |
| 314 | + * Gets a draft message from a channel |
| 315 | + * |
| 316 | + * @param type the channel type |
| 317 | + * @param id the channel id |
| 318 | + * @return the created request |
| 319 | + */ |
| 320 | + @NotNull |
| 321 | + public static GetDraftRequest getDraft(@NotNull String type, @NotNull String id) { |
| 322 | + return new GetDraftRequest(type, id); |
| 323 | + } |
| 324 | + |
| 325 | + /** |
| 326 | + * Queries all drafts for a user |
| 327 | + * |
| 328 | + * @return the created request |
| 329 | + */ |
| 330 | + @NotNull |
| 331 | + public static QueryDraftsRequestData.QueryDraftsRequest queryDrafts() { |
| 332 | + return new QueryDraftsRequestData.QueryDraftsRequest(); |
| 333 | + } |
| 334 | +} |
0 commit comments