Skip to content

Commit

Permalink
Merge pull request #151 from GetStream/moderation
Browse files Browse the repository at this point in the history
[MOD-195] add moderation endpoints
  • Loading branch information
vishalnarkhede authored Oct 8, 2024
2 parents 3951db9 + 2b24dae commit dbac346
Show file tree
Hide file tree
Showing 12 changed files with 435 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/main/java/io/getstream/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ public ReactionsClient reactions() {
return new ReactionsClient(secret, stream.reactions());
}

public ModerationClient moderation() {
return new ModerationClient(secret, stream.moderation());
}

public FileStorageClient files() {
return new FileStorageClient(secret, stream.files());
}
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/io/getstream/client/ModerationClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.getstream.client;

import static io.getstream.core.utils.Auth.buildReactionsToken;
import static io.getstream.core.utils.Routes.*;
import static io.getstream.core.utils.Serialization.*;

import io.getstream.core.Moderation;
import io.getstream.core.exceptions.StreamException;
import io.getstream.core.http.Response;
import io.getstream.core.http.Token;
import io.getstream.core.utils.Auth;
import java.util.Map;
import java8.util.concurrent.CompletableFuture;

public class ModerationClient {
private final String secret;
private final Moderation mod;

ModerationClient(String secret, Moderation mod) {
this.secret = secret;
this.mod = mod;
}

public CompletableFuture<Response> flagUser(
String flaggedUserId, String reason, Map<String, Object> options) throws StreamException {
return flag("stream:user", flaggedUserId, "", reason, options);
}

public CompletableFuture<Response> flagActivity(
String entityId, String entityCreatorId, String reason, Map<String, Object> options)
throws StreamException {
return flag("stream:feeds:v2:activity", entityId, entityCreatorId, reason, options);
}

public CompletableFuture<Response> flagReaction(
String entityId, String entityCreatorId, String reason, Map<String, Object> options)
throws StreamException {
return flag("stream:feeds:v2:reaction", entityId, entityCreatorId, reason, options);
}

private CompletableFuture<Response> flag(
String entityType,
String entityId,
String entityCreatorId,
String reason,
Map<String, Object> options)
throws StreamException {
final Token token = buildReactionsToken(secret, Auth.TokenAction.WRITE);
return mod.flag(token, entityType, entityId, entityCreatorId, reason, options);
}
}
54 changes: 54 additions & 0 deletions src/main/java/io/getstream/core/Moderation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.getstream.core;

import static io.getstream.core.utils.Request.buildPost;
import static io.getstream.core.utils.Routes.*;
import static io.getstream.core.utils.Serialization.*;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.getstream.core.exceptions.StreamException;
import io.getstream.core.http.HTTPClient;
import io.getstream.core.http.Response;
import io.getstream.core.http.Token;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;
import java8.util.concurrent.CompletableFuture;
import java8.util.concurrent.CompletionException;

public class Moderation {
private final String key;
private final URL baseURL;
private final HTTPClient httpClient;

public Moderation(String key, URL baseURL, HTTPClient httpClient) {
this.key = key;
this.baseURL = baseURL;
this.httpClient = httpClient;
}

public CompletableFuture<Response> flag(
Token token,
String entityType,
String entityId,
String entityCreatorId,
String reason,
Map<String, Object> options)
throws StreamException {
try {
final byte[] payload =
toJSON(
new Object() {
public final String UserId = entityCreatorId;
public final String EntityType = entityType;
public final String EntityId = entityId;
public final String Reason = reason;
});

final URL url = buildModerationFlagURL(baseURL);
return httpClient.execute(buildPost(url, key, token, payload));
} catch (JsonProcessingException | MalformedURLException | URISyntaxException e) {
throw new CompletionException(e);
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/io/getstream/core/Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public StreamReactions reactions() {
return new StreamReactions(key, baseURL, httpClient);
}

public Moderation moderation() {
return new Moderation(key, baseURL, httpClient);
}

public StreamFiles files() {
return new StreamFiles(key, baseURL, httpClient);
}
Expand Down
32 changes: 19 additions & 13 deletions src/main/java/io/getstream/core/StreamReactions.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ public CompletableFuture<Reaction> add(
if (reaction.getExtra() != null) {
payloadBuilder.put("data", reaction.getExtra());
}
if (reaction.getModerationTemplate() != null) {
payloadBuilder.put("moderation_template", reaction.getModerationTemplate());
}
final byte[] payload = toJSON(payloadBuilder.build());
final URL url = buildReactionsURL(baseURL);
return httpClient
Expand Down Expand Up @@ -284,26 +287,29 @@ public CompletableFuture<Void> update(Token token, Reaction reaction, FeedID...
}
}

public CompletableFuture<Void> delete(Token token, String id, Boolean soft) throws StreamException {
public CompletableFuture<Void> delete(Token token, String id, Boolean soft)
throws StreamException {
checkNotNull(id, "Reaction id can't be null");
checkArgument(!id.isEmpty(), "Reaction id can't be empty");

try {
final URL url = buildReactionsURL(baseURL, id + '/');

final Request deleteRequest = soft ? buildDelete(url, key, token, new CustomQueryParameter("soft", "true"))
: buildDelete(url, key, token);

final Request deleteRequest =
soft
? buildDelete(url, key, token, new CustomQueryParameter("soft", "true"))
: buildDelete(url, key, token);

return httpClient
.execute(deleteRequest)
.thenApply(
response -> {
try {
return deserializeError(response);
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
.execute(deleteRequest)
.thenApply(
response -> {
try {
return deserializeError(response);
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/getstream/core/http/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,4 @@ public Request build() throws MalformedURLException, URISyntaxException {
return new Request(this);
}
}
}
}
37 changes: 37 additions & 0 deletions src/main/java/io/getstream/core/models/APIError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.getstream.core.models;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class APIError {
private String Code;
private String Message;
private String Status;

public String toString() {
return "{Code='" + Code + "', Message=" + Message + "}";
}

// Default constructor
public APIError() {}

// Constructor with parameters
@JsonCreator
public APIError(
@JsonProperty("code") String code,
@JsonProperty("message") String message,
@JsonProperty("status") String status) {
this.Code = code;
this.Message = message;
this.Status = status;
}

// Getters
public String getCode() {
return Code;
}

public String getMessage() {
return Message;
}
}
30 changes: 30 additions & 0 deletions src/main/java/io/getstream/core/models/Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
@JsonInclude(Include.NON_NULL)
@JsonDeserialize(builder = Activity.Builder.class)
public class Activity {

private final String id;
private final String actor;
private final String verb;
Expand All @@ -31,6 +32,8 @@ public class Activity {
private final List<FeedID> to;
private final Double score;
private final Map<String, Object> extra;
private final String moderationTemplate;
private final ModerationResponse moderationResponse;

private Activity(Builder builder) {
id = builder.id;
Expand All @@ -44,6 +47,8 @@ private Activity(Builder builder) {
to = builder.to;
score = builder.score;
extra = builder.extra;
moderationTemplate = builder.moderationTemplate;
moderationResponse = builder.moderationResponse;
}

public String getID() {
Expand Down Expand Up @@ -97,6 +102,16 @@ public Map<String, Object> getExtra() {
return extra;
}

@JsonProperty("moderation")
public ModerationResponse getModerationResponse() {
return moderationResponse;
}

@JsonProperty("moderation_template")
public String getModerationTemplate() {
return moderationTemplate;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down Expand Up @@ -154,12 +169,19 @@ public static final class Builder {
private List<FeedID> to;
private Double score;
private Map<String, Object> extra;
private String moderationTemplate;
private ModerationResponse moderationResponse;

public Builder id(String id) {
this.id = id;
return this;
}

public Builder moderationTemplate(String moderationTemplate) {
this.moderationTemplate = moderationTemplate;
return this;
}

public Builder actor(String actor) {
this.actor = actor;
return this;
Expand All @@ -181,6 +203,12 @@ public Builder foreignID(String foreignID) {
return this;
}

@JsonProperty("moderation")
public Builder setModerationResponse(ModerationResponse mod) {
this.moderationResponse = mod;
return this;
}

public Builder target(String target) {
this.target = target;
return this;
Expand Down Expand Up @@ -250,6 +278,8 @@ public Builder fromActivity(Activity activity) {
this.to = activity.to;
this.score = activity.score;
this.extra = activity.extra;
this.moderationTemplate = activity.moderationTemplate;
this.moderationResponse = activity.moderationResponse;
return this;
}

Expand Down
44 changes: 44 additions & 0 deletions src/main/java/io/getstream/core/models/ModerationResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.getstream.core.models;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class ModerationResponse {
private String Status;
private String RecommendedAction;
private APIError APIError;
private String OriginFeed;

// Default constructor
public ModerationResponse() {}

// Constructor with parameters
@JsonCreator
public ModerationResponse(
@JsonProperty("status") String status,
@JsonProperty("recommended_action") String recommendedAction,
@JsonProperty("api_error") APIError apiError,
@JsonProperty("origin_feed") String originFeed) {
this.Status = status;
this.RecommendedAction = recommendedAction;
this.APIError = apiError;
this.OriginFeed = originFeed;
}

// Getters
public String getStatus() {
return Status;
}

public String getRecommendedAction() {
return RecommendedAction;
}

public APIError getAPIError() {
return APIError;
}

public String getOriginFeed() {
return OriginFeed;
}
}
Loading

0 comments on commit dbac346

Please sign in to comment.