Skip to content

Commit

Permalink
Protocol.translate_ids: use URIs for mention tag url field
Browse files Browse the repository at this point in the history
for #1366
  • Loading branch information
snarfed committed Oct 8, 2024
1 parent a5cbc9b commit 4a4d965
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
6 changes: 4 additions & 2 deletions protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ def translate_ids(to_cls, obj):
outer_obj = copy.deepcopy(obj)
inner_objs = outer_obj['object'] = as1.get_objects(outer_obj)

def translate(elem, field, fn):
def translate(elem, field, fn, uri=False):
elem[field] = as1.get_objects(elem, field)
for obj in elem[field]:
if id := obj.get('id'):
Expand All @@ -722,6 +722,8 @@ def translate(elem, field, fn):
# make it a noop if we don't know enough about from/to?
if from_cls and from_cls != to_cls:
obj['id'] = fn(id=id, from_=from_cls, to=to_cls)
if obj['id'] and uri:
obj['id'] = to_cls(id=obj['id']).id_uri()

elem[field] = [o['id'] if o.keys() == {'id'} else o
for o in elem[field]]
Expand All @@ -746,7 +748,7 @@ def translate(elem, field, fn):
translate(o, field, translate_user_id)
for tag in as1.get_objects(o, 'tags'):
if tag.get('objectType') == 'mention':
translate(tag, 'url', translate_user_id)
translate(tag, 'url', translate_user_id, uri=True)
for att in as1.get_objects(o, 'attachments'):
translate(att, 'id', translate_object_id)
url = att.get('url')
Expand Down
4 changes: 2 additions & 2 deletions tests/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ def test_translate_ids_reply(self):
'author': 'other:u:fake:alice',
'tags': [{
'objectType': 'mention',
'url': 'other:u:fake:bob',
'url': 'uri:other:u:fake:bob',
}],
},
}, OtherFake.translate_ids({
Expand Down Expand Up @@ -624,7 +624,7 @@ def test_translate_ids_attachments_mention_tags(self):
'url': 'fake:456'},
],
'tags': [
{'objectType': 'mention', 'url': 'other:u:fake:alice'},
{'objectType': 'mention', 'url': 'uri:other:u:fake:alice'},
{'url': 'fake:000'},
],
}, OtherFake.translate_ids({
Expand Down
18 changes: 18 additions & 0 deletions tests/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -3333,6 +3333,24 @@ def test_convert_translates_ids(self, *_):
<a class="u-in-reply-to" href="https://ap.brid.gy/convert/web/http://fed/post"></a>
</article>""", Web.convert(obj, from_user=None), ignore_blanks=True)

def test_convert_mention_web_user_translate_domain_id_to_homepage_url(self, *_):
obj = self.store_object(id='fake:mention', source_protocol='fake', our_as1={
'objectType': 'note',
'id': 'fake:mention',
'tags': [{
'objectType': 'mention',
'url': 'user.com',
'displayName': '@[email protected]',
}],
})
self.assert_multiline_in("""\
<article class="h-entry">
<span class="p-uid">https://fa.brid.gy/convert/web/fake:mention</span>
<div class="e-content p-name">
<a class="u-mention" aria-hidden="true" href="https://user.com/"></a>
</div>
</article>""", Web.convert(obj), ignore_blanks=True)

def test_target_for(self, _, __):
self.assertIsNone(Web.target_for(Object(id='x')))

Expand Down

0 comments on commit 4a4d965

Please sign in to comment.