AP: compact outgoing actor, author, and attributedTo fields to string ids

hope this improves compatibility with many other AP implementations that choke on objects instead of ids. fixes #658
pull/672/head
Ryan Barrett 2023-10-11 16:17:43 -07:00
rodzic c01375a300
commit 15a325c928
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
4 zmienionych plików z 23 dodań i 33 usunięć

Wyświetl plik

@ -494,8 +494,8 @@ def postprocess_as2(activity, orig_obj=None, wrap=True):
Args: Args:
activity (dict): AS2 object or activity activity (dict): AS2 object or activity
orig_obj (dict): AS2 object, optional. The target of activity's ``inReplyTo`` or orig_obj (dict): AS2 object, optional. The target of activity's
``Like``/``Announce``/etc object, if any. ``inReplyTo`` or ``Like``/``Announce``/etc object, if any.
wrap (bool): whether to wrap ``id``, ``url``, ``object``, ``actor``, and wrap (bool): whether to wrap ``id``, ``url``, ``object``, ``actor``, and
``attributedTo`` ``attributedTo``
""" """
@ -524,12 +524,15 @@ def postprocess_as2(activity, orig_obj=None, wrap=True):
}) })
return activity return activity
if wrap: # actors. wrap in our domain if necessary, then compact to just string id
for field in 'actor', 'attributedTo': # for compatibility, since many other AP implementations choke on objects.
activity[field] = [postprocess_as2_actor(actor, wrap=wrap) # https://github.com/snarfed/bridgy-fed/issues/658
for actor in util.get_list(activity, field)] for field in 'actor', 'attributedTo', 'author':
if len(activity[field]) == 1: actors = as1.get_objects(activity, field)
activity[field] = activity[field][0] if wrap:
actors = [postprocess_as2_actor(actor, wrap=wrap) for actor in actors]
ids = [a['id'] for a in actors if a.get('id')]
activity[field] = ids[0] if len(ids) == 1 else ids
# inReplyTo: singly valued, prefer id over url # inReplyTo: singly valued, prefer id over url
# TODO: ignore orig_obj, do for all inReplyTo # TODO: ignore orig_obj, do for all inReplyTo

Wyświetl plik

@ -224,7 +224,10 @@ ACCEPT = {
'id': 'http://localhost/user.com/followers#accept-https://mas.to/6d1a', 'id': 'http://localhost/user.com/followers#accept-https://mas.to/6d1a',
'actor': 'http://localhost/user.com', 'actor': 'http://localhost/user.com',
'object': { 'object': {
**ACCEPT_FOLLOW, 'type': 'Follow',
'id': 'https://mas.to/6d1a',
'object': 'http://localhost/user.com',
'actor': 'https://mas.to/users/swentel',
'url': 'https://mas.to/users/swentel#followed-https://user.com/', 'url': 'https://mas.to/users/swentel#followed-https://user.com/',
'to': ['https://www.w3.org/ns/activitystreams#Public'], 'to': ['https://www.w3.org/ns/activitystreams#Public'],
}, },
@ -1635,33 +1638,17 @@ class ActivityPubUtilsTest(TestCase):
'image': [{'url': 'http://r/foo'}, {'url': 'http://r/bar'}], 'image': [{'url': 'http://r/foo'}, {'url': 'http://r/bar'}],
})) }))
def test_postprocess_as2_actor_attributedTo(self): def test_postprocess_as2_actor_attributedTo_author(self):
g.user = Fake(id='site') g.user = Fake(id='site')
self.assert_equals({ self.assert_equals({
'actor': { 'actor': 'baj',
'id': 'baj', 'attributedTo': ['bar', 'baz'],
'preferredUsername': 'site', 'author': 'biff',
'url': 'http://localhost/r/site',
'inbox': 'https://fa.brid.gy/ap/site/inbox',
'outbox': 'https://fa.brid.gy/ap/site/outbox',
},
'attributedTo': [{
'id': 'bar',
'preferredUsername': 'site',
'url': 'http://localhost/r/site',
'inbox': 'https://fa.brid.gy/ap/site/inbox',
'outbox': 'https://fa.brid.gy/ap/site/outbox',
}, {
'id': 'baz',
'preferredUsername': 'site',
'url': 'http://localhost/r/site',
'inbox': 'https://fa.brid.gy/ap/site/inbox',
'outbox': 'https://fa.brid.gy/ap/site/outbox',
}],
'to': [as2.PUBLIC_AUDIENCE], 'to': [as2.PUBLIC_AUDIENCE],
}, postprocess_as2({ }, postprocess_as2({
'attributedTo': [{'id': 'bar'}, {'id': 'baz'}], 'attributedTo': [{'id': 'bar'}, {'id': 'baz'}],
'actor': {'id': 'baj'}, 'actor': {'id': 'baj'},
'author': {'id': 'biff'},
})) }))
def test_postprocess_as2_note(self): def test_postprocess_as2_note(self):

Wyświetl plik

@ -23,7 +23,7 @@ from .test_web import (
REPOST_AS2 = { REPOST_AS2 = {
**REPOST_AS2, **REPOST_AS2,
'actor': ACTOR_AS2, 'actor': 'http://localhost/user.com',
} }
del REPOST_AS2['cc'] del REPOST_AS2['cc']

Wyświetl plik

@ -251,7 +251,7 @@ AS2_CREATE = {
'https://mas.to/recipient', 'https://mas.to/recipient',
as2.PUBLIC_AUDIENCE, as2.PUBLIC_AUDIENCE,
], ],
'attributedTo': ACTOR_AS2, 'attributedTo': 'http://localhost/user.com',
'tag': [{ 'tag': [{
'type': 'Mention', 'type': 'Mention',
'href': 'https://mas.to/author', 'href': 'https://mas.to/author',
@ -344,7 +344,7 @@ NOTE_AS2 = {
'type': 'Note', 'type': 'Note',
'id': 'http://localhost/r/https://user.com/post', 'id': 'http://localhost/r/https://user.com/post',
'url': 'http://localhost/r/https://user.com/post', 'url': 'http://localhost/r/https://user.com/post',
'attributedTo': ACTOR_AS2, 'attributedTo': 'http://localhost/user.com',
'name': 'hello i am a post', 'name': 'hello i am a post',
'content': 'hello i am a post', 'content': 'hello i am a post',
'to': [as2.PUBLIC_AUDIENCE], 'to': [as2.PUBLIC_AUDIENCE],