Skip to content

Commit

Permalink
feat: add moderation apis (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdeme authored Mar 23, 2022
1 parent 3125bba commit 106ded4
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 5 deletions.
152 changes: 151 additions & 1 deletion src/main/java/io/getstream/chat/java/models/Flag.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import io.getstream.chat.java.models.Flag.FlagCreateRequestData.FlagCreateRequest;
import io.getstream.chat.java.models.Flag.FlagDeleteRequestData.FlagDeleteRequest;
import io.getstream.chat.java.models.Flag.FlagMessageQueryRequestData.FlagMessageQueryRequest;
import io.getstream.chat.java.models.Flag.QueryFlagReportsRequestData.QueryFlagReportsRequest;
import io.getstream.chat.java.models.Flag.ReviewFlagReportRequestData.ReviewFlagReportRequest;
import io.getstream.chat.java.models.Message.Moderation;
import io.getstream.chat.java.models.User.UserRequestObject;
import io.getstream.chat.java.models.framework.StreamRequest;
Expand Down Expand Up @@ -102,6 +104,51 @@ public static class MessageFlag {
private Date rejectedAt;
}

@Data
@NoArgsConstructor
public static class FlagReport {
@NotNull
@JsonProperty("id")
private String Id;

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

@Nullable
@JsonProperty("flags_count")
private Integer flagsCount;

@Nullable
@JsonProperty("message_user_id")
private String messageUserId;

@Nullable
@JsonProperty("channel_cid")
private String channelCid;

@Nullable
@JsonProperty("created_at")
private Date createdAt;

@Nullable
@JsonProperty("updated_at")
private Date updatedAt;
}

@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public static class ExtendedFlagReport extends FlagReport {
@NotNull
@JsonProperty("review_result")
private String reviewResult;

@Nullable
@JsonProperty("review_details")
private Map<String, Object> reviewDetails;
}

@Data
@NoArgsConstructor
public static class MessageModerationResult {
Expand Down Expand Up @@ -224,7 +271,7 @@ protected Call<FlagDeleteResponse> generateCall(Client client) {
buildMethodName = "internalBuild")
public static class FlagMessageQueryRequestData {
@Nullable
@JsonProperty("filterConditions")
@JsonProperty("filter_conditions")
@Singular
private Map<String, Object> filterConditions;

Expand Down Expand Up @@ -252,6 +299,71 @@ protected Call<FlagMessageQueryResponse> generateCall(Client client) {
}
}

@Builder(
builderClassName = "QueryFlagReportsRequest",
builderMethodName = "",
buildMethodName = "internalBuild")
public static class QueryFlagReportsRequestData {
@Nullable
@JsonProperty("filter_conditions")
@Singular
private Map<String, Object> filterConditions;

@Nullable
@JsonProperty("limit")
private Integer limit;

@Nullable
@JsonProperty("offset")
private Integer offset;

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

@Nullable
@JsonProperty("user")
private UserRequestObject user;

public static class QueryFlagReportsRequest extends StreamRequest<QueryFlagReportsResponse> {
@Override
protected Call<QueryFlagReportsResponse> generateCall(Client client) {
return client.create(FlagService.class).queryFlagReports(this.internalBuild());
}
}
}

@Builder(
builderClassName = "ReviewFlagReportRequest",
builderMethodName = "",
buildMethodName = "internalBuild")
public static class ReviewFlagReportRequestData {
@NotNull
@JsonProperty("review_result")
private String reviewResult;

@Nullable
@JsonProperty("review_details")
private Map<String, Object> reviewDetails;

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

public static class ReviewFlagReportRequest extends StreamRequest<ReviewFlagReportResponse> {
@NotNull private String id;

public ReviewFlagReportRequest(@NotNull String id) {
this.id = id;
}

@Override
protected Call<ReviewFlagReportResponse> generateCall(Client client) {
return client.create(FlagService.class).reviewFlagReport(id, this.internalBuild());
}
}
}

@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
Expand Down Expand Up @@ -279,6 +391,24 @@ public static class FlagMessageQueryResponse extends StreamResponseObject {
private List<MessageFlag> flags;
}

@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public static class QueryFlagReportsResponse extends StreamResponseObject {
@NotNull
@JsonProperty("flag_reports")
private List<FlagReport> flagReports;
}

@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public static class ReviewFlagReportResponse extends StreamResponseObject {
@NotNull
@JsonProperty("flag_report")
private ExtendedFlagReport flagReport;
}

/**
* Creates a create request
*
Expand Down Expand Up @@ -308,4 +438,24 @@ public static FlagDeleteRequest delete() {
public static FlagMessageQueryRequest queryMessages() {
return new FlagMessageQueryRequest();
}

/**
* Creates a query flag report request
*
* @return the created request
*/
@NotNull
public static QueryFlagReportsRequest queryFlagReports() {
return new QueryFlagReportsRequest();
}

/**
* Creates a review flag report request
*
* @return the created request
*/
@NotNull
public static ReviewFlagReportRequest reviewFlagReport(String id) {
return new ReviewFlagReportRequest(id);
}
}
18 changes: 14 additions & 4 deletions src/main/java/io/getstream/chat/java/services/FlagService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import io.getstream.chat.java.models.Flag.FlagDeleteResponse;
import io.getstream.chat.java.models.Flag.FlagMessageQueryRequestData;
import io.getstream.chat.java.models.Flag.FlagMessageQueryResponse;
import io.getstream.chat.java.models.Flag.QueryFlagReportsRequestData;
import io.getstream.chat.java.models.Flag.QueryFlagReportsResponse;
import io.getstream.chat.java.models.Flag.ReviewFlagReportRequestData;
import io.getstream.chat.java.models.Flag.ReviewFlagReportResponse;
import io.getstream.chat.java.services.framework.ToJson;
import org.jetbrains.annotations.NotNull;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;
import retrofit2.http.*;

public interface FlagService {

Expand All @@ -25,4 +26,13 @@ public interface FlagService {
@GET("moderation/flags/message")
Call<FlagMessageQueryResponse> messageQuery(
@NotNull @ToJson @Query("payload") FlagMessageQueryRequestData flagMessageQueryRequestData);

@POST("moderation/reports")
Call<QueryFlagReportsResponse> queryFlagReports(
@NotNull @Body QueryFlagReportsRequestData queryFlagReportsRequestData);

@PATCH("moderation/reports/{id}")
Call<ReviewFlagReportResponse> reviewFlagReport(
@NotNull @Path("id") String id,
@NotNull @Body ReviewFlagReportRequestData reviewFlagReportRequestData);
}
42 changes: 42 additions & 0 deletions src/test/java/io/getstream/chat/java/FlagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,46 @@ void whenQueryingMessageFlags_thenRetrieved() {
response.getFlags().stream()
.anyMatch(flag -> flag.getMessage().getId().equals(message.getId())));
}

@DisplayName("Can look up flag reports")
@Test
void whenQueryingFlagReports_thenRetrieved() {
Message message = Assertions.assertDoesNotThrow(() -> sendTestMessage());
Assertions.assertDoesNotThrow(
() -> Flag.create().targetMessageId(message.getId()).user(testUserRequestObject).request());
var flagReports =
Assertions.assertDoesNotThrow(
() ->
Flag.queryFlagReports()
.filterCondition("message_id", message.getId())
.request()
.getFlagReports());
Assertions.assertEquals(message.getId(), flagReports.get(0).getMessage().getId());
}

@DisplayName("Can send flag report")
@Test
void whenReviewingFlagReports_thenSucceeds() {
Message message = Assertions.assertDoesNotThrow(() -> sendTestMessage());
Assertions.assertDoesNotThrow(
() -> Flag.create().targetMessageId(message.getId()).user(testUserRequestObject).request());
var flagReports =
Assertions.assertDoesNotThrow(
() ->
Flag.queryFlagReports()
.filterCondition("message_id", message.getId())
.request()
.getFlagReports());
Assertions.assertEquals(message.getId(), flagReports.get(0).getMessage().getId());

var report =
Assertions.assertDoesNotThrow(
() ->
Flag.reviewFlagReport(flagReports.get(0).getId())
.reviewResult("reviewed")
.userId(testUserRequestObject.getId())
.request()
.getFlagReport());
Assertions.assertEquals(message.getId(), report.getMessage().getId());
}
}

0 comments on commit 106ded4

Please sign in to comment.