Skip to content

Commit

Permalink
dms: add did command for atproto
Browse files Browse the repository at this point in the history
for #826
  • Loading branch information
snarfed committed Oct 29, 2024
1 parent 2d0e825 commit 0bbd2a1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
13 changes: 11 additions & 2 deletions dms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from common import create_task, memcache, memcache_key
import ids
import models
from models import PROTOCOLS
import protocol

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -112,6 +113,10 @@ def reply(text, type=None):
cmd = split[0].lstrip('/')
arg = split[1] if len(split) > 1 else None

extra = ''
if to_proto.LABEL == 'atproto':
extra = """<li><em>did</em>: get your bridged Bluesky account's <a href="https://atproto.com/guides/identity#identifiers">DID</a>"""

if cmd in ('?', 'help', 'commands', 'info', 'hi', 'hello'):
return reply(f"""\
<p>Hi! I'm a friendly bot that can help you bridge your account into {to_proto.PHRASE}. Here are some commands I respond to:</p>
Expand All @@ -120,6 +125,7 @@ def reply(text, type=None):
<li><em>stop</em>: disable bridging for your account
<li><em>username [domain]</em>: set a custom domain username (handle)
<li><em>[handle]</em>: ask me to DM a user on {to_proto.PHRASE} to request that they bridge their account into {from_user.PHRASE}
{extra}
<li><em>help</em>: print this message
</ul>""")

Expand All @@ -132,15 +138,18 @@ def reply(text, type=None):
if not from_user.is_enabled(to_proto):
return reply(f"Looks like you're not bridged to {to_proto.PHRASE} yet! Please bridge your account first by following this account.")

if cmd == 'did' and not arg and to_proto.LABEL == 'atproto':
return reply(f'Your DID is <code>{from_user.get_copy(PROTOCOLS["atproto"])}</code>')
return 'OK', 200

if cmd in ('no', 'stop') and not arg:
from_user.delete(to_proto)
from_user.disable_protocol(to_proto)
return 'OK', 200

if cmd in ('username', 'handle') and arg:
username = content.split(maxsplit=1)[1]
try:
to_proto.set_username(from_user, username)
to_proto.set_username(from_user, arg)
except NotImplementedError:
return reply(f"Sorry, Bridgy Fed doesn't support custom usernames for {to_proto.PHRASE} yet.")
except (ValueError, RuntimeError) as e:
Expand Down
16 changes: 15 additions & 1 deletion tests/test_dms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import dms
from dms import maybe_send, receive
import ids
from models import DM, Follower, Object
from models import DM, Follower, Object, Target
from web import Web

from oauth_dropins.webutil.flask_util import NotModified
Expand Down Expand Up @@ -370,3 +370,17 @@ def test_receive_help(self):
self.assertEqual(('OK', 200), receive(from_user=alice, obj=obj))
self.assert_replied(OtherFake, alice, '?', "<p>Hi! I'm a friendly bot")
self.assertEqual({}, OtherFake.usernames)

def test_receive_did_atproto(self):
self.make_user(id='bsky.brid.gy', cls=Web)
alice = self.make_user(id='efake:alice', cls=ExplicitFake,
enabled_protocols=['atproto'], obj_as1={'x': 'y'},
copies=[Target(protocol='atproto', uri='did:abc:123')])
obj = Object(our_as1={
**DM_BASE,
'to': ['bsky.brid.gy'],
'content': 'did',
})
self.assertEqual(('OK', 200), receive(from_user=alice, obj=obj))
self.assert_replied(ATProto, alice, '?',
'Your DID is <code>did:abc:123</code>')

0 comments on commit 0bbd2a1

Please sign in to comment.