Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
bots: Rename SERVICE_BOT_TYPES to MESSAGE_TRIGGERED_BOT_TYPES.
sathwikshetty33c5e7b9f69037
diff --git a/web/tests/bot_data.test.cjs b/web/tests/bot_data.test.cjsindex 49a89c3af10..4fa2d65cc58 100644--- a/web/tests/bot_data.test.cjs+++ b/web/tests/bot_data.test.cjs@@ -10,9 +10,8 @@ const bot_data = zrequire("bot_data");const people = zrequire("people");-// Bot types and service bot types can be found-// in zerver/models/users.py - UserProfile Class or-// zever/openapi/zulip.yaml+// Bot types can be found in zerver/models/users.py+// UserProfile class or zerver/openapi/zulip.yamlconst me = make_user({email: "[email protected]",diff --git a/zerver/actions/message_send.py b/zerver/actions/message_send.pyindex 6b3e3078ee7..83b7c0f75cd 100644--- a/zerver/actions/message_send.py+++ b/zerver/actions/message_send.py@@ -215,7 +215,7 @@ class RecipientInfoResult:um_eligible_user_ids: set[int]long_term_idle_user_ids: set[int]default_bot_user_ids: set[int]- service_bot_tuples: list[tuple[int, int]]+ message_triggered_bot_tuples: list[tuple[int, int]]all_bot_user_ids: set[int]topic_participant_user_ids: set[int]sender_muted_stream: bool | None@@ -542,10 +542,10 @@ def get_recipient_info(row["id"] for row in rows if row["is_bot"] and row["bot_type"] == UserProfile.DEFAULT_BOT}- service_bot_tuples = [+ message_triggered_bot_tuples = [(row["id"], row["bot_type"])for row in rows- if row["is_bot"] and row["bot_type"] in UserProfile.SERVICE_BOT_TYPES+ if row["is_bot"] and row["bot_type"] in UserProfile.MESSAGE_TRIGGERED_BOT_TYPES]# We also need the user IDs of all bots, to avoid trying to send push/email@@ -573,7 +573,7 @@ def get_recipient_info(um_eligible_user_ids=um_eligible_user_ids,long_term_idle_user_ids=long_term_idle_user_ids,default_bot_user_ids=default_bot_user_ids,- service_bot_tuples=service_bot_tuples,+ message_triggered_bot_tuples=message_triggered_bot_tuples,all_bot_user_ids=all_bot_user_ids,topic_participant_user_ids=topic_participant_user_ids,sender_muted_stream=sender_muted_stream,@@ -581,17 +581,17 @@ def get_recipient_info()-def get_service_bot_events(+def get_message_triggered_bot_events(sender: UserProfile,- service_bot_tuples: list[tuple[int, int]],+ message_triggered_bot_tuples: list[tuple[int, int]],mentioned_user_ids: set[int],active_user_ids: set[int],recipient_type: int,) -> dict[str, list[dict[str, Any]]]:event_dict: dict[str, list[dict[str, Any]]] = defaultdict(list)- # Avoid infinite loops by preventing messages sent by bots from generating- # Service events.+ # Avoid infinite loops by preventing messages sent by bots from+ # generating message-triggered bot events.if sender.is_bot:return event_dict@@ -602,7 +602,7 @@ def get_service_bot_events(queue_name = "embedded_bots"else:logging.error(- "Unexpected bot_type for Service bot id=%s: %s",+ "Unexpected bot_type for message-triggered bot id=%s: %s",user_profile_id,bot_type,)@@ -610,12 +610,12 @@ def get_service_bot_events(is_stream = recipient_type == Recipient.STREAM- # Important note: service_bot_tuples may contain service bots- # who were not actually mentioned in the message (e.g. if- # mention syntax for that bot appeared in a code block).- # Thus, it is important to filter any users who aren't part of- # either mentioned_user_ids (the actual mentioned users) or- # active_user_ids (the actual recipients).+ # Important note: message_triggered_bot_tuples may contain+ # message-triggered bots who were not actually mentioned in the+ # message (e.g. if mention syntax for that bot appeared in a code+ # block). Thus, it is important to filter any users who aren't+ # part of either mentioned_user_ids (the actual mentioned users)+ # or active_user_ids (the actual recipients).## So even though this is implied by the logic below, we filter# these not-actually-mentioned users here, to help keep this@@ -639,7 +639,7 @@ def get_service_bot_events(})- for user_profile_id, bot_type in service_bot_tuples:+ for user_profile_id, bot_type in message_triggered_bot_tuples:maybe_add_event(user_profile_id=user_profile_id,bot_type=bot_type,@@ -780,7 +780,7 @@ def build_message_send_dict(um_eligible_user_ids=info.um_eligible_user_ids,long_term_idle_user_ids=info.long_term_idle_user_ids,default_bot_user_ids=info.default_bot_user_ids,- service_bot_tuples=info.service_bot_tuples,+ message_triggered_bot_tuples=info.message_triggered_bot_tuples,all_bot_user_ids=info.all_bot_user_ids,push_device_registered_user_ids=info.push_device_registered_user_ids,topic_wildcard_mention_user_ids=topic_wildcard_mention_user_ids,@@ -1012,9 +1012,9 @@ def do_send_messages(ums.extend(user_messages)- send_request.service_queue_events = get_service_bot_events(+ send_request.message_triggered_bot_queue_events = get_message_triggered_bot_events(sender=send_request.message.sender,- service_bot_tuples=send_request.service_bot_tuples,+ message_triggered_bot_tuples=send_request.message_triggered_bot_tuples,mentioned_user_ids=mentioned_user_ids,active_user_ids=send_request.active_user_ids,recipient_type=send_request.message.recipient.type,@@ -1030,7 +1030,7 @@ def do_send_messages(# * Sender automatically follows or unmutes the topic depending on 'automatically_follow_topics_policy'# and 'automatically_unmute_topics_in_muted_streams_policy' user settings.# * Notifying clients via send_event_on_commit- # * Triggering outgoing webhooks via the service event queue.+ # * Triggering outgoing webhooks via the message-triggered bot event queue.# * Updating the `first_message_id` field for streams without any message history.# * Implementing the Welcome Bot reply hack# * Adding links to the embed_links queue for open graph processing.@@ -1317,8 +1317,8 @@ def do_send_messages(send_welcome_bot_response(send_request)- assert send_request.service_queue_events is not None- for queue_name, events in send_request.service_queue_events.items():+ assert send_request.message_triggered_bot_queue_events is not None+ for queue_name, events in send_request.message_triggered_bot_queue_events.items():for event in events:queue_event_on_commit(queue_name,diff --git a/zerver/lib/bot_lib.py b/zerver/lib/bot_lib.pyindex 7be86b7faba..b8d8049f5f2 100644--- a/zerver/lib/bot_lib.py+++ b/zerver/lib/bot_lib.py@@ -156,8 +156,11 @@ class EmbeddedBotHandler:raise EmbeddedBotQuitError(message)-def do_flag_service_bots_messages_as_processed(+def do_flag_message_triggered_bots_messages_as_processed(bot_profile: UserProfile, message_ids: list[int]) -> None:- assert bot_profile.is_bot is True and bot_profile.bot_type in UserProfile.SERVICE_BOT_TYPES+ assert (+ bot_profile.is_bot is True+ and bot_profile.bot_type in UserProfile.MESSAGE_TRIGGERED_BOT_TYPES+ )do_update_message_flags(bot_profile, "add", "read", message_ids)diff --git a/zerver/lib/message.py b/zerver/lib/message.pyindex 36dc04fdeb3..edd4809ebd6 100644--- a/zerver/lib/message.py+++ b/zerver/lib/message.py@@ -147,7 +147,7 @@ class SendMessageRequest:um_eligible_user_ids: set[int]long_term_idle_user_ids: set[int]default_bot_user_ids: set[int]- service_bot_tuples: list[tuple[int, int]]+ message_triggered_bot_tuples: list[tuple[int, int]]all_bot_user_ids: set[int]push_device_registered_user_ids: set[int]# IDs of topic participants who should be notified of topic wildcard mention.@@ -179,7 +179,7 @@ class SendMessageRequest:deliver_at: datetime | None = Nonedelivery_type: str | None = Nonelimit_unread_user_ids: set[int] | None = None- service_queue_events: dict[str, list[dict[str, Any]]] | None = None+ message_triggered_bot_queue_events: dict[str, list[dict[str, Any]]] | None = Nonedisable_external_notifications: bool = Falseautomatic_new_visibility_policy: int | None = Nonerecipients_for_user_creation_events: dict[UserProfile, set[int]] | None = Nonediff --git a/zerver/models/users.py b/zerver/models/users.pyindex 4ecf00606a9..ef2a5978121 100644--- a/zerver/models/users.py+++ b/zerver/models/users.py@@ -472,7 +472,7 @@ class UserProfile(AbstractBaseUser, PermissionsMixin, UserBaseSettings):EMBEDDED_BOT: "Embedded bot",}- SERVICE_BOT_TYPES = [+ MESSAGE_TRIGGERED_BOT_TYPES = [OUTGOING_WEBHOOK_BOT,EMBEDDED_BOT,]diff --git a/zerver/tests/test_embedded_bot_system.py b/zerver/tests/test_embedded_bot_system.pyindex 79b7eb123f0..891bd09d0fb 100644--- a/zerver/tests/test_embedded_bot_system.py+++ b/zerver/tests/test_embedded_bot_system.py@@ -135,7 +135,7 @@ class TestEmbeddedBotFailures(ZulipTestCase):)-class TestServiceBotStateHandler(ZulipTestCase):+class TestEmbeddedBotStateHandler(ZulipTestCase):@overridedef setUp(self) -> None:super().setUp()@@ -322,7 +322,7 @@ class TestServiceBotStateHandler(ZulipTestCase):self.assertEqual(response_dict["storage"], {})-class TestServiceBotConfigHandler(ZulipTestCase):+class TestEmbeddedBotConfigHandler(ZulipTestCase):@overridedef setUp(self) -> None:super().setUp()diff --git a/zerver/tests/test_message_flags.py b/zerver/tests/test_message_flags.pyindex ed629dbd29c..c0e6ae6180f 100644--- a/zerver/tests/test_message_flags.py+++ b/zerver/tests/test_message_flags.py@@ -2041,11 +2041,11 @@ class MessageAccessTests(ZulipTestCase):)self.assert_length(filtered_messages, 4)- # Test private message access for service bot- service_bot = self.example_user("outgoing_webhook_bot")- self.subscribe(service_bot, stream_name)+ # Test private message access for message-triggered bot+ message_triggered_bot = self.example_user("outgoing_webhook_bot")+ self.subscribe(message_triggered_bot, stream_name)filtered_messages = self.assert_bulk_access(- service_bot,+ message_triggered_bot,more_message_ids,stream,bulk_access_messages_query_count=6,@@ -2106,11 +2106,11 @@ class MessageAccessTests(ZulipTestCase):)self.assert_length(filtered_messages, 2)- # Test public message access for service bot- service_bot = self.example_user("outgoing_webhook_bot")- self.subscribe(service_bot, stream_name)+ # Test public message access for message-triggered bot+ message_triggered_bot = self.example_user("outgoing_webhook_bot")+ self.subscribe(message_triggered_bot, stream_name)filtered_messages = self.assert_bulk_access(- service_bot,+ message_triggered_bot,message_ids,stream,bulk_access_messages_query_count=4,diff --git a/zerver/tests/test_service_bot_system.py b/zerver/tests/test_message_triggered_bots.pysimilarity index 84%rename from zerver/tests/test_service_bot_system.pyrename to zerver/tests/test_message_triggered_bots.pyindex 070a38ee371..e2a24fbe5b4 100644--- a/zerver/tests/test_service_bot_system.py+++ b/zerver/tests/test_message_triggered_bots.py@@ -7,7 +7,7 @@ import responsesfrom typing_extensions import ParamSpec, overridefrom zerver.actions.create_user import do_create_user-from zerver.actions.message_send import get_service_bot_events+from zerver.actions.message_send import get_message_triggered_bot_eventsfrom zerver.lib.test_classes import ZulipTestCasefrom zerver.lib.test_helpers import mock_queue_publishfrom zerver.models import Recipient, UserProfile@@ -21,7 +21,7 @@ BOT_TYPE_TO_QUEUE_NAME = {}-class TestServiceBotBasics(ZulipTestCase):+class TestMessageTriggeredBotBasics(ZulipTestCase):def _get_outgoing_bot(self) -> UserProfile:outgoing_bot = do_create_user(email="[email protected]",@@ -35,16 +35,16 @@ class TestServiceBotBasics(ZulipTestCase):return outgoing_bot- def test_service_events_for_pms(self) -> None:+ def test_message_triggered_bot_events_for_direct_messages(self) -> None:sender = self.example_user("hamlet")assert not sender.is_botoutgoing_bot = self._get_outgoing_bot()assert outgoing_bot.bot_type is not None- event_dict = get_service_bot_events(+ event_dict = get_message_triggered_bot_events(sender=sender,- service_bot_tuples=[+ message_triggered_bot_tuples=[(outgoing_bot.id, outgoing_bot.bot_type),],active_user_ids={outgoing_bot.id},@@ -71,10 +71,10 @@ class TestServiceBotBasics(ZulipTestCase):# we will skip over it. This tests an anomaly# of the code that our query for bots can include# bots that may not actually be mentioned, and it's- # easiest to just filter them in get_service_bot_events.- event_dict = get_service_bot_events(+ # easiest to just filter them in get_message_triggered_bot_events.+ event_dict = get_message_triggered_bot_events(sender=sender,- service_bot_tuples=[+ message_triggered_bot_tuples=[(outgoing_bot.id, outgoing_bot.bot_type),],active_user_ids={outgoing_bot.id},@@ -84,7 +84,7 @@ class TestServiceBotBasics(ZulipTestCase):self.assert_length(event_dict, 0)- def test_service_events_for_stream_mentions(self) -> None:+ def test_message_triggered_bot_events_for_channel_mentions(self) -> None:sender = self.example_user("hamlet")assert not sender.is_bot@@ -98,9 +98,9 @@ class TestServiceBotBasics(ZulipTestCase):user_profile=cordelia,)- event_dict = get_service_bot_events(+ event_dict = get_message_triggered_bot_events(sender=sender,- service_bot_tuples=[+ message_triggered_bot_tuples=[(outgoing_bot.id, outgoing_bot.bot_type),(red_herring_bot.id, UserProfile.OUTGOING_WEBHOOK_BOT),],@@ -117,18 +117,18 @@ class TestServiceBotBasics(ZulipTestCase):self.assertEqual(event_dict, expected)- def test_service_events_for_private_mentions(self) -> None:- """Service bots should not get access to mentions if they aren't a- direct recipient."""+ def test_no_bot_events_when_mentioned_without_being_recipient(self) -> None:+ """Message-triggered bots should not get access to mentions if they+ aren't a direct recipient."""sender = self.example_user("hamlet")assert not sender.is_botoutgoing_bot = self._get_outgoing_bot()assert outgoing_bot.bot_type is not None- event_dict = get_service_bot_events(+ event_dict = get_message_triggered_bot_events(sender=sender,- service_bot_tuples=[+ message_triggered_bot_tuples=[(outgoing_bot.id, outgoing_bot.bot_type),],active_user_ids=set(),@@ -138,7 +138,7 @@ class TestServiceBotBasics(ZulipTestCase):self.assert_length(event_dict, 0)- def test_service_events_with_unexpected_bot_type(self) -> None:+ def test_message_triggered_bot_events_with_unexpected_bot_type(self) -> None:hamlet = self.example_user("hamlet")cordelia = self.example_user("cordelia")@@ -151,9 +151,9 @@ class TestServiceBotBasics(ZulipTestCase):bot.save()with self.assertLogs(level="ERROR") as m:- event_dict = get_service_bot_events(+ event_dict = get_message_triggered_bot_events(sender=hamlet,- service_bot_tuples=[+ message_triggered_bot_tuples=[(bot.id, wrong_bot_type),],active_user_ids=set(),@@ -164,7 +164,9 @@ class TestServiceBotBasics(ZulipTestCase):self.assert_length(event_dict, 0)self.assertEqual(m.output,- [f"ERROR:root:Unexpected bot_type for Service bot id={bot.id}: {wrong_bot_type}"],+ [+ f"ERROR:root:Unexpected bot_type for message-triggered bot id={bot.id}: {wrong_bot_type}"+ ],)@@ -172,11 +174,11 @@ ParamT = ParamSpec("ParamT")def for_all_bot_types(- test_func: Callable[Concatenate["TestServiceBotEventTriggers", ParamT], None],-) -> Callable[Concatenate["TestServiceBotEventTriggers", ParamT], None]:+ test_func: Callable[Concatenate["TestMessageTriggeredBotEventTriggers", ParamT], None],+) -> Callable[Concatenate["TestMessageTriggeredBotEventTriggers", ParamT], None]:@wraps(test_func)def _wrapped(- self: "TestServiceBotEventTriggers", /, *args: ParamT.args, **kwargs: ParamT.kwargs+ self: "TestMessageTriggeredBotEventTriggers", /, *args: ParamT.args, **kwargs: ParamT.kwargs) -> None:for bot_type in BOT_TYPE_TO_QUEUE_NAME:self.bot_profile.bot_type = bot_type@@ -189,14 +191,14 @@ def for_all_bot_types(def patch_queue_publish(method_to_patch: str,) -> Callable[- [Callable[["TestServiceBotEventTriggers", mock.Mock], None]],- Callable[["TestServiceBotEventTriggers"], None],+ [Callable[["TestMessageTriggeredBotEventTriggers", mock.Mock], None]],+ Callable[["TestMessageTriggeredBotEventTriggers"], None],]:def inner(- func: Callable[["TestServiceBotEventTriggers", mock.Mock], None],- ) -> Callable[["TestServiceBotEventTriggers"], None]:+ func: Callable[["TestMessageTriggeredBotEventTriggers", mock.Mock], None],+ ) -> Callable[["TestMessageTriggeredBotEventTriggers"], None]:@wraps(func)- def _wrapped(self: "TestServiceBotEventTriggers") -> None:+ def _wrapped(self: "TestMessageTriggeredBotEventTriggers") -> None:with mock_queue_publish(method_to_patch) as m:func(self, m)@@ -205,7 +207,7 @@ def patch_queue_publish(return inner-class TestServiceBotEventTriggers(ZulipTestCase):+class TestMessageTriggeredBotEventTriggers(ZulipTestCase):@overridedef setUp(self) -> None:super().setUp()@@ -356,10 +358,11 @@ class TestServiceBotEventTriggers(ZulipTestCase):@responses.activate@for_all_bot_types- def test_flag_messages_service_bots_has_processed(self) -> None:+ def test_message_flagged_read_after_bot_processes_event(self) -> None:"""- Verifies that once an event has been processed by the service bot's- queue processor, the message is marked as processed (flagged with `read`).+ Verifies that once an event has been processed by the message-triggered+ bot's queue processor, the message is marked as processed (flagged with+ `read`)."""sender = self.user_profilerecipients = [self.user_profile, self.bot_profile, self.second_bot_profile]diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex f725339304a..da83d33ce40 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -2928,7 +2928,7 @@ class RecipientInfoTest(ZulipTestCase):um_eligible_user_ids=all_user_ids,long_term_idle_user_ids=set(),default_bot_user_ids=set(),- service_bot_tuples=[],+ message_triggered_bot_tuples=[],all_bot_user_ids=set(),topic_participant_user_ids=set(),sender_muted_stream=False,@@ -3151,9 +3151,9 @@ class RecipientInfoTest(ZulipTestCase):self.assertEqual(info.stream_push_user_ids, set())self.assertEqual(info.stream_wildcard_mention_user_ids, {othello.id})- # Add a service bot.- service_bot = do_create_user(- email="[email protected]",+ # Add a message-triggered bot.+ message_triggered_bot = do_create_user(+ email="[email protected]",password="",realm=realm,full_name="",@@ -3166,12 +3166,12 @@ class RecipientInfoTest(ZulipTestCase):recipient=recipient,sender_id=hamlet.id,stream_topic=stream_topic,- possibly_mentioned_user_ids={service_bot.id},+ possibly_mentioned_user_ids={message_triggered_bot.id},)self.assertEqual(- info.service_bot_tuples,+ info.message_triggered_bot_tuples,[- (service_bot.id, UserProfile.EMBEDDED_BOT),+ (message_triggered_bot.id, UserProfile.EMBEDDED_BOT),],)@@ -3190,10 +3190,10 @@ class RecipientInfoTest(ZulipTestCase):recipient=recipient,sender_id=hamlet.id,stream_topic=stream_topic,- possibly_mentioned_user_ids={service_bot.id, normal_bot.id},+ possibly_mentioned_user_ids={message_triggered_bot.id, normal_bot.id},)self.assertEqual(info.default_bot_user_ids, {normal_bot.id})- self.assertEqual(info.all_bot_user_ids, {normal_bot.id, service_bot.id})+ self.assertEqual(info.all_bot_user_ids, {normal_bot.id, message_triggered_bot.id})# Now Hamlet follows the topic with the 'followed_topic_email_notifications',# 'followed_topic_push_notifications' and 'followed_topic_wildcard_mention_notify'diff --git a/zerver/worker/embedded_bots.py b/zerver/worker/embedded_bots.pyindex 552bad63bd3..b79384e099c 100644--- a/zerver/worker/embedded_bots.py+++ b/zerver/worker/embedded_bots.py@@ -9,7 +9,7 @@ from zulip_bots.lib import extract_query_without_mentionfrom zerver.lib.bot_lib import (EmbeddedBotHandler,EmbeddedBotQuitError,- do_flag_service_bots_messages_as_processed,+ do_flag_message_triggered_bots_messages_as_processed,get_bot_handler,)from zerver.models import UserProfile@@ -58,4 +58,4 @@ class EmbeddedBotWorker(QueueProcessingWorker):)except EmbeddedBotQuitError as e:logging.warning("%s", e)- do_flag_service_bots_messages_as_processed(user_profile, [message["id"]])+ do_flag_message_triggered_bots_messages_as_processed(user_profile, [message["id"]])diff --git a/zerver/worker/outgoing_webhooks.py b/zerver/worker/outgoing_webhooks.pyindex 7612213a730..cbd76a86754 100644--- a/zerver/worker/outgoing_webhooks.py+++ b/zerver/worker/outgoing_webhooks.py@@ -4,7 +4,7 @@ from typing import Anyfrom typing_extensions import override-from zerver.lib.bot_lib import do_flag_service_bots_messages_as_processed+from zerver.lib.bot_lib import do_flag_message_triggered_bots_messages_as_processedfrom zerver.lib.outgoing_webhook import do_rest_call, get_outgoing_webhook_service_handlerfrom zerver.models.bots import get_bot_servicesfrom zerver.models.users import get_user_profile_by_id@@ -27,4 +27,4 @@ class OutgoingWebhookWorker(QueueProcessingWorker):service_handler = get_outgoing_webhook_service_handler(service)do_rest_call(service.base_url, event, service_handler)- do_flag_service_bots_messages_as_processed(bot_profile, [message["id"]])+ do_flag_message_triggered_bots_messages_as_processed(bot_profile, [message["id"]])