From 1896b944fd04cb579ab0c658634e9ef415ba630d Mon Sep 17 00:00:00 2001 From: Aditya Agarwal Date: Thu, 21 Nov 2024 12:14:31 +0100 Subject: [PATCH] batch reactions get --- .../io/getstream/client/ReactionsClient.java | 5 ++++ .../io/getstream/core/StreamReactions.java | 27 ++++++++++++++++++ .../getstream/core/models/ReactionBatch.java | 26 +++++++++++++++++ .../java/io/getstream/core/utils/Routes.java | 5 ++++ .../getstream/client/ReactionsClientTest.java | 28 +++++++++++++++++++ 5 files changed, 91 insertions(+) create mode 100644 src/main/java/io/getstream/core/models/ReactionBatch.java diff --git a/src/main/java/io/getstream/client/ReactionsClient.java b/src/main/java/io/getstream/client/ReactionsClient.java index f42db8e6..9a09c966 100644 --- a/src/main/java/io/getstream/client/ReactionsClient.java +++ b/src/main/java/io/getstream/client/ReactionsClient.java @@ -33,6 +33,11 @@ public CompletableFuture get(String id) throws StreamException { return reactions.get(token, id); } + public CompletableFuture> getBatch(List ids) throws StreamException { + final Token token = buildReactionsToken(secret, TokenAction.READ); + return reactions.getBatchReactions(token, ids); + } + public CompletableFuture> filter(LookupKind lookup, String id) throws StreamException { return filter(lookup, id, DefaultOptions.DEFAULT_FILTER, DefaultOptions.DEFAULT_LIMIT, ""); diff --git a/src/main/java/io/getstream/core/StreamReactions.java b/src/main/java/io/getstream/core/StreamReactions.java index 7aa2aa28..89e9799f 100644 --- a/src/main/java/io/getstream/core/StreamReactions.java +++ b/src/main/java/io/getstream/core/StreamReactions.java @@ -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; @@ -341,4 +342,30 @@ public CompletableFuture restore(Token token, String id) throws StreamExce throw new StreamException(e); } } + + + + public CompletableFuture> getBatchReactions(Token token, List 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> 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); + } + } } diff --git a/src/main/java/io/getstream/core/models/ReactionBatch.java b/src/main/java/io/getstream/core/models/ReactionBatch.java new file mode 100644 index 00000000..a3583578 --- /dev/null +++ b/src/main/java/io/getstream/core/models/ReactionBatch.java @@ -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; + } +} \ No newline at end of file diff --git a/src/main/java/io/getstream/core/utils/Routes.java b/src/main/java/io/getstream/core/utils/Routes.java index 14f16b3e..dbab60a6 100644 --- a/src/main/java/io/getstream/core/utils/Routes.java +++ b/src/main/java/io/getstream/core/utils/Routes.java @@ -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/"; @@ -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); } diff --git a/src/test/java/io/getstream/client/ReactionsClientTest.java b/src/test/java/io/getstream/client/ReactionsClientTest.java index ba4d784d..cba0d29c 100644 --- a/src/test/java/io/getstream/client/ReactionsClientTest.java +++ b/src/test/java/io/getstream/client/ReactionsClientTest.java @@ -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 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();