fix multi mentions

This commit is contained in:
HgO 2024-04-20 17:50:56 +02:00
parent a80429bd7c
commit 8ea210a699
2 changed files with 39 additions and 1 deletions

View file

@ -96,7 +96,7 @@ class Callbacks:
user_id_patterns.append(rf"@?{username}(:{homeserver})?") user_id_patterns.append(rf"@?{username}(:{homeserver})?")
pattern = re.compile( pattern = re.compile(
rf"(^|\s+)({'|'.join(user_id_patterns)})(\s+|$)", rf"(^|\s+)({'|'.join(user_id_patterns)}):?(?=\s+|$)",
re.IGNORECASE | re.MULTILINE, re.IGNORECASE | re.MULTILINE,
) )
if pattern.search(msg) is None: if pattern.search(msg) is None:
@ -123,6 +123,11 @@ class Callbacks:
# Remove the mention of the bot # Remove the mention of the bot
cmd = pattern.sub(" ", msg).strip() cmd = pattern.sub(" ", msg).strip()
logger.debug(
"Bot {self.matrix_client.user_id} | Room ID {room.room_id} | "
f"Event ID {event.event_id} | Sender {event.sender} | "
f"Processing command {cmd}"
)
try: try:
command = CommandFactory.create( command = CommandFactory.create(
cmd, cmd,

View file

@ -307,6 +307,39 @@ class CallbacksTestCase(unittest.IsolatedAsyncioTestCase):
) )
fake_command.return_value.process.assert_called_once() fake_command.return_value.process.assert_called_once()
@patch.object(matrix_alertbot.command, "AckAlertCommand", autospec=True)
async def test_message_ack_in_reply_with_multi_mentions(
self, fake_command: Mock
) -> None:
"""Tests the callback for RoomMessageText with a mention of the bot"""
# Tests that the bot process messages in the room that contain a command
fake_message_event = Mock(spec=nio.RoomMessageText)
fake_message_event.event_id = "some event id"
fake_message_event.sender = "@some_other_fake_user:example.com"
fake_message_event.body = "@fake_user:example.com @fake_user:example.com: ack"
fake_message_event.source = {
"content": {
"m.relates_to": {"m.in_reply_to": {"event_id": "some alert event id"}}
}
}
# Pretend that we received a text message event
await self.callbacks.message(self.fake_room, fake_message_event)
# Check that the command was not executed
fake_command.assert_called_once_with(
self.fake_matrix_client,
self.fake_cache,
self.fake_alertmanager_client,
self.fake_config,
self.fake_room,
fake_message_event.sender,
fake_message_event.event_id,
"some alert event id",
(),
)
fake_command.return_value.process.assert_called_once()
@patch.object(matrix_alertbot.callback, "UnackAlertCommand", autospec=True) @patch.object(matrix_alertbot.callback, "UnackAlertCommand", autospec=True)
async def test_message_unack_not_in_reply_with_mention( async def test_message_unack_not_in_reply_with_mention(
self, fake_command: Mock self, fake_command: Mock