Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEEDS-44]batch reactions get #159

Merged
merged 2 commits into from
Nov 21, 2024
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
6 changes: 6 additions & 0 deletions src/main/java/io/getstream/client/ReactionsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.getstream.core.models.FeedID;
import io.getstream.core.models.Paginated;
import io.getstream.core.models.Reaction;
import io.getstream.core.models.ReactionBatch;
import io.getstream.core.options.Filter;
import io.getstream.core.options.Limit;
import io.getstream.core.utils.Auth.TokenAction;
Expand All @@ -33,6 +34,11 @@ public CompletableFuture<Reaction> get(String id) throws StreamException {
return reactions.get(token, id);
}

public CompletableFuture<ReactionBatch> 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 All @@ -17,6 +18,7 @@
import io.getstream.core.models.FeedID;
import io.getstream.core.models.Paginated;
import io.getstream.core.models.Reaction;
import io.getstream.core.models.ReactionBatch;
import io.getstream.core.options.CustomQueryParameter;
import io.getstream.core.options.Filter;
import io.getstream.core.options.Limit;
Expand Down Expand Up @@ -341,4 +343,29 @@ public CompletableFuture<Void> restore(Token token, String id) throws StreamExce
throw new StreamException(e);
}
}

public CompletableFuture<ReactionBatch> 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);
RequestOption optionIds =
new CustomQueryParameter(
"ids", String.join(",", ids));

return httpClient
.execute(buildGet(url, key, token, optionIds))
.thenApply(
response -> {
try {
return deserialize(response, ReactionBatch.class);
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
}
31 changes: 31 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,31 @@
package io.getstream.core.models;

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

import java.util.List;

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

@JsonProperty("reactions")
private List<Reaction> reactions;

@JsonProperty("duration")
private String duration;

public ReactionBatch() {
}

public ReactionBatch(List<Reaction> reactions) {
this.reactions = reactions;
}

public List<Reaction> getReactions() {
return reactions;
}

public void setReactions(List<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
41 changes: 37 additions & 4 deletions src/test/java/io/getstream/client/ReactionsClientTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package io.getstream.client;

import io.getstream.core.LookupKind;
import io.getstream.core.models.Activity;
import io.getstream.core.models.FeedID;
import io.getstream.core.models.Paginated;
import io.getstream.core.models.Reaction;
import io.getstream.core.models.*;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import io.getstream.core.options.Filter;
import io.getstream.core.options.Limit;
Expand Down Expand Up @@ -85,6 +86,38 @@ 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();

Map<String, Reaction> reactionsRequest = Map.of(r1.getId(), r1, r2.getId(), r2, r3.getId(), r3, r4.getId(), r4, r5.getId(), r5, r6.getId(), r6);

ReactionBatch response = client.reactions().getBatch(List.of(r1.getId(), r2.getId(), r3.getId(), r4.getId(), r5.getId(), r6.getId())).join();
List<Reaction> result = response.getReactions();

//convert result to map and compare each id and type mapping from reactionsRequest to result
Map<String, Reaction> resultMap = result.stream().collect(Collectors.toMap(Reaction::getId, Function.identity()));
assertEquals(6, resultMap.size());
for (Reaction r : result) {
Reaction req = reactionsRequest.get(r.getId());
assertEquals(req.getActivityID(), r.getActivityID());
assertEquals(req.getKind(), r.getKind());
}
}

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