Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHA-18] feat: member custom data, pinning and archiving #162

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 171 additions & 0 deletions src/main/java/io/getstream/chat/java/models/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Object> additionalFields = new HashMap<>();

@JsonAnyGetter
public Map<String, Object> getAdditionalFields() {
return this.additionalFields;
}

@JsonAnySetter
public void setAdditionalField(String name, Object value) {
this.additionalFields.put(name, value);
}
}

@Data
Expand Down Expand Up @@ -1097,6 +1133,54 @@ protected Call<ChannelPartialUpdateResponse> generateCall(Client client) {
}
}

@Builder(
builderClassName = "ChannelMemberPartialUpdateRequest",
builderMethodName = "",
buildMethodName = "internalBuild")
public static class ChannelMemberPartialUpdateRequestData {
@Singular
@Nullable
@JsonProperty("set")
private Map<String, Object> setValues;

@Singular
@Nullable
@JsonProperty("unset")
private List<String> unsetValues;

public static class ChannelMemberPartialUpdateRequest
extends StreamRequest<ChannelMemberResponse> {
@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<ChannelMemberResponse> 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)
Expand Down Expand Up @@ -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<ChannelMute> channelMutes;

@Nullable
@JsonProperty("own_user")
private OwnUser ownUser;
}

@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,11 @@ Call<ChannelUpdateResponse> assignRoles(
@NotNull @Path("type") String channelType,
@NotNull @Path("id") String channelId,
@NotNull @Body AssignRoleRequestData assignRoleRequestData);

@PATCH("channels/{type}/{id}/member/{user_id}")
Call<ChannelMemberResponse> updateMemberPartial(
@NotNull @Path("type") String channelType,
@NotNull @Path("id") String channelId,
@NotNull @Path("user_id") String userId,
@NotNull @Body ChannelMemberPartialUpdateRequestData updateMemberPartialRequestData);
}
116 changes: 116 additions & 0 deletions src/test/java/io/getstream/chat/java/ChannelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Loading