Skip to content

Commit 3c94ac6

Browse files
authored
Merge pull request #159 from GetStream/batch_reactions
[FEEDS-44]batch reactions get
2 parents 64d0c23 + c3f21a9 commit 3c94ac6

File tree

5 files changed

+106
-4
lines changed

5 files changed

+106
-4
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.getstream.core.models.FeedID;
1313
import io.getstream.core.models.Paginated;
1414
import io.getstream.core.models.Reaction;
15+
import io.getstream.core.models.ReactionBatch;
1516
import io.getstream.core.options.Filter;
1617
import io.getstream.core.options.Limit;
1718
import io.getstream.core.utils.Auth.TokenAction;
@@ -33,6 +34,11 @@ public CompletableFuture<Reaction> get(String id) throws StreamException {
3334
return reactions.get(token, id);
3435
}
3536

37+
public CompletableFuture<ReactionBatch> getBatch(List<String> ids) throws StreamException {
38+
final Token token = buildReactionsToken(secret, TokenAction.READ);
39+
return reactions.getBatchReactions(token, ids);
40+
}
41+
3642
public CompletableFuture<List<Reaction>> filter(LookupKind lookup, String id)
3743
throws StreamException {
3844
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;
@@ -17,6 +18,7 @@
1718
import io.getstream.core.models.FeedID;
1819
import io.getstream.core.models.Paginated;
1920
import io.getstream.core.models.Reaction;
21+
import io.getstream.core.models.ReactionBatch;
2022
import io.getstream.core.options.CustomQueryParameter;
2123
import io.getstream.core.options.Filter;
2224
import io.getstream.core.options.Limit;
@@ -341,4 +343,29 @@ public CompletableFuture<Void> restore(Token token, String id) throws StreamExce
341343
throw new StreamException(e);
342344
}
343345
}
346+
347+
public CompletableFuture<ReactionBatch> getBatchReactions(Token token, List<String> ids) throws StreamException {
348+
checkNotNull(ids, "Reaction IDs can't be null");
349+
checkArgument(!ids.isEmpty(), "Reaction IDs can't be empty");
350+
351+
try {
352+
final URL url = buildGetReactionsBatchURL(baseURL);
353+
RequestOption optionIds =
354+
new CustomQueryParameter(
355+
"ids", String.join(",", ids));
356+
357+
return httpClient
358+
.execute(buildGet(url, key, token, optionIds))
359+
.thenApply(
360+
response -> {
361+
try {
362+
return deserialize(response, ReactionBatch.class);
363+
} catch (StreamException | IOException e) {
364+
throw new CompletionException(e);
365+
}
366+
});
367+
} catch (MalformedURLException | URISyntaxException e) {
368+
throw new StreamException(e);
369+
}
370+
}
344371
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.getstream.core.models;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
6+
import java.util.List;
7+
8+
@JsonInclude(JsonInclude.Include.NON_NULL)
9+
public class ReactionBatch {
10+
11+
@JsonProperty("reactions")
12+
private List<Reaction> reactions;
13+
14+
@JsonProperty("duration")
15+
private String duration;
16+
17+
public ReactionBatch() {
18+
}
19+
20+
public ReactionBatch(List<Reaction> reactions) {
21+
this.reactions = reactions;
22+
}
23+
24+
public List<Reaction> getReactions() {
25+
return reactions;
26+
}
27+
28+
public void setReactions(List<Reaction> reactions) {
29+
this.reactions = reactions;
30+
}
31+
}

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: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package io.getstream.client;
22

33
import io.getstream.core.LookupKind;
4-
import io.getstream.core.models.Activity;
5-
import io.getstream.core.models.FeedID;
6-
import io.getstream.core.models.Paginated;
7-
import io.getstream.core.models.Reaction;
4+
import io.getstream.core.models.*;
5+
86
import java.util.List;
7+
import java.util.Map;
8+
import java.util.function.Function;
9+
import java.util.stream.Collectors;
910

1011
import io.getstream.core.options.Filter;
1112
import io.getstream.core.options.Limit;
@@ -85,6 +86,38 @@ public void filterWithUserID() throws Exception {
8586
assertEquals(1, result.size());
8687
}
8788

89+
@Test
90+
public void batchFetchReactions() throws Exception {
91+
Client client = Client.builder(apiKey, secret).build();
92+
93+
Activity activity =
94+
client
95+
.flatFeed("flat", "reactor")
96+
.addActivity(Activity.builder().actor("this").verb("done").object("that").build())
97+
.join();
98+
99+
Reaction r1=client.reactions().add("user1", "like", activity.getID()).join();
100+
Reaction r2=client.reactions().add("user1", "comment", activity.getID()).join();
101+
Reaction r3=client.reactions().add("user1", "share", activity.getID()).join();
102+
Reaction r4=client.reactions().add("user2", "like", activity.getID()).join();
103+
Reaction r5=client.reactions().add("user2", "comment", activity.getID()).join();
104+
Reaction r6=client.reactions().add("user3", "comment", activity.getID()).join();
105+
106+
Map<String, Reaction> reactionsRequest = Map.of(r1.getId(), r1, r2.getId(), r2, r3.getId(), r3, r4.getId(), r4, r5.getId(), r5, r6.getId(), r6);
107+
108+
ReactionBatch response = client.reactions().getBatch(List.of(r1.getId(), r2.getId(), r3.getId(), r4.getId(), r5.getId(), r6.getId())).join();
109+
List<Reaction> result = response.getReactions();
110+
111+
//convert result to map and compare each id and type mapping from reactionsRequest to result
112+
Map<String, Reaction> resultMap = result.stream().collect(Collectors.toMap(Reaction::getId, Function.identity()));
113+
assertEquals(6, resultMap.size());
114+
for (Reaction r : result) {
115+
Reaction req = reactionsRequest.get(r.getId());
116+
assertEquals(req.getActivityID(), r.getActivityID());
117+
assertEquals(req.getKind(), r.getKind());
118+
}
119+
}
120+
88121
@Test
89122
public void pagedFilter() throws Exception {
90123
Client client = Client.builder(apiKey, secret).build();

0 commit comments

Comments
 (0)