From 41b2aaa1a804f172a2494d7ac0cd04f8edbb584e Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Wed, 24 Apr 2024 16:45:43 -0700 Subject: [PATCH] incoming DMs to protocol bot users: filter out @-mentions for #880 --- protocol.py | 12 ++++++++---- tests/test_protocol.py | 6 +++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/protocol.py b/protocol.py index 546871c..aeaac9a 100644 --- a/protocol.py +++ b/protocol.py @@ -11,7 +11,7 @@ from flask import g, request from google.cloud import ndb from google.cloud.ndb import OR from google.cloud.ndb.model import _entity_to_protobuf -from granary import as1 +from granary import as1, as2 from oauth_dropins.webutil.appengine_info import DEBUG from oauth_dropins.webutil.flask_util import cloud_tasks_only from oauth_dropins.webutil import models @@ -831,11 +831,15 @@ class Protocol: elif obj.type == 'post': to_cc = (as1.get_ids(inner_obj_as1, 'to') + as1.get_ids(inner_obj_as1, 'cc')) - content = inner_obj_as1.get('content', '').strip().lower() - if len(to_cc) == 1: - logger.info(f'got DM to {to_cc}: {content}') + if len(to_cc) == 1 and to_cc != [as2.PUBLIC_AUDIENCE]: proto = Protocol.for_bridgy_subdomain(to_cc[0]) if proto: + # remove @-mentions of bot user in HTML links + soup = util.parse_html(inner_obj_as1.get('content', '')) + for link in soup.find_all('a'): + link.extract() + content = soup.get_text().strip().lower() + logger.info(f'got DM to {to_cc}: {content}') if content in ('yes', 'ok'): from_user.enable_protocol(proto) elif content == 'no': diff --git a/tests/test_protocol.py b/tests/test_protocol.py index 250f254..ed8fe4d 100644 --- a/tests/test_protocol.py +++ b/tests/test_protocol.py @@ -1913,7 +1913,7 @@ class ProtocolReceiveTest(TestCase): # yes DM should add to enabled_protocols dm['id'] += '2' - dm['content'] = 'yes' + dm['content'] = '

@bsky.brid.gy yes

' self.assertEqual(('OK', 200), ExplicitEnableFake.receive_as1(dm)) user = user.key.get() self.assertEqual(['fake'], user.enabled_protocols) @@ -1928,9 +1928,9 @@ class ProtocolReceiveTest(TestCase): self.assertEqual(['fake'], user.enabled_protocols) self.assertEqual([], Fake.created_for) - # block should remove from enabled_protocols + # no DM should remove from enabled_protocols dm['id'] += '4' - dm['content'] = ' \n NO ' + dm['content'] = '

@bsky.brid.gy\n NO \n

' self.assertEqual(('OK', 200), ExplicitEnableFake.receive_as1(dm)) user = user.key.get() self.assertEqual([], user.enabled_protocols)