ActivityPub: translate ids in convert

pull/714/head
Ryan Barrett 2023-11-03 15:11:21 -07:00
rodzic fdc5b8e1e9
commit aa5c6a396e
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
6 zmienionych plików z 88 dodań i 30 usunięć

Wyświetl plik

@ -338,10 +338,12 @@ class ActivityPub(User, Protocol):
if obj.as2:
return obj.as2
elif obj.source_protocol in ('ap', 'activitypub'):
return as2.from_as1(obj.as1)
return postprocess_as2(as2.from_as1(obj.as1), **kwargs)
translated = as2.from_as1(cls.translate_ids(obj.as1))
if obj.source_protocol in ('ap', 'activitypub'):
return translated
return postprocess_as2(translated, **kwargs)
@classmethod
def verify_signature(cls, activity):

Wyświetl plik

@ -412,7 +412,10 @@ class ActivityPubTest(TestCase):
}, got.json, ignore=['publicKeyPem'])
def test_actor_handle_new_user(self, _, __, ___):
Fake.fetchable['fake:user'] = as2.to_as1(ACTOR)
Fake.fetchable['fake:user'] = as2.to_as1({
**ACTOR,
'id': 'fake:user',
})
got = self.client.get('/ap/fake:handle:user', base_url='https://fa.brid.gy/')
self.assertEqual(200, got.status_code)
self.assert_equals({
@ -1987,25 +1990,28 @@ class ActivityPubUtilsTest(TestCase):
obj.our_as1 = {}
self.assertEqual({}, ActivityPub.convert(obj))
obj = Object(id='http://orig', our_as1={
'id': 'http://user.com/like',
'objectType': 'activity',
'verb': 'like',
'actor': 'https://user.com/',
'object': 'https://mas.to/post',
})
self.assertEqual({
'@context': 'https://www.w3.org/ns/activitystreams',
'id': 'http://localhost/r/http://user.com/like',
'type': 'Like',
'actor': 'http://localhost/user.com',
'object': 'https://mas.to/post',
'to': [as2.PUBLIC_AUDIENCE],
}, ActivityPub.convert(obj))
obj.as2 = {'baz': 'biff'}
self.assertEqual({'baz': 'biff'}, ActivityPub.convert(obj))
# prevent HTTP fetch to infer protocol
self.store_object(id='https://mas.to/thing', source_protocol='activitypub')
obj.as2 = None
obj.our_as1 = {
'id': 'fake:like',
'objectType': 'activity',
'verb': 'like',
'actor': 'fake:user',
'object': 'https://mas.to/thing',
}
self.assertEqual({
'@context': 'https://www.w3.org/ns/activitystreams',
'id': 'https://fa.brid.gy/convert/ap/fake:like',
'type': 'Like',
'actor': 'https://fa.brid.gy/ap/fake:user',
'object': 'https://mas.to/thing',
'to': [as2.PUBLIC_AUDIENCE],
}, ActivityPub.convert(obj))
def test_postprocess_as2_idempotent(self):
g.user = self.make_user('foo.com')
@ -2127,6 +2133,8 @@ class ActivityPubUtilsTest(TestCase):
@patch('requests.get')
def test_target_for_author_is_object_id(self, mock_get):
mock_get.return_value = HTML
obj = self.store_object(id='http://the/author', our_as1={
'author': 'http://the/author',
})
@ -2138,3 +2146,28 @@ class ActivityPubUtilsTest(TestCase):
self.assertFalse(ActivityPub.send(Object(as2=NOTE),
'https://fed.brid.gy/ap/sharedInbox'))
mock_post.assert_not_called()
@patch('requests.post')
def test_send_convert_ids(self, mock_post):
mock_post.return_value = requests_response()
like = Object(our_as1={
'id': 'fake:like',
'objectType': 'activity',
'verb': 'like',
'object': 'fake:post',
'actor': 'fake:user',
})
self.assertTrue(ActivityPub.send(like, 'https://inbox'))
self.assertEqual(1, len(mock_post.call_args_list))
args, kwargs = mock_post.call_args_list[0]
self.assertEqual(('https://inbox',), args)
self.assertEqual({
'@context': 'https://www.w3.org/ns/activitystreams',
'id': 'https://fa.brid.gy/convert/ap/fake:like',
'type': 'Like',
'object': 'https://fa.brid.gy/convert/ap/fake:post',
'actor': 'https://fa.brid.gy/ap/fake:user',
'to': [as2.PUBLIC_AUDIENCE],
}, json_loads(kwargs['data']))

Wyświetl plik

@ -21,7 +21,7 @@ COMMENT_AS2 = {
'url': 'https://web.brid.gy/r/https://fake.com/123456',
'name': 'A ☕ reply',
'contentMap': {'en': COMMENT['content']},
'inReplyTo': 'https://fake.com/123',
'inReplyTo': 'https://fed.brid.gy/r/https://fake.com/123',
}
HTML = """\
<!DOCTYPE html>
@ -241,14 +241,16 @@ A ☕ reply
# self.assertEqual(f'https://ap.brid.gy/convert/web/https:/foo%3Fbar%23baz',
# resp.headers['Location'])
def test_web_to_activitypub_object(self):
url = 'https://user.com/bar?baz=baj&biff'
@patch('requests.get')
def test_web_to_activitypub_object(self, mock_get):
mock_get.return_value = requests_response(HTML)
self.make_user('user.com')
url = 'https://user.com/bar?baz=baj&biff'
Object(id=url, mf2=parse_mf2(HTML)['items'][0]).put()
resp = self.client.get(f'/convert/ap/{url}',
base_url='https://web.brid.gy/')
resp = self.client.get(f'/convert/ap/{url}', base_url='https://web.brid.gy/')
self.assertEqual(200, resp.status_code)
self.assert_equals(COMMENT_AS2, resp.json, ignore=['to'])
@ -270,8 +272,11 @@ A ☕ reply
base_url='https://web.brid.gy/')
self.assertEqual(400, resp.status_code)
def test_web_to_activitypub_url_decode(self):
@patch('requests.get')
def test_web_to_activitypub_url_decode(self, mock_get):
"""https://github.com/snarfed/bridgy-fed/issues/581"""
mock_get.return_value = requests_response(HTML)
self.make_user('user.com')
self.store_object(id='http://user.com/a#b', mf2=parse_mf2(HTML)['items'][0])

Wyświetl plik

@ -189,7 +189,8 @@ class FollowTest(TestCase):
self.check('https://bar/actor', resp, FOLLOW_URL, mock_get, mock_post)
def test_callback_stored_followee_with_our_as1(self, mock_get, mock_post):
self.store_object(id='https://bar/id', our_as1=as2.to_as1(FOLLOWEE))
self.store_object(id='https://bar/id', our_as1=as2.to_as1(FOLLOWEE),
source_protocol='activitypub')
mock_get.side_effect = (
requests_response(''),

Wyświetl plik

@ -19,6 +19,7 @@ from .test_web import (
ACTOR_HTML,
REPOST_AS2,
REPOST_HTML,
TOOT_AS2,
)
REPOST_AS2 = {
@ -89,7 +90,11 @@ class RedirectTest(testutil.TestCase):
@patch('requests.get')
def test_as2_fetch_post(self, mock_get):
mock_get.return_value = requests_response(REPOST_HTML)
mock_get.side_effect = [
requests_response(REPOST_HTML),
TOOT_AS2,
TOOT_AS2,
]
resp = self.client.get('/r/https://user.com/repost',
headers={'Accept': as2.CONTENT_TYPE})
@ -98,8 +103,12 @@ class RedirectTest(testutil.TestCase):
@patch('requests.get')
def test_as2_fetch_post_no_backlink(self, mock_get):
mock_get.return_value = requests_response(
REPOST_HTML.replace('<a href="http://localhost/"></a>', ''))
mock_get.side_effect = [
requests_response(
REPOST_HTML.replace('<a href="http://localhost/"></a>', '')),
TOOT_AS2,
TOOT_AS2,
]
resp = self.client.get('/r/https://user.com/repost',
headers={'Accept': as2.CONTENT_TYPE})

Wyświetl plik

@ -382,6 +382,8 @@ ACTIVITYPUB_GETS = [
NOT_FEDIVERSE, # Web
TOOT_AS2, # AP
ACTOR,
NOT_FEDIVERSE, # AP
NOT_FEDIVERSE, # Web
]
@ -819,6 +821,8 @@ class WebTest(TestCase):
NOT_FEDIVERSE, # Web
self.as2_resp(toot_as2_data), # AP
ACTOR,
NOT_FEDIVERSE, # AP
NOT_FEDIVERSE, # Web
]
mock_post.return_value = requests_response('abc xyz')
@ -900,6 +904,8 @@ class WebTest(TestCase):
TOOT_HTML, # AP
TOOT_AS2, # AP via rel-alternate
ACTOR,
NOT_FEDIVERSE, # AP
NOT_FEDIVERSE, # Web
]
mock_post.return_value = requests_response('abc xyz')
@ -1827,6 +1833,8 @@ http://this/404s
</a>
</body>
""", url='https://user.com/'),
NOT_FEDIVERSE,
NOT_FEDIVERSE,
]
self._test_verify(True, True, {})