Skip to content

Commit 647e482

Browse files
committed
as2.link_tags: add class="mention" for Mention tags
see snarfed/bridgy-fed#887 (comment)
1 parent 2a1fa24 commit 647e482

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ Standardize function and method names in all modules to `to_as1`, `from_as`, etc
297297
* Handle other types of tags better, eg non-standard `Hashtag` and inner `tag` field for name.
298298
* Bug fix for videos, `mimeType` goes in outer object, not in `stream`.
299299
* Bug fix for `to`/`cc` with mixed dict and string elements.
300+
* `link_tags`: add `class="mention"` for `Mention` tags ([bridgy-fed/#887](https://github.com/snarfed/bridgy-fed/issues/887#issuecomment-2452141758)).
300301
* `atom`:
301302
* `atom_to_activity/ies`: Get URL from `link` for activities as well as objects. ([Thanks @imax9000!](https://github.com/snarfed/granary/issues/752))
302303
* `bluesky`:

granary/as2.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -752,12 +752,16 @@ def link_tags(obj):
752752
linked = ''
753753
for tag in tags:
754754
url = tag.get('href') or tag.get('url')
755+
# Mastodon seems to need class="mention" to convert a mention link to point
756+
# to the local instance's view of the remote actor profile
757+
# https://github.com/snarfed/bridgy-fed/issues/887#issuecomment-2452141758
758+
cls = 'class="mention" ' if tag.get('type') == 'Mention' else ''
755759
start = tag['startIndex']
756760
if start < last_end:
757761
logger.warning(f'tag indices overlap! skipping {url}')
758762
continue
759763
end = start + tag['length']
760-
linked = f"{linked}{orig[last_end:start]}<a href=\"{url}\">{orig[start:end]}</a>"
764+
linked = f"{linked}{orig[last_end:start]}<a {cls}href=\"{url}\">{orig[start:end]}</a>"
761765
last_end = end
762766
obj['content_is_html'] = True
763767
del tag['startIndex']

granary/tests/test_as2.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Most of the tests are in testdata/. This is just a few things that are too small
44
for full testdata tests.
55
"""
6+
import copy
67
from oauth_dropins.webutil import testutil
78

89
from .. import as2
@@ -600,11 +601,11 @@ def test_link_tags(self):
600601
self.assert_equals('foo\nbar\nbaz', obj['content'])
601602

602603
# with indices, should link and then remove indices
603-
obj['tag'] = [
604+
tags_with_indices = [
604605
{'href': 'http://bar', 'startIndex': 4, 'length': 3},
605606
{'url': 'http://baz', 'startIndex': 8, 'length': 3},
606607
]
607-
608+
obj['tag'] = copy.deepcopy(tags_with_indices)
608609
as2.link_tags(obj)
609610
self.assert_equals({
610611
'content': """\
@@ -619,6 +620,29 @@ def test_link_tags(self):
619620
],
620621
}, obj)
621622

623+
# Mention tag should include class="mention"
624+
obj = {
625+
'content': 'foo\nbar\nbaz',
626+
'tag': [
627+
{'href': 'http://bar', 'startIndex': 4, 'length': 3, 'type': 'Mention'},
628+
{'url': 'http://baz', 'startIndex': 8, 'length': 3},
629+
],
630+
}
631+
as2.link_tags(obj)
632+
self.assert_equals({
633+
'content': """\
634+
foo
635+
<a class="mention" href="http://bar">bar</a>
636+
<a href="http://baz">baz</a>
637+
""",
638+
'content_is_html': True,
639+
'tag': [
640+
{'href': 'http://bar', 'type': 'Mention'},
641+
{'url': 'http://baz'},
642+
],
643+
}, obj)
644+
645+
622646
def test_is_server_actor(self):
623647
self.assertFalse(as2.is_server_actor({}))
624648

0 commit comments

Comments
 (0)