Skip to content

Commit

Permalink
batch reactions get
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmeadi committed Nov 21, 2024
1 parent 64d0c23 commit 1896b94
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/io/getstream/client/ReactionsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public CompletableFuture<Reaction> get(String id) throws StreamException {
return reactions.get(token, id);
}

public CompletableFuture<List<Reaction>> getBatch(List<String> ids) throws StreamException {
final Token token = buildReactionsToken(secret, TokenAction.READ);
return reactions.getBatchReactions(token, ids);
}

public CompletableFuture<List<Reaction>> filter(LookupKind lookup, String id)
throws StreamException {
return filter(lookup, id, DefaultOptions.DEFAULT_FILTER, DefaultOptions.DEFAULT_LIMIT, "");
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/io/getstream/core/StreamReactions.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static io.getstream.core.utils.Request.*;
import static io.getstream.core.utils.Routes.buildReactionsURL;
import static io.getstream.core.utils.Routes.buildGetReactionsBatchURL;
import static io.getstream.core.utils.Serialization.*;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -341,4 +342,30 @@ public CompletableFuture<Void> restore(Token token, String id) throws StreamExce
throw new StreamException(e);
}
}



public CompletableFuture<List<Reaction>> getBatchReactions(Token token, List<String> ids) throws StreamException {
checkNotNull(ids, "Reaction IDs can't be null");
checkArgument(!ids.isEmpty(), "Reaction IDs can't be empty");

try {
final URL url = buildGetReactionsBatchURL(baseURL);
Map<String, List<String>> payload = ImmutableMap.of("ids", ids);
final byte[] payloadBytes = toJSON(payload);

return httpClient
.execute(buildPost(url, key, token, payloadBytes))
.thenApply(
response -> {
try {
return deserializeContainer(response, Reaction.class);
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (JsonProcessingException | MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
}
26 changes: 26 additions & 0 deletions src/main/java/io/getstream/core/models/ReactionBatch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.getstream.core.models;

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

@JsonInclude(JsonInclude.Include.NON_NULL)
public class ReactionBatch {

@JsonProperty("reactions")
private Reaction[] reactions;

public ReactionBatch() {
}

public ReactionBatch(Reaction[] reactions) {
this.reactions = reactions;
}

public Reaction[] getReactions() {
return reactions;
}

public void setReactions(Reaction[] reactions) {
this.reactions = reactions;
}
}
5 changes: 5 additions & 0 deletions src/main/java/io/getstream/core/utils/Routes.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public final class Routes {
private static final String imagesPath = "images/";
private static final String openGraphPath = "og/";
private static final String reactionsPath = "reaction/";
private static final String reactionsBatchPath = "reaction/get_many/";
private static final String toTargetUpdatePath = "/activity_to_targets/";
private static final String usersPath = "user/";
private static final String followStatsPath = "stats/follow/";
Expand Down Expand Up @@ -70,6 +71,10 @@ public static URL buildReactionsURL(URL baseURL, String path) throws MalformedUR
return new URL(baseURL, basePath + reactionsPath + path);
}

public static URL buildGetReactionsBatchURL(URL baseURL) throws MalformedURLException {
return new URL(baseURL, basePath + reactionsBatchPath);
}

public static URL buildUsersURL(URL baseURL) throws MalformedURLException {
return new URL(baseURL, basePath + usersPath);
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/io/getstream/client/ReactionsClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,34 @@ public void filterWithUserID() throws Exception {
assertEquals(1, result.size());
}

@Test
public void batchFetchReactions() 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();

Reaction r1=client.reactions().add("user1", "like", activity.getID()).join();
Reaction r2=client.reactions().add("user1", "comment", activity.getID()).join();
Reaction r3=client.reactions().add("user1", "share", activity.getID()).join();
Reaction r4=client.reactions().add("user2", "like", activity.getID()).join();
Reaction r5=client.reactions().add("user2", "comment", activity.getID()).join();
Reaction r6=client.reactions().add("user3", "comment", activity.getID()).join();

List<Reaction> result = client.reactions().getBatch(List.of(r1.getId(), r2.getId(), r3.getId(), r4.getId(), r5.getId(), r6.getId())).join();
assertEquals(6, result.size());

assertEquals("like", result.get(0).getKind());
assertEquals("comment", result.get(1).getKind());
assertEquals("share", result.get(2).getKind());
assertEquals("like", result.get(3).getKind());
assertEquals("comment", result.get(4).getKind());
assertEquals("comment", result.get(5).getKind());
}

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

0 comments on commit 1896b94

Please sign in to comment.