activitypub: move compacting actors from postprocess_as2 to convert

for #690
as2-actor-ids
Ryan Barrett 2023-11-24 08:34:39 -08:00
rodzic 5d0b275ca8
commit ee571e5b25
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
2 zmienionych plików z 25 dodań i 29 usunięć

Wyświetl plik

@ -325,15 +325,24 @@ class ActivityPub(User, Protocol):
Returns:
dict: AS2 JSON
"""
if not obj:
if not obj or not obj.as1:
return {}
if obj.as2:
return obj.as2
translated = cls.translate_ids(obj.as1)
if translated:
translated['actor'] = as1.get_object(translated, 'actor').get('id')
# compact actors to just string id for compatibility, since many other
# AP implementations choke on objects.
# https://github.com/snarfed/bridgy-fed/issues/658
#
# TODO: expand this to general purpose compact() function and use elsewhere,
# eg in models.resolve_id
for o in translated, as1.get_object(translated):
for field in 'actor', 'attributedTo', 'author':
actors = as1.get_objects(o, field)
ids = [a['id'] for a in actors if a.get('id')]
o[field] = ids[0] if len(ids) == 1 else ids
converted = as2.from_as1(translated)
if obj.source_protocol in ('ap', 'activitypub'):
@ -559,18 +568,6 @@ def postprocess_as2(activity, orig_obj=None, wrap=True):
})
return activity
# actors. wrap in our domain if necessary, then compact to just string id
# for compatibility, since many other AP implementations choke on objects.
# https://github.com/snarfed/bridgy-fed/issues/658
# TODO: expand this to general purpose compact() function and use elsewhere,
# eg in models.resolve_id
for field in 'actor', 'attributedTo', 'author':
actors = as1.get_objects(activity, field)
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
# TODO: ignore orig_obj, do for all inReplyTo
orig_id = orig_obj.get('id') if orig_obj else None

Wyświetl plik

@ -1741,19 +1741,6 @@ class ActivityPubUtilsTest(TestCase):
'image': [{'url': 'http://r/foo'}, {'url': 'http://r/bar'}],
}))
def test_postprocess_as2_actor_attributedTo_author(self):
g.user = Fake(id='fake:site')
self.assert_equals({
'actor': 'baj',
'attributedTo': ['bar', 'baz'],
'author': 'biff',
'to': [as2.PUBLIC_AUDIENCE],
}, postprocess_as2({
'attributedTo': [{'id': 'bar'}, {'id': 'baz'}],
'actor': {'id': 'baj'},
'author': {'id': 'biff'},
}))
def test_postprocess_as2_note(self):
self.assert_equals({
'id': 'http://localhost/r/xyz',
@ -2016,6 +2003,18 @@ class ActivityPubUtilsTest(TestCase):
'to': [as2.PUBLIC_AUDIENCE],
}, ActivityPub.convert(obj))
def test_convert_compact_actor_attributedTo_author(self):
obj = Object(our_as1={
'actor': {'id': 'baj'},
'author': [{'id': 'bar'}],
'object': {'author': {'id': 'biff'}},
})
self.assert_equals({
'actor': 'baj',
'attributedTo': 'bar',
'object': {'attributedTo': 'biff'},
}, ActivityPub.convert(obj), ignore=['to'])
def test_postprocess_as2_idempotent(self):
g.user = self.user