Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
streams: Add can_add_subscribers_group as a setting.
Shubham Padia97996b9929a5
diff --git a/api_docs/changelog.md b/api_docs/changelog.mdindex 77d4c3b1f25..c5fe6affc6c 100644--- a/api_docs/changelog.md+++ b/api_docs/changelog.md@@ -20,6 +20,19 @@ format used by the Zulip server that they are interacting with.## Changes in Zulip 10.0+**Feature level 342**++* [`GET /users/me/subscriptions`](/api/get-subscriptions),+ [`GET /streams`](/api/get-streams), [`GET /events`](/api/get-events),+ [`POST /register`](/api/register-queue): Added+ `can_add_subscribers_group` field to Stream and Subscription+ objects.+* [`POST /users/me/subscriptions`](/api/subscribe),+ [`PATCH /streams/{stream_id}`](/api/update-stream): Added+ `can_add_subscribers_group` parameter to support setting and+ changing the user group whose members can add other subscribers+ to the specified stream.+**Feature level 341*** `PATCH /realm`, [`GET /events`](/api/get-events),diff --git a/version.py b/version.pyindex 3cfd322cdc3..a6c4835e42c 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 = 341 # Last bumped for adding realm.can_add_subscribers_group+API_FEATURE_LEVEL = 342 # Last bumped for adding stream.can_add_subscribers_group# 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. Bumpdiff --git a/zerver/actions/streams.py b/zerver/actions/streams.pyindex e65d54127cf..485daa131da 100644--- a/zerver/actions/streams.py+++ b/zerver/actions/streams.py@@ -392,6 +392,7 @@ def send_subscription_add_events(subscribers=stream_subscribers,# Fields from Stream.API_FIELDSis_archived=stream_dict["is_archived"],+ can_add_subscribers_group=stream_dict["can_add_subscribers_group"],can_administer_channel_group=stream_dict["can_administer_channel_group"],can_send_message_group=stream_dict["can_send_message_group"],can_remove_subscribers_group=stream_dict["can_remove_subscribers_group"],diff --git a/zerver/lib/streams.py b/zerver/lib/streams.pyindex a42c15e337c..6ed755abaab 100644--- a/zerver/lib/streams.py+++ b/zerver/lib/streams.py@@ -78,6 +78,7 @@ class StreamDict(TypedDict, total=False):stream_post_policy: inthistory_public_to_subscribers: bool | Nonemessage_retention_days: int | None+ can_add_subscribers_group: UserGroup | Nonecan_administer_channel_group: UserGroup | Nonecan_send_message_group: UserGroup | Nonecan_remove_subscribers_group: UserGroup | None@@ -186,6 +187,7 @@ def create_stream_if_needed(history_public_to_subscribers: bool | None = None,stream_description: str = "",message_retention_days: int | None = None,+ can_add_subscribers_group: UserGroup | None = None,can_administer_channel_group: UserGroup | None = None,can_send_message_group: UserGroup | None = None,can_remove_subscribers_group: UserGroup | None = None,@@ -288,6 +290,7 @@ def create_streams_if_needed(history_public_to_subscribers=stream_dict.get("history_public_to_subscribers"),stream_description=stream_dict.get("description", ""),message_retention_days=stream_dict.get("message_retention_days", None),+ can_add_subscribers_group=stream_dict.get("can_add_subscribers_group", None),can_administer_channel_group=stream_dict.get("can_administer_channel_group", None),can_send_message_group=stream_dict.get("can_send_message_group", None),can_remove_subscribers_group=stream_dict.get("can_remove_subscribers_group", None),@@ -1020,10 +1023,14 @@ def stream_to_dict(stream_weekly_traffic = Noneif setting_groups_dict is not None:+ can_add_subscribers_group = setting_groups_dict[stream.can_add_subscribers_group_id]can_administer_channel_group = setting_groups_dict[stream.can_administer_channel_group_id]can_send_message_group = setting_groups_dict[stream.can_send_message_group_id]can_remove_subscribers_group = setting_groups_dict[stream.can_remove_subscribers_group_id]else:+ can_add_subscribers_group = get_group_setting_value_for_api(+ stream.can_add_subscribers_group+ )can_administer_channel_group = get_group_setting_value_for_api(stream.can_administer_channel_group)@@ -1038,6 +1045,7 @@ def stream_to_dict(return APIStreamDict(is_archived=stream.deactivated,+ can_add_subscribers_group=can_add_subscribers_group,can_administer_channel_group=can_administer_channel_group,can_send_message_group=can_send_message_group,can_remove_subscribers_group=can_remove_subscribers_group,diff --git a/zerver/lib/subscription_info.py b/zerver/lib/subscription_info.pyindex 17050c7c29c..ae925f024c9 100644--- a/zerver/lib/subscription_info.py+++ b/zerver/lib/subscription_info.py@@ -53,6 +53,7 @@ def get_web_public_subs(realm: Realm) -> SubscriptionInfo:for stream in streams:# Add Stream fields.is_archived = stream.deactivated+ can_add_subscribers_group = setting_groups_dict[stream.can_add_subscribers_group_id]can_administer_channel_group = setting_groups_dict[stream.can_administer_channel_group_id]can_send_message_group = setting_groups_dict[stream.can_send_message_group_id]can_remove_subscribers_group = setting_groups_dict[stream.can_remove_subscribers_group_id]@@ -91,6 +92,7 @@ def get_web_public_subs(realm: Realm) -> SubscriptionInfo:sub = SubscriptionStreamDict(is_archived=is_archived,audible_notifications=audible_notifications,+ can_add_subscribers_group=can_add_subscribers_group,can_administer_channel_group=can_administer_channel_group,can_send_message_group=can_send_message_group,can_remove_subscribers_group=can_remove_subscribers_group,@@ -153,6 +155,7 @@ def build_stream_api_dict(# migration.is_announcement_only = raw_stream_dict["stream_post_policy"] == Stream.STREAM_POST_POLICY_ADMINS+ can_add_subscribers_group = setting_groups_dict[raw_stream_dict["can_add_subscribers_group_id"]]can_administer_channel_group = setting_groups_dict[raw_stream_dict["can_administer_channel_group_id"]]@@ -163,6 +166,7 @@ def build_stream_api_dict(return APIStreamDict(is_archived=raw_stream_dict["deactivated"],+ can_add_subscribers_group=can_add_subscribers_group,can_administer_channel_group=can_administer_channel_group,can_send_message_group=can_send_message_group,can_remove_subscribers_group=can_remove_subscribers_group,@@ -191,6 +195,7 @@ def build_stream_dict_for_sub() -> SubscriptionStreamDict:# Handle Stream.API_FIELDSis_archived = stream_dict["is_archived"]+ can_add_subscribers_group = stream_dict["can_add_subscribers_group"]can_administer_channel_group = stream_dict["can_administer_channel_group"]can_send_message_group = stream_dict["can_send_message_group"]can_remove_subscribers_group = stream_dict["can_remove_subscribers_group"]@@ -228,6 +233,7 @@ def build_stream_dict_for_sub(return SubscriptionStreamDict(is_archived=is_archived,audible_notifications=audible_notifications,+ can_add_subscribers_group=can_add_subscribers_group,can_administer_channel_group=can_administer_channel_group,can_send_message_group=can_send_message_group,can_remove_subscribers_group=can_remove_subscribers_group,@@ -284,6 +290,9 @@ def build_stream_dict_for_never_sub(else:stream_weekly_traffic = None+ can_add_subscribers_group_value = setting_groups_dict[+ raw_stream_dict["can_add_subscribers_group_id"]+ ]can_administer_channel_group_value = setting_groups_dict[raw_stream_dict["can_administer_channel_group_id"]]@@ -298,6 +307,7 @@ def build_stream_dict_for_never_sub(# Our caller may add a subscribers field.return NeverSubscribedStreamDict(is_archived=is_archived,+ can_add_subscribers_group=can_add_subscribers_group_value,can_administer_channel_group=can_administer_channel_group_value,can_send_message_group=can_send_message_group_value,can_remove_subscribers_group=can_remove_subscribers_group_value,diff --git a/zerver/lib/types.py b/zerver/lib/types.pyindex 2fc1f7ad69b..5ef69a90821 100644--- a/zerver/lib/types.py+++ b/zerver/lib/types.py@@ -153,6 +153,7 @@ class RawStreamDict(TypedDict):are needed to encode the stream for the API."""+ can_add_subscribers_group_id: intcan_administer_channel_group_id: intcan_send_message_group_id: intcan_remove_subscribers_group_id: int@@ -197,6 +198,7 @@ class SubscriptionStreamDict(TypedDict):"""audible_notifications: bool | None+ can_add_subscribers_group: int | AnonymousSettingGroupDictcan_administer_channel_group: int | AnonymousSettingGroupDictcan_send_message_group: int | AnonymousSettingGroupDictcan_remove_subscribers_group: int | AnonymousSettingGroupDict@@ -229,6 +231,7 @@ class SubscriptionStreamDict(TypedDict):class NeverSubscribedStreamDict(TypedDict):is_archived: bool+ can_add_subscribers_group: int | AnonymousSettingGroupDictcan_administer_channel_group: int | AnonymousSettingGroupDictcan_send_message_group: int | AnonymousSettingGroupDictcan_remove_subscribers_group: int | AnonymousSettingGroupDict@@ -257,6 +260,7 @@ class DefaultStreamDict(TypedDict):"""is_archived: bool+ can_add_subscribers_group: int | AnonymousSettingGroupDictcan_administer_channel_group: int | AnonymousSettingGroupDictcan_send_message_group: int | AnonymousSettingGroupDictcan_remove_subscribers_group: int | AnonymousSettingGroupDictdiff --git a/zerver/migrations/0653_stream_can_add_subscribers_group.py b/zerver/migrations/0653_stream_can_add_subscribers_group.pynew file mode 100644index 00000000000..6772fb29c99--- /dev/null+++ b/zerver/migrations/0653_stream_can_add_subscribers_group.py@@ -0,0 +1,23 @@+# Generated by Django 5.0.9 on 2024-12-17 05:55++import django.db.models.deletion+from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ("zerver", "0652_remove_realm_invite_to_stream_policy"),+ ]++ operations = [+ migrations.AddField(+ model_name="stream",+ name="can_add_subscribers_group",+ field=models.ForeignKey(+ null=True,+ on_delete=django.db.models.deletion.RESTRICT,+ related_name="+",+ to="zerver.usergroup",+ ),+ ),+ ]diff --git a/zerver/migrations/0654_set_default_for_stream_can_add_subscribers_group.py b/zerver/migrations/0654_set_default_for_stream_can_add_subscribers_group.pynew file mode 100644index 00000000000..2c4cdba039e--- /dev/null+++ b/zerver/migrations/0654_set_default_for_stream_can_add_subscribers_group.py@@ -0,0 +1,56 @@+# Generated by Django 5.0.9 on 2024-12-17 05:55++from django.db import migrations, transaction+from django.db.backends.base.schema import BaseDatabaseSchemaEditor+from django.db.migrations.state import StateApps+from django.db.models import Max, Min, OuterRef+++def set_default_value_for_can_add_subscribers_group(+ apps: StateApps, schema_editor: BaseDatabaseSchemaEditor+) -> None:+ Stream = apps.get_model("zerver", "stream")+ NamedUserGroup = apps.get_model("zerver", "NamedUserGroup")+ BATCH_SIZE = 1000++ max_id = Stream.objects.filter(can_add_subscribers_group=None).aggregate(Max("id"))["id__max"]+ if max_id is None:+ # Do nothing if there are no channels on the server.+ return++ lower_bound = Stream.objects.filter(can_add_subscribers_group=None).aggregate(Min("id"))[+ "id__min"+ ]+ while lower_bound <= max_id + BATCH_SIZE / 2:+ upper_bound = lower_bound + BATCH_SIZE - 1+ print(f"Processing batch {lower_bound} to {upper_bound} for Stream")++ with transaction.atomic():+ Stream.objects.filter(+ id__range=(lower_bound, upper_bound),+ can_add_subscribers_group=None,+ ).update(+ can_add_subscribers_group=NamedUserGroup.objects.filter(+ name="role:nobody",+ realm_for_sharding=OuterRef("realm_id"),+ is_system_group=True,+ ).values("pk")+ )++ lower_bound += BATCH_SIZE+++class Migration(migrations.Migration):+ atomic = False++ dependencies = [+ ("zerver", "0653_stream_can_add_subscribers_group"),+ ]++ operations = [+ migrations.RunPython(+ set_default_value_for_can_add_subscribers_group,+ elidable=True,+ reverse_code=migrations.RunPython.noop,+ ),+ ]diff --git a/zerver/migrations/0655_alter_stream_can_add_subscribers_group.py b/zerver/migrations/0655_alter_stream_can_add_subscribers_group.pynew file mode 100644index 00000000000..c6215ff4f15--- /dev/null+++ b/zerver/migrations/0655_alter_stream_can_add_subscribers_group.py@@ -0,0 +1,22 @@+# Generated by Django 5.0.9 on 2024-12-17 05:56++import django.db.models.deletion+from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ("zerver", "0654_set_default_for_stream_can_add_subscribers_group"),+ ]++ operations = [+ migrations.AlterField(+ model_name="stream",+ name="can_add_subscribers_group",+ field=models.ForeignKey(+ on_delete=django.db.models.deletion.RESTRICT,+ related_name="+",+ to="zerver.usergroup",+ ),+ ),+ ]diff --git a/zerver/models/streams.py b/zerver/models/streams.pyindex da313bf49d3..74104b96f21 100644--- a/zerver/models/streams.py+++ b/zerver/models/streams.py@@ -125,6 +125,9 @@ class Stream(models.Model):# is referenced by the respective setting. We are not using PROTECT# since we want to allow deletion of user groups when the realm# itself is deleted.+ can_add_subscribers_group = models.ForeignKey(+ UserGroup, on_delete=models.RESTRICT, related_name="+"+ )can_administer_channel_group = models.ForeignKey(UserGroup, on_delete=models.RESTRICT, related_name="+")@@ -144,6 +147,13 @@ class Stream(models.Model):is_recently_active = models.BooleanField(default=True, db_default=True)stream_permission_group_settings = {+ "can_add_subscribers_group": GroupPermissionSetting(+ require_system_group=False,+ allow_internet_group=False,+ allow_nobody_group=True,+ allow_everyone_group=False,+ default_group_name=SystemGroups.NOBODY,+ ),"can_administer_channel_group": GroupPermissionSetting(require_system_group=False,allow_internet_group=False,@@ -205,6 +215,7 @@ class Stream(models.Model):"message_retention_days","name","rendered_description",+ "can_add_subscribers_group_id","can_administer_channel_group_id","can_send_message_group_id","can_remove_subscribers_group_id",diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yamlindex 7dd0499ce2a..8e2204d26bc 100644--- a/zerver/openapi/zulip.yaml+++ b/zerver/openapi/zulip.yaml@@ -716,6 +716,7 @@ paths:"wildcard_mentions_notify": null,"in_home_view": true,"stream_weekly_traffic": null,+ "can_add_subscribers_group": 2,"can_remove_subscribers_group": 2,"subscribers": [10],},@@ -1352,6 +1353,7 @@ paths:"is_recently_active": true,"message_retention_days": null,"is_announcement_only": false,+ "can_add_subscribers_group": 2,"can_remove_subscribers_group": 2,"stream_weekly_traffic": null,},@@ -1412,6 +1414,7 @@ paths:"is_recently_active": true,"message_retention_days": null,"is_announcement_only": false,+ "can_add_subscribers_group": 2,"can_remove_subscribers_group": 2,"stream_weekly_traffic": null,},@@ -10513,6 +10516,8 @@ paths:$ref: "#/components/schemas/HistoryPublicToSubscribers"message_retention_days:$ref: "#/components/schemas/MessageRetentionDays"+ can_add_subscribers_group:+ $ref: "#/components/schemas/ChannelCanAddSubscribersGroup"can_remove_subscribers_group:$ref: "#/components/schemas/CanRemoveSubscribersGroup"can_administer_channel_group:@@ -10538,6 +10543,8 @@ paths:contentType: application/jsonhistory_public_to_subscribers:contentType: application/json+ can_add_subscribers_group:+ contentType: application/jsoncan_remove_subscribers_group:contentType: application/jsoncan_administer_channel_group:@@ -15057,6 +15064,7 @@ paths:nullable: trueis_recently_active: {}is_announcement_only: {}+ can_add_subscribers_group: {}can_remove_subscribers_group: {}can_administer_channel_group: {}can_send_message_group: {}@@ -19985,6 +19993,7 @@ paths:nullable: trueis_recently_active: {}is_announcement_only: {}+ can_add_subscribers_group: {}can_remove_subscribers_group: {}can_administer_channel_group: {}can_send_message_group: {}@@ -20038,6 +20047,7 @@ paths:"streams":[{+ "can_add_subscribers_group": 10,"can_remove_subscribers_group": 10,"creator_id": null,"date_created": 1691057093,@@ -20058,6 +20068,7 @@ paths:"stream_weekly_traffic": null,},{+ "can_add_subscribers_group": 9,"can_remove_subscribers_group": 9,"creator_id": 12,"date_created": 1691057093,@@ -20143,6 +20154,7 @@ paths:"rendered_description": "<p>A Scandinavian country</p>","stream_id": 7,"stream_post_policy": 1,+ "can_add_subscribers_group": 2,"can_remove_subscribers_group": 2,"stream_weekly_traffic": null,},@@ -20291,6 +20303,31 @@ paths:example: falsemessage_retention_days:$ref: "#/components/schemas/MessageRetentionDays"+ can_add_subscribers_group:+ allOf:+ - description: |+ The set of users who have permission to add subscribers to this+ channel expressed as an [update to a group-setting+ value][update-group-setting].++ [update-group-setting]: /api/group-setting-values#updating-group-setting-values++ Users who can administer the channel or have the similar+ realm-level permission can add subscribers regardless of the value+ of this setting.++ Note that a user must also [have access](/help/channel-permissions) to a+ channel in order to modify it.++ **Changes**: New in Zulip 10.0 (feature level 342). Previously, there was no+ channel-level setting for this permission.+ example:+ {+ "new":+ {"direct_members": [10], "direct_subgroups": [11]},+ "old": 15,+ }+ - $ref: "#/components/schemas/GroupSettingValueUpdate"can_remove_subscribers_group:allOf:- description: |@@ -20380,6 +20417,8 @@ paths:contentType: application/jsonis_default_stream:contentType: application/json+ can_add_subscribers_group:+ contentType: application/jsoncan_remove_subscribers_group:contentType: application/jsoncan_administer_channel_group:@@ -22087,6 +22126,7 @@ components:nullable: trueis_recently_active: {}is_announcement_only: {}+ can_add_subscribers_group: {}can_remove_subscribers_group: {}can_administer_channel_group: {}can_send_message_group: {}@@ -22269,6 +22309,8 @@ components:**Changes**: Deprecated in Zulip 3.0 (feature level 1). Clientsshould use `stream_post_policy` instead.+ can_add_subscribers_group:+ $ref: "#/components/schemas/ChannelCanAddSubscribersGroup"can_remove_subscribers_group:$ref: "#/components/schemas/CanRemoveSubscribersGroup"can_administer_channel_group:@@ -23331,6 +23373,8 @@ components:If `null`, the channel was recently created and there isinsufficient data to estimate the average traffic.+ can_add_subscribers_group:+ $ref: "#/components/schemas/ChannelCanAddSubscribersGroup"can_remove_subscribers_group:$ref: "#/components/schemas/CanRemoveSubscribersGroup"can_administer_channel_group:@@ -24982,6 +25026,23 @@ components:- type: string- type: integerexample: "20"+ ChannelCanAddSubscribersGroup:+ allOf:+ - $ref: "#/components/schemas/GroupSettingValue"+ - description: |+ A [group-setting value][setting-values] defining the set of users+ who have permission to add subscribers to this channel.++ Users who can administer the channel or have the similar realm-level+ permission can add subscribers regardless of the value of this setting.++ Note that a user must also [have access](/help/channel-permissions) to a+ channel in order to modify it.++ **Changes**: New in Zulip 10.0 (feature level 342). Previously, there was no+ channel-level setting for this permission.++ [setting-values]: /api/group-setting-valuesCanRemoveSubscribersGroup:allOf:- $ref: "#/components/schemas/GroupSettingValue"diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.pyindex 1fa721abbee..a8332d47724 100644--- a/zerver/tests/test_subs.py+++ b/zerver/tests/test_subs.py@@ -3054,7 +3054,7 @@ class StreamAdminTest(ZulipTestCase):are on."""result = self.attempt_unsubscribe_of_principal(- query_count=19,+ query_count=21,target_users=[self.example_user("cordelia")],is_realm_admin=True,is_subbed=True,@@ -3071,7 +3071,7 @@ class StreamAdminTest(ZulipTestCase):streams you aren't on."""result = self.attempt_unsubscribe_of_principal(- query_count=19,+ query_count=21,target_users=[self.example_user("cordelia")],is_realm_admin=True,is_subbed=False,diff --git a/zerver/views/streams.py b/zerver/views/streams.pyindex 4a2395200ed..291caa0ffa1 100644--- a/zerver/views/streams.py+++ b/zerver/views/streams.py@@ -261,6 +261,7 @@ def update_stream_backend(is_web_public: Json[bool] | None = None,new_name: str | None = None,message_retention_days: Json[str] | Json[int] | None = None,+ can_add_subscribers_group: Json[GroupSettingChangeRequest] | None = None,can_administer_channel_group: Json[GroupSettingChangeRequest] | None = None,can_send_message_group: Json[GroupSettingChangeRequest] | None = None,can_remove_subscribers_group: Json[GroupSettingChangeRequest] | None = None,@@ -577,6 +578,7 @@ def add_subscriptions_backend(is_default_stream: Json[bool] = False,history_public_to_subscribers: Json[bool] | None = None,message_retention_days: Json[str] | Json[int] = RETENTION_DEFAULT,+ can_add_subscribers_group: Json[int | AnonymousSettingGroupDict] | None = None,can_administer_channel_group: Json[int | AnonymousSettingGroupDict] | None = None,can_send_message_group: Json[int | AnonymousSettingGroupDict] | None = None,can_remove_subscribers_group: Json[int | AnonymousSettingGroupDict] | None = None,@@ -638,6 +640,9 @@ def add_subscriptions_backend(stream_dict_copy["message_retention_days"] = parse_message_retention_days(message_retention_days, Stream.MESSAGE_RETENTION_SPECIAL_VALUES_MAP)+ stream_dict_copy["can_add_subscribers_group"] = group_settings_map[+ "can_add_subscribers_group"+ ]stream_dict_copy["can_administer_channel_group"] = group_settings_map["can_administer_channel_group"]