Skip to content

Commit

Permalink
AP: strip internal content_is_html field before sending activities
Browse files Browse the repository at this point in the history
for #1584, maybe
  • Loading branch information
snarfed committed Dec 2, 2024
1 parent f802c90 commit 2eca1c0
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 18 deletions.
2 changes: 1 addition & 1 deletion activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,12 +869,12 @@ def postprocess_as2(activity, orig_obj=None, wrap=True):
# https://github.com/snarfed/bridgy-fed/issues/990
if not content.startswith('<p>'):
content = obj_or_activity['content'] = f'<p>{content}</p>'
obj_or_activity['content_is_html'] = True

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

activity.pop('content_is_html', None)
return util.trim_nulls(activity)


Expand Down
1 change: 0 additions & 1 deletion pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ def serve_feed(*, objects, format, user, title, as_snippets=False, quiet=False):
'objectType': 'note',
'id': obj.key.id(),
'content': f'{obj.actor_link(image=False, user=user)} {obj.phrase} {obj.content}',
'content_is_html': True,
'updated': obj.updated.isoformat(),
'url': as1.get_url(obj.as1) or as1.get_url(as1.get_object(obj.as1)),
} for obj in objects]
Expand Down
9 changes: 2 additions & 7 deletions tests/test_activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -2392,12 +2392,12 @@ def test_postprocess_as2_note(self):
'type': 'Note',
'content': '<p>foo</p>',
'contentMap': {'en': '<p>foo</p>'},
'content_is_html': True,
'to': [as2.PUBLIC_AUDIENCE],
}, postprocess_as2({
'id': 'xyz',
'type': 'Note',
'content': 'foo',
'content_is_html': True, # should be removed
}))

def test_postprocess_as2_hashtag(self):
Expand All @@ -2424,7 +2424,6 @@ def test_postprocess_as2_plain_text_content_links_hashtags_mentions(self):
self.assert_equals({
'content': expected,
'contentMap': {'en': expected},
'content_is_html': True,
'tag': [{
'type': 'Mention',
'href': 'http://inst/bar',
Expand Down Expand Up @@ -2457,7 +2456,6 @@ def test_postprocess_as2_moves_link_attachments_to_content(self):
'contentMap': {
'en': '<p><a href="http://a/link">check it out</a><br><br><a href="http://another/link">another/link</a></p>',
},
'content_is_html': True,
}, postprocess_as2({
'type': 'Note',
'attachment': [{
Expand All @@ -2478,7 +2476,6 @@ def test_postprocess_as2_appends_link_attachments_to_content(self):
'contentMap': {
'en': '<p>original<br><br><a href="http://a/link">a/link</a></p>',
},
'content_is_html': True,
}, postprocess_as2({
'type': 'Note',
'content': 'original',
Expand Down Expand Up @@ -2924,7 +2921,7 @@ def test_convert_quote_post(self):
'href': 'https://bsky.brid.gy/convert/ap/at://did:bob/app.bsky.feed.post/456',
'name': 'RE: https://bsky.app/profile/did:bob/post/456',
}],
}, ActivityPub.convert(obj), ignore=['contentMap', 'content_is_html', 'to'])
}, ActivityPub.convert(obj), ignore=['contentMap', 'to'])

def test_send_convert_mention_non_bridged_id_uses_profile_url(self):
self.store_object(id='did:plc:5zspv27pk4iqtrl2ql2nykjh', raw={'foo': 'bar'})
Expand Down Expand Up @@ -2952,7 +2949,6 @@ def test_send_convert_mention_non_bridged_id_uses_profile_url(self):
}],
'to': ['https://www.w3.org/ns/activitystreams#Public'],
'cc': ['https://bsky.app/profile/did:plc:5zspv27pk4iqtrl2ql2nykjh'],
'content_is_html': True,
}, ActivityPub.convert(obj))

def test_postprocess_as2_idempotent(self):
Expand Down Expand Up @@ -3157,6 +3153,5 @@ def test_send_dm(self, mock_post):
'attributedTo': 'https://web.brid.gy/web.brid.gy',
'content': '<p>hello world</p>',
'contentMap': {'en': '<p>hello world</p>'},
'content_is_html': True,
'to': [ACTOR['id']],
}, json_loads(kwargs['data']))
3 changes: 1 addition & 2 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
'name': 'A ☕ reply',
'content': '<p>A ☕ reply</p>',
'contentMap': {'en': '<p>A ☕ reply</p>'},
'content_is_html': True,
'inReplyTo': 'https://web.brid.gy/r/https://fake.com/123',
'published': '2012-12-05T00:58:26+00:00',
}
Expand Down Expand Up @@ -399,7 +398,7 @@ def test_web_to_activitypub_no_user(self, mock_get):
hcard,
hcard,
hcard,
requests_response(status=404), # webfinger for protocol inference
requests_response(status=404), # webfinger for protocol inference
]

resp = self.client.get(f'/convert/ap/https://nope.com/post',
Expand Down
5 changes: 1 addition & 4 deletions tests/test_integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ def test_atproto_notify_reply_to_activitypub(self, mock_post):
'attributedTo': 'https://bsky.brid.gy/ap/did:plc:alice',
'content': '<p>I hereby reply</p>',
'contentMap': {'en': '<p>I hereby reply</p>'},
'content_is_html': True,
'inReplyTo': 'http://inst/post',
'tag': [{'type': 'Mention', 'href': 'http://inst/bob'}],
'to': ['https://www.w3.org/ns/activitystreams#Public'],
Expand Down Expand Up @@ -425,8 +424,7 @@ def test_activitypub_follow_bsky_bot_user_enables_protocol(self, mock_get, mock_
'attributedTo': 'https://bsky.brid.gy/bsky.brid.gy',
'content': message,
'contentMap': {'en': message},
'content_is_html': True,
'tag': [{
'tag': [{
'type': 'Mention',
'href': 'https://inst/alice',
}],
Expand Down Expand Up @@ -761,7 +759,6 @@ def test_atproto_mention_activitypub(self, mock_get, mock_post):
'url': 'http://localhost/r/https://bsky.app/profile/did:plc:alice/post/123',
'attributedTo': 'https://bsky.brid.gy/ap/did:plc:alice',
'content': '<p>maybe if <a class="mention" href="https://inst/bob">@bob.inst.ap.brid.gy</a> and Alf meet up</p>',
'content_is_html': True,
'tag': [{
'type': 'Mention',
'name': '@bob.inst.ap.brid.gy',
Expand Down
3 changes: 0 additions & 3 deletions tests/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@
<a class="u-in-reply-to" href="https://mas.to/toot">foo ☕ bar</a>
<a href="http://localhost/"></a></p>""",
},
'content_is_html': True,
'inReplyTo': 'https://mas.to/toot/id',
'to': [as2.PUBLIC_AUDIENCE],
'cc': [
Expand Down Expand Up @@ -376,7 +375,6 @@
'name': 'hello i am a post',
'content': '<p>hello i am a post</p>',
'contentMap': {'en': '<p>hello i am a post</p>'},
'content_is_html': True,
'to': [as2.PUBLIC_AUDIENCE],
}
CREATE_AS2 = {
Expand Down Expand Up @@ -1073,7 +1071,6 @@ def test_post_type_discovery_multiple_types(self, mock_get, mock_post):
'type': 'Create',
'actor': 'http://localhost/user.com',
'content': 'hello i am a post',
'content_is_html': None,
# TODO: this is an awkward wart left over from the multi-type mf2.
# remove it eventually.
'object': {
Expand Down

0 comments on commit 2eca1c0

Please sign in to comment.