From 53aaef506c28d04d7b303f877b9f1ce01c525e69 Mon Sep 17 00:00:00 2001 From: Lauryn Menard Date: Mon, 25 Aug 2025 16:12:12 +0200 Subject: [PATCH] create-channel: Clarify flow of new channel and subscription notices. Updates comments in send_user_subscribed_and_new_channel_notifications so that it is clearer what notification each "if" block of the function is generating. Also, moves variables that are only used in the user subscribed DMs to that "if" block. (cherry picked from commit dda2c6e285b67d41ea5350897c7addd41c641fb1) --- zerver/views/streams.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/zerver/views/streams.py b/zerver/views/streams.py index e0c729e3df..940ae79cb9 100644 --- a/zerver/views/streams.py +++ b/zerver/views/streams.py @@ -1045,24 +1045,22 @@ def send_user_subscribed_and_new_channel_notifications( announce: bool, ) -> None: """ - If you are subscribing lots of new users to new streams, - this function can be pretty expensive in terms of generating - lots of queries and sending lots of messages. We isolate + If a user is subscribing lots of other users to existing channels, + then this function can be pretty expensive in terms of generating + lots of queries and sending lots of direct messages. We isolate the code partly to make it easier to test things like excessive query counts by mocking this function so that it doesn't drown out query counts from other code. """ - bots = {str(subscriber.id): subscriber.is_bot for subscriber in subscribers} - - newly_created_stream_names = {s.name for s in created_streams} - - realm = user_profile.realm - mention_backend = MentionBackend(realm.id) - - # Inform the user if someone else subscribed them to stuff, - # or if a new stream was created with the "announce" option. notifications = [] + # Inform users if someone else subscribed them to an existing channel. if new_subscriptions: + bots = {str(subscriber.id): subscriber.is_bot for subscriber in subscribers} + + newly_created_stream_names = {s.name for s in created_streams} + + realm = user_profile.realm + mention_backend = MentionBackend(realm.id) for id, subscribed_stream_names in new_subscriptions.items(): if id == str(user_profile.id): # Don't send a notification DM if you subscribed yourself. @@ -1071,8 +1069,8 @@ def send_user_subscribed_and_new_channel_notifications( # Don't send notification DMs to bots. continue - # For each user, we notify them about newly subscribed streams, except for - # streams that were newly created. + # For each user, we notify them about newly subscribed channels, except for + # channels that were newly created. notify_stream_names = set(subscribed_stream_names) - newly_created_stream_names if not notify_stream_names: @@ -1097,6 +1095,7 @@ def send_user_subscribed_and_new_channel_notifications( ) ) + # Send notification if a new channel was created with the "announce" option. if announce and len(created_streams) > 0: new_stream_announcements_stream = user_profile.realm.new_stream_announcements_stream if new_stream_announcements_stream is not None: @@ -1132,6 +1131,7 @@ def send_user_subscribed_and_new_channel_notifications( ), ) + # Send an initial "channel created" notification to newly created channel events topic. if not user_profile.realm.is_zephyr_mirror_realm and len(created_streams) > 0: sender = get_system_bot(settings.NOTIFICATION_BOT, user_profile.realm_id) for stream in created_streams: