Skip to content

Commit

Permalink
merge commments and likes from FB photo and photo post objects
Browse files Browse the repository at this point in the history
yet more work to handle FB's awful data model for photo posts. kind of for #189...kind of.
  • Loading branch information
snarfed committed Oct 28, 2014
1 parent 8758b97 commit 018d440
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion activitystreams
18 changes: 14 additions & 4 deletions facebook.py
Expand Up @@ -164,23 +164,33 @@ def get_activities_response(self, **kwargs):
# id. the post's object_id field points to the photo's id. de-dupe by
# switching the post to use the fb_object_id when it's provided.
activities = resp.setdefault('items', [])
fb_ids = set()
activities_by_fb_id = {}
for activity in activities:
obj = activity.get('object', {})
fb_id = obj.get('fb_object_id')
if not fb_id:
continue

fb_ids.add(fb_id)
activities_by_fb_id[fb_id] = activity
for x in activity, obj:
parsed = util.parse_tag_uri(x.get('id', ''))
if parsed:
_, orig_id = parsed
x['id'] = self.as_source.tag_uri(fb_id)
x['url'] = x.get('url', '').replace(orig_id, fb_id)

activities += [self.as_source.post_to_activity(p) for p in photos
if p.get('id') not in fb_ids]

for photo in photos:
photo_activity = self.as_source.post_to_activity(photo)
existing = activities_by_fb_id.get(photo.get('id'))
if existing:
existing['object'].setdefault('replies', {}).setdefault('items', []).extend(
photo_activity['object'].get('replies', {}).get('items', []))
existing['object'].setdefault('tags', []).extend(
[t for t in photo_activity['object'].get('tags', [])
if t.get('verb') == 'like'])
else:
activities.append(photo_activity)

# add events
activities += [self.as_source.event_to_activity(e, rsvps=r)
Expand Down
8 changes: 7 additions & 1 deletion facebook_test.py
Expand Up @@ -108,7 +108,13 @@ def test_get_activities_post_and_photo_duplicates(self):
json.dumps({}))
self.mox.ReplayAll()

self.assert_equals([self.post_activity], self.fb.get_activities())
got = self.fb.get_activities()
self.assertEquals(1, len(got))
obj = got[0]['object']
self.assertEquals('tag:facebook.com,2013:222', obj['id'])
self.assertEquals('https://facebook.com/212038/posts/222', obj['url'])
self.assertEquals(3, len(obj['replies']['items']))
self.assertEquals(3, len([t for t in obj['tags'] if t.get('verb') == 'like']))

def test_revoked(self):
self.expect_urlopen(
Expand Down

0 comments on commit 018d440

Please sign in to comment.