Skip to content

Commit

Permalink
chore: reactons batch change verb to GET, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmeadi committed Nov 21, 2024
1 parent 2597669 commit c3f21a9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
3 changes: 2 additions & 1 deletion 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,7 +34,7 @@ public CompletableFuture<Reaction> get(String id) throws StreamException {
return reactions.get(token, id);
}

public CompletableFuture<List<Reaction>> getBatch(List<String> ids) throws StreamException {
public CompletableFuture<ReactionBatch> getBatch(List<String> ids) throws StreamException {
final Token token = buildReactionsToken(secret, TokenAction.READ);
return reactions.getBatchReactions(token, ids);
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/io/getstream/core/StreamReactions.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,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 @@ -343,28 +344,27 @@ public CompletableFuture<Void> restore(Token token, String id) throws StreamExce
}
}



public CompletableFuture<List<Reaction>> getBatchReactions(Token token, List<String> ids) throws StreamException {
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);
Map<String, List<String>> payload = ImmutableMap.of("ids", ids);
final byte[] payloadBytes = toJSON(payload);
RequestOption optionIds =
new CustomQueryParameter(
"ids", String.join(",", ids));

return httpClient
.execute(buildPost(url, key, token, payloadBytes))
.execute(buildGet(url, key, token, optionIds))
.thenApply(
response -> {
try {
return deserializeContainer(response, Reaction.class);
return deserialize(response, ReactionBatch.class);
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (JsonProcessingException | MalformedURLException | URISyntaxException e) {
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/io/getstream/core/models/ReactionBatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,29 @@
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 Reaction[] reactions;
private List<Reaction> reactions;

@JsonProperty("duration")
private String duration;

public ReactionBatch() {
}

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

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

public void setReactions(Reaction[] reactions) {
public void setReactions(List<Reaction> reactions) {
this.reactions = reactions;
}
}
29 changes: 17 additions & 12 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 @@ -102,15 +103,19 @@ public void batchFetchReactions() throws Exception {
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());
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();

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());
//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
Expand Down

0 comments on commit c3f21a9

Please sign in to comment.