add_subscriptions: Only exclude DMs and not new channel notifications.

When the number of new subscriptions crosses a threshold, we wish to
avoid sending DMs to the user as implemented in #31206. But there was
a bug causing announcement channel and new channel notification
messages to also not be sent.

This commit fixes that bug by ensuring that we only exclude direct
message notications.
This commit is contained in:
Kislay Verma
2025-08-26 12:08:19 +05:30
committed by Tim Abbott
parent 06d2ecb482
commit 18d9b686b7
2 changed files with 31 additions and 17 deletions

View File

@@ -5147,8 +5147,11 @@ class SubscriptionAPITest(ZulipTestCase):
recipient__type_id=announce.id,
date_sent__gt=now,
)
# Currently, this notification is not sent even though it should be.
self.assertEqual(announcement_channel_message.count(), 0)
self.assertEqual(announcement_channel_message.count(), 1)
self.assertEqual(
announcement_channel_message[0].content,
f"@_**{desdemona.full_name}|{desdemona.id}** created a new channel #**test D**.",
)
new_channel = get_stream("test D", realm)
new_channel_message = Message.objects.filter(
@@ -5158,8 +5161,15 @@ class SubscriptionAPITest(ZulipTestCase):
recipient__type_id=new_channel.id,
date_sent__gt=now,
)
# Currently, this notification is not sent even though it should be.
self.assert_length(new_channel_message.values(), 0)
self.assert_length(new_channel_message.values(), 1)
self.assertEqual(
new_channel_message[0].topic_name(), Realm.STREAM_EVENTS_NOTIFICATION_TOPIC_NAME
)
self.assertEqual(
new_channel_message[0].content,
f"**Public** channel created by @_**{desdemona.full_name}|{desdemona.id}**. **Description:**\n"
"```` quote\n*No description.*\n````",
)
class InviteOnlyStreamTest(ZulipTestCase):

View File

@@ -1013,18 +1013,21 @@ def add_subscriptions_backend(
result["already_subscribed"] = dict(result["already_subscribed"])
if send_new_subscription_messages:
if len(result["subscribed"]) <= settings.MAX_BULK_NEW_SUBSCRIPTION_MESSAGES:
send_user_subscribed_and_new_channel_notifications(
user_profile=user_profile,
subscribers=subscribers,
new_subscriptions=result["subscribed"],
id_to_user_profile=id_to_user_profile,
created_streams=created_streams,
announce=announce,
)
result["new_subscription_messages_sent"] = True
else:
result["new_subscription_messages_sent"] = False
send_user_subscribed_direct_messages = (
len(result["subscribed"]) <= settings.MAX_BULK_NEW_SUBSCRIPTION_MESSAGES
)
result["new_subscription_messages_sent"] = send_user_subscribed_direct_messages
else:
send_user_subscribed_direct_messages = False
send_user_subscribed_and_new_channel_notifications(
user_profile=user_profile,
subscribers=subscribers,
new_subscriptions=result["subscribed"],
id_to_user_profile=id_to_user_profile,
created_streams=created_streams,
announce=announce,
send_user_subscribed_direct_messages=send_user_subscribed_direct_messages,
)
result["subscribed"] = dict(result["subscribed"])
result["already_subscribed"] = dict(result["already_subscribed"])
@@ -1040,6 +1043,7 @@ def send_user_subscribed_and_new_channel_notifications(
id_to_user_profile: dict[str, UserProfile],
created_streams: list[Stream],
announce: bool,
send_user_subscribed_direct_messages: bool = True,
) -> None:
"""
If a user is subscribing lots of other users to existing channels,
@@ -1051,7 +1055,7 @@ def send_user_subscribed_and_new_channel_notifications(
"""
notifications = []
# Inform users if someone else subscribed them to an existing channel.
if new_subscriptions:
if new_subscriptions and send_user_subscribed_direct_messages:
bots = {str(subscriber.id): subscriber.is_bot for subscriber in subscribers}
newly_created_stream_names = {s.name for s in created_streams}