Skip to content

Commit

Permalink
as2: add support for Question (poll) object types, to and from as1
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Mar 4, 2023
1 parent 60a16f5 commit 4cdb01a
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ _Non-breaking changes:_
* `activity_changed`: ignore `inReplyTo.author` ([snarfed/bridgy#1338](https://github.com/snarfed/bridgy/issues/1338))
* `as2`:
* Support converting between AS1 `stop-following` and AS2 `Undo` `Follow`.
* Add the `Organization` and `Delete` object types.
* Add support for the `Question` (ie poll), `Organization`, and `Delete` object types.
* Convert `to`/`cc` to/from AS1 `to` for public and unlisted.
* `from_as1`: bug fix for image objects with `url` and `value` fields (for alt text).
* `from_as1`: convert `urls.displayName` to `attachment.name` ([bridgy-fed#331](https://github.com/snarfed/bridgy-fed/issues/331)).
Expand Down
27 changes: 22 additions & 5 deletions granary/as2.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,24 @@ def _invert(d):
'collection': 'Collection',
'comment': 'Note',
'event': 'Event',
'hashtag': 'Tag', # not in AS2 spec; needed for correct round trip conversion
# not in AS2 spec; needed for correct round trip conversion
'hashtag': 'Tag',
'image': 'Image',
# not in AS1 spec; needed to identify mentions in eg Bridgy Fed
'mention': 'Mention',
'note': 'Note',
'organization': 'Organization',
'person': 'Person',
'place': 'Place',
'question': 'Question',
'video': 'Video',
}
TYPE_TO_OBJECT_TYPE = _invert(OBJECT_TYPE_TO_TYPE)
TYPE_TO_OBJECT_TYPE['Note'] = 'note' # disambiguate

VERB_TO_TYPE = {
'delete': 'Delete',
'favorite': 'Like',
# not in AS1 spec; undo isn't a real AS1 verb
# https://activitystrea.ms/specs/json/schema/activity-schema.html#verbs
'undo': 'Undo',
'follow': 'Follow',
'invite': 'Invite',
'like': 'Like',
Expand All @@ -70,8 +70,10 @@ def _invert(d):
'rsvp-yes': 'Accept',
'share': 'Announce',
'tag': 'Add',
# not in AS1 spec; undo isn't a real AS1 verb
# https://activitystrea.ms/specs/json/schema/activity-schema.html#verbs
'undo': 'Undo',
'update': 'Update',
'delete': 'Delete',
}
TYPE_TO_VERB = _invert(VERB_TO_TYPE)
TYPE_TO_VERB['Like'] = 'like' # disambiguate
Expand Down Expand Up @@ -121,6 +123,8 @@ def all_from_as1(field, type=None):
'object': inner_objs.get('id'),
}

replies = obj.get('replies', {})

obj.update({
'type': type,
'name': obj.pop('displayName', None),
Expand All @@ -132,8 +136,18 @@ def all_from_as1(field, type=None):
'tag': all_from_as1('tags'),
'preferredUsername': obj.pop('username', None),
'url': as1.object_urls(obj),
'replies': from_as1(replies, context=None),
})

# question (poll) responses
# HACK: infer single vs multiple choice from whether
# votersCount matches the sum of votes for each option. not ideal!
voters = obj.get('votersCount')
votes = sum(opt.get('replies', {}).get('totalItems', 0)
for opt in util.get_list(obj, 'options'))
vote_field = 'oneOf' if voters == votes else 'anyOf'
obj[vote_field] = all_from_as1('options')

# images; separate featured (aka header) and non-featured.
images = util.get_list(obj, 'image')
featured = []
Expand Down Expand Up @@ -314,6 +328,9 @@ def all_to_as1(field):
'to': [{'objectType': 'group', 'alias': '@unlisted'}] if PUBLICS.intersection(cc)
else [{'objectType': 'group', 'alias': '@public'}] if PUBLICS.intersection(to)
else None,
# question (poll) responses
'options': all_to_as1('anyOf') + all_to_as1('oneOf'),
'replies': to_as1(obj.get('replies')),
})

# media
Expand Down
33 changes: 33 additions & 0 deletions granary/tests/testdata/question.as.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"id": "https://mastodon.social/users/Dyani/statuses/109960958872631870",
"objectType": "question",
"content": "foo?",
"endTime": "2023-03-04T19:17:56Z",
"votersCount": 24,
"options": [
{
"displayName": "I ❤️ polls",
"replies": {
"totalItems": 17,
"objectType": "collection"
},
"objectType": "note"
},
{
"displayName": "I am indifferent",
"replies": {
"totalItems": 6,
"objectType": "collection"
},
"objectType": "note"
},
{
"displayName": "Polls annoy me",
"replies": {
"totalItems": 1,
"objectType": "collection"
},
"objectType": "note"
}
]
}
34 changes: 34 additions & 0 deletions granary/tests/testdata/question.as2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"@context": "https://www.w3.org/ns/activitystreams",
"id": "https://mastodon.social/users/Dyani/statuses/109960958872631870",
"type": "Question",
"content": "foo?",
"endTime": "2023-03-04T19:17:56Z",
"votersCount": 24,
"oneOf": [
{
"name": "I ❤️ polls",
"replies": {
"totalItems": 17,
"type": "Collection"
},
"type": "Note"
},
{
"name": "I am indifferent",
"replies": {
"totalItems": 6,
"type": "Collection"
},
"type": "Note"
},
{
"name": "Polls annoy me",
"replies": {
"totalItems": 1,
"type": "Collection"
},
"type": "Note"
}
]
}
42 changes: 42 additions & 0 deletions granary/tests/testdata/question_multiple_choice.as.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"id": "https://oslo.town/users/matt/statuses/108244351334928989",
"objectType": "question",
"content": "foo?",
"closed": "2022-05-04T21:22:04Z",
"endTime": "2022-05-04T21:22:04Z",
"votersCount": 44,
"options": [
{
"displayName": "😊",
"replies": {
"totalItems": 10,
"objectType": "collection"
},
"objectType": "note"
},
{
"displayName": "🙂",
"replies": {
"totalItems": 27,
"objectType": "collection"
},
"objectType": "note"
},
{
"displayName": "😐",
"replies": {
"totalItems": 10,
"objectType": "collection"
},
"objectType": "note"
},
{
"displayName": "🙁",
"replies": {
"totalItems": 4,
"objectType": "collection"
},
"objectType": "note"
}
]
}
43 changes: 43 additions & 0 deletions granary/tests/testdata/question_multiple_choice.as2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"@context": "https://www.w3.org/ns/activitystreams",
"id": "https://oslo.town/users/matt/statuses/108244351334928989",
"type": "Question",
"content": "foo?",
"closed": "2022-05-04T21:22:04Z",
"endTime": "2022-05-04T21:22:04Z",
"votersCount": 44,
"anyOf": [
{
"name": "😊",
"replies": {
"totalItems": 10,
"type": "Collection"
},
"type": "Note"
},
{
"name": "🙂",
"replies": {
"totalItems": 27,
"type": "Collection"
},
"type": "Note"
},
{
"name": "😐",
"replies": {
"totalItems": 10,
"type": "Collection"
},
"type": "Note"
},
{
"name": "🙁",
"replies": {
"totalItems": 4,
"type": "Collection"
},
"type": "Note"
}
]
}

0 comments on commit 4cdb01a

Please sign in to comment.