Skip to content
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
58 changes: 58 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 @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.*;
import io.getstream.chat.java.exceptions.StreamException;
import io.getstream.chat.java.models.Channel.AssignRoleRequestData.AssignRoleRequest;
import io.getstream.chat.java.models.Channel.ChannelExportRequestData.ChannelExportRequest;
import io.getstream.chat.java.models.Channel.ChannelGetRequestData.ChannelGetRequest;
import io.getstream.chat.java.models.Channel.ChannelHideRequestData.ChannelHideRequest;
Expand Down Expand Up @@ -178,6 +179,18 @@ public static class ChannelMember {
private Boolean shadowBanned;
}

@Data
@NoArgsConstructor
public static class RoleAssignment {
@Nullable
@JsonProperty("channel_role")
private String channelRole;

@Nullable
@JsonProperty("user_id")
private String userId;
}

@Builder
@Setter
public static class ChannelRequestObject {
Expand Down Expand Up @@ -490,6 +503,39 @@ protected Call<ChannelUpdateResponse> generateCall(Client client) {
}
}

@Builder(
builderClassName = "AssignRoleRequest",
builderMethodName = "",
buildMethodName = "internalBuild")
public static class AssignRoleRequestData {
@Singular
@Nullable
@JsonProperty("assign_roles")
private List<RoleAssignment> assignRoles;

@Nullable
@JsonProperty("message")
private MessageRequestObject message;

public static class AssignRoleRequest extends StreamRequest<ChannelUpdateResponse> {
@NotNull private String channelType;

@NotNull private String channelId;

private AssignRoleRequest(@NotNull String channelType, @NotNull String channelId) {
this.channelId = channelId;
this.channelType = channelType;
}

@Override
protected Call<ChannelUpdateResponse> generateCall(Client client) {
return client
.create(ChannelService.class)
.assignRoles(this.channelType, this.channelId, this.internalBuild());
}
}
}

@RequiredArgsConstructor
public static class ChannelDeleteRequest extends StreamRequest<ChannelDeleteResponse> {
@NotNull private String channelType;
Expand Down Expand Up @@ -1416,4 +1462,16 @@ public static ChannelPartialUpdateRequest partialUpdate(
@NotNull String type, @NotNull String id) {
return new ChannelPartialUpdateRequest(type, id);
}

/**
* Creates an assign role request
*
* @param type the channel type
* @param id the channel id
* @return the created request
*/
@NotNull
public static AssignRoleRequest assignRoles(@NotNull String type, @NotNull String id) {
return new AssignRoleRequest(type, id);
}
}
12 changes: 12 additions & 0 deletions src/main/java/io/getstream/chat/java/models/ChannelType.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ public class ChannelType {
@JsonProperty("permissions")
private List<Policy> permissions;

@Nullable
@JsonProperty("grants")
private Map<String, List<String>> grants;

@Data
@NoArgsConstructor
public static class Threshold {
Expand Down Expand Up @@ -368,6 +372,10 @@ public static class ChannelTypeCreateRequestData {
@JsonProperty("permissions")
protected List<PermissionRequestObject> permissions;

@Nullable
@JsonProperty("grants")
protected Map<String, List<String>> grants;

@Nullable
@JsonProperty("name")
private String name;
Expand Down Expand Up @@ -496,6 +504,10 @@ public static class ChannelTypeUpdateRequestData {
@JsonProperty("permissions")
protected List<PermissionRequestObject> permissions;

@Nullable
@JsonProperty("grants")
protected Map<String, List<String>> grants;

public static class ChannelTypeUpdateRequest extends StreamRequest<ChannelTypeUpdateResponse> {
@NotNull private String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,10 @@ Call<ChannelPartialUpdateResponse> partialUpdate(
@NotNull @Path("type") String channelType,
@NotNull @Path("id") String channelId,
@NotNull @Body ChannelPartialUpdateRequestData channelPartialUpdateRequestData);

@POST("channels/{type}/{id}")
Call<ChannelUpdateResponse> assignRoles(
@NotNull @Path("type") String channelType,
@NotNull @Path("id") String channelId,
@NotNull @Body AssignRoleRequestData assignRoleRequestData);
}
15 changes: 15 additions & 0 deletions src/test/java/io/getstream/chat/java/ChannelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,19 @@ void whenUpdatingTeamWithPartialUpdate_thenIsUpdated() {
.getChannel());
Assertions.assertEquals(updatedTeam, updateChannel.getTeam());
}

@DisplayName("Can assign roles")
@Test
void whenAssigningRole_throwsNoError() {
Channel channel = Assertions.assertDoesNotThrow(() -> createRandomChannel()).getChannel();
var assignment = new RoleAssignment();
assignment.setChannelRole("channel_moderator");
assignment.setUserId(testUserRequestObject.getId());

Assertions.assertDoesNotThrow(
() ->
Channel.assignRoles(channel.getType(), channel.getId())
.assignRole(assignment)
.request());
}
}
26 changes: 26 additions & 0 deletions src/test/java/io/getstream/chat/java/ChannelTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import io.getstream.chat.java.models.ChannelType.ChannelTypeListResponse;
import io.getstream.chat.java.models.Command;
import io.getstream.chat.java.models.ResourceAction;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.*;
Expand Down Expand Up @@ -132,4 +134,28 @@ void whenCreatingDefaultChannelType_thenCanUpdateWithNoException() {
Assertions.assertDoesNotThrow(
() -> ChannelType.update(channelName).automod(AutoMod.SIMPLE).request());
}

@DisplayName("Can create channel type with specific grants")
@Test
void whenManipulatingChannelTypeWithGrants_throwsNoException() {
String channelTypeName = RandomStringUtils.randomAlphabetic(10);
var expectedGrants = List.of("read-channel", "create-message");
var channelGrants = new HashMap<String, List<String>>();
channelGrants.put("channel_member", expectedGrants);
Assertions.assertDoesNotThrow(
() ->
ChannelType.create()
.withDefaultConfig()
.grants(channelGrants)
.name(channelTypeName)
.request());
waitFor(
() -> {
var channelType =
Assertions.assertDoesNotThrow(() -> ChannelType.get(channelTypeName).request());
var actualGrants = channelType.getGrants().get("channel_member");

return new HashSet<>(actualGrants).equals(new HashSet<>(expectedGrants));
});
}
}