Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 31420ae

Browse files
committed
add enrichment params for feed.get
1 parent cfb69e2 commit 31420ae

File tree

4 files changed

+205
-110
lines changed

4 files changed

+205
-110
lines changed

stream/collections.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ def __init__(self, client, token):
1111
self.token = token
1212

1313
def create_reference(self, collection_name, id):
14-
return "SO:%s:%s" % (collection_name, id)
14+
_id = id
15+
if isinstance(id, (dict,)) and id.get("id") is not None:
16+
_id = id.get("id")
17+
return "SO:%s:%s" % (collection_name, _id)
1518

16-
def create_user_reference(self, id):
17-
return self.create_reference("user", id)
18-
1919
def upsert(self, collection_name, data):
2020
"""
2121
"Insert new or update existing data.

stream/feed.py

+116-104
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,43 @@
22

33

44
class Feed(object):
5-
65
def __init__(self, client, feed_slug, user_id, token):
7-
'''
6+
"""
87
Initializes the Feed class
98
109
:param client: the api client
1110
:param slug: the slug of the feed, ie user, flat, notification
1211
:param user_id: the id of the user
1312
:param token: the token
14-
'''
13+
"""
1514
self.client = client
1615
self.slug = feed_slug
1716
self.user_id = str(user_id)
18-
self.id = '%s:%s' % (feed_slug, user_id)
17+
self.id = "%s:%s" % (feed_slug, user_id)
1918
self.token = token
2019

21-
self.feed_url = 'feed/%s/' % self.id.replace(':', '/')
22-
self.feed_targets_url = 'feed_targets/%s/' % self.id.replace(':', '/')
23-
self.feed_together = self.id.replace(':', '')
24-
self.signature = self.feed_together + ' ' + self.token
20+
self.feed_url = "feed/%s/" % self.id.replace(":", "/")
21+
self.enriched_feed_url = "enrich/feed/%s/" % self.id.replace(":", "/")
22+
self.feed_targets_url = "feed_targets/%s/" % self.id.replace(":", "/")
23+
self.feed_together = self.id.replace(":", "")
24+
self.signature = self.feed_together + " " + self.token
2525

2626
def create_scope_token(self, resource, action):
27-
'''
27+
"""
2828
creates the JWT token to perform an action on a owned resource
29-
'''
30-
return self.client.create_jwt_token(resource, action, feed_id=self.feed_together)
29+
"""
30+
return self.client.create_jwt_token(
31+
resource, action, feed_id=self.feed_together
32+
)
3133

3234
def get_readonly_token(self):
33-
'''
35+
"""
3436
creates the JWT token to perform readonly operations
35-
'''
36-
return self.create_scope_token('*', 'read')
37+
"""
38+
return self.create_scope_token("*", "read")
3739

3840
def add_activity(self, activity_data):
39-
'''
41+
"""
4042
Adds an activity to the feed, this will also trigger an update
4143
to all the feeds which follow this feed
4244
@@ -46,21 +48,24 @@ def add_activity(self, activity_data):
4648
4749
activity_data = {'actor': 1, 'verb': 'tweet', 'object': 1}
4850
activity_id = feed.add_activity(activity_data)
49-
'''
50-
if activity_data.get('to') and not isinstance(activity_data.get('to'), (list, tuple, set)):
51-
raise TypeError('please provide the activity\'s to field as a list not a string')
52-
53-
if activity_data.get('to'):
51+
"""
52+
if activity_data.get("to") and not isinstance(
53+
activity_data.get("to"), (list, tuple, set)
54+
):
55+
raise TypeError(
56+
"please provide the activity's to field as a list not a string"
57+
)
58+
59+
if activity_data.get("to"):
5460
activity_data = activity_data.copy()
55-
activity_data['to'] = self.add_to_signature(activity_data['to'])
61+
activity_data["to"] = self.add_to_signature(activity_data["to"])
5662

57-
token = self.create_scope_token('feed', 'write')
58-
result = self.client.post(
59-
self.feed_url, data=activity_data, signature=token)
63+
token = self.create_scope_token("feed", "write")
64+
result = self.client.post(self.feed_url, data=activity_data, signature=token)
6065
return result
6166

6267
def add_activities(self, activity_list):
63-
'''
68+
"""
6469
Adds a list of activities to the feed
6570
6671
:param activity_list: a list with the activity data dicts
@@ -72,43 +77,40 @@ def add_activities(self, activity_list):
7277
{'actor': 2, 'verb': 'watch', 'object': 2},
7378
]
7479
result = feed.add_activities(activity_data)
75-
'''
80+
"""
7681
activities = []
7782
for activity_data in activity_list:
7883
activity_data = activity_data.copy()
7984
activities.append(activity_data)
80-
if activity_data.get('to'):
81-
activity_data['to'] = self.add_to_signature(
82-
activity_data['to'])
83-
token = self.create_scope_token('feed', 'write')
85+
if activity_data.get("to"):
86+
activity_data["to"] = self.add_to_signature(activity_data["to"])
87+
token = self.create_scope_token("feed", "write")
8488
data = dict(activities=activities)
8589
if activities:
86-
result = self.client.post(
87-
self.feed_url, data=data, signature=token)
90+
result = self.client.post(self.feed_url, data=data, signature=token)
8891
return result
8992

9093
def remove_activity(self, activity_id=None, foreign_id=None):
91-
'''
94+
"""
9295
Removes an activity from the feed
9396
9497
:param activity_id: the activity id to remove from this feed
9598
(note this will also remove the activity from feeds which follow this feed)
9699
:param foreign_id: the foreign id you provided when adding the activity
97-
'''
100+
"""
98101
identifier = activity_id or foreign_id
99102
if not identifier:
100-
raise ValueError('please either provide activity_id or foreign_id')
101-
url = self.feed_url + '%s/' % identifier
103+
raise ValueError("please either provide activity_id or foreign_id")
104+
url = self.feed_url + "%s/" % identifier
102105
params = dict()
103-
token = self.create_scope_token('feed', 'delete')
106+
token = self.create_scope_token("feed", "delete")
104107
if foreign_id is not None:
105-
params['foreign_id'] = '1'
106-
result = self.client.delete(
107-
url, signature=token, params=params)
108+
params["foreign_id"] = "1"
109+
result = self.client.delete(url, signature=token, params=params)
108110
return result
109111

110-
def get(self, **params):
111-
'''
112+
def get(self, enrich=False, reactions=None, **params):
113+
"""
112114
Get the activities in this feed
113115
114116
**Example**::
@@ -118,117 +120,127 @@ def get(self, **params):
118120
119121
# slow pagination using offset
120122
feed.get(limit=10, offset=10)
121-
'''
122-
for field in ['mark_read', 'mark_seen']:
123+
"""
124+
for field in ["mark_read", "mark_seen"]:
123125
value = params.get(field)
124126
if isinstance(value, (list, tuple)):
125-
params[field] = ','.join(value)
126-
token = self.create_scope_token('feed', 'read')
127-
response = self.client.get(
128-
self.feed_url, params=params, signature=token)
127+
params[field] = ",".join(value)
128+
token = self.create_scope_token("feed", "read")
129+
130+
if enrich or reactions is not None:
131+
feed_url = self.enriched_feed_url
132+
else:
133+
feed_url = self.feed_url
134+
135+
if reactions is not None and not isinstance(reactions, (dict, )):
136+
raise TypeError("reactions argument should be a dictionary")
137+
138+
if reactions is not None:
139+
if reactions.get('own'):
140+
params['withOwnReactions'] = True
141+
if reactions.get('recent'):
142+
params['withRecentReactions'] = True
143+
if reactions.get('counts'):
144+
params['withReactionCounts'] = True
145+
146+
response = self.client.get(feed_url, params=params, signature=token)
129147
return response
130148

131-
def follow(self, target_feed_slug, target_user_id, activity_copy_limit=None, **extra_data):
132-
'''
149+
def follow(
150+
self, target_feed_slug, target_user_id, activity_copy_limit=None, **extra_data
151+
):
152+
"""
133153
Follows the given feed
134154
155+
:param activity_copy_limit: how many activities should be copied from target feed
135156
:param target_feed_slug: the slug of the target feed
136157
:param target_user_id: the user id
137-
'''
158+
"""
138159
target_feed_slug = validate_feed_slug(target_feed_slug)
139160
target_user_id = validate_user_id(target_user_id)
140-
target_feed_id = '%s:%s' % (target_feed_slug, target_user_id)
141-
url = self.feed_url + 'follows/'
161+
target_feed_id = "%s:%s" % (target_feed_slug, target_user_id)
162+
url = self.feed_url + "follows/"
142163
data = {
143-
'target': target_feed_id,
144-
'target_token': self.client.feed(target_feed_slug, target_user_id).token
164+
"target": target_feed_id,
165+
"target_token": self.client.feed(target_feed_slug, target_user_id).token,
145166
}
146167
if activity_copy_limit != None:
147-
data['activity_copy_limit'] = activity_copy_limit
148-
token = self.create_scope_token('follower', 'write')
168+
data["activity_copy_limit"] = activity_copy_limit
169+
token = self.create_scope_token("follower", "write")
149170
data.update(extra_data)
150-
response = self.client.post(
151-
url, data=data, signature=token)
171+
response = self.client.post(url, data=data, signature=token)
152172
return response
153173

154174
def unfollow(self, target_feed_slug, target_user_id, keep_history=False):
155-
'''
175+
"""
156176
Unfollow the given feed
157-
'''
177+
"""
158178
target_feed_slug = validate_feed_slug(target_feed_slug)
159179
target_user_id = validate_user_id(target_user_id)
160-
target_feed_id = '%s:%s' % (target_feed_slug, target_user_id)
161-
token = self.create_scope_token('follower', 'delete')
162-
url = self.feed_url + 'follows/%s/' % target_feed_id
180+
target_feed_id = "%s:%s" % (target_feed_slug, target_user_id)
181+
token = self.create_scope_token("follower", "delete")
182+
url = self.feed_url + "follows/%s/" % target_feed_id
163183
params = {}
164184
if keep_history:
165-
params['keep_history'] = True
185+
params["keep_history"] = True
166186
response = self.client.delete(url, signature=token, params=params)
167187
return response
168188

169189
def followers(self, offset=0, limit=25, feeds=None):
170-
'''
190+
"""
171191
Lists the followers for the given feed
172-
'''
173-
feeds = feeds is not None and ','.join(feeds) or ''
174-
params = {
175-
'limit': limit,
176-
'offset': offset,
177-
'filter': feeds
178-
}
179-
url = self.feed_url + 'followers/'
180-
token = self.create_scope_token('follower', 'read')
181-
response = self.client.get(
182-
url, params=params, signature=token)
192+
"""
193+
feeds = feeds is not None and ",".join(feeds) or ""
194+
params = {"limit": limit, "offset": offset, "filter": feeds}
195+
url = self.feed_url + "followers/"
196+
token = self.create_scope_token("follower", "read")
197+
response = self.client.get(url, params=params, signature=token)
183198
return response
184199

185200
def following(self, offset=0, limit=25, feeds=None):
186-
'''
201+
"""
187202
List the feeds which this feed is following
188-
'''
203+
"""
189204
if feeds is not None:
190-
feeds = feeds is not None and ','.join(feeds) or ''
191-
params = {
192-
'offset': offset,
193-
'limit': limit,
194-
'filter': feeds
195-
}
196-
url = self.feed_url + 'follows/'
197-
token = self.create_scope_token('follower', 'read')
198-
response = self.client.get(
199-
url, params=params, signature=token)
205+
feeds = feeds is not None and ",".join(feeds) or ""
206+
params = {"offset": offset, "limit": limit, "filter": feeds}
207+
url = self.feed_url + "follows/"
208+
token = self.create_scope_token("follower", "read")
209+
response = self.client.get(url, params=params, signature=token)
200210
return response
201211

202212
def add_to_signature(self, recipients):
203-
'''
213+
"""
204214
Takes a list of recipients such as ['user:1', 'user:2']
205215
and turns it into a list with the tokens included
206216
['user:1 token', 'user:2 token']
207-
'''
217+
"""
208218
data = []
209219
for recipient in recipients:
210220
validate_feed_id(recipient)
211-
feed_slug, user_id = recipient.split(':')
221+
feed_slug, user_id = recipient.split(":")
212222
feed = self.client.feed(feed_slug, user_id)
213223
data.append("%s %s" % (recipient, feed.token))
214224
return data
215225

216-
def update_activity_to_targets(self, foreign_id, time,
217-
new_targets=None, added_targets=None,
218-
removed_targets=None):
219-
data = {
220-
'foreign_id': foreign_id,
221-
'time': time,
222-
}
226+
def update_activity_to_targets(
227+
self,
228+
foreign_id,
229+
time,
230+
new_targets=None,
231+
added_targets=None,
232+
removed_targets=None,
233+
):
234+
data = {"foreign_id": foreign_id, "time": time}
223235

224236
if new_targets is not None:
225-
data['new_targets'] = new_targets
237+
data["new_targets"] = new_targets
226238
if added_targets is not None:
227-
data['added_targets'] = added_targets
239+
data["added_targets"] = added_targets
228240
if removed_targets is not None:
229-
data['removed_targets'] = removed_targets
241+
data["removed_targets"] = removed_targets
230242

231-
url = self.feed_targets_url + 'activity_to_targets/'
243+
url = self.feed_targets_url + "activity_to_targets/"
232244

233-
token = self.create_scope_token('feed_targets', 'write')
245+
token = self.create_scope_token("feed_targets", "write")
234246
return self.client.post(url, data=data, signature=token)

0 commit comments

Comments
 (0)