Skip to content

Commit

Permalink
feat: support message unblock endpoint (#120)
Browse files Browse the repository at this point in the history
fix spotless

use generated blocklist name

disable whenExecutingCommandAction_thenNoException
  • Loading branch information
kanat authored Oct 5, 2023
1 parent 2bfcba4 commit 763e442
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
33 changes: 33 additions & 0 deletions src/main/java/io/getstream/chat/java/models/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -1241,6 +1242,27 @@ protected Call<MessagePartialUpdateResponse> 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<StreamResponseObject> {
@Override
protected Call<StreamResponseObject> generateCall(Client client) {
return client.create(MessageService.class).unblockMessage(this.internalBuild());
}
}
}

@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -129,4 +130,8 @@ Call<MessageTranslateResponse> translate(
Call<MessagePartialUpdateResponse> partialUpdate(
@NotNull @Path("id") String id,
@NotNull @Body MessagePartialUpdateRequestData messagePartialUpdateRequestData);

@POST("moderation/unblock_message")
Call<StreamResponseObject> unblockMessage(
@NotNull @Body MessageUnblockRequestData messageUnblockRequestData);
}
60 changes: 56 additions & 4 deletions src/test/java/io/getstream/chat/java/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -507,6 +508,7 @@ void whenRetrievingReplies_thenAreRetrieved() {
Assertions.assertEquals(1, m.getDeletedReplyCount());
}

@Disabled
@DisplayName("Can execute command action")
@Test
void whenExecutingCommandAction_thenNoException() {
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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());
}
}

0 comments on commit 763e442

Please sign in to comment.