Skip to content

Commit

Permalink
as2: populate icon as well as image
Browse files Browse the repository at this point in the history
e.g. for profile pictures
  • Loading branch information
snarfed committed Oct 25, 2017
1 parent f7874d2 commit 0f18bb4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
20 changes: 15 additions & 5 deletions granary/as2.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,24 @@ def all_from_as1(field, type=None):
return [from_as1(elem, type=type, context=None)
for elem in util.pop_list(obj, field)]

images = all_from_as1('image', type='Image')
obj.update({
'type': type,
'name': obj.pop('displayName', None),
'actor': from_as1(obj.get('actor'), context=None),
'attachment': all_from_as1('attachments'),
'attributedTo': all_from_as1('author', type='Person'),
'image': all_from_as1('image', type='Image'),
'image': images,
'inReplyTo': util.trim_nulls([orig.get('id') or orig.get('url')
for orig in obj.get('inReplyTo', [])]),
'object': from_as1(obj.get('object'), context=None),
'tag': all_from_as1('tags')
})

if obj_type == 'person':
# TODO: something better. (we don't know aspect ratio though.)
obj['icon'] = images

loc = obj.get('location')
if loc:
obj['location'] = from_as1(loc, type='Place', context=None)
Expand Down Expand Up @@ -134,14 +139,19 @@ def url_or_as1(val):
def all_to_as1(field):
return [to_as1(elem) for elem in util.pop_list(obj, field)]

images = []
# icon first since e.g. Mastodon uses icon for profile picture,
# image for featured photo.
for as2_img in util.pop_list(obj, 'icon') + util.pop_list(obj, 'image'):
as1_img = to_as1(as2_img, use_type=False)
if as1_img not in images:
images.append(as1_img)

obj.update({
'displayName': obj.pop('name', None),
'actor': to_as1(obj.get('actor')),
'attachments': all_to_as1('attachment'),
'image': [to_as1(img, use_type=False) for img in
# icon first since e.g. Mastodon uses icon for profile picture,
# image for featured photo.
util.pop_list(obj, 'icon') + util.pop_list(obj, 'image')],
'image': images,
'inReplyTo': [url_or_as1(orig) for orig in util.get_list(obj, 'inReplyTo')],
'location': url_or_as1(obj.get('location')),
'object': to_as1(obj.get('object')),
Expand Down
6 changes: 2 additions & 4 deletions granary/test/testdata/actor.as2.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
"id": "tag:example.com,2011:martin",
"url": "http://example.com/martin",
"name": "Martin Smith",
"image": [{
"type": "Image",
"url": "http://example.com/martin/image"
}]
"icon": [{"type": "Image", "url": "http://example.com/martin/image"}],
"image": [{"type": "Image", "url": "http://example.com/martin/image"}]
}
6 changes: 2 additions & 4 deletions granary/test/testdata/note.as2.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
"type": "Person",
"id": "tag:example.com,2001:ryan",
"name": "Ryan Barrett",
"image": [{
"type": "Image",
"url": "http://example.com/ryan/image"
}]
"icon": [{"type": "Image", "url": "http://example.com/ryan/image"}],
"image": [{"type": "Image", "url": "http://example.com/ryan/image"}]
}],
"location": {
"type": "Place",
Expand Down

0 comments on commit 0f18bb4

Please sign in to comment.