@@ -312,7 +312,8 @@ def get_event(self, event_id):
312
312
"""
313
313
raise NotImplementedError ()
314
314
315
- def get_comment (self , comment_id , activity_id = None , activity_author_id = None ):
315
+ def get_comment (self , comment_id , activity_id = None , activity_author_id = None ,
316
+ activity = None ):
316
317
"""Returns an ActivityStreams comment object.
317
318
318
319
Subclasses should override this.
@@ -322,10 +323,11 @@ def get_comment(self, comment_id, activity_id=None, activity_author_id=None):
322
323
activity_id: string activity id, optional
323
324
activity_author_id: string activity author id, optional. Needed for some
324
325
sources (e.g. Facebook) to construct the comment permalink.
326
+ activity: activity object, optional. May avoid an API call if provided.
325
327
"""
326
328
raise NotImplementedError ()
327
329
328
- def get_like (self , activity_user_id , activity_id , like_user_id ):
330
+ def get_like (self , activity_user_id , activity_id , like_user_id , activity = None ):
329
331
"""Returns an ActivityStreams 'like' activity object.
330
332
331
333
Default implementation that fetches the activity and its likes, then
@@ -336,14 +338,14 @@ def get_like(self, activity_user_id, activity_id, like_user_id):
336
338
activity_user_id: string id of the user who posted the original activity
337
339
activity_id: string activity id
338
340
like_user_id: string id of the user who liked the activity
341
+ activity: activity object, optional. May avoid an API call if provided.
339
342
"""
340
- activities = self .get_activities (user_id = activity_user_id ,
341
- activity_id = activity_id ,
342
- fetch_likes = True )
343
- return self ._get_tag (activities , 'like' , like_user_id )
343
+ if not activity :
344
+ activity = self ._get_activity (activity_user_id , activity_id , fetch_likes = True )
345
+ return self ._get_tag (activity , 'like' , like_user_id )
344
346
345
347
def get_reaction (self , activity_user_id , activity_id , reaction_user_id ,
346
- reaction_id ):
348
+ reaction_id , activity = None ):
347
349
"""Returns an ActivityStreams 'reaction' activity object.
348
350
349
351
Default implementation that fetches the activity and its reactions, then
@@ -355,33 +357,45 @@ def get_reaction(self, activity_user_id, activity_id, reaction_user_id,
355
357
activity_id: string activity id
356
358
reaction_user_id: string id of the user who reacted
357
359
reaction_id: string id of the reaction
360
+ activity: activity object, optional. May avoid an API call if provided.
358
361
"""
359
- activities = self . get_activities ( user_id = activity_user_id ,
360
- activity_id = activity_id )
361
- return self ._get_tag (activities , 'react' , reaction_user_id , reaction_id )
362
+ if not activity :
363
+ activity = self . _get_activity ( activity_user_id , activity_id )
364
+ return self ._get_tag (activity , 'react' , reaction_user_id , reaction_id )
362
365
363
- def get_share (self , activity_user_id , activity_id , share_id ):
366
+ def get_share (self , activity_user_id , activity_id , share_id , activity = None ):
364
367
"""Returns an ActivityStreams 'share' activity object.
365
368
366
369
Args:
367
370
activity_user_id: string id of the user who posted the original activity
368
371
activity_id: string activity id
369
372
share_id: string id of the share object or the user who shared it
373
+ activity: activity object, optional. May avoid an API call if provided.
370
374
"""
371
- activities = self .get_activities (user_id = activity_user_id ,
372
- activity_id = activity_id ,
373
- fetch_shares = True )
374
- return self ._get_tag (activities , 'share' , share_id )
375
+ if not activity :
376
+ activity = self ._get_activity (activity_user_id , activity_id , fetch_shares = True )
377
+ return self ._get_tag (activity , 'share' , share_id )
375
378
376
- def get_rsvp (self , activity_user_id , event_id , user_id ):
377
- """Returns an ActivityStreams 'rsvp-*' activity object.
379
+ def get_rsvp (self , activity_user_id , event_id , user_id , event = None ):
380
+ """Returns an ActivityStreams RSVP activity object.
378
381
379
382
Args:
380
- activity_user_id: string id of the user who posted the original activity
383
+ activity_user_id: string id of the user who posted the event. unused.
381
384
event_id: string event id
382
- user_id: string id of the user who RSVPed
385
+ user_id: string user id
386
+ event: AS event activity (optional)
383
387
"""
384
- raise NotImplementedError ()
388
+ user_tag_id = self .tag_uri (user_id )
389
+ if not event :
390
+ event = self .get_event (event_id )
391
+ if not event :
392
+ return None
393
+
394
+ for rsvp in self .get_rsvps_from_event (event ['object' ]):
395
+ for field in 'actor' , 'object' :
396
+ id = rsvp .get (field , {}).get ('id' )
397
+ if id and user_id == util .parse_tag_uri (id )[1 ]:
398
+ return rsvp
385
399
386
400
def user_to_actor (self , user ):
387
401
"""Converts a user to an actor.
@@ -398,15 +412,21 @@ def user_to_actor(self, user):
398
412
"""
399
413
raise NotImplementedError ()
400
414
401
- def _get_tag (self , activities , verb , user_id , tag_id = None ):
402
- if not activities :
415
+ def _get_activity (self , user_id , activity_id , ** kwargs ):
416
+ activities = self .get_activities (user_id = user_id , activity_id = activity_id ,
417
+ ** kwargs )
418
+ if activities :
419
+ return activities [0 ]
420
+
421
+ def _get_tag (self , activity , verb , user_id , tag_id = None ):
422
+ if not activity :
403
423
return None
404
424
405
425
user_tag_id = self .tag_uri (user_id )
406
426
if tag_id :
407
427
tag_id = self .tag_uri (tag_id )
408
428
409
- for tag in activities [ 0 ] .get ('object' , {}).get ('tags' , []):
429
+ for tag in activity .get ('object' , {}).get ('tags' , []):
410
430
author = tag .get ('author' , {})
411
431
if (tag .get ('verb' ) == verb and
412
432
(not tag_id or tag_id == tag .get ('id' )) and
@@ -634,20 +654,26 @@ def get_rsvps_from_event(event):
634
654
return []
635
655
domain , event_id = parsed
636
656
url = event .get ('url' )
657
+ author = event .get ('author' )
637
658
638
659
rsvps = []
639
660
for verb , field in RSVP_TO_EVENT .items ():
640
661
for actor in event .get (field , []):
641
662
rsvp = {'objectType' : 'activity' ,
642
663
'verb' : verb ,
643
- 'actor' : actor ,
664
+ 'object' if verb == 'invite' else ' actor' : actor ,
644
665
'url' : url ,
645
666
}
667
+
646
668
if event_id and 'id' in actor :
647
669
_ , actor_id = util .parse_tag_uri (actor ['id' ])
648
670
rsvp ['id' ] = util .tag_uri (domain , '%s_rsvp_%s' % (event_id , actor_id ))
649
671
if url :
650
672
rsvp ['url' ] = '#' .join ((url , actor_id ))
673
+
674
+ if verb == 'invite' and author :
675
+ rsvp ['actor' ] = author
676
+
651
677
rsvps .append (rsvp )
652
678
653
679
return rsvps
0 commit comments