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
35 changes: 35 additions & 0 deletions src/main/java/io/getstream/client/ReactionsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.getstream.core.exceptions.StreamException;
import io.getstream.core.http.Token;
import io.getstream.core.models.FeedID;
import io.getstream.core.models.Pagenated;
import io.getstream.core.models.Reaction;
import io.getstream.core.options.Filter;
import io.getstream.core.options.Limit;
Expand Down Expand Up @@ -62,6 +63,40 @@ public CompletableFuture<List<Reaction>> filter(LookupKind lookup, String id, Fi
return reactions.filter(token, lookup, id, filter, limit, kind);
}

public CompletableFuture<Pagenated<Reaction>> pagenatedFilter(LookupKind lookup, String id) throws StreamException {
return pagenatedFilter(lookup, id, DefaultOptions.DEFAULT_FILTER, DefaultOptions.DEFAULT_LIMIT, "");
}

public CompletableFuture<Pagenated<Reaction>> pagenatedFilter(LookupKind lookup, String id, Limit limit) throws StreamException {
return pagenatedFilter(lookup, id, DefaultOptions.DEFAULT_FILTER, limit, "");
}

public CompletableFuture<Pagenated<Reaction>> pagenatedFilter(LookupKind lookup, String id, Filter filter) throws StreamException {
return pagenatedFilter(lookup, id, filter, DefaultOptions.DEFAULT_LIMIT, "");
}

public CompletableFuture<Pagenated<Reaction>> pagenatedFilter(LookupKind lookup, String id, String kind) throws StreamException {
return pagenatedFilter(lookup, id, DefaultOptions.DEFAULT_FILTER, DefaultOptions.DEFAULT_LIMIT, kind);
}

public CompletableFuture<Pagenated<Reaction>> pagenatedFilter(LookupKind lookup, String id, Filter filter, Limit limit) throws StreamException {
return pagenatedFilter(lookup, id, filter, limit, "");
}

public CompletableFuture<Pagenated<Reaction>> pagenatedFilter(LookupKind lookup, String id, Limit limit, String kind) throws StreamException {
return pagenatedFilter(lookup, id, DefaultOptions.DEFAULT_FILTER, limit, kind);
}

public CompletableFuture<Pagenated<Reaction>> pagenatedFilter(LookupKind lookup, String id, Filter filter, Limit limit, String kind) throws StreamException {
final Token token = buildReactionsToken(secret, TokenAction.READ);
return reactions.pagenatedFilter(token, lookup, id, filter, limit, kind);
}

public CompletableFuture<Pagenated<Reaction>> pagenatedFilter(String next) throws StreamException {
final Token token = buildReactionsToken(secret, TokenAction.READ);
return reactions.pagenatedFilter(token, next);
}

public CompletableFuture<Reaction> add(String userID, String kind, String activityID, Iterable<FeedID> targetFeeds) throws StreamException {
return add(userID, kind, activityID, Iterables.toArray(targetFeeds, FeedID.class));
}
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/io/getstream/core/StreamReactions.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package io.getstream.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.ImmutableMap;
import io.getstream.core.exceptions.StreamException;
import io.getstream.core.http.HTTPClient;
import io.getstream.core.http.Token;
import io.getstream.core.models.FeedID;
import io.getstream.core.models.Pagenated;
import io.getstream.core.models.Reaction;
import io.getstream.core.options.CustomQueryParameter;
import io.getstream.core.options.Filter;
Expand Down Expand Up @@ -59,6 +61,25 @@ public CompletableFuture<Reaction> get(Token token, String id) throws StreamExce
}
}

public CompletableFuture<Pagenated> getPagenated(Token token, String id) 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 + '/');
return httpClient.execute(buildGet(url, key, token))
.thenApply(response -> {
try {
return deserialize(response, Pagenated.class);
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}

public CompletableFuture<List<Reaction>> filter(Token token, LookupKind lookup, String id, Filter filter, Limit limit, String kind) throws StreamException {
checkNotNull(lookup, "Lookup kind can't be null");
checkNotNull(id, "Reaction ID can't be null");
Expand All @@ -82,6 +103,47 @@ public CompletableFuture<List<Reaction>> filter(Token token, LookupKind lookup,
}
}

public CompletableFuture<Pagenated<Reaction>> pagenatedFilter(Token token, LookupKind lookup, String id, Filter filter, Limit limit, String kind) throws StreamException {
checkNotNull(lookup, "Lookup kind can't be null");
checkNotNull(id, "Reaction ID can't be null");
checkArgument(!id.isEmpty(), "Reaction ID can't be empty");
checkNotNull(filter, "Filter can't be null");
checkNotNull(kind, "Kind can't be null");

try {
final URL url = buildReactionsURL(baseURL, lookup.getKind() + '/' + id + '/' + kind);
RequestOption withActivityData = new CustomQueryParameter("with_activity_data", Boolean.toString(lookup == LookupKind.ACTIVITY_WITH_DATA));
return httpClient.execute(buildGet(url, key, token, filter, limit, withActivityData))
.thenApply(response -> {
try {
return deserialize(response, new TypeReference<Pagenated<Reaction>>() {});
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}

public CompletableFuture<Pagenated<Reaction>> pagenatedFilter(Token token, String next) throws StreamException {
checkNotNull(next, "next can't be null");

try {
final URL url = new URL(baseURL, next);
return httpClient.execute(buildGet(url, key, token))
.thenApply(response -> {
try {
return deserialize(response, new TypeReference<Pagenated<Reaction>>() {});
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}

public CompletableFuture<Reaction> add(Token token, String userID, Reaction reaction, FeedID... targetFeeds) throws StreamException {
checkNotNull(reaction, "Reaction can't be null");
checkArgument(reaction.getActivityID() != null || reaction.getParent() != null, "Reaction has to either have and activity ID or parent");
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/io/getstream/core/models/Pagenated.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.getstream.core.models;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.MoreObjects;
import java.util.List;
import java.util.Objects;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Pagenated<T> {

private final String next;
private List<T> results;
private final String duration;

public String getNext() {
return next;
}

public List<T> getResults() {
return results;
}

public void setResults(List<T> results) {
this.results = results;
}

public String getDuration() {
return duration;
}

@JsonCreator
private Pagenated(
@JsonProperty("next")
String next,
@JsonProperty("results")
List<T> results,
@JsonProperty("duration")
String duration
) {
this.next = next;
this.results = results;
this.duration = duration;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Pagenated that = (Pagenated) o;
return next.equals(that.next) &&
duration.equals(that.duration);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), next, duration);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("next", this.next)
.add("results", this.results)
.add("duration", this.duration)
.toString();
}

}
24 changes: 23 additions & 1 deletion src/test/java/io/getstream/client/ReactionsClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.getstream.core.LookupKind;
import io.getstream.core.models.Activity;
import io.getstream.core.models.FeedID;
import io.getstream.core.models.Pagenated;
import io.getstream.core.models.Reaction;
import org.junit.Test;

Expand Down Expand Up @@ -41,6 +42,27 @@ public void filter() throws Exception {
List<Reaction> result = client.reactions().filter(LookupKind.ACTIVITY_WITH_DATA, activity.getID(), "comment").join();
}

@Test
public void pagedFilter() throws Exception {
Client client = Client.builder(apiKey, secret).build();

Activity activity = client.flatFeed("flat", "reactor").addActivity(Activity.builder()
.actor("this")
.verb("done")
.object("that")
.build()).join();

client.reactions().add("john-doe", "like", activity.getID()).join();

client.reactions().pagenatedFilter(LookupKind.ACTIVITY, activity.getID()).join();

Pagenated<Reaction> result = client.reactions()
.pagenatedFilter(LookupKind.ACTIVITY_WITH_DATA, activity.getID(), "comment").join();
while (result.getNext() != null && !result.getNext().isEmpty()) {
result = client.reactions().pagenatedFilter(result.getNext()).join();
}
}

@Test
public void add() throws Exception {
Client client = Client.builder(apiKey, secret).build();
Expand Down Expand Up @@ -87,4 +109,4 @@ public void delete() throws Exception {
Reaction reply = client.reactions().add("user-id", data, new FeedID("flat", "1")).join();
client.reactions().delete(reply.getId()).join();
}
}
}