Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

channel: Add option to notify users newly added to a channel.

Kislay Verma5f80f0a970f7
diff --git a/api_docs/changelog.md b/api_docs/changelog.md
index e70872b683a..f8322cc96d6 100644
--- a/api_docs/changelog.md
+++ b/api_docs/changelog.md
@@ -20,6 +20,20 @@ format used by the Zulip server that they are interacting with.
## Changes in Zulip 11.0
+**Feature level 397**
+
+* [`POST /users/me/subscriptions`](/api/subscribe): Added parameter
+ `send_new_subscription_messages` which determines whether the user
+ would like Notification Bot to notify other users who the request
+ adds to a channel.
+
+* [`POST /users/me/subscriptions`](/api/subscribe): Added
+ `new_subscription_messages_sent` to the response, which is only
+ present if `send_new_subscription_messages` was `true` in the request.
+
+* [`POST /register`](/api/register-queue): Added `max_bulk_new_subscription_messages`
+ to the response.
+
**Feature level 396**
* [`GET /users/me/subscriptions`](/api/get-subscriptions),
diff --git a/version.py b/version.py
index a4fb58a8628..537b4d07df1 100644
--- a/version.py
+++ b/version.py
@@ -34,7 +34,7 @@ DESKTOP_WARNING_VERSION = "5.9.3"
# new level means in api_docs/changelog.md, as well as "**Changes**"
# entries in the endpoint's documentation in `zulip.yaml`.
-API_FEATURE_LEVEL = 396
+API_FEATURE_LEVEL = 397
# Bump the minor PROVISION_VERSION to indicate that folks should provision
# only when going from an old version of the code to a newer version. Bump
diff --git a/zerver/lib/events.py b/zerver/lib/events.py
index 08ac9f6733f..e771c4164c4 100644
--- a/zerver/lib/events.py
+++ b/zerver/lib/events.py
@@ -550,6 +550,7 @@ def fetch_initial_state_data(
state["max_stream_name_length"] = Stream.MAX_NAME_LENGTH
state["max_stream_description_length"] = Stream.MAX_DESCRIPTION_LENGTH
+ state["max_bulk_new_subscription_messages"] = settings.MAX_BULK_NEW_SUBSCRIPTION_MESSAGES
state["max_topic_length"] = MAX_TOPIC_NAME_LENGTH
state["max_message_length"] = settings.MAX_MESSAGE_LENGTH
if realm.demo_organization_scheduled_deletion_date is not None:
diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml
index 9170d712149..36e73cc7d8f 100644
--- a/zerver/openapi/zulip.yaml
+++ b/zerver/openapi/zulip.yaml
@@ -11596,6 +11596,23 @@ paths:
**Changes**: New in Zulip 11.0 (feature level 389).
type: integer
example: 1
+ send_new_subscription_messages:
+ description: |
+ Whether any other users newly subscribed via this request should be
+ sent a Notification Bot DM notifying them about their new
+ subscription.
+
+ The server will never send Notification Bot DMs if more than
+ `max_bulk_new_subscription_messages` (available in the [`POST
+ /register`](/api/register-queue) response) users were subscribed in
+ this request.
+
+ **Changes**: Before Zulip 11.0 (feature level 397), new subscribers
+ were always sent a Notification Bot DM, which was unduly expensive
+ when bulk-subscribing thousands of users to a channel.
+ type: boolean
+ default: true
+ example: true
required:
- subscriptions
encoding:
@@ -11686,6 +11703,18 @@ paths:
description: |
A list of names of channels that the requesting user/bot was not
authorized to subscribe to. Only present if `"authorization_errors_fatal": false`.
+ new_subscription_messages_sent:
+ type: boolean
+ description: |
+ Only present if the parameter `send_new_subscription_messages`
+ in the request was `true`.
+
+ Whether Notification Bot DMs in fact sent to the added
+ subscribers as requested by the `send_new_subscription_messages`
+ parameter. Clients may find this value useful to communicate
+ with users about the effect of this request.
+
+ **Changes**: New in Zulip 11.0 (feature level 397).
example:
{
"already_subscribed": {"1": ["testing-help"]},
@@ -11822,6 +11851,18 @@ paths:
description: |
A list of the names of channels which were unsubscribed
from as a result of the query.
+ new_subscription_messages_sent:
+ type: boolean
+ description: |
+ Only present if the parameter `send_new_subscription_messages`
+ in the request was `true`.
+
+ Whether Notification Bot DMs in fact sent to the added
+ subscribers as requested by the `send_new_subscription_messages`
+ parameter. Clients may find this value useful to communicate
+ with users about the effect of this request.
+
+ **Changes**: New in Zulip 11.0 (feature level 397).
example:
{
"msg": "",
@@ -20083,6 +20124,15 @@ paths:
Configuration for group level group permission settings.
additionalProperties:
$ref: "#/components/schemas/GroupPermissionSetting"
+ max_bulk_new_subscription_messages:
+ description: |
+ Maximum number of new subscribers for which the server will
+ respect the `send_new_subscription_messages` parameter when
+ [adding subscribers to a channel](/api/subscribe#parameter-send_new_subscription_messages).
+
+ **Changes**: New in Zulip 11.0 (feature level 397).
+ type: number
+ example: 100
example:
{
"last_event_id": -1,
diff --git a/zerver/tests/test_channel_creation.py b/zerver/tests/test_channel_creation.py
index 8b236aecf0e..44aee46fd9b 100644
--- a/zerver/tests/test_channel_creation.py
+++ b/zerver/tests/test_channel_creation.py
@@ -328,6 +328,7 @@ class TestCreateStreams(ZulipTestCase):
"8": ["brand new stream"],
},
"already_subscribed": {},
+ "new_subscription_messages_sent": True,
}
self.assertEqual(response.status_code, 200)
self.assertEqual(orjson.loads(response.content), expected_response)
diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.py
index 6f02961509e..86220b141be 100644
--- a/zerver/tests/test_home.py
+++ b/zerver/tests/test_home.py
@@ -104,6 +104,7 @@ class HomeTest(ZulipTestCase):
"max_message_length",
"max_stream_description_length",
"max_stream_name_length",
+ "max_bulk_new_subscription_messages",
"max_topic_length",
"muted_topics",
"muted_users",
diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py
index c03ed9b0ea2..63a46144b49 100644
--- a/zerver/tests/test_subs.py
+++ b/zerver/tests/test_subs.py
@@ -4926,6 +4926,32 @@ class SubscriptionAPITest(ZulipTestCase):
expected_difference=0,
)
+ def test_notification_bot_dm_on_subscription(self) -> None:
+ desdemona = self.example_user("desdemona")
+ self.login_user(desdemona)
+
+ user_ids = [
+ desdemona.id,
+ self.example_user("cordelia").id,
+ self.example_user("hamlet").id,
+ self.example_user("othello").id,
+ self.example_user("iago").id,
+ self.example_user("prospero").id,
+ ]
+
+ response = self.subscribe_via_post(
+ desdemona, ["Test stream 1"], dict(principals=orjson.dumps(user_ids).decode())
+ )
+ data = self.assert_json_success(response)
+ self.assertEqual(data["new_subscription_messages_sent"], True)
+
+ with self.settings(MAX_BULK_NEW_SUBSCRIPTION_MESSAGES=5):
+ response = self.subscribe_via_post(
+ desdemona, ["Test stream 2"], dict(principals=orjson.dumps(user_ids).decode())
+ )
+ data = self.assert_json_success(response)
+ self.assertEqual(data["new_subscription_messages_sent"], False)
+
class InviteOnlyStreamTest(ZulipTestCase):
def test_must_be_subbed_to_send(self) -> None:
diff --git a/zerver/views/streams.py b/zerver/views/streams.py
index d79a34bbdad..97186dfbd29 100644
--- a/zerver/views/streams.py
+++ b/zerver/views/streams.py
@@ -670,6 +670,7 @@ def add_subscriptions_backend(
principals: Json[list[str] | list[int]] | None = None,
authorization_errors_fatal: Json[bool] = True,
folder_id: Json[int] | None = None,
+ send_new_subscription_messages: Json[bool] = True,
) -> HttpResponse:
realm = user_profile.realm
stream_dicts = []
@@ -843,14 +844,19 @@ def add_subscriptions_backend(
result["subscribed"] = dict(result["subscribed"])
result["already_subscribed"] = dict(result["already_subscribed"])
- send_messages_for_new_subscribers(
- user_profile=user_profile,
- subscribers=subscribers,
- new_subscriptions=result["subscribed"],
- id_to_user_profile=id_to_user_profile,
- created_streams=created_streams,
- announce=announce,
- )
+ if send_new_subscription_messages:
+ if len(result["subscribed"]) <= settings.MAX_BULK_NEW_SUBSCRIPTION_MESSAGES:
+ send_messages_for_new_subscribers(
+ 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
result["subscribed"] = dict(result["subscribed"])
result["already_subscribed"] = dict(result["already_subscribed"])
diff --git a/zproject/default_settings.py b/zproject/default_settings.py
index 61bf1b3ad7e..490a419cf4f 100644
--- a/zproject/default_settings.py
+++ b/zproject/default_settings.py
@@ -681,6 +681,10 @@ MAX_STREAM_SIZE_FOR_TYPING_NOTIFICATIONS = 100
# be soft-reactivated in the case of user group mention.
MAX_GROUP_SIZE_FOR_MENTION_REACTIVATION = 11
+# The maximum number of newly subscribed users for which the server
+# will consider sending DMs to each new subscriber.
+MAX_BULK_NEW_SUBSCRIPTION_MESSAGES = 100
+
# Limiting guest access to other users via the
# can_access_all_users_group setting makes presence queries much more
# expensive. This can be a significant performance problem for