2
2
3
3
4
4
class Feed (object ):
5
-
6
5
def __init__ (self , client , feed_slug , user_id , token ):
7
- '''
6
+ """
8
7
Initializes the Feed class
9
8
10
9
:param client: the api client
11
10
:param slug: the slug of the feed, ie user, flat, notification
12
11
:param user_id: the id of the user
13
12
:param token: the token
14
- '''
13
+ """
15
14
self .client = client
16
15
self .slug = feed_slug
17
16
self .user_id = str (user_id )
18
- self .id = ' %s:%s' % (feed_slug , user_id )
17
+ self .id = " %s:%s" % (feed_slug , user_id )
19
18
self .token = token
20
19
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
25
25
26
26
def create_scope_token (self , resource , action ):
27
- '''
27
+ """
28
28
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
+ )
31
33
32
34
def get_readonly_token (self ):
33
- '''
35
+ """
34
36
creates the JWT token to perform readonly operations
35
- '''
36
- return self .create_scope_token ('*' , ' read' )
37
+ """
38
+ return self .create_scope_token ("*" , " read" )
37
39
38
40
def add_activity (self , activity_data ):
39
- '''
41
+ """
40
42
Adds an activity to the feed, this will also trigger an update
41
43
to all the feeds which follow this feed
42
44
@@ -46,21 +48,24 @@ def add_activity(self, activity_data):
46
48
47
49
activity_data = {'actor': 1, 'verb': 'tweet', 'object': 1}
48
50
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" ):
54
60
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" ])
56
62
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 )
60
65
return result
61
66
62
67
def add_activities (self , activity_list ):
63
- '''
68
+ """
64
69
Adds a list of activities to the feed
65
70
66
71
:param activity_list: a list with the activity data dicts
@@ -72,43 +77,40 @@ def add_activities(self, activity_list):
72
77
{'actor': 2, 'verb': 'watch', 'object': 2},
73
78
]
74
79
result = feed.add_activities(activity_data)
75
- '''
80
+ """
76
81
activities = []
77
82
for activity_data in activity_list :
78
83
activity_data = activity_data .copy ()
79
84
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" )
84
88
data = dict (activities = activities )
85
89
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 )
88
91
return result
89
92
90
93
def remove_activity (self , activity_id = None , foreign_id = None ):
91
- '''
94
+ """
92
95
Removes an activity from the feed
93
96
94
97
:param activity_id: the activity id to remove from this feed
95
98
(note this will also remove the activity from feeds which follow this feed)
96
99
:param foreign_id: the foreign id you provided when adding the activity
97
- '''
100
+ """
98
101
identifier = activity_id or foreign_id
99
102
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
102
105
params = dict ()
103
- token = self .create_scope_token (' feed' , ' delete' )
106
+ token = self .create_scope_token (" feed" , " delete" )
104
107
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 )
108
110
return result
109
111
110
- def get (self , ** params ):
111
- '''
112
+ def get (self , enrich = False , reactions = None , ** params ):
113
+ """
112
114
Get the activities in this feed
113
115
114
116
**Example**::
@@ -118,117 +120,127 @@ def get(self, **params):
118
120
119
121
# slow pagination using offset
120
122
feed.get(limit=10, offset=10)
121
- '''
122
- for field in [' mark_read' , ' mark_seen' ]:
123
+ """
124
+ for field in [" mark_read" , " mark_seen" ]:
123
125
value = params .get (field )
124
126
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 )
129
147
return response
130
148
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
+ """
133
153
Follows the given feed
134
154
155
+ :param activity_copy_limit: how many activities should be copied from target feed
135
156
:param target_feed_slug: the slug of the target feed
136
157
:param target_user_id: the user id
137
- '''
158
+ """
138
159
target_feed_slug = validate_feed_slug (target_feed_slug )
139
160
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/"
142
163
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 ,
145
166
}
146
167
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" )
149
170
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 )
152
172
return response
153
173
154
174
def unfollow (self , target_feed_slug , target_user_id , keep_history = False ):
155
- '''
175
+ """
156
176
Unfollow the given feed
157
- '''
177
+ """
158
178
target_feed_slug = validate_feed_slug (target_feed_slug )
159
179
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
163
183
params = {}
164
184
if keep_history :
165
- params [' keep_history' ] = True
185
+ params [" keep_history" ] = True
166
186
response = self .client .delete (url , signature = token , params = params )
167
187
return response
168
188
169
189
def followers (self , offset = 0 , limit = 25 , feeds = None ):
170
- '''
190
+ """
171
191
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 )
183
198
return response
184
199
185
200
def following (self , offset = 0 , limit = 25 , feeds = None ):
186
- '''
201
+ """
187
202
List the feeds which this feed is following
188
- '''
203
+ """
189
204
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 )
200
210
return response
201
211
202
212
def add_to_signature (self , recipients ):
203
- '''
213
+ """
204
214
Takes a list of recipients such as ['user:1', 'user:2']
205
215
and turns it into a list with the tokens included
206
216
['user:1 token', 'user:2 token']
207
- '''
217
+ """
208
218
data = []
209
219
for recipient in recipients :
210
220
validate_feed_id (recipient )
211
- feed_slug , user_id = recipient .split (':' )
221
+ feed_slug , user_id = recipient .split (":" )
212
222
feed = self .client .feed (feed_slug , user_id )
213
223
data .append ("%s %s" % (recipient , feed .token ))
214
224
return data
215
225
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 }
223
235
224
236
if new_targets is not None :
225
- data [' new_targets' ] = new_targets
237
+ data [" new_targets" ] = new_targets
226
238
if added_targets is not None :
227
- data [' added_targets' ] = added_targets
239
+ data [" added_targets" ] = added_targets
228
240
if removed_targets is not None :
229
- data [' removed_targets' ] = removed_targets
241
+ data [" removed_targets" ] = removed_targets
230
242
231
- url = self .feed_targets_url + ' activity_to_targets/'
243
+ url = self .feed_targets_url + " activity_to_targets/"
232
244
233
- token = self .create_scope_token (' feed_targets' , ' write' )
245
+ token = self .create_scope_token (" feed_targets" , " write" )
234
246
return self .client .post (url , data = data , signature = token )
0 commit comments