Skip to content

Commit

Permalink
facebook: support photos in comments. for snarfed/bridgy#646
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Apr 9, 2016
1 parent 3fbded0 commit 8f23e84
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
11 changes: 10 additions & 1 deletion granary/facebook.py
Expand Up @@ -69,7 +69,7 @@
API_BASE = 'https://graph.facebook.com/v2.2/'
API_COMMENTS = '%s/comments'
API_COMMENTS_ALL = 'comments?filter=stream&ids=%s'
API_COMMENT = '%s?fields=id,message,from,created_time,message_tags,parent'
API_COMMENT = '%s?fields=id,message,from,created_time,message_tags,parent,attachment'
# Ideally this fields arg would just be [default fields plus comments], but
# there's no way to ask for that. :/
# https://developers.facebook.com/docs/graph-api/using-graph-api/v2.1#fields
Expand Down Expand Up @@ -1198,6 +1198,15 @@ def comment_to_object(self, comment, post_id=None, post_author_id=None):
post_author_id=post_author_id)
})

att = comment.get('attachment')
if att and att.get('type') == 'photo' and not obj.get('image'):
obj['image'] = {'url': att.get('media', {}).get('image', {}).get('src')}
obj.setdefault('attachments', []).append({
'objectType': 'image',
'image': obj['image'],
'url': att.get('url'),
})

return self.postprocess_object(obj)

def share_to_object(self, share):
Expand Down
39 changes: 34 additions & 5 deletions granary/test/test_facebook.py
Expand Up @@ -123,6 +123,22 @@ def tag_uri(name):
'privacy': {'value': ''}, # empty means public
'actions': [{'name': 'See Original', 'link': 'http://ald.com/foobar'}]
}]
COMMENT_WITH_PHOTO = copy.deepcopy(COMMENTS[0])
COMMENT_WITH_PHOTO['attachment'] = {
'type': 'photo',
'url': 'https://www.facebook.com/photo.php?fbid=10154842838994408&set=p.10154842838994408&type=3',
'media': {
'image': {
'src': 'https://scontent.xx.fbcdn.net/hphotos-xat1/v/t1.0-9/s720x720/12932713_10154842838994408_8485929365544529955_n.jpg?oh=d27b9a87ea83b77f00ef1f30a78d914d&oe=5775B834',
'height': 405,
'width': 720,
}
},
'target': {
'id': '10154842838994408',
'url': 'https://www.facebook.com/photo.php?fbid=10154842838994408&set=p.10154842838994408&type=3',
},
}
SHARE = { # Facebook
'id': '321_654',
'from': {
Expand Down Expand Up @@ -329,6 +345,15 @@ def tag_uri(name):
'upstreamDuplicates': ['http://ald.com/foobar'],
},
]
COMMENT_WITH_PHOTO_OBJ = copy.deepcopy(COMMENT_OBJS[0])
COMMENT_WITH_PHOTO_OBJ.update({
'image': {'url': 'https://scontent.xx.fbcdn.net/hphotos-xat1/v/t1.0-9/s720x720/12932713_10154842838994408_8485929365544529955_n.jpg?oh=d27b9a87ea83b77f00ef1f30a78d914d&oe=5775B834'},
'attachments': [{
'objectType': 'image',
'image': {'url': 'https://scontent.xx.fbcdn.net/hphotos-xat1/v/t1.0-9/s720x720/12932713_10154842838994408_8485929365544529955_n.jpg?oh=d27b9a87ea83b77f00ef1f30a78d914d&oe=5775B834'},
'url': 'https://www.facebook.com/photo.php?fbid=10154842838994408&set=p.10154842838994408&type=3',
}],
})
LIKE_OBJS = [{ # ActivityStreams
'id': tag_uri('10100176064482163_liked_by_100004'),
'url': 'https://www.facebook.com/212038/posts/10100176064482163#liked-by-100004',
Expand Down Expand Up @@ -1409,14 +1434,14 @@ def test_get_activities_search_not_implemented(self):

def test_get_comment(self):
self.expect_urlopen(
'123_456?fields=id,message,from,created_time,message_tags,parent',
'123_456?fields=id,message,from,created_time,message_tags,parent,attachment',
COMMENTS[0])
self.mox.ReplayAll()
self.assert_equals(COMMENT_OBJS[0], self.fb.get_comment('123_456'))

def test_get_comment_activity_author_id(self):
self.expect_urlopen(
'123_456?fields=id,message,from,created_time,message_tags,parent',
'123_456?fields=id,message,from,created_time,message_tags,parent,attachment',
COMMENTS[0])
self.mox.ReplayAll()

Expand All @@ -1427,17 +1452,17 @@ def test_get_comment_activity_author_id(self):

def test_get_comment_400s_id_with_underscore(self):
self.expect_urlopen(
'123_456_789?fields=id,message,from,created_time,message_tags,parent',
'123_456_789?fields=id,message,from,created_time,message_tags,parent,attachment',
{}, status=400)
self.expect_urlopen(
'789?fields=id,message,from,created_time,message_tags,parent',
'789?fields=id,message,from,created_time,message_tags,parent,attachment',
COMMENTS[0])
self.mox.ReplayAll()
self.assert_equals(COMMENT_OBJS[0], self.fb.get_comment('123_456_789'))

def test_get_comment_400s_id_without_underscore(self):
self.expect_urlopen(
'123?fields=id,message,from,created_time,message_tags,parent',
'123?fields=id,message,from,created_time,message_tags,parent,attachment',
{}, status=400)
self.mox.ReplayAll()
self.assertRaises(urllib2.HTTPError, self.fb.get_comment, '123')
Expand Down Expand Up @@ -1639,6 +1664,10 @@ def test_comment_to_object_with_parent_comment(self):
},
}))

def test_comment_with_photo_to_object(self):
self.assert_equals(COMMENT_WITH_PHOTO_OBJ,
self.fb.comment_to_object(COMMENT_WITH_PHOTO))

def test_share_to_object_empty(self):
self.assert_equals({}, self.fb.share_to_object({}))

Expand Down

0 comments on commit 8f23e84

Please sign in to comment.