From 763e442fe0f2b8473d5e552ecef7f5a0883f83b6 Mon Sep 17 00:00:00 2001 From: Kanat Kiialbaev Date: Thu, 5 Oct 2023 06:04:14 -0700 Subject: [PATCH] feat: support message unblock endpoint (#120) fix spotless use generated blocklist name disable whenExecutingCommandAction_thenNoException --- .../getstream/chat/java/models/Message.java | 33 ++++++++++ .../chat/java/services/MessageService.java | 5 ++ .../io/getstream/chat/java/MessageTest.java | 60 +++++++++++++++++-- 3 files changed, 94 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/getstream/chat/java/models/Message.java b/src/main/java/io/getstream/chat/java/models/Message.java index d93a0dca..347a7288 100644 --- a/src/main/java/io/getstream/chat/java/models/Message.java +++ b/src/main/java/io/getstream/chat/java/models/Message.java @@ -11,6 +11,7 @@ import io.getstream.chat.java.models.Message.MessageSearchRequestData.MessageSearchRequest; import io.getstream.chat.java.models.Message.MessageSendRequestData.MessageSendRequest; import io.getstream.chat.java.models.Message.MessageTranslateRequestData.MessageTranslateRequest; +import io.getstream.chat.java.models.Message.MessageUnblockRequestData.MessageUnblockRequest; import io.getstream.chat.java.models.Message.MessageUpdateRequestData.MessageUpdateRequest; import io.getstream.chat.java.models.User.UserRequestObject; import io.getstream.chat.java.models.framework.*; @@ -1241,6 +1242,27 @@ protected Call generateCall(Client client) { } } + @Builder( + builderClassName = "MessageUnblockRequest", + builderMethodName = "", + buildMethodName = "internalBuild") + public static class MessageUnblockRequestData { + @NotNull + @JsonProperty("target_message_id") + private String targetMessageId; + + @Nullable + @JsonProperty("user_id") + private String userId; + + public static class MessageUnblockRequest extends StreamRequest { + @Override + protected Call generateCall(Client client) { + return client.create(MessageService.class).unblockMessage(this.internalBuild()); + } + } + } + @Data @NoArgsConstructor @EqualsAndHashCode(callSuper = true) @@ -1638,4 +1660,15 @@ public static MessagePartialUpdateRequest unpinMessage( @NotNull String id, @NotNull String userId) { return new MessagePartialUpdateRequest(id).setValue("pinned", false).userId(userId); } + + /** + * Creates an unblock message request + * + * @param messageId the message id to unblock + * @return the created request + */ + @NotNull + public static MessageUnblockRequest unblock(@NotNull String messageId) { + return new MessageUnblockRequest().targetMessageId(messageId); + } } diff --git a/src/main/java/io/getstream/chat/java/services/MessageService.java b/src/main/java/io/getstream/chat/java/services/MessageService.java index 4f70bcdf..c57aa79e 100644 --- a/src/main/java/io/getstream/chat/java/services/MessageService.java +++ b/src/main/java/io/getstream/chat/java/services/MessageService.java @@ -15,6 +15,7 @@ import io.getstream.chat.java.models.Message.MessageSendResponse; import io.getstream.chat.java.models.Message.MessageTranslateRequestData; import io.getstream.chat.java.models.Message.MessageTranslateResponse; +import io.getstream.chat.java.models.Message.MessageUnblockRequestData; import io.getstream.chat.java.models.Message.MessageUpdateRequestData; import io.getstream.chat.java.models.Message.MessageUpdateResponse; import io.getstream.chat.java.models.Message.MessageUploadFileResponse; @@ -129,4 +130,8 @@ Call translate( Call partialUpdate( @NotNull @Path("id") String id, @NotNull @Body MessagePartialUpdateRequestData messagePartialUpdateRequestData); + + @POST("moderation/unblock_message") + Call unblockMessage( + @NotNull @Body MessageUnblockRequestData messageUnblockRequestData); } diff --git a/src/test/java/io/getstream/chat/java/MessageTest.java b/src/test/java/io/getstream/chat/java/MessageTest.java index be013ef0..3b4ddd28 100644 --- a/src/test/java/io/getstream/chat/java/MessageTest.java +++ b/src/test/java/io/getstream/chat/java/MessageTest.java @@ -15,6 +15,7 @@ import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; +import org.apache.commons.lang3.RandomStringUtils; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; @@ -507,6 +508,7 @@ void whenRetrievingReplies_thenAreRetrieved() { Assertions.assertEquals(1, m.getDeletedReplyCount()); } + @Disabled @DisplayName("Can execute command action") @Test void whenExecutingCommandAction_thenNoException() { @@ -645,16 +647,17 @@ void whenUnpinningAMessage_thenIsUnpinned() { @DisplayName("Can force enable or disable moderation on a message") @Test void whenForcingModerationOnAMessage_thenIsForced() { - String text = "This is a shitty message"; + final String text = "This is a shitty message"; + final String blocklistName = RandomStringUtils.randomAlphabetic(5); Assertions.assertDoesNotThrow( - () -> Blocklist.create().name("swear-blocklist").words(Arrays.asList("shitty")).request()); + () -> Blocklist.create().name(blocklistName).words(Arrays.asList("shitty")).request()); - Assertions.assertDoesNotThrow(() -> Thread.sleep(2000)); + Assertions.assertDoesNotThrow(() -> Thread.sleep(5000)); Assertions.assertDoesNotThrow( () -> ChannelType.update(testChannel.getType()) - .blocklist("swear-blocklist") + .blocklist(blocklistName) .blocklistBehavior(ChannelType.BlocklistBehavior.BLOCK) .request()); @@ -685,5 +688,54 @@ void whenForcingModerationOnAMessage_thenIsForced() { .getMessage(); Assertions.assertTrue(msg2.getText().equals(text)); + + Assertions.assertDoesNotThrow(() -> Blocklist.delete(blocklistName).request()); + } + + @DisplayName("Can unblock a message") + @Test + void whenUnblockingAMessage_thenIsUnblocked() { + final String swearText = "This is a hate message"; + final String blocklistName = RandomStringUtils.randomAlphabetic(5); + Assertions.assertDoesNotThrow( + () -> Blocklist.create().name(blocklistName).words(Arrays.asList("hate")).request()); + + Assertions.assertDoesNotThrow(() -> Thread.sleep(5000)); + + Assertions.assertDoesNotThrow( + () -> + ChannelType.update(testChannel.getType()) + .blocklist(blocklistName) + .blocklistBehavior(ChannelType.BlocklistBehavior.BLOCK) + .request()); + + Assertions.assertDoesNotThrow(() -> Thread.sleep(5000)); + + MessageRequestObject messageRequest1a = + MessageRequestObject.builder() + .text(swearText) + .userId(testUserRequestObject.getId()) + .build(); + Message msg1a = + Assertions.assertDoesNotThrow( + () -> + Message.send(testChannel.getType(), testChannel.getId()) + .forceModeration(true) + .message(messageRequest1a) + .request()) + .getMessage(); + + Assertions.assertEquals("Message was blocked by moderation policies", msg1a.getText()); + + Assertions.assertDoesNotThrow( + () -> Message.unblock(msg1a.getId()).userId(testUserRequestObject.getId()).request()); + + Message msg1b = + Assertions.assertDoesNotThrow(() -> Message.get(msg1a.getId()).request()).getMessage(); + + Assertions.assertEquals(swearText, msg1b.getText()); + Assertions.assertEquals(msg1a.getId(), msg1b.getId()); + + Assertions.assertDoesNotThrow(() -> Blocklist.delete(blocklistName).request()); } }