From 38c73481c5df5e513ba54c73f5852f03db0f0bb3 Mon Sep 17 00:00:00 2001 From: Ali Momen Sani Date: Fri, 13 Dec 2024 11:47:06 +0100 Subject: [PATCH] feat: member custom data, pinning and archiving (#162) --- .../getstream/chat/java/models/Channel.java | 171 ++++++++++++++++++ .../chat/java/services/ChannelService.java | 7 + .../io/getstream/chat/java/ChannelTest.java | 116 ++++++++++++ 3 files changed, 294 insertions(+) diff --git a/src/main/java/io/getstream/chat/java/models/Channel.java b/src/main/java/io/getstream/chat/java/models/Channel.java index e7121f43..f51e3909 100644 --- a/src/main/java/io/getstream/chat/java/models/Channel.java +++ b/src/main/java/io/getstream/chat/java/models/Channel.java @@ -9,6 +9,7 @@ import io.getstream.chat.java.models.Channel.ChannelListRequestData.ChannelListRequest; import io.getstream.chat.java.models.Channel.ChannelMarkAllReadRequestData.ChannelMarkAllReadRequest; import io.getstream.chat.java.models.Channel.ChannelMarkReadRequestData.ChannelMarkReadRequest; +import io.getstream.chat.java.models.Channel.ChannelMemberPartialUpdateRequestData.ChannelMemberPartialUpdateRequest; import io.getstream.chat.java.models.Channel.ChannelMuteRequestData.ChannelMuteRequest; import io.getstream.chat.java.models.Channel.ChannelPartialUpdateRequestData.ChannelPartialUpdateRequest; import io.getstream.chat.java.models.Channel.ChannelQueryMembersRequestData.ChannelQueryMembersRequest; @@ -138,6 +139,17 @@ public static class ChannelRead { @Data @NoArgsConstructor public static class ChannelMember { + public enum InviteStatus { + @JsonProperty("pending") + PENDING, + @JsonProperty("accepted") + ACCEPTED, + @JsonProperty("rejected") + REJECTED, + @JsonEnumDefaultValue + UNKNOWN + } + @Nullable @JsonProperty("user_id") private String userId; @@ -193,6 +205,30 @@ public static class ChannelMember { @Nullable @JsonProperty("notifications_muted") private Boolean notificationsMuted; + + @Nullable + @JsonProperty("status") + private InviteStatus status; + + @Nullable + @JsonProperty("archived_at") + private Date archivedAt; + + @Nullable + @JsonProperty("pinned_at") + private Date pinnedAt; + + @Singular @Nullable @JsonIgnore private Map additionalFields = new HashMap<>(); + + @JsonAnyGetter + public Map getAdditionalFields() { + return this.additionalFields; + } + + @JsonAnySetter + public void setAdditionalField(String name, Object value) { + this.additionalFields.put(name, value); + } } @Data @@ -1097,6 +1133,54 @@ protected Call generateCall(Client client) { } } + @Builder( + builderClassName = "ChannelMemberPartialUpdateRequest", + builderMethodName = "", + buildMethodName = "internalBuild") + public static class ChannelMemberPartialUpdateRequestData { + @Singular + @Nullable + @JsonProperty("set") + private Map setValues; + + @Singular + @Nullable + @JsonProperty("unset") + private List unsetValues; + + public static class ChannelMemberPartialUpdateRequest + extends StreamRequest { + @NotNull private String channelType; + + @NotNull private String channelId; + + @NotNull private String userId; + + private ChannelMemberPartialUpdateRequest( + @NotNull String channelType, @NotNull String channelId, @NotNull String userId) { + this.channelType = channelType; + this.channelId = channelId; + this.userId = userId; + } + + @Override + protected Call generateCall(Client client) { + return client + .create(ChannelService.class) + .updateMemberPartial(channelType, channelId, userId, this.internalBuild()); + } + } + } + + @Data + @NoArgsConstructor + @EqualsAndHashCode(callSuper = true) + public static class ChannelMemberResponse extends StreamResponseObject { + @Nullable + @JsonProperty("channel_member") + private ChannelMember member; + } + @Data @NoArgsConstructor @EqualsAndHashCode(callSuper = true) @@ -1344,6 +1428,23 @@ public static class ChannelUnMuteResponse extends StreamResponseObject { private OwnUser ownUser; } + @Data + @NoArgsConstructor + @EqualsAndHashCode(callSuper = true) + public static class ChannelPinResponse extends StreamResponseObject { + @Nullable + @JsonProperty("channel_mute") + private ChannelMute channelMute; + + @Nullable + @JsonProperty("channel_mutes") + private List channelMutes; + + @Nullable + @JsonProperty("own_user") + private OwnUser ownUser; + } + @Data @NoArgsConstructor @EqualsAndHashCode(callSuper = true) @@ -1552,4 +1653,74 @@ public static ChannelPartialUpdateRequest partialUpdate( public static AssignRoleRequest assignRoles(@NotNull String type, @NotNull String id) { return new AssignRoleRequest(type, id); } + + /** + * Creates a update member partial request + * + * @param type the channel type + * @param id the channel id + * @param userId the user id + * @return the created request + */ + @NotNull + public static ChannelMemberPartialUpdateRequest updateMemberPartial( + @NotNull String type, @NotNull String id, @NotNull String userId) { + return new ChannelMemberPartialUpdateRequest(type, id, userId); + } + + /** + * Creates a pin channel request + * + * @param type the channel type + * @param id the channel id + * @param userId the user id + * @return the created request + */ + @NotNull + public static ChannelMemberPartialUpdateRequest pin( + @NotNull String type, @NotNull String id, @NotNull String userId) { + return new ChannelMemberPartialUpdateRequest(type, id, userId).setValue("pinned", true); + } + + /** + * Creates a unpin channel request + * + * @param type the channel type + * @param id the channel id + * @param userId the user id + * @return the created request + */ + @NotNull + public static ChannelMemberPartialUpdateRequest unpin( + @NotNull String type, @NotNull String id, @NotNull String userId) { + return new ChannelMemberPartialUpdateRequest(type, id, userId).setValue("pinned", false); + } + + /** + * Creates a archive channel request + * + * @param type the channel type + * @param id the channel id + * @param userId the user id + * @return the created request + */ + @NotNull + public static ChannelMemberPartialUpdateRequest archive( + @NotNull String type, @NotNull String id, @NotNull String userId) { + return new ChannelMemberPartialUpdateRequest(type, id, userId).setValue("archived", true); + } + + /** + * Creates a unarchive channel request + * + * @param type the channel type + * @param id the channel id + * @param userId the user id + * @return the created request + */ + @NotNull + public static ChannelMemberPartialUpdateRequest unarchive( + @NotNull String type, @NotNull String id, @NotNull String userId) { + return new ChannelMemberPartialUpdateRequest(type, id, userId).setValue("archived", false); + } } diff --git a/src/main/java/io/getstream/chat/java/services/ChannelService.java b/src/main/java/io/getstream/chat/java/services/ChannelService.java index 57ea03bb..fc59def5 100644 --- a/src/main/java/io/getstream/chat/java/services/ChannelService.java +++ b/src/main/java/io/getstream/chat/java/services/ChannelService.java @@ -96,4 +96,11 @@ Call assignRoles( @NotNull @Path("type") String channelType, @NotNull @Path("id") String channelId, @NotNull @Body AssignRoleRequestData assignRoleRequestData); + + @PATCH("channels/{type}/{id}/member/{user_id}") + Call updateMemberPartial( + @NotNull @Path("type") String channelType, + @NotNull @Path("id") String channelId, + @NotNull @Path("user_id") String userId, + @NotNull @Body ChannelMemberPartialUpdateRequestData updateMemberPartialRequestData); } diff --git a/src/test/java/io/getstream/chat/java/ChannelTest.java b/src/test/java/io/getstream/chat/java/ChannelTest.java index 6c735642..17d45b5c 100644 --- a/src/test/java/io/getstream/chat/java/ChannelTest.java +++ b/src/test/java/io/getstream/chat/java/ChannelTest.java @@ -431,4 +431,120 @@ void whenAssigningRole_throwsNoError() { .assignRole(assignment) .request()); } + + @DisplayName("Can update a channel member partially") + @Test + void whenUpdatingChannelMemberPartially_thenIsUpdated() { + // We should not use testChannel to not modify it + Channel channel = Assertions.assertDoesNotThrow(() -> createRandomChannel()).getChannel(); + ChannelMemberResponse channelMemberResponse = + Assertions.assertDoesNotThrow( + () -> + Channel.updateMemberPartial( + channel.getType(), channel.getId(), testUserRequestObject.getId()) + .setValue("custom_key", "custom_value") + .setValue("channel_role", "channel_moderator") + .request()); + + System.out.println(channelMemberResponse.getMember()); + Assertions.assertEquals( + "custom_value", channelMemberResponse.getMember().getAdditionalFields().get("custom_key")); + Assertions.assertEquals( + "channel_moderator", channelMemberResponse.getMember().getChannelRole()); + + // unset + channelMemberResponse = + Assertions.assertDoesNotThrow( + () -> + Channel.updateMemberPartial( + channel.getType(), channel.getId(), testUserRequestObject.getId()) + .unsetValue("custom_key") + .request()); + + Assertions.assertNull( + channelMemberResponse.getMember().getAdditionalFields().get("custom_key")); + } + + @DisplayName("Can pin and unpin a channel") + @Test + void whenPinningAndUnpinningAChannel_thenIsPinnedAndUnpinned() { + Channel channel = Assertions.assertDoesNotThrow(() -> createRandomChannel()).getChannel(); + ChannelMemberResponse channelMemberResponse = + Assertions.assertDoesNotThrow( + () -> + Channel.pin(channel.getType(), channel.getId(), testUserRequestObject.getId()) + .request()); + Date pinnedAt = channelMemberResponse.getMember().getPinnedAt(); + Assertions.assertNotNull(pinnedAt); + + channelMemberResponse = + Assertions.assertDoesNotThrow( + () -> + Channel.unpin(channel.getType(), channel.getId(), testUserRequestObject.getId()) + .request()); + Assertions.assertNull(channelMemberResponse.getMember().getPinnedAt()); + } + + @DisplayName("Can pin and unpin a channel using unset") + @Test + void whenPinningAndUnpinningAChannelUsingUnset_thenIsPinnedAndUnpinned() { + Channel channel = Assertions.assertDoesNotThrow(() -> createRandomChannel()).getChannel(); + ChannelMemberResponse channelMemberResponse = + Assertions.assertDoesNotThrow( + () -> + Channel.pin(channel.getType(), channel.getId(), testUserRequestObject.getId()) + .request()); + System.out.println(channelMemberResponse.getMember()); + System.out.println(channelMemberResponse.getMember().getPinnedAt()); + Assertions.assertNotNull(channelMemberResponse.getMember().getPinnedAt()); + + channelMemberResponse = + Assertions.assertDoesNotThrow( + () -> + Channel.updateMemberPartial( + channel.getType(), channel.getId(), testUserRequestObject.getId()) + .unsetValue("pinned") + .request()); + Assertions.assertNull(channelMemberResponse.getMember().getPinnedAt()); + } + + @DisplayName("Can archive and unarchive a channel") + @Test + void whenArchivingChannel_thenIsArchived() { + Channel channel = Assertions.assertDoesNotThrow(() -> createRandomChannel()).getChannel(); + ChannelMemberResponse channelMemberResponse = + Assertions.assertDoesNotThrow( + () -> + Channel.archive(channel.getType(), channel.getId(), testUserRequestObject.getId()) + .request()); + Assertions.assertNotNull(channelMemberResponse.getMember().getArchivedAt()); + + channelMemberResponse = + Assertions.assertDoesNotThrow( + () -> + Channel.unarchive(channel.getType(), channel.getId(), testUserRequestObject.getId()) + .request()); + Assertions.assertNull(channelMemberResponse.getMember().getArchivedAt()); + } + + @DisplayName("Can archive and unarchive a channel using unset") + @Test + void whenArchivingChannelUsingUnset_thenIsArchived() { + Channel channel = Assertions.assertDoesNotThrow(() -> createRandomChannel()).getChannel(); + ChannelMemberResponse channelMemberResponse = + Assertions.assertDoesNotThrow( + () -> + Channel.archive(channel.getType(), channel.getId(), testUserRequestObject.getId()) + .request()); + Assertions.assertNotNull(channelMemberResponse.getMember().getArchivedAt()); + + channelMemberResponse = + Assertions.assertDoesNotThrow( + () -> + Channel.updateMemberPartial( + channel.getType(), channel.getId(), testUserRequestObject.getId()) + .unsetValue("archived") + .request()); + Assertions.assertNull(channelMemberResponse.getMember().getArchivedAt()); + } }