Skip to content

Commit

Permalink
activitypub.postprocess_as2: when adding <p> to content, also add to …
Browse files Browse the repository at this point in the history
…contentMap

for #1609
  • Loading branch information
snarfed committed Feb 7, 2025
1 parent 0865089 commit 6be749e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
13 changes: 8 additions & 5 deletions activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,16 +861,19 @@ def postprocess_as2(activity, orig_obj=None, wrap=True):
activity['object'] = activity['object'][0]

if content := obj_or_activity.get('content'):
# language, in contentMap
# https://github.com/snarfed/bridgy-fed/issues/681
content_map = obj_or_activity.setdefault('contentMap', {'en': content})

# wrap in <p>. some fediverse servers (eg Mastodon) have a white-space:
# pre-wrap style that applies to p inside content. this preserves
# meaningful whitespace in plain text content.
# https://github.com/snarfed/bridgy-fed/issues/990
if not content.startswith('<p>'):
content = obj_or_activity['content'] = f'<p>{content}</p>'

# language, in contentMap
# https://github.com/snarfed/bridgy-fed/issues/681
obj_or_activity.setdefault('contentMap', {'en': content})
obj_or_activity['content'] = f'<p>{content}</p>'
for lang, val in content_map.items():
if val == content:
content_map[lang] = obj_or_activity['content']

activity.pop('content_is_html', None)
return util.trim_nulls(activity)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -2314,6 +2314,18 @@ def test_postprocess_as2_note(self):
'content_is_html': True, # should be removed
}))

def test_postprocess_as2_note_update_contentMap(self):
self.assert_equals({
'type': 'Note',
'content': '<p>foo</p>',
'contentMap': {'en': '<p>foo</p>'},
'to': [as2.PUBLIC_AUDIENCE],
}, postprocess_as2({
'type': 'Note',
'content': 'foo',
'contentMap': {'en': 'foo'},
}))

def test_postprocess_as2_hashtag(self):
"""https://github.com/snarfed/bridgy-fed/issues/45"""
self.assert_equals({
Expand Down
6 changes: 4 additions & 2 deletions tests/test_integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,11 +986,13 @@ def test_atproto_convert_hashtag_to_activitypub_preserves_bsky_app_link(self):
'type': 'Note',
'id': 'https://bsky.brid.gy/convert/ap/at://xyz',
'content': '<p>My <a class="hashtag" href="https://bsky.app/search?q=%23original">#original</a> post</p>',
'contentMap': {
'en': '<p>My <a class="hashtag" href="https://bsky.app/search?q=%23original">#original</a> post</p>',
},
'url': 'http://localhost/r/https://bsky.app/profile/xyz',
'tag': [{
'type': 'Hashtag',
'name': '#original',
'href': 'https://bsky.app/search?q=%23original',
}],
}, ActivityPub.convert(obj),
ignore=['@context', 'attributedTo', 'contentMap', 'to'])
}, ActivityPub.convert(obj), ignore=['@context', 'attributedTo', 'to'])

0 comments on commit 6be749e

Please sign in to comment.