diff --git a/src/main/java/io/getstream/chat/java/models/BlockUser.java b/src/main/java/io/getstream/chat/java/models/BlockUser.java new file mode 100644 index 00000000..d0f1f743 --- /dev/null +++ b/src/main/java/io/getstream/chat/java/models/BlockUser.java @@ -0,0 +1,146 @@ +package io.getstream.chat.java.models; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.getstream.chat.java.models.framework.StreamRequest; +import io.getstream.chat.java.models.framework.StreamResponseObject; +import io.getstream.chat.java.services.BlockUserService; +import io.getstream.chat.java.services.framework.Client; +import java.util.Date; +import java.util.List; +import lombok.*; +import org.jetbrains.annotations.NotNull; +import retrofit2.Call; + +@Data +@NoArgsConstructor +public class BlockUser { + + @Builder( + builderClassName = "BlockUserRequest", + builderMethodName = "", + buildMethodName = "internalBuild") + public static class BlockUserRequestData { + @NotNull + @JsonProperty("blocked_user_id") + private String blockedUserID; + + @NotNull + @JsonProperty("user_id") + private String userID; + + public static class BlockUserRequest extends StreamRequest { + @Override + protected Call generateCall(Client client) { + var data = this.internalBuild(); + return client.create(BlockUserService.class).blockUser(data); + } + } + } + + @Builder( + builderClassName = "UnblockUserRequest", + builderMethodName = "", + buildMethodName = "internalBuild") + public static class UnblockUserRequestData { + @NotNull + @JsonProperty("blocked_user_id") + private String blockedUserID; + + @NotNull + @JsonProperty("user_id") + private String userID; + + public static class UnblockUserRequest extends StreamRequest { + @Override + protected Call generateCall(Client client) { + var data = this.internalBuild(); + return client.create(BlockUserService.class).unblockUser(data); + } + } + } + + @NotNull + public static BlockUserRequestData.BlockUserRequest blockUser() { + return new BlockUserRequestData.BlockUserRequest(); + } + + @NotNull + public static UnblockUserRequestData.UnblockUserRequest unblockUser() { + return new UnblockUserRequestData.UnblockUserRequest(); + } + + @Data + @EqualsAndHashCode(callSuper = true) + @NoArgsConstructor + public static class BlockUserResponse extends StreamResponseObject { + @JsonProperty("blocked_by_user_id") + private String blockedByUserID; + + @JsonProperty("blocked_user_id") + private String blockedUserID; + + @JsonProperty("created_at") + private Date createdAt; + } + + @Data + @EqualsAndHashCode(callSuper = true) + @NoArgsConstructor + public static class UnblockUserResponse extends StreamResponseObject {} + + @Data + @EqualsAndHashCode(callSuper = true) + @NoArgsConstructor + public static class GetBlockedUsersResponse extends StreamResponseObject { + @JsonProperty("blocks") + private List blockedUsers; + } + + @Data + @NoArgsConstructor + public static class BlockedUserResponse { + @JsonProperty("user") + private User blockedByUser; + + @JsonProperty("user_id") + private String blockedByUserID; + + @JsonProperty("blocked_user") + private User blockedUser; + + @JsonProperty("blocked_user_id") + private String blockedUserID; + + @JsonProperty("created_at") + private Date createdAt; + } + + @Builder( + builderClassName = "GetBlockedUsersRequest", + builderMethodName = "", + buildMethodName = "internalBuild") + public static class GetBlockedUsersRequestData { + @NotNull + @JsonProperty("user_id") + private String blockedByUserID; + + public static class GetBlockedUsersRequest extends StreamRequest { + private String blockedByUserID; + + public GetBlockedUsersRequest(String blockedByUserID) { + this.blockedByUserID = blockedByUserID; + } + + @Override + protected Call generateCall(Client client) { + return client.create(BlockUserService.class).getBlockedUsers(blockedByUserID); + } + } + } + + @NotNull + public static GetBlockedUsersRequestData.GetBlockedUsersRequest getBlockedUsers( + String blockedByUserID) { + return new GetBlockedUsersRequestData.GetBlockedUsersRequest(blockedByUserID); + } +} diff --git a/src/main/java/io/getstream/chat/java/models/User.java b/src/main/java/io/getstream/chat/java/models/User.java index f37afc8c..f62bbb22 100644 --- a/src/main/java/io/getstream/chat/java/models/User.java +++ b/src/main/java/io/getstream/chat/java/models/User.java @@ -119,6 +119,10 @@ public Map getAdditionalFields() { return this.additionalFields; } + @Nullable + @JsonProperty("blocked_user_ids") + private List blockedUserIDs; + @JsonAnySetter public void setAdditionalField(String name, Object value) { this.additionalFields.put(name, value); diff --git a/src/main/java/io/getstream/chat/java/services/BlockUserService.java b/src/main/java/io/getstream/chat/java/services/BlockUserService.java new file mode 100644 index 00000000..8af4fada --- /dev/null +++ b/src/main/java/io/getstream/chat/java/services/BlockUserService.java @@ -0,0 +1,23 @@ +package io.getstream.chat.java.services; + +import io.getstream.chat.java.models.BlockUser; +import io.getstream.chat.java.models.BlockUser.BlockUserResponse; +import io.getstream.chat.java.models.BlockUser.GetBlockedUsersResponse; +import org.jetbrains.annotations.NotNull; +import retrofit2.Call; +import retrofit2.http.Body; +import retrofit2.http.GET; +import retrofit2.http.POST; +import retrofit2.http.Query; + +public interface BlockUserService { + @POST("users/block") + Call blockUser(@NotNull @Body BlockUser.BlockUserRequestData data); + + @POST("users/unblock") + Call unblockUser( + @NotNull @Body BlockUser.UnblockUserRequestData data); + + @GET("users/block") + Call getBlockedUsers(@Query("user_id") String blockedByUserID); +} diff --git a/src/test/java/io/getstream/chat/java/BlockUserTest.java b/src/test/java/io/getstream/chat/java/BlockUserTest.java new file mode 100644 index 00000000..4ac32d7d --- /dev/null +++ b/src/test/java/io/getstream/chat/java/BlockUserTest.java @@ -0,0 +1,63 @@ +package io.getstream.chat.java; + +import io.getstream.chat.java.models.*; +import io.getstream.chat.java.models.BlockUser.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class BlockUserTest extends BasicTest { + @Test + @DisplayName("Block User") + void blockUserGetUnBlock() { + Assertions.assertDoesNotThrow( + () -> { + var channel = Assertions.assertDoesNotThrow(BasicTest::createRandomChannel).getChannel(); + Assertions.assertNotNull(channel); + + var blockingUser = testUsersRequestObjects.get(0); + var blockedUser = testUsersRequestObjects.get(1); + BlockUser.BlockUserRequestData.BlockUserRequest blockRequest = + BlockUser.blockUser().blockedUserID(blockedUser.getId()).userID(blockingUser.getId()); + BlockUser.BlockUserResponse blockResponse = blockRequest.request(); + Assertions.assertEquals(blockResponse.getBlockedByUserID(), blockingUser.getId()); + Assertions.assertEquals(blockResponse.getBlockedUserID(), blockedUser.getId()); + Assertions.assertNotNull(blockResponse.getCreatedAt()); + + BlockUser.GetBlockedUsersRequestData.GetBlockedUsersRequest getBlockedUsersRequest = + BlockUser.getBlockedUsers(blockingUser.getId()); + + BlockUser.GetBlockedUsersResponse getBlockedUsersResponse = + getBlockedUsersRequest.request(); + Assertions.assertFalse(getBlockedUsersResponse.getBlockedUsers().isEmpty()); + Assertions.assertEquals( + getBlockedUsersResponse.getBlockedUsers().get(0).getBlockedUserID(), + blockedUser.getId()); + + var users = User.list().filterCondition("id", blockingUser.getId()).request(); + Assertions.assertNotNull(users.getUsers().get(0).getBlockedUserIDs()); + Assertions.assertEquals( + users.getUsers().get(0).getBlockedUserIDs().get(0), blockedUser.getId()); + + // Unblocking the user + BlockUser.UnblockUserRequestData.UnblockUserRequest unblockRequest = + BlockUser.unblockUser() + .blockedUserID(blockedUser.getId()) + .userID(blockingUser.getId()); + + BlockUser.UnblockUserResponse unblockResponse = unblockRequest.request(); + Assertions.assertNotNull(unblockResponse); + + // Verify user is unblocked + getBlockedUsersRequest = BlockUser.getBlockedUsers(blockingUser.getId()); + + getBlockedUsersResponse = getBlockedUsersRequest.request(); + Assertions.assertTrue(getBlockedUsersResponse.getBlockedUsers().isEmpty()); + + users = User.list().filterCondition("id", blockingUser.getId()).request(); + Assertions.assertTrue( + users.getUsers().get(0).getBlockedUserIDs() == null + || users.getUsers().get(0).getBlockedUserIDs().isEmpty()); + }); + } +}