Skip to content

Commit

Permalink
Bluesky => fediverse: link mentions of non-bridged users to their bsk…
Browse files Browse the repository at this point in the history
…y.app profile

fixes #1288
  • Loading branch information
snarfed committed Sep 17, 2024
1 parent 4df76d0 commit 2fad8e1
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
3 changes: 3 additions & 0 deletions ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions tests/test_activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -2819,6 +2819,63 @@ def test_convert_quote_post(self):
}],
}, 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,
Expand Down
11 changes: 10 additions & 1 deletion tests/test_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ def test_translate_user_id(self):
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'),
Expand All @@ -54,6 +60,9 @@ def test_translate_user_id(self):
(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'),
Expand Down

0 comments on commit 2fad8e1

Please sign in to comment.