Skip to content

Commit 876cd1e

Browse files
authored
Add kind filter support for enrichment (#123)
1 parent d0d5814 commit 876cd1e

File tree

4 files changed

+84
-23
lines changed

4 files changed

+84
-23
lines changed

stream/client.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
from stream.reactions import Reactions
1414
from stream.serializer import _datetime_encoder
1515
from stream.users import Users
16-
from stream.utils import validate_feed_slug, validate_foreign_id_time, validate_user_id
16+
from stream.utils import (
17+
validate_feed_slug,
18+
validate_foreign_id_time,
19+
validate_user_id,
20+
get_reaction_params,
21+
)
1722

1823
try:
1924
from urllib.parse import urlparse
@@ -389,16 +394,7 @@ def get_activities(
389394
query_params["foreign_ids"] = ",".join(foreign_ids)
390395
query_params["timestamps"] = ",".join(timestamps)
391396

392-
if reactions is not None and not isinstance(reactions, (dict,)):
393-
raise TypeError("reactions argument should be a dictionary")
394-
395-
if reactions is not None:
396-
if reactions.get("own"):
397-
query_params["withOwnReactions"] = True
398-
if reactions.get("recent"):
399-
query_params["withRecentReactions"] = True
400-
if reactions.get("counts"):
401-
query_params["withReactionCounts"] = True
397+
query_params.update(get_reaction_params(reactions))
402398

403399
return self.get(endpoint, auth_token, params=query_params)
404400

stream/feed.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
from stream.utils import validate_feed_id, validate_feed_slug, validate_user_id
1+
from stream.utils import (
2+
validate_feed_id,
3+
validate_feed_slug,
4+
validate_user_id,
5+
get_reaction_params,
6+
)
27

38

49
class Feed:
@@ -130,17 +135,7 @@ def get(self, enrich=False, reactions=None, **params):
130135
else:
131136
feed_url = self.feed_url
132137

133-
if reactions is not None and not isinstance(reactions, (dict,)):
134-
raise TypeError("reactions argument should be a dictionary")
135-
136-
if reactions is not None:
137-
if reactions.get("own"):
138-
params["withOwnReactions"] = True
139-
if reactions.get("recent"):
140-
params["withRecentReactions"] = True
141-
if reactions.get("counts"):
142-
params["withReactionCounts"] = True
143-
138+
params.update(get_reaction_params(reactions))
144139
return self.client.get(feed_url, params=params, signature=token)
145140

146141
def follow(

stream/tests/test_client.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,56 @@ def validate(response):
11981198
self.c.get_activities(foreign_id_times=[(fid, dt)], reactions=reactions)
11991199
)
12001200

1201+
def test_get_activities_full_with_enrichment_and_reaction_kinds(self):
1202+
dt = datetime.datetime.utcnow()
1203+
fid = "awesome-test"
1204+
1205+
actor = self.c.users.add(str(uuid1()), data={"name": "barry"})
1206+
activity = {
1207+
"actor": self.c.users.create_reference(actor["id"]),
1208+
"object": "09",
1209+
"verb": "tweet",
1210+
"time": dt,
1211+
"foreign_id": fid,
1212+
}
1213+
1214+
feed = getfeed("user", "test_get_activity")
1215+
activity = feed.add_activity(activity)
1216+
1217+
self.c.reactions.add("like", activity["id"], "liker")
1218+
self.c.reactions.add("reshare", activity["id"], "sharer")
1219+
self.c.reactions.add("comment", activity["id"], "commenter")
1220+
1221+
reactions = {"recent": True, "counts": True, "kinds": "like,comment"}
1222+
response = self.c.get_activities(ids=[activity["id"]], reactions=reactions)
1223+
self.assertEqual(len(response["results"]), 1)
1224+
self.assertEqual(response["results"][0]["id"], activity["id"])
1225+
self.assertEqual(
1226+
sorted(response["results"][0]["latest_reactions"].keys()),
1227+
["comment", "like"],
1228+
)
1229+
self.assertEqual(
1230+
response["results"][0]["reaction_counts"], {"like": 1, "comment": 1}
1231+
)
1232+
1233+
reactions = {
1234+
"recent": True,
1235+
"counts": True,
1236+
"kinds": ["", "reshare ", "comment\n"],
1237+
}
1238+
response = self.c.get_activities(
1239+
foreign_id_times=[(fid, dt)], reactions=reactions
1240+
)
1241+
self.assertEqual(len(response["results"]), 1)
1242+
self.assertEqual(response["results"][0]["id"], activity["id"])
1243+
self.assertEqual(
1244+
sorted(response["results"][0]["latest_reactions"].keys()),
1245+
["comment", "reshare"],
1246+
)
1247+
self.assertEqual(
1248+
response["results"][0]["reaction_counts"], {"comment": 1, "reshare": 1}
1249+
)
1250+
12011251
def test_activity_partial_update(self):
12021252
now = datetime.datetime.utcnow()
12031253
feed = self.c.feed("user", uuid4())

stream/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,23 @@ def validate_foreign_id_time(foreign_id_time):
5454

5555
if len(v) != 2:
5656
raise ValueError("foreign_id_time elements should have two elements")
57+
58+
59+
def get_reaction_params(reactions):
60+
if reactions is not None and not isinstance(reactions, (dict,)):
61+
raise TypeError("reactions argument should be a dictionary")
62+
63+
params = {}
64+
if reactions is not None:
65+
if reactions.get("own"):
66+
params["withOwnReactions"] = True
67+
if reactions.get("recent"):
68+
params["withRecentReactions"] = True
69+
if reactions.get("counts"):
70+
params["withReactionCounts"] = True
71+
kinds = reactions.get("kinds")
72+
if kinds:
73+
if isinstance(kinds, list):
74+
kinds = ",".join(k.strip() for k in kinds if k.strip())
75+
params["reactionKindsFilter"] = kinds
76+
return params

0 commit comments

Comments
 (0)