-
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: moderation v2 blocklist * fix: update_by_user_id is reflecting the state before update
- Loading branch information
Showing
5 changed files
with
293 additions
and
15 deletions.
There are no files selected for viewing
213 changes: 213 additions & 0 deletions
213
src/main/java/io/getstream/chat/java/models/Moderation.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,213 @@ | ||
package io.getstream.chat.java.models; | ||
|
||
import com.fasterxml.jackson.annotation.*; | ||
import io.getstream.chat.java.models.Moderation.UpsertConfigRequestData.UpsertConfigRequest; | ||
import io.getstream.chat.java.models.framework.StreamRequest; | ||
import io.getstream.chat.java.models.framework.StreamResponseObject; | ||
import io.getstream.chat.java.services.ModerationService; | ||
import io.getstream.chat.java.services.framework.Client; | ||
import java.util.Date; | ||
import java.util.List; | ||
import lombok.*; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import retrofit2.Call; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
public class Moderation { | ||
|
||
@Builder( | ||
builderClassName = "ConfigGetRequest", | ||
builderMethodName = "", | ||
buildMethodName = "internalBuild") | ||
public static class ConfigGetRequestData { | ||
public static class ConfigGetRequest extends StreamRequest<ConfigGetResponse> { | ||
@NotNull private String key; | ||
|
||
private ConfigGetRequest(@NotNull String key) { | ||
this.key = key; | ||
} | ||
|
||
@Override | ||
protected Call<ConfigGetResponse> generateCall(Client client) { | ||
return client.create(ModerationService.class).getConfig(this.key); | ||
} | ||
} | ||
} | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@EqualsAndHashCode(callSuper = true) | ||
public static class ConfigGetResponse extends StreamResponseObject { | ||
@Nullable | ||
@JsonProperty("config") | ||
private Config config; | ||
} | ||
|
||
@Data | ||
@NoArgsConstructor | ||
public static class Config { | ||
@Nullable | ||
@JsonProperty("key") | ||
private String key; | ||
|
||
@Nullable | ||
@JsonProperty("async") | ||
private Boolean async; | ||
|
||
@Nullable | ||
@JsonProperty("block_list_config") | ||
private BlockListConfig blockListConfig; | ||
|
||
@Nullable | ||
@JsonProperty("created_at") | ||
private Date createdAt; | ||
|
||
@Nullable | ||
@JsonProperty("updated_at") | ||
private Date updatedAt; | ||
} | ||
|
||
@Data | ||
@NoArgsConstructor | ||
public static class BlockListConfig { | ||
@Nullable | ||
@JsonProperty("async") | ||
private Boolean async; | ||
|
||
@NotNull | ||
@JsonProperty("enabled") | ||
private Boolean enabled; | ||
|
||
@NotNull | ||
@JsonProperty("rules") | ||
private List<BlockListRule> rules; | ||
} | ||
|
||
public enum Action { | ||
@JsonProperty("flag") | ||
FLAG, | ||
@JsonProperty("shadow") | ||
SHADOW, | ||
@JsonProperty("remove") | ||
REMOVE, | ||
@JsonProperty("bounce") | ||
BOUNCE, | ||
@JsonProperty("bounce_flag") | ||
BOUNCE_FLAG, | ||
@JsonProperty("bounce_remove") | ||
BOUNCE_REMOVE, | ||
@JsonEnumDefaultValue | ||
UNKNOWN | ||
} | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@Builder | ||
@AllArgsConstructor | ||
public static class BlockListRule { | ||
@NotNull | ||
@JsonProperty("name") | ||
private String name; | ||
|
||
@NotNull | ||
@JsonProperty("action") | ||
private Action action; | ||
} | ||
|
||
@Builder | ||
public static class BlockListConfigRequestObject { | ||
@Nullable | ||
@JsonProperty("async") | ||
private Boolean async; | ||
|
||
@NotNull | ||
@JsonProperty("rules") | ||
private List<BlockListRule> rules; | ||
} | ||
|
||
@Builder( | ||
builderClassName = "UpsertConfigRequest", | ||
builderMethodName = "", | ||
buildMethodName = "internalBuild") | ||
public static class UpsertConfigRequestData { | ||
@Nullable | ||
@JsonProperty("key") | ||
private String key; | ||
|
||
@Nullable | ||
@JsonProperty("async") | ||
private Boolean async; | ||
|
||
@Nullable | ||
@JsonProperty("block_list_config") | ||
private BlockListConfigRequestObject blockListConfig; | ||
|
||
public static class UpsertConfigRequest extends StreamRequest<UpsertConfigResponse> { | ||
@NotNull private String key; | ||
|
||
private UpsertConfigRequest(@NotNull String key) { | ||
this.key = key; | ||
} | ||
|
||
@Override | ||
protected Call<UpsertConfigResponse> generateCall(Client client) { | ||
return client | ||
.create(ModerationService.class) | ||
.upsertConfig(this.key(this.key).internalBuild()); | ||
} | ||
} | ||
} | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@EqualsAndHashCode(callSuper = true) | ||
public static class UpsertConfigResponse extends StreamResponseObject { | ||
@Nullable | ||
@JsonProperty("config") | ||
private Config config; | ||
} | ||
|
||
@RequiredArgsConstructor | ||
public static class DeleteConfigRequest extends StreamRequest<StreamResponseObject> { | ||
@NotNull private String key; | ||
|
||
@Override | ||
protected Call<StreamResponseObject> generateCall(Client client) { | ||
return client.create(ModerationService.class).deleteConfig(this.key); | ||
} | ||
} | ||
|
||
@RequiredArgsConstructor | ||
public static class ConfigGetRequest extends StreamRequest<ConfigGetResponse> { | ||
@NotNull private String key; | ||
|
||
@Override | ||
protected Call<ConfigGetResponse> generateCall(Client client) { | ||
return client.create(ModerationService.class).getConfig(this.key); | ||
} | ||
} | ||
|
||
/** | ||
* Creates a get or create request | ||
* | ||
* @param type the channel type | ||
* @param id the channel id | ||
* @return the created request | ||
*/ | ||
@NotNull | ||
public static UpsertConfigRequest upsertConfig(@NotNull String key) { | ||
return new UpsertConfigRequest(key); | ||
} | ||
|
||
@NotNull | ||
public static DeleteConfigRequest deleteConfig(@NotNull String key) { | ||
return new DeleteConfigRequest(key); | ||
} | ||
|
||
@NotNull | ||
public static ConfigGetRequest getConfig(@NotNull String key) { | ||
return new ConfigGetRequest(key); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/io/getstream/chat/java/services/ModerationService.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,19 @@ | ||
package io.getstream.chat.java.services; | ||
|
||
import io.getstream.chat.java.models.Moderation.*; | ||
import io.getstream.chat.java.models.framework.StreamResponseObject; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import retrofit2.Call; | ||
import retrofit2.http.*; | ||
|
||
public interface ModerationService { | ||
@GET("api/v2/moderation/config/{key}") | ||
Call<ConfigGetResponse> getConfig(@NotNull @Path("key") String key); | ||
|
||
@DELETE("api/v2/moderation/config/{key}") | ||
Call<StreamResponseObject> deleteConfig(@NotNull @Path("key") String key); | ||
|
||
@POST("api/v2/moderation/config") | ||
Call<UpsertConfigResponse> upsertConfig(@Nullable @Body UpsertConfigRequestData upsertConfig); | ||
} |
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
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
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,36 @@ | ||
package io.getstream.chat.java; | ||
|
||
import io.getstream.chat.java.exceptions.StreamException; | ||
import io.getstream.chat.java.models.Moderation; | ||
import io.getstream.chat.java.models.Moderation.*; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ModerationTest extends BasicTest { | ||
@DisplayName("Can upsert, get and delete moderation config") | ||
@Test | ||
void whenUpsertingGetttingDeletingModerationConfig_thenNoException() { | ||
BlockListRule rule = | ||
BlockListRule.builder().name("test").action(Moderation.Action.REMOVE).build(); | ||
|
||
String key = "chat:messaging:1234"; | ||
Assertions.assertDoesNotThrow( | ||
() -> | ||
Moderation.upsertConfig(key) | ||
.blockListConfig( | ||
BlockListConfigRequestObject.builder().rules(List.of(rule)).build()) | ||
.request()); | ||
|
||
ConfigGetResponse response = | ||
Assertions.assertDoesNotThrow(() -> Moderation.getConfig(key).request()); | ||
|
||
Assertions.assertEquals( | ||
response.getConfig().getBlockListConfig().getRules().get(0).getName(), "test"); | ||
|
||
Assertions.assertDoesNotThrow(() -> Moderation.deleteConfig(key).request()); | ||
|
||
Assertions.assertThrows(StreamException.class, () -> Moderation.getConfig(key).request()); | ||
} | ||
} |