Skip to content

Commit 7758ab6

Browse files
authored
Merge pull request #177 from GetStream/reaction-extra
[FEEDS-0]chore: add extradata support reaction
2 parents bd21017 + c50df8d commit 7758ab6

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-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: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
import static org.junit.Assert.assertTrue;
18+
19+
public class TargetFeedsExtraDataTest {
20+
private static final String apiKey =
21+
System.getenv("STREAM_KEY") != null
22+
? System.getenv("STREAM_KEY")
23+
: System.getProperty("STREAM_KEY");
24+
private static final String secret =
25+
System.getenv("STREAM_SECRET") != null
26+
? System.getenv("STREAM_SECRET")
27+
: System.getProperty("STREAM_SECRET");
28+
29+
@Test
30+
public void testTargetFeedsExtraData() throws Exception {
31+
// Create client
32+
Client client = Client.builder(apiKey, secret).build();
33+
34+
// Use unique user id to avoid conflicts in notification feed
35+
String uniqueId = UUID.randomUUID().toString().replace("-", "");
36+
String userId = "test-user-" + uniqueId;
37+
38+
// 1. Create a test activity
39+
String activityId = UUID.randomUUID().toString();
40+
Activity activity = Activity.builder()
41+
.actor(userId)
42+
.verb("post")
43+
.object("test-object")
44+
.foreignID("test-foreignId-" + activityId)
45+
.time(new Date())
46+
.build();
47+
48+
Activity postedActivity = client.flatFeed("user", userId).addActivity(activity).join();
49+
50+
// 2. Create a comment reaction on the activity
51+
Map<String, Object> commentData = new HashMap<>();
52+
commentData.put("text", "This is a test comment");
53+
54+
Reaction comment = Reaction.builder()
55+
.kind("comment")
56+
.activityID(postedActivity.getID())
57+
.extraField("data", commentData)
58+
.build();
59+
60+
Reaction postedComment = client.reactions().add(userId, comment, new FeedID[0]).join();
61+
62+
// 3. Create a like reaction on the comment with targetFeedsExtraData
63+
Map<String, Object> targetFeedsExtraData = new HashMap<>();
64+
String extraDataValue = "SR:" + postedComment.getId();
65+
targetFeedsExtraData.put("parent_reaction", extraDataValue);
66+
67+
FeedID[] targetFeeds = new FeedID[] {
68+
new FeedID("notification", userId)
69+
};
70+
71+
// The critical part of the test: Can we successfully create a reaction with targetFeedsExtraData
72+
Reaction like = client.reactions().addChild(
73+
"actor-" + uniqueId, // Different user performs the like action
74+
"like",
75+
postedComment.getId(),
76+
targetFeeds,
77+
targetFeedsExtraData
78+
).join();
79+
80+
// 4. Verify that the reaction was created successfully
81+
assertNotNull("Like reaction should not be null", like);
82+
assertEquals("Like reaction should have kind='like'", "like", like.getKind());
83+
assertEquals("Like reaction should have parent ID", postedComment.getId(), like.getParent());
84+
85+
// Check if the reaction has extra data
86+
Map<String, Object> reactionExtra = like.getExtra();
87+
assertNotNull("Reaction should have extra data", reactionExtra);
88+
89+
// Check for targetFeedsExtraData directly
90+
assertTrue("Reaction should contain target_feeds_extra_data",
91+
reactionExtra.containsKey("target_feeds_extra_data"));
92+
93+
// Verify the content of targetFeedsExtraData
94+
Object extraDataObj = reactionExtra.get("target_feeds_extra_data");
95+
assertTrue("target_feeds_extra_data should be a Map", extraDataObj instanceof Map);
96+
97+
Map<String, Object> extraDataMap = (Map<String, Object>) extraDataObj;
98+
assertTrue("target_feeds_extra_data should contain parent_reaction",
99+
extraDataMap.containsKey("parent_reaction"));
100+
assertEquals("parent_reaction value should match what we sent",
101+
extraDataValue, extraDataMap.get("parent_reaction"));
102+
103+
// 5. Get the reactions to verify
104+
List<Reaction> reactions = client.reactions().filter(
105+
LookupKind.REACTION,
106+
postedComment.getId(),
107+
"like"
108+
).join();
109+
110+
assertEquals("Should have one like reaction", 1, reactions.size());
111+
assertEquals("Reaction should match the one we created", like.getId(), reactions.get(0).getId());
112+
113+
// Clean up
114+
client.reactions().delete(like.getId()).join();
115+
client.reactions().delete(postedComment.getId()).join();
116+
client.flatFeed("user", userId).removeActivityByID(postedActivity.getID()).join();
117+
}
118+
}

0 commit comments

Comments
 (0)