Skip to content

Commit

Permalink
feat: add block user feature (#147)
Browse files Browse the repository at this point in the history
* feat: add block user feature

* fix: unused comments

* fix: lint

---------

Co-authored-by: Ali Momen Sani <[email protected]>
  • Loading branch information
itsmeadi and shaljam authored Aug 15, 2024
1 parent 7dd4839 commit 76bfad2
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 0 deletions.
146 changes: 146 additions & 0 deletions src/main/java/io/getstream/chat/java/models/BlockUser.java
Original file line number Diff line number Diff line change
@@ -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<BlockUserResponse> {
@Override
protected Call<BlockUserResponse> 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<UnblockUserResponse> {
@Override
protected Call<UnblockUserResponse> 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<BlockedUserResponse> 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<GetBlockedUsersResponse> {
private String blockedByUserID;

public GetBlockedUsersRequest(String blockedByUserID) {
this.blockedByUserID = blockedByUserID;
}

@Override
protected Call<GetBlockedUsersResponse> generateCall(Client client) {
return client.create(BlockUserService.class).getBlockedUsers(blockedByUserID);
}
}
}

@NotNull
public static GetBlockedUsersRequestData.GetBlockedUsersRequest getBlockedUsers(
String blockedByUserID) {
return new GetBlockedUsersRequestData.GetBlockedUsersRequest(blockedByUserID);
}
}
4 changes: 4 additions & 0 deletions src/main/java/io/getstream/chat/java/models/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ public Map<String, Object> getAdditionalFields() {
return this.additionalFields;
}

@Nullable
@JsonProperty("blocked_user_ids")
private List<String> blockedUserIDs;

@JsonAnySetter
public void setAdditionalField(String name, Object value) {
this.additionalFields.put(name, value);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<BlockUserResponse> blockUser(@NotNull @Body BlockUser.BlockUserRequestData data);

@POST("users/unblock")
Call<BlockUser.UnblockUserResponse> unblockUser(
@NotNull @Body BlockUser.UnblockUserRequestData data);

@GET("users/block")
Call<GetBlockedUsersResponse> getBlockedUsers(@Query("user_id") String blockedByUserID);
}
63 changes: 63 additions & 0 deletions src/test/java/io/getstream/chat/java/BlockUserTest.java
Original file line number Diff line number Diff line change
@@ -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());
});
}
}

0 comments on commit 76bfad2

Please sign in to comment.