Bluesky => fediverse: link mentions of non-bridged users to their bsky.app profile

fixes #1288
pull/1336/head
Ryan Barrett 2024-09-17 13:34:57 -07:00
rodzic 4df76d0db7
commit 2fad8e1c24
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
3 zmienionych plików z 70 dodań i 1 usunięć

3
ids.py
Wyświetl plik

@ -148,6 +148,9 @@ def translate_user_id(*, id, from_, to):
return id
case _, 'activitypub' | 'web':
from activitypub import ActivityPub
if user and not user.is_enabled(ActivityPub):
return user.web_url()
return subdomain_wrap(from_, f'/{to.ABBREV}/{id}')
# only for unit tests

Wyświetl plik

@ -2819,6 +2819,63 @@ class ActivityPubUtilsTest(TestCase):
}],
}, ActivityPub.convert(obj), ignore=['contentMap', 'content_is_html', 'to'])
def test_send_convert_mention_non_bridged_id_uses_profile_url(self):
self.store_object(id='did:plc:5zspv27pk4iqtrl2ql2nykjh', raw={'foo': 'bar'})
self.make_user(id='did:plc:5zspv27pk4iqtrl2ql2nykjh', cls=ATProto)
obj = Object(our_as1={
'objectType': 'note',
'content': 'hello @snarfed2.bsky.social',
'tags': [{
'objectType': 'mention',
'url': 'did:plc:5zspv27pk4iqtrl2ql2nykjh',
'displayName': '@snarfed2.bsky.social',
'startIndex': 6,
'length': 21,
}],
})
self.assertEqual({
'@context': 'https://www.w3.org/ns/activitystreams',
'type': 'Note',
'content': '<p>hello <a href="https://bsky.app/profile/did:plc:5zspv27pk4iqtrl2ql2nykjh">@snarfed2.bsky.social</a></p>',
'contentMap': {'en': '<p>hello <a href="https://bsky.app/profile/did:plc:5zspv27pk4iqtrl2ql2nykjh">@snarfed2.bsky.social</a></p>'},
'tag': [{
'type': 'Mention',
'name': '@snarfed2.bsky.social',
'href': 'https://bsky.app/profile/did:plc:5zspv27pk4iqtrl2ql2nykjh',
}],
'to': ['https://www.w3.org/ns/activitystreams#Public'],
'cc': ['https://bsky.app/profile/did:plc:5zspv27pk4iqtrl2ql2nykjh'],
'content_is_html': True,
}, ActivityPub.convert(obj))
@patch('requests.post', return_value=requests_response())
def test_send_dm(self, mock_post):
bot = self.make_user('web.brid.gy', cls=Web)
user = self.make_user(ACTOR['id'], cls=ActivityPub, obj_as2=ACTOR)
dm = Object(id='https://internal.brid.gy/dm', source_protocol='web', our_as1={
'objectType': 'note',
'author': 'web.brid.gy',
'content': 'hello world',
'to': [ACTOR['id']],
})
dm.put()
self.assertTrue(ActivityPub.send(dm, ACTOR['inbox'], from_user=bot))
self.assertEqual(1, len(mock_post.call_args_list))
args, kwargs = mock_post.call_args_list[0]
self.assertEqual((ACTOR['inbox'],), args)
self.assertEqual({
'@context': 'https://www.w3.org/ns/activitystreams',
'type': 'Note',
'id': 'http://localhost/r/https://internal.brid.gy/dm',
'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']))
def test_postprocess_as2_idempotent(self):
for obj in (ACTOR, REPLY_OBJECT, REPLY_OBJECT_WRAPPED, REPLY,
NOTE_OBJECT, NOTE, MENTION_OBJECT, MENTION, LIKE,

Wyświetl plik

@ -25,13 +25,19 @@ class IdsTest(TestCase):
Fake(id='fake:user',
copies=[Target(uri='did:plc:789', protocol='atproto')]).put()
# DID doc and ATProto, used to resolve handle in bsky.app URL
# ATProto with DID docs, used to resolve handle in bsky.app URL
did = self.store_object(id='did:plc:123', raw={
'id': 'did:plc:123',
'alsoKnownAs': ['at://user.com'],
})
ATProto(id='did:plc:123', obj_key=did.key).put()
did = self.store_object(id='did:plc:000', raw={
'id': 'did:plc:000',
'alsoKnownAs': ['at://zero.com'],
})
ATProto(id='did:plc:000').put()
for from_, id, to, expected in [
(ActivityPub, 'https://inst/user', ActivityPub, 'https://inst/user'),
(ActivityPub, 'https://inst/user', ATProto, 'did:plc:456'),
@ -54,6 +60,9 @@ class IdsTest(TestCase):
(ATProto, 'https://bsky.app/profile/user.com', ATProto, 'did:plc:123'),
(ATProto, 'https://bsky.app/profile/did:plc:123', ATProto, 'did:plc:123'),
# user, not enabled, no copy
(ATProto, 'did:plc:000', ActivityPub, 'https://bsky.app/profile/zero.com'),
(Fake, 'fake:user', ActivityPub, 'https://fa.brid.gy/ap/fake:user'),
(Fake, 'fake:user', ATProto, 'did:plc:789'),
(Fake, 'fake:user', Fake, 'fake:user'),