Skip to content

Commit

Permalink
dms: add help command, bug fix for replying
Browse files Browse the repository at this point in the history
for #826
  • Loading branch information
snarfed committed Oct 29, 2024
1 parent c0f8945 commit 6b9bec1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 24 deletions.
17 changes: 14 additions & 3 deletions dms.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ def maybe_send(*, from_proto, to_user, text, type=None, in_reply_to=None):
'verb': 'post',
'id': f'{id}-create',
'actor': bot.key.id(),
'inReplyTo': in_reply_to,
'object': {
'objectType': 'note',
'id': id,
'author': bot.key.id(),
'content': text,
'inReplyTo': in_reply_to,
'tags': [{
'objectType': 'mention',
'url': to_user.key.id(),
Expand Down Expand Up @@ -109,10 +109,21 @@ def reply(text, type=None):

# parse and handle message
split = content.split(maxsplit=1)
cmd = split[0]
cmd = split[0].lstrip('/')
arg = split[1] if len(split) > 1 else None

if cmd in ('yes', 'ok') and not arg:
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.LABEL}. Here are some commands I respond to:</p>
<ul>
<li><code>start</code>: enable bridging for your account
<li><code>stop</code>: disable bridging for your account
<li><code>username [domain]</code>: set a custom domain username for your bridged account
<li><code>[handle]</code>: make me DM a user on {to_proto.LABEL} to request that they bridge their account into {from_user.LABEL}
<li><code>help</code>: print this message
</ul>""")

elif cmd in ('yes', 'ok', 'start') and not arg:
from_user.enable_protocol(to_proto)
to_proto.bot_follow(from_user)
return 'OK', 200
Expand Down
2 changes: 2 additions & 0 deletions ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ def translate_handle(*, handle, from_, to, enhanced):
enhanced (bool): whether to convert to an "enhanced" handle based on the
user's domain
TODO: drop enhanced arg, always use if available?
Returns:
str: the corresponding handle in ``to``
Expand Down
59 changes: 38 additions & 21 deletions tests/test_dms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
from .testutil import ExplicitFake, Fake, OtherFake, TestCase
from .test_atproto import DID_DOC

DM_EFAKE_ALICE_REQUESTS_OTHER_BOB = {
DM_BASE = {
'objectType': 'note',
'id': 'efake:dm',
'actor': 'efake:alice',
'to': ['other.brid.gy'],
}

DM_EFAKE_ALICE_REQUESTS_OTHER_BOB = {
**DM_BASE,
'content': ' other:handle:bob ',
}
ALICE_CONFIRMATION_CONTENT = """Got it! We'll send <a class="h-card u-author" rel="me" href="web:other:bob" title="other:handle:bob">other:handle:bob</a> a message and say that you hope they'll enable the bridge. Fingers crossed!"""
Expand All @@ -27,10 +31,7 @@
<p>Bridgy Fed will only send you this message once."""

DM_EFAKE_ALICE_SET_USERNAME_OTHER = {
'objectType': 'note',
'id': 'efake:dm',
'actor': 'efake:alice',
'to': ['other.brid.gy'],
**DM_BASE,
'content': 'username new-handle',
}
ALICE_USERNAME_CONFIRMATION_CONTENT = 'Your username in other-phrase has been set to <a class="h-card u-author" rel="me" href="web:other:efake:alice" title="other:handle:efake:handle:alice">other:handle:efake:handle:alice</a>. It should appear soon!'
Expand All @@ -51,25 +52,29 @@ def assert_replied(self, *args):
def assert_sent(self, from_cls, tos, type, text, in_reply_to=None):
if not isinstance(tos, list):
tos = [tos]

from_id = f'{from_cls.ABBREV}.brid.gy'
self.assertEqual([
(f'{to.key.id()}:target', {
for expected, (target, activity) in zip(tos, tos[0].sent):
id = expected.key.id()
self.assertEqual(f'{id}:target', target)
content = activity['object'].pop('content')
if content != text:
assert content.startswith(text), content
self.assertEqual({
'objectType': 'activity',
'verb': 'post',
'id': f'https://{from_id}/#{type}-dm-{to.key.id()}-2022-01-02T03:04:05+00:00-create',
'id': f'https://{from_id}/#{type}-dm-{id}-2022-01-02T03:04:05+00:00-create',
'actor': from_id,
'inReplyTo': in_reply_to,
'object': {
'objectType': 'note',
'id': f'https://{from_id}/#{type}-dm-{to.key.id()}-2022-01-02T03:04:05+00:00',
'id': f'https://{from_id}/#{type}-dm-{id}-2022-01-02T03:04:05+00:00',
'author': from_id,
'content': text,
'tags': [{'objectType': 'mention', 'url': to.key.id()}],
'to': [to.key.id()],
'inReplyTo': in_reply_to,
'tags': [{'objectType': 'mention', 'url': id}],
'to': [id],
},
'to': [to.key.id()],
})
for to in tos], tos[0].sent)
'to': [id],
}, activity)

def test_maybe_send(self):
self.make_user(id='fa.brid.gy', cls=Web)
Expand Down Expand Up @@ -117,7 +122,7 @@ def test_receive_unknown_text(self):
enabled_protocols=['other'], obj_id='efake:alice')

obj = Object(our_as1={
**DM_EFAKE_ALICE_REQUESTS_OTHER_BOB,
**DM_BASE,
'content': 'foo bar',
})
with self.assertRaises(NotModified) as e:
Expand Down Expand Up @@ -199,7 +204,7 @@ def test_receive_prompt_strips_leading_at_sign(self):
alice, bob = self.make_alice_bob()

obj = Object(our_as1={
**DM_EFAKE_ALICE_REQUESTS_OTHER_BOB,
**DM_BASE,
'content': '@other:handle:bob',
})
self.assertEqual(('OK', 200), receive(from_user=alice, obj=obj))
Expand Down Expand Up @@ -275,7 +280,7 @@ def test_receive_prompt_request_rate_limit(self):
self.assertEqual(('OK', 200), receive(from_user=alice, obj=obj))

obj = Object(our_as1={
**DM_EFAKE_ALICE_REQUESTS_OTHER_BOB,
**DM_BASE,
'content': 'other:handle:eve',
})
self.assertEqual(('OK', 200), receive(from_user=alice, obj=obj))
Expand All @@ -288,7 +293,7 @@ def test_receive_prompt_request_rate_limit(self):
OtherFake.sent = []
ExplicitFake.sent = []
obj = Object(our_as1={
**DM_EFAKE_ALICE_REQUESTS_OTHER_BOB,
**DM_BASE,
'content': 'other:handle:frank',
})
self.assertEqual(('OK', 200), receive(from_user=alice, obj=obj))
Expand All @@ -300,7 +305,7 @@ def test_receive_prompt_wrong_protocol(self):
self.make_user(id='other.brid.gy', cls=Web)

obj = Object(our_as1={
**DM_EFAKE_ALICE_REQUESTS_OTHER_BOB,
**DM_BASE,
'content': 'fake:eve',
})
with self.assertRaises(NotModified) as e:
Expand Down Expand Up @@ -342,3 +347,15 @@ def test_receive_username_fails(self, _):
self.assertEqual(('OK', 200), receive(from_user=alice, obj=obj))
self.assert_replied(OtherFake, alice, '?', 'nopey')
self.assertEqual({}, alice.usernames)

def test_receive_help(self):
self.make_user(id='other.brid.gy', cls=Web)
alice = self.make_user(id='efake:alice', cls=ExplicitFake,
enabled_protocols=['other'], obj_as1={'x': 'y'})
obj = Object(our_as1={
**DM_BASE,
'content': '/help',
})
self.assertEqual(('OK', 200), receive(from_user=alice, obj=obj))
self.assert_replied(OtherFake, alice, '?', "<p>Hi! I'm a friendly bot")
self.assertEqual({}, alice.usernames)

0 comments on commit 6b9bec1

Please sign in to comment.