Skip to content

Commit

Permalink
DMs: improve bot mention stripping
Browse files Browse the repository at this point in the history
...to handle fully qualified fediverse handles, eg @[email protected]

for #1474
  • Loading branch information
snarfed committed Nov 12, 2024
1 parent 741ce52 commit 36bbec3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
18 changes: 10 additions & 8 deletions dms.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,6 @@ def receive(*, from_user, obj):

soup = util.parse_html(inner_obj.get('content', ''))

# remove @-mention of bot
if first_link := soup.a:
if first_link.get_text().strip().lstrip('@') in DOMAINS + ids.BOT_ACTOR_AP_IDS:
first_link.extract()

content = soup.get_text().strip().lower()
if not content:
return r'¯\_(ツ)_/¯', 204
Expand All @@ -114,9 +109,16 @@ def reply(text, type=None):
return 'OK', 200

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

# remove @-mention of bot, if any
bot_handles = (DOMAINS + ids.BOT_ACTOR_AP_IDS
+ tuple(h.lstrip('@') for h in ids.BOT_ACTOR_AP_HANDLES))
if tokens[0].lstrip('@') in bot_handles:
tokens = tokens[1:]

cmd = tokens[0].lstrip('/')
arg = tokens[1] if len(tokens) > 1 else None

extra = ''
if to_proto.LABEL == 'atproto':
Expand Down
1 change: 1 addition & 0 deletions ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
# can't use translate_user_id because Web.owns_id checks valid_domain, which
# doesn't allow our protocol subdomains
BOT_ACTOR_AP_IDS = tuple(f'https://{domain}/{domain}' for domain in PROTOCOL_DOMAINS)
BOT_ACTOR_AP_HANDLES = tuple(f'@{domain}@{domain}' for domain in PROTOCOL_DOMAINS)


def validate(id, from_, to):
Expand Down
22 changes: 21 additions & 1 deletion tests/test_dms.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def assert_sent(self, from_cls, tos, type, text, in_reply_to=None):
tos = [tos]

from_id = f'{from_cls.ABBREV}.brid.gy'
for expected, (target, activity) in zip(tos, tos[0].sent):
for expected, (target, activity) in zip(tos, tos[-1].sent):
id = expected.key.id()
self.assertEqual(f'{id}:target', target)
content = activity['object'].pop('content')
Expand Down Expand Up @@ -396,6 +396,26 @@ def test_receive_help(self):
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',
):
ExplicitFake.sent = []
with self.subTest(content=content):
obj = Object(our_as1={
**DM_BASE,
'content': content,
})
self.assertEqual(('OK', 200), receive(from_user=alice, obj=obj))
self.assert_replied(OtherFake, alice, '?', "<p>Hi! I'm a friendly bot")

def test_receive_did_atproto(self):
self.make_user(id='bsky.brid.gy', cls=Web)
alice = self.make_user(id='efake:alice', cls=ExplicitFake,
Expand Down

0 comments on commit 36bbec3

Please sign in to comment.