Skip to content

Commit

Permalink
as2.link_tags: add class="mention" for Mention tags
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Nov 6, 2024
1 parent 2a1fa24 commit 647e482
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ Standardize function and method names in all modules to `to_as1`, `from_as`, etc
* Handle other types of tags better, eg non-standard `Hashtag` and inner `tag` field for name.
* Bug fix for videos, `mimeType` goes in outer object, not in `stream`.
* Bug fix for `to`/`cc` with mixed dict and string elements.
* `link_tags`: add `class="mention"` for `Mention` tags ([bridgy-fed/#887](https://github.com/snarfed/bridgy-fed/issues/887#issuecomment-2452141758)).
* `atom`:
* `atom_to_activity/ies`: Get URL from `link` for activities as well as objects. ([Thanks @imax9000!](https://github.com/snarfed/granary/issues/752))
* `bluesky`:
Expand Down
6 changes: 5 additions & 1 deletion granary/as2.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,12 +752,16 @@ def link_tags(obj):
linked = ''
for tag in tags:
url = tag.get('href') or tag.get('url')
# Mastodon seems to need class="mention" to convert a mention link to point
# to the local instance's view of the remote actor profile
# https://github.com/snarfed/bridgy-fed/issues/887#issuecomment-2452141758
cls = 'class="mention" ' if tag.get('type') == 'Mention' else ''
start = tag['startIndex']
if start < last_end:
logger.warning(f'tag indices overlap! skipping {url}')
continue
end = start + tag['length']
linked = f"{linked}{orig[last_end:start]}<a href=\"{url}\">{orig[start:end]}</a>"
linked = f"{linked}{orig[last_end:start]}<a {cls}href=\"{url}\">{orig[start:end]}</a>"
last_end = end
obj['content_is_html'] = True
del tag['startIndex']
Expand Down
28 changes: 26 additions & 2 deletions granary/tests/test_as2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Most of the tests are in testdata/. This is just a few things that are too small
for full testdata tests.
"""
import copy
from oauth_dropins.webutil import testutil

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

# with indices, should link and then remove indices
obj['tag'] = [
tags_with_indices = [
{'href': 'http://bar', 'startIndex': 4, 'length': 3},
{'url': 'http://baz', 'startIndex': 8, 'length': 3},
]

obj['tag'] = copy.deepcopy(tags_with_indices)
as2.link_tags(obj)
self.assert_equals({
'content': """\
Expand All @@ -619,6 +620,29 @@ def test_link_tags(self):
],
}, obj)

# Mention tag should include class="mention"
obj = {
'content': 'foo\nbar\nbaz',
'tag': [
{'href': 'http://bar', 'startIndex': 4, 'length': 3, 'type': 'Mention'},
{'url': 'http://baz', 'startIndex': 8, 'length': 3},
],
}
as2.link_tags(obj)
self.assert_equals({
'content': """\
foo
<a class="mention" href="http://bar">bar</a>
<a href="http://baz">baz</a>
""",
'content_is_html': True,
'tag': [
{'href': 'http://bar', 'type': 'Mention'},
{'url': 'http://baz'},
],
}, obj)


def test_is_server_actor(self):
self.assertFalse(as2.is_server_actor({}))

Expand Down

0 comments on commit 647e482

Please sign in to comment.