33
44class Feed (object ):
55
6- def __init__ (self , client , feed_id , token ):
6+ def __init__ (self , client , feed_slug , user_id , token ):
77 '''
88 Initializes the Feed class
99
1010 :param client: the api client
11- :param feed_id : the feed id (string)
12-
13-
11+ :param feed_slug : the slug of the feed, ie user, flat, notification
12+ :param user_id: the id of the user
13+ :param token: the token
1414 '''
1515 self .client = client
16- # TODO: rename feed_id to feed.id everywhere
17- self .id = feed_id
18- self .feed_id = feed_id
19- self .feed_url = 'feed/%s/' % feed_id .replace (':' , '/' )
20- self .feed_together = feed_id .replace (':' , '' )
16+ self .feed_slug = feed_slug
17+ self .user_id = user_id
18+ self .id = '%s:%s' % (feed_slug , user_id )
2119 self .token = token
22- self .authorization = self .feed_together + ' ' + self .token
23-
24- def add_to_signature (self , recipients ):
25- data = []
26- for recipient in recipients :
27- feed = self .client .feed (recipient )
28- data .append ("%s %s" % (recipient , feed .token ))
29- return data
20+
21+ self .feed_url = 'feed/%s/' % self .id .replace (':' , '/' )
22+ self .feed_together = self .id .replace (':' , '' )
23+ self .signature = self .feed_together + ' ' + self .token
3024
3125 def add_activity (self , activity_data ):
3226 '''
@@ -44,7 +38,7 @@ def add_activity(self, activity_data):
4438 activity_data ['to' ] = self .add_to_signature (activity_data ['to' ])
4539
4640 result = self .client .post (
47- self .feed_url , data = activity_data , authorization = self .authorization )
41+ self .feed_url , data = activity_data , signature = self .signature )
4842 return result
4943
5044 def add_activities (self , activity_list ):
@@ -68,7 +62,7 @@ def add_activities(self, activity_list):
6862
6963 data = dict (activities = activity_list )
7064 result = self .client .post (
71- self .feed_url , data = data , authorization = self .authorization )
65+ self .feed_url , data = data , signature = self .signature )
7266 return result
7367
7468 def remove_activity (self , activity_id = None , foreign_id = None ):
@@ -87,70 +81,94 @@ def remove_activity(self, activity_id=None, foreign_id=None):
8781 if foreign_id is not None :
8882 params ['foreign_id' ] = '1'
8983 result = self .client .delete (
90- url , authorization = self .authorization , params = params )
84+ url , signature = self .signature , params = params )
9185 return result
86+
87+ def get (self , ** params ):
88+ '''
89+ Get the activities in this feed
90+
91+ **Example**::
92+
93+ # fast pagination using id filtering
94+ feed.get(limit=10, id_lte=100292310)
9295
93- def follow (self , target_feed ):
96+ # slow pagination using offset
97+ feed.get(limit=10, offset=10)
98+ '''
99+ mark_read = params .get ('mark_read' )
100+ if isinstance (mark_read , (list , tuple )):
101+ params ['mark_read' ] = ',' .join (mark_read )
102+ response = self .client .get (
103+ self .feed_url , params = params , signature = self .signature )
104+ return response
105+
106+ def follow (self , target_feed_slug , target_user_id ):
94107 '''
95108 Follows the given feed
96109
97- :param target_feed: the feed to follow, ie flat:3
110+ :param target_feed_slug: the slug of the target feed
111+ :param target_user_id: the user id
98112 '''
113+ target_feed_id = '%s:%s' % (target_feed_slug , target_user_id )
99114 url = self .feed_url + 'follows/'
100115 data = {
101- 'target' : target_feed ,
102- 'target_token' : self .client .feed (target_feed ).token
116+ 'target' : target_feed_id ,
117+ 'target_token' : self .client .feed (target_feed_slug , target_user_id ).token
103118 }
104119 response = self .client .post (
105- url , data = data , authorization = self .authorization )
120+ url , data = data , signature = self .signature )
121+ return response
122+
123+ def unfollow (self , target_feed_slug , target_user_id ):
124+ '''
125+ Unfollow the given feed
126+ '''
127+ target_feed_id = '%s:%s' % (target_feed_slug , target_user_id )
128+ url = self .feed_url + 'follows/%s/' % target_feed_id
129+ response = self .client .delete (url , signature = self .signature )
106130 return response
107131
108- def followers (self , offset = 0 , limit = 25 ):
132+ def followers (self , offset = 0 , limit = 25 , filter = None ):
133+ '''
134+ Lists the followers for the given feed
135+ '''
136+ filter = filter is not None and ',' .join (filter ) or ''
109137 params = {
110138 'limit' : limit ,
111- 'offset' : offset
139+ 'offset' : offset ,
140+ 'filter' : filter
112141 }
113142 url = self .feed_url + 'followers/'
114143 response = self .client .get (
115- url , params = params , authorization = self .authorization )
144+ url , params = params , signature = self .signature )
116145 return response
117146
118- def following (self , offset = 0 , limit = 25 , feeds = None ):
119- feeds = feeds is not None and ',' .join (feeds ) or ''
147+ def following (self , offset = 0 , limit = 25 , filter = None ):
148+ '''
149+ List the feeds which this feed is following
150+ '''
151+ filter = filter is not None and ',' .join (filter ) or ''
120152 params = {
121153 'offset' : offset ,
122154 'limit' : limit ,
123- 'filter' : feeds
155+ 'filter' : filter
124156 }
125157 url = self .feed_url + 'follows/'
126158 response = self .client .get (
127- url , params = params , authorization = self .authorization )
159+ url , params = params , signature = self .signature )
128160 return response
129161
130- def unfollow (self , target_feed ):
162+ def add_to_signature (self , recipients ):
131163 '''
132- Unfollow the given feed
164+ Takes a list of recipients such as ['user:1', 'user:2']
165+ and turns it into a list with the tokens included
166+ ['user:1 token', 'user:2 token']
133167 '''
134- validate_feed (target_feed )
135- url = self .feed_url + 'follows/%s/' % target_feed
136- response = self .client .delete (url , authorization = self .authorization )
137- return response
138-
139- def get (self , ** params ):
140- '''
141- Get the activities in this feed
142-
143- **Example**::
144-
145- # fast pagination using id filtering
146- feed.get(limit=10, id_lte=100292310)
147-
148- # slow pagination using offset
149- feed.get(limit=10, offset=10)
150- '''
151- mark_read = params .get ('mark_read' )
152- if isinstance (mark_read , (list , tuple )):
153- params ['mark_read' ] = ',' .join (mark_read )
154- response = self .client .get (
155- self .feed_url , params = params , authorization = self .authorization )
156- return response
168+ data = []
169+ for recipient in recipients :
170+ validate_feed (recipient )
171+ feed_slug , user_id = recipient .split (':' )
172+ feed = self .client .feed (feed_slug , user_id )
173+ data .append ("%s %s" % (recipient , feed .token ))
174+ return data
0 commit comments