Skip to content

Commit

Permalink
Protocol.translate_ids: translate object.object
Browse files Browse the repository at this point in the history
for #1492
  • Loading branch information
snarfed committed Nov 28, 2024
1 parent f94e697 commit cf80138
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
6 changes: 5 additions & 1 deletion protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ def translate_ids(to_cls, obj):
* ``object.author``
* ``object.id``
* ``object.inReplyTo``
* ``object.object``
* ``attachments[].id``
* ``tags[objectType=mention].url``
* ``to``
Expand Down Expand Up @@ -756,6 +757,9 @@ def translate(elem, field, fn, uri=False):
or as1.get_owner(outer_obj) == o.get('id')
or type in ('follow', 'stop-following'))
translate(o, 'id', translate_user_id if is_actor else translate_object_id)
obj_is_actor = o.get('verb') in as1.VERBS_WITH_ACTOR_OBJECT
translate(o, 'object', translate_user_id if obj_is_actor
else translate_object_id)

for o in [outer_obj] + inner_objs:
translate(o, 'inReplyTo', translate_object_id)
Expand All @@ -774,7 +778,7 @@ def translate(elem, field, fn, uri=False):

outer_obj = util.trim_nulls(outer_obj)

if objs := outer_obj.get('object', []):
if objs := util.get_list(outer_obj ,'object'):
outer_obj['object'] = [o['id'] if o.keys() == {'id'} else o for o in objs]
if len(outer_obj['object']) == 1:
outer_obj['object'] = outer_obj['object'][0]
Expand Down
51 changes: 39 additions & 12 deletions tests/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,34 @@ def test_translate_ids_multiple_objects(self):
]
}))

def test_translate_ids_inner_object_object_id(self):
# https://github.com/snarfed/bridgy-fed/issues/1492
self.assert_equals({
'object': {
'verb': 'flag',
'object': 'other:o:fa:fake:post',
},
}, OtherFake.translate_ids({
'object': {
'verb': 'flag',
'object': 'fake:post',
},
}))

def test_translate_ids_inner_object_actor_id(self):
# https://github.com/snarfed/bridgy-fed/issues/1492
self.assert_equals({
'object': {
'verb': 'follow',
'object': 'other:u:fake:bob',
},
}, OtherFake.translate_ids({
'object': {
'verb': 'follow',
'object': 'fake:bob',
},
}))

def test_translate_ids_to_cc(self):
self.assert_equals({
'id': 'xyz',
Expand All @@ -729,6 +757,17 @@ def test_translate_ids_to_cc(self):
'cc': ['efake:eve', as2.PUBLIC_AUDIENCE],
}))

def test_translate_ids_empty(self):
self.assertEqual({}, Fake.translate_ids({}))

def test_translate_ids_single_inReplyTo(self):
obj = {'inReplyTo': 'foo'}
self.assertEqual(obj, Fake.translate_ids(obj))

def test_translate_ids_multiple_inReplyTo(self):
obj = {'inReplyTo': ['foo', 'bar']}
self.assertEqual(obj, Fake.translate_ids(obj))

def test_convert_object_is_from_user_adds_source_links(self):
alice = Fake(id='fake:alice')
self.assertEqual({
Expand Down Expand Up @@ -886,18 +925,6 @@ def test_bot_follow_user_missing_obj(self):
Fake.bot_follow(user)
self.assertEqual([], Fake.sent)

# TODO: translate_ids tests that actually test translation
def test_translate_ids_empty(self):
self.assertEqual({}, Fake.translate_ids({}))

def test_translate_ids_single_inReplyTo(self):
obj = {'inReplyTo': 'foo'}
self.assertEqual(obj, Fake.translate_ids(obj))

def test_translate_ids_multiple_inReplyTo(self):
obj = {'inReplyTo': ['foo', 'bar']}
self.assertEqual(obj, Fake.translate_ids(obj))


class ProtocolReceiveTest(TestCase):

Expand Down

0 comments on commit cf80138

Please sign in to comment.