Skip to content

Commit 7ae0feb

Browse files
committed
chore: add extradata support reaction
1 parent bd21017 commit 7ae0feb

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.getstream.core.utils.Auth.TokenAction;
1919
import io.getstream.core.utils.DefaultOptions;
2020
import java.util.List;
21+
import java.util.Map;
2122
import java8.util.concurrent.CompletableFuture;
2223

2324
public final class ReactionsClient {
@@ -167,6 +168,12 @@ public CompletableFuture<Reaction> add(String userID, Reaction reaction, FeedID.
167168
return reactions.add(token, userID, reaction, targetFeeds);
168169
}
169170

171+
public CompletableFuture<Reaction> add(String userID, Reaction reaction, FeedID[] targetFeeds, Map<String, Object> targetFeedsExtraData)
172+
throws StreamException {
173+
final Token token = buildReactionsToken(secret, TokenAction.WRITE);
174+
return reactions.add(token, userID, reaction, targetFeeds, targetFeedsExtraData);
175+
}
176+
170177
public CompletableFuture<Reaction> addChild(
171178
String userID, String kind, String parentID, Iterable<FeedID> targetFeeds)
172179
throws StreamException {
@@ -180,6 +187,12 @@ public CompletableFuture<Reaction> addChild(
180187
return add(userID, child, targetFeeds);
181188
}
182189

190+
public CompletableFuture<Reaction> addChild(
191+
String userID, String kind, String parentID, FeedID[] targetFeeds, Map<String, Object> targetFeedsExtraData) throws StreamException {
192+
Reaction child = Reaction.builder().kind(kind).parent(parentID).build();
193+
return add(userID, child, targetFeeds, targetFeedsExtraData);
194+
}
195+
183196
public CompletableFuture<Reaction> addChild(
184197
String userID, String parentID, Reaction reaction, Iterable<FeedID> targetFeeds)
185198
throws StreamException {
@@ -194,6 +207,13 @@ public CompletableFuture<Reaction> addChild(
194207
return add(userID, child, targetFeeds);
195208
}
196209

210+
public CompletableFuture<Reaction> addChild(
211+
String userID, String parentID, Reaction reaction, FeedID[] targetFeeds, Map<String, Object> targetFeedsExtraData)
212+
throws StreamException {
213+
Reaction child = Reaction.builder().fromReaction(reaction).parent(parentID).build();
214+
return add(userID, child, targetFeeds, targetFeedsExtraData);
215+
}
216+
197217
public CompletableFuture<Void> update(String id, Iterable<FeedID> targetFeeds)
198218
throws StreamException {
199219
return update(id, Iterables.toArray(targetFeeds, FeedID.class));

src/main/java/io/getstream/cloud/CloudReactionsClient.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import io.getstream.core.options.Limit;
1515
import io.getstream.core.utils.DefaultOptions;
1616
import java.util.List;
17+
import java.util.Map;
1718
import java8.util.concurrent.CompletableFuture;
1819

1920
public final class CloudReactionsClient {
@@ -150,6 +151,11 @@ public CompletableFuture<Reaction> add(String userID, Reaction reaction, FeedID.
150151
return reactions.add(token, userID, reaction, targetFeeds);
151152
}
152153

154+
public CompletableFuture<Reaction> add(String userID, Reaction reaction, FeedID[] targetFeeds, Map<String, Object> targetFeedsExtraData)
155+
throws StreamException {
156+
return reactions.add(token, userID, reaction, targetFeeds, targetFeedsExtraData);
157+
}
158+
153159
public CompletableFuture<Reaction> addChild(
154160
String userID, String kind, String parentID, Iterable<FeedID> targetFeeds)
155161
throws StreamException {
@@ -163,6 +169,12 @@ public CompletableFuture<Reaction> addChild(
163169
return add(userID, child, targetFeeds);
164170
}
165171

172+
public CompletableFuture<Reaction> addChild(
173+
String userID, String kind, String parentID, FeedID[] targetFeeds, Map<String, Object> targetFeedsExtraData) throws StreamException {
174+
Reaction child = Reaction.builder().kind(kind).parent(parentID).build();
175+
return add(userID, child, targetFeeds, targetFeedsExtraData);
176+
}
177+
166178
public CompletableFuture<Reaction> addChild(
167179
String userID, String parentID, Reaction reaction, Iterable<FeedID> targetFeeds)
168180
throws StreamException {
@@ -177,6 +189,13 @@ public CompletableFuture<Reaction> addChild(
177189
return add(userID, child, targetFeeds);
178190
}
179191

192+
public CompletableFuture<Reaction> addChild(
193+
String userID, String parentID, Reaction reaction, FeedID[] targetFeeds, Map<String, Object> targetFeedsExtraData)
194+
throws StreamException {
195+
Reaction child = Reaction.builder().fromReaction(reaction).parent(parentID).build();
196+
return add(userID, child, targetFeeds, targetFeedsExtraData);
197+
}
198+
180199
public CompletableFuture<Void> update(String id, Iterable<FeedID> targetFeeds)
181200
throws StreamException {
182201
return update(id, Iterables.toArray(targetFeeds, FeedID.class));

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ public CompletableFuture<Paginated<Reaction>> paginatedFilter(Token token, Strin
204204

205205
public CompletableFuture<Reaction> add(
206206
Token token, String userID, Reaction reaction, FeedID... targetFeeds) throws StreamException {
207+
return add(token, userID, reaction, targetFeeds, null);
208+
}
209+
210+
public CompletableFuture<Reaction> add(
211+
Token token, String userID, Reaction reaction, FeedID[] targetFeeds, Map<String, Object> targetFeedsExtraData) throws StreamException {
207212
checkNotNull(reaction, "Reaction can't be null");
208213
checkArgument(
209214
reaction.getActivityID() != null || reaction.getParent() != null,
@@ -227,6 +232,9 @@ public CompletableFuture<Reaction> add(
227232
ImmutableMap.Builder<String, Object> payloadBuilder = ImmutableMap.builder();
228233
payloadBuilder.put("kind", reaction.getKind());
229234
payloadBuilder.put("target_feeds", targetFeedIDs);
235+
if (targetFeedsExtraData != null) {
236+
payloadBuilder.put("target_feeds_extra_data", targetFeedsExtraData);
237+
}
230238
if (reaction.getActivityID() != null) {
231239
payloadBuilder.put("activity_id", reaction.getActivityID());
232240
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package io.getstream.client;
2+
3+
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.Reaction;
7+
import org.junit.Test;
8+
9+
import java.util.Date;
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Map;
13+
import java.util.UUID;
14+
15+
import static org.junit.Assert.assertEquals;
16+
import static org.junit.Assert.assertNotNull;
17+
18+
public class TargetFeedsExtraDataTest {
19+
private static final String apiKey =
20+
System.getenv("STREAM_KEY") != null
21+
? System.getenv("STREAM_KEY")
22+
: System.getProperty("STREAM_KEY");
23+
private static final String secret =
24+
System.getenv("STREAM_SECRET") != null
25+
? System.getenv("STREAM_SECRET")
26+
: System.getProperty("STREAM_SECRET");
27+
28+
@Test
29+
public void testTargetFeedsExtraData() throws Exception {
30+
// Create client
31+
Client client = Client.builder(apiKey, secret).build();
32+
33+
// 1. Create a test activity
34+
String activityId = UUID.randomUUID().toString();
35+
Activity activity = Activity.builder()
36+
.actor("test-user")
37+
.verb("post")
38+
.object("test-object")
39+
.foreignID("test-foreignId-" + activityId)
40+
.time(new Date())
41+
.build();
42+
43+
Activity postedActivity = client.flatFeed("user", "test-user").addActivity(activity).join();
44+
45+
// 2. Create a comment reaction on the activity
46+
Map<String, Object> commentData = new HashMap<>();
47+
commentData.put("text", "This is a test comment");
48+
49+
Reaction comment = Reaction.builder()
50+
.kind("comment")
51+
.activityID(postedActivity.getID())
52+
.extraField("data", commentData)
53+
.build();
54+
55+
Reaction postedComment = client.reactions().add("test-user", comment, new FeedID[0]).join();
56+
57+
// 3. Create a like reaction on the comment with targetFeedsExtraData
58+
Map<String, Object> targetFeedsExtraData = new HashMap<>();
59+
targetFeedsExtraData.put("parent_reaction", "SR:" + postedComment.getId());
60+
61+
FeedID[] targetFeeds = new FeedID[] {
62+
new FeedID("notification", "test-user")
63+
};
64+
65+
Reaction like = client.reactions().addChild(
66+
"test-user",
67+
"like",
68+
postedComment.getId(),
69+
targetFeeds,
70+
targetFeedsExtraData
71+
).join();
72+
73+
// 4. Verify that the reaction was created successfully
74+
assertNotNull("Like reaction should not be null", like);
75+
assertEquals("Like reaction should have kind='like'", "like", like.getKind());
76+
assertEquals("Like reaction should have parent ID", postedComment.getId(), like.getParent());
77+
78+
// 5. Get the reactions to verify
79+
List<Reaction> reactions = client.reactions().filter(
80+
LookupKind.REACTION,
81+
postedComment.getId(),
82+
"like"
83+
).join();
84+
85+
assertEquals("Should have one like reaction", 1, reactions.size());
86+
assertEquals("Reaction should match the one we created", like.getId(), reactions.get(0).getId());
87+
88+
// Clean up
89+
client.reactions().delete(like.getId()).join();
90+
client.reactions().delete(postedComment.getId()).join();
91+
client.flatFeed("user", "test-user").removeActivityByID(postedActivity.getID()).join();
92+
}
93+
}

0 commit comments

Comments
 (0)