Skip to content

Commit 2597669

Browse files
committed
chore: batch reactions get
1 parent 64d0c23 commit 2597669

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed

src/main/java/io/getstream/client/ReactionsClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public CompletableFuture<Reaction> get(String id) throws StreamException {
3333
return reactions.get(token, id);
3434
}
3535

36+
public CompletableFuture<List<Reaction>> getBatch(List<String> ids) throws StreamException {
37+
final Token token = buildReactionsToken(secret, TokenAction.READ);
38+
return reactions.getBatchReactions(token, ids);
39+
}
40+
3641
public CompletableFuture<List<Reaction>> filter(LookupKind lookup, String id)
3742
throws StreamException {
3843
return filter(lookup, id, DefaultOptions.DEFAULT_FILTER, DefaultOptions.DEFAULT_LIMIT, "");

src/main/java/io/getstream/core/StreamReactions.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static com.google.common.base.Preconditions.checkNotNull;
66
import static io.getstream.core.utils.Request.*;
77
import static io.getstream.core.utils.Routes.buildReactionsURL;
8+
import static io.getstream.core.utils.Routes.buildGetReactionsBatchURL;
89
import static io.getstream.core.utils.Serialization.*;
910

1011
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -341,4 +342,30 @@ public CompletableFuture<Void> restore(Token token, String id) throws StreamExce
341342
throw new StreamException(e);
342343
}
343344
}
345+
346+
347+
348+
public CompletableFuture<List<Reaction>> getBatchReactions(Token token, List<String> ids) throws StreamException {
349+
checkNotNull(ids, "Reaction IDs can't be null");
350+
checkArgument(!ids.isEmpty(), "Reaction IDs can't be empty");
351+
352+
try {
353+
final URL url = buildGetReactionsBatchURL(baseURL);
354+
Map<String, List<String>> payload = ImmutableMap.of("ids", ids);
355+
final byte[] payloadBytes = toJSON(payload);
356+
357+
return httpClient
358+
.execute(buildPost(url, key, token, payloadBytes))
359+
.thenApply(
360+
response -> {
361+
try {
362+
return deserializeContainer(response, Reaction.class);
363+
} catch (StreamException | IOException e) {
364+
throw new CompletionException(e);
365+
}
366+
});
367+
} catch (JsonProcessingException | MalformedURLException | URISyntaxException e) {
368+
throw new StreamException(e);
369+
}
370+
}
344371
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.getstream.core.models;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
6+
@JsonInclude(JsonInclude.Include.NON_NULL)
7+
public class ReactionBatch {
8+
9+
@JsonProperty("reactions")
10+
private Reaction[] reactions;
11+
12+
public ReactionBatch() {
13+
}
14+
15+
public ReactionBatch(Reaction[] reactions) {
16+
this.reactions = reactions;
17+
}
18+
19+
public Reaction[] getReactions() {
20+
return reactions;
21+
}
22+
23+
public void setReactions(Reaction[] reactions) {
24+
this.reactions = reactions;
25+
}
26+
}

src/main/java/io/getstream/core/utils/Routes.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public final class Routes {
2424
private static final String imagesPath = "images/";
2525
private static final String openGraphPath = "og/";
2626
private static final String reactionsPath = "reaction/";
27+
private static final String reactionsBatchPath = "reaction/get_many/";
2728
private static final String toTargetUpdatePath = "/activity_to_targets/";
2829
private static final String usersPath = "user/";
2930
private static final String followStatsPath = "stats/follow/";
@@ -70,6 +71,10 @@ public static URL buildReactionsURL(URL baseURL, String path) throws MalformedUR
7071
return new URL(baseURL, basePath + reactionsPath + path);
7172
}
7273

74+
public static URL buildGetReactionsBatchURL(URL baseURL) throws MalformedURLException {
75+
return new URL(baseURL, basePath + reactionsBatchPath);
76+
}
77+
7378
public static URL buildUsersURL(URL baseURL) throws MalformedURLException {
7479
return new URL(baseURL, basePath + usersPath);
7580
}

src/test/java/io/getstream/client/ReactionsClientTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,34 @@ public void filterWithUserID() throws Exception {
8585
assertEquals(1, result.size());
8686
}
8787

88+
@Test
89+
public void batchFetchReactions() throws Exception {
90+
Client client = Client.builder(apiKey, secret).build();
91+
92+
Activity activity =
93+
client
94+
.flatFeed("flat", "reactor")
95+
.addActivity(Activity.builder().actor("this").verb("done").object("that").build())
96+
.join();
97+
98+
Reaction r1=client.reactions().add("user1", "like", activity.getID()).join();
99+
Reaction r2=client.reactions().add("user1", "comment", activity.getID()).join();
100+
Reaction r3=client.reactions().add("user1", "share", activity.getID()).join();
101+
Reaction r4=client.reactions().add("user2", "like", activity.getID()).join();
102+
Reaction r5=client.reactions().add("user2", "comment", activity.getID()).join();
103+
Reaction r6=client.reactions().add("user3", "comment", activity.getID()).join();
104+
105+
List<Reaction> result = client.reactions().getBatch(List.of(r1.getId(), r2.getId(), r3.getId(), r4.getId(), r5.getId(), r6.getId())).join();
106+
assertEquals(6, result.size());
107+
108+
assertEquals("like", result.get(0).getKind());
109+
assertEquals("comment", result.get(1).getKind());
110+
assertEquals("share", result.get(2).getKind());
111+
assertEquals("like", result.get(3).getKind());
112+
assertEquals("comment", result.get(4).getKind());
113+
assertEquals("comment", result.get(5).getKind());
114+
}
115+
88116
@Test
89117
public void pagedFilter() throws Exception {
90118
Client client = Client.builder(apiKey, secret).build();

0 commit comments

Comments
 (0)