Skip to content

Commit

Permalink
DMs bug fix for commands without args
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Feb 4, 2025
1 parent 31e2296 commit d0d8dab
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
30 changes: 8 additions & 22 deletions dms.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,6 @@
REQUESTS_LIMIT_EXPIRE = timedelta(days=1)
REQUESTS_LIMIT_USER = 10

COMMANDS = (
'block',
'did',
'help',
'no',
'ok',
'start',
'stop',
'unblock',
'username',
'yes',
)
# populated by the command() decorator
_commands = {}

Expand Down Expand Up @@ -339,17 +327,15 @@ def receive(*, from_user, obj):
if not tokens or len(tokens) > 2:
return r'¯\_(ツ)_/¯', 204

if tokens[0].lstrip('/') in COMMANDS:
cmd = tokens[0].lstrip('/')
arg = tokens[1] if len(tokens) > 1 else None
else:
cmd = None
arg = tokens[0]
if fn := _commands.get(tokens[0]):
return fn(from_user, to_proto, dm_as1=inner_as1,
cmd=tokens[0], cmd_arg=tokens[1] if len(tokens) == 2 else None)
elif len(tokens) == 1:
fn = _commands.get(None)
assert fn, tokens[0]
return fn(from_user, to_proto, dm_as1=inner_as1, cmd=None, cmd_arg=tokens[0])

if fn := _commands.get(cmd):
return fn(from_user, to_proto, cmd, arg, inner_as1)

error(f"Couldn't understand DM: {text}", status=304)
return r'¯\_(ツ)_/¯', 204


def load_user(proto, handle):
Expand Down
30 changes: 16 additions & 14 deletions tests/test_dms.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,27 +429,29 @@ def test_receive_username_fails(self, _):
self.assertEqual({}, OtherFake.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({}, OtherFake.usernames)
for command in 'help', 'hello', '?':
ExplicitFake.sent = []
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': command,
})
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_help_strip_mention_of_bot(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'})

for content in (
'@other.brid.gy /help',
'[email protected] /help',
'@[email protected] /help',
'https://other.brid.gy/other.brid.gy /help',
'@other.brid.gy help',
'[email protected] help',
'@[email protected] help',
'https://other.brid.gy/other.brid.gy help',
):
ExplicitFake.sent = []
with self.subTest(content=content):
Expand Down

0 comments on commit d0d8dab

Please sign in to comment.