-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add block user feature * fix: unused comments * fix: lint --------- Co-authored-by: Ali Momen Sani <[email protected]>
- Loading branch information
Showing
4 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
src/main/java/io/getstream/chat/java/models/BlockUser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/io/getstream/chat/java/services/BlockUserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); | ||
} | ||
} |