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.

streams: Pass stream_post_policy value according to the group setting.

Sahil Batra1d7a77b9a8bc
diff --git a/api_docs/changelog.md b/api_docs/changelog.md
index c2a7e449d68..c5eef718cc3 100644
--- a/api_docs/changelog.md
+++ b/api_docs/changelog.md
@@ -33,6 +33,13 @@ format used by the Zulip server that they are interacting with.
[`PATCH /streams/{stream_id}`](/api/update-stream): Added `can_send_message_group`
which is a [group-setting value](/api/group-setting-values) describing the
set of users with permissions to post in the channel.
+* [`GET /users/me/subscriptions`](/api/get-subscriptions),
+ [`GET /streams`](/api/get-streams), [`GET /events`](/api/get-events),
+ [`POST /register`](/api/register-queue): `stream_post_policy` field is
+ deprecated, having been replaced by `can_send_message_group`. Notably,
+ this backwards-compatible `stream_post_policy` value now contains the
+ superset of the true value that best approximates the actual permission
+ setting.
**Feature level 332**
diff --git a/zerver/actions/streams.py b/zerver/actions/streams.py
index 6380423206c..bac9694eabf 100644
--- a/zerver/actions/streams.py
+++ b/zerver/actions/streams.py
@@ -42,6 +42,7 @@ from zerver.lib.streams import (
get_group_setting_value_dict_for_streams,
get_occupied_streams,
get_stream_permission_policy_name,
+ get_stream_post_policy_value_based_on_group_setting,
render_stream_description,
send_stream_creation_event,
stream_to_dict,
@@ -1607,14 +1608,6 @@ def do_change_stream_group_based_setting(
old_setting_api_value = get_group_setting_value_for_api(old_user_group)
new_setting_api_value = get_group_setting_value_for_api(user_group)
- if not hasattr(old_user_group, "named_user_group") and hasattr(user_group, "named_user_group"):
- # We delete the UserGroup which the setting was set to
- # previously if it does not have any linked NamedUserGroup
- # object, as it is not used anywhere else. A new UserGroup
- # object would be created if the setting is later set to
- # a combination of users and groups.
- old_user_group.delete()
-
RealmAuditLog.objects.create(
realm=stream.realm,
acting_user=acting_user,
@@ -1640,3 +1633,40 @@ def do_change_stream_group_based_setting(
name=stream.name,
)
send_event_on_commit(stream.realm, event, can_access_stream_user_ids(stream))
+
+ if setting_name == "can_send_message_group":
+ old_stream_post_policy = get_stream_post_policy_value_based_on_group_setting(old_user_group)
+ stream_post_policy = get_stream_post_policy_value_based_on_group_setting(user_group)
+
+ if old_stream_post_policy != stream_post_policy:
+ event = dict(
+ op="update",
+ type="stream",
+ property="stream_post_policy",
+ value=stream_post_policy,
+ stream_id=stream.id,
+ name=stream.name,
+ )
+ send_event_on_commit(stream.realm, event, can_access_stream_user_ids(stream))
+
+ # Backwards-compatibility code: We removed the
+ # is_announcement_only property in early 2020, but we send a
+ # duplicate event for legacy mobile clients that might want the
+ # data.
+ event = dict(
+ op="update",
+ type="stream",
+ property="is_announcement_only",
+ value=stream_post_policy == Stream.STREAM_POST_POLICY_ADMINS,
+ stream_id=stream.id,
+ name=stream.name,
+ )
+ send_event_on_commit(stream.realm, event, can_access_stream_user_ids(stream))
+
+ if not hasattr(old_user_group, "named_user_group") and hasattr(user_group, "named_user_group"):
+ # We delete the UserGroup which the setting was set to
+ # previously if it does not have any linked NamedUserGroup
+ # object, as it is not used anywhere else. A new UserGroup
+ # object would be created if the setting is later set to
+ # a combination of users and groups.
+ old_user_group.delete()
diff --git a/zerver/lib/event_schema.py b/zerver/lib/event_schema.py
index 9ff0464b61c..8e36b8bffa3 100644
--- a/zerver/lib/event_schema.py
+++ b/zerver/lib/event_schema.py
@@ -538,6 +538,9 @@ def check_stream_update(
elif prop == "is_recently_active":
assert extra_keys == set()
assert isinstance(value, bool)
+ elif prop == "is_announcement_only":
+ assert extra_keys == set()
+ assert isinstance(value, bool)
else:
raise AssertionError(f"Unknown property: {prop}")
diff --git a/zerver/lib/streams.py b/zerver/lib/streams.py
index 5eaa6d8a646..de71b93e7dd 100644
--- a/zerver/lib/streams.py
+++ b/zerver/lib/streams.py
@@ -594,7 +594,7 @@ def get_web_public_streams_queryset(realm: Realm) -> QuerySet[Stream]:
# these in the query.
invite_only=False,
history_public_to_subscribers=True,
- )
+ ).select_related("can_send_message_group", "can_send_message_group__named_user_group")
def check_stream_name_available(realm: Realm, name: str) -> None:
@@ -994,6 +994,18 @@ def get_occupied_streams(realm: Realm) -> QuerySet[Stream]:
return occupied_streams
+def get_stream_post_policy_value_based_on_group_setting(setting_group: UserGroup) -> int:
+ if (
+ hasattr(setting_group, "named_user_group")
+ and setting_group.named_user_group.is_system_group
+ ):
+ group_name = setting_group.named_user_group.name
+ if group_name in Stream.SYSTEM_GROUPS_ENUM_MAP:
+ return Stream.SYSTEM_GROUPS_ENUM_MAP[group_name]
+
+ return Stream.STREAM_POST_POLICY_EVERYONE
+
+
def stream_to_dict(
stream: Stream,
recent_traffic: dict[int, int] | None = None,
@@ -1025,6 +1037,10 @@ def stream_to_dict(
stream.can_remove_subscribers_group
)
+ stream_post_policy = get_stream_post_policy_value_based_on_group_setting(
+ stream.can_send_message_group
+ )
+
return APIStreamDict(
is_archived=stream.deactivated,
can_administer_channel_group=can_administer_channel_group,
@@ -1042,8 +1058,8 @@ def stream_to_dict(
name=stream.name,
rendered_description=stream.rendered_description,
stream_id=stream.id,
- stream_post_policy=stream.stream_post_policy,
- is_announcement_only=stream.stream_post_policy == Stream.STREAM_POST_POLICY_ADMINS,
+ stream_post_policy=stream_post_policy,
+ is_announcement_only=stream_post_policy == Stream.STREAM_POST_POLICY_ADMINS,
stream_weekly_traffic=stream_weekly_traffic,
)
@@ -1071,13 +1087,17 @@ def get_streams_for_user(
include_public = include_public and user_profile.can_access_public_streams()
# Start out with all streams in the realm.
- query = Stream.objects.filter(realm=user_profile.realm)
+ query = Stream.objects.select_related(
+ "can_send_message_group", "can_send_message_group__named_user_group"
+ ).filter(realm=user_profile.realm)
if exclude_archived:
query = query.filter(deactivated=False)
if include_all_active:
- streams = query.only(*Stream.API_FIELDS)
+ streams = query.only(
+ *Stream.API_FIELDS, "can_send_message_group", "can_send_message_group__named_user_group"
+ )
else:
# We construct a query as the or (|) of the various sources
# this user requested streams from.
diff --git a/zerver/lib/subscription_info.py b/zerver/lib/subscription_info.py
index 8e407cd152e..17050c7c29c 100644
--- a/zerver/lib/subscription_info.py
+++ b/zerver/lib/subscription_info.py
@@ -19,6 +19,7 @@ from zerver.lib.stream_traffic import get_average_weekly_stream_traffic, get_str
from zerver.lib.streams import (
get_group_setting_value_dict_for_streams,
get_setting_values_for_group_settings,
+ get_stream_post_policy_value_based_on_group_setting,
get_web_public_streams_queryset,
subscribed_to_stream,
)
@@ -62,13 +63,15 @@ def get_web_public_subs(realm: Realm) -> SubscriptionInfo:
is_recently_active = stream.is_recently_active
history_public_to_subscribers = stream.history_public_to_subscribers
invite_only = stream.invite_only
- is_announcement_only = stream.stream_post_policy == Stream.STREAM_POST_POLICY_ADMINS
is_web_public = stream.is_web_public
message_retention_days = stream.message_retention_days
name = stream.name
rendered_description = stream.rendered_description
stream_id = stream.id
- stream_post_policy = stream.stream_post_policy
+ stream_post_policy = get_stream_post_policy_value_based_on_group_setting(
+ stream.can_send_message_group
+ )
+ is_announcement_only = stream_post_policy == Stream.STREAM_POST_POLICY_ADMINS
# Add versions of the Subscription fields based on a simulated
# new user subscription set.
@@ -506,21 +509,31 @@ def gather_subscriptions_helper(
realm = user_profile.realm
all_streams = get_all_streams(
realm, include_archived_channels=include_archived_channels
- ).values(
+ ).select_related("can_send_message_group", "can_send_message_group__named_user_group")
+
+ all_stream_dicts = all_streams.values(
*Stream.API_FIELDS,
# The realm_id and recipient_id are generally not needed in the API.
"realm_id",
"recipient_id",
)
recip_id_to_stream_id: dict[int, int] = {
- stream["recipient_id"]: stream["id"] for stream in all_streams
+ stream["recipient_id"]: stream["id"] for stream in all_stream_dicts
+ }
+ all_streams_map: dict[int, RawStreamDict] = {
+ stream["id"]: stream for stream in all_stream_dicts
}
- all_streams_map: dict[int, RawStreamDict] = {stream["id"]: stream for stream in all_streams}
- setting_group_ids = set()
for stream in all_streams:
+ stream_post_policy = get_stream_post_policy_value_based_on_group_setting(
+ stream.can_send_message_group
+ )
+ all_streams_map[stream.id]["stream_post_policy"] = stream_post_policy
+
+ setting_group_ids = set()
+ for stream_dict in all_stream_dicts:
for setting_name in Stream.stream_permission_group_settings:
- setting_group_ids.add(stream[setting_name + "_id"])
+ setting_group_ids.add(stream_dict[setting_name + "_id"])
setting_groups_dict = get_setting_values_for_group_settings(list(setting_group_ids))
@@ -584,10 +597,12 @@ def gather_subscriptions_helper(
if user_profile.can_access_public_streams():
never_subscribed_stream_ids = {
- stream["id"] for stream in all_streams if not stream["deactivated"]
+ stream["id"] for stream in all_stream_dicts if not stream["deactivated"]
} - sub_unsub_stream_ids
else:
- web_public_stream_ids = {stream["id"] for stream in all_streams if stream["is_web_public"]}
+ web_public_stream_ids = {
+ stream["id"] for stream in all_stream_dicts if stream["is_web_public"]
+ }
never_subscribed_stream_ids = web_public_stream_ids - sub_unsub_stream_ids
never_subscribed_streams = [
@@ -614,7 +629,7 @@ def gather_subscriptions_helper(
}
subscriber_map = bulk_get_subscriber_user_ids(
- all_streams,
+ all_stream_dicts,
user_profile,
subscribed_stream_ids,
)
diff --git a/zerver/models/streams.py b/zerver/models/streams.py
index 4c9b5349545..ebe90912679 100644
--- a/zerver/models/streams.py
+++ b/zerver/models/streams.py
@@ -100,6 +100,13 @@ class Stream(models.Model):
}
STREAM_POST_POLICY_TYPES = list(POST_POLICIES.keys())
+ SYSTEM_GROUPS_ENUM_MAP = {
+ SystemGroups.EVERYONE: STREAM_POST_POLICY_EVERYONE,
+ SystemGroups.ADMINISTRATORS: STREAM_POST_POLICY_ADMINS,
+ SystemGroups.FULL_MEMBERS: STREAM_POST_POLICY_RESTRICT_NEW_MEMBERS,
+ SystemGroups.MODERATORS: STREAM_POST_POLICY_MODERATORS,
+ }
+
# The unique thing about Zephyr public streams is that we never list their
# users. We may try to generalize this concept later, but for now
# we just use a concrete field. (Zephyr public streams aren't exactly like
@@ -286,7 +293,11 @@ def bulk_get_streams(realm: Realm, stream_names: set[str]) -> dict[str, Any]:
where_clause = (
"upper(zerver_stream.name::text) IN (SELECT upper(name) FROM unnest(%s) AS name)"
)
- return get_active_streams(realm).extra(where=[where_clause], params=(list(stream_names),)) # noqa: S610
+ return (
+ get_active_streams(realm)
+ .select_related("can_send_message_group", "can_send_message_group__named_user_group")
+ .extra(where=[where_clause], params=(list(stream_names),)) # noqa: S610
+ )
if not stream_names:
return {}
diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml
index efad5e11868..4e8ea932128 100644
--- a/zerver/openapi/zulip.yaml
+++ b/zerver/openapi/zulip.yaml
@@ -22038,18 +22038,29 @@ components:
**Changes**: New in Zulip 2.1.0.
stream_post_policy:
type: integer
+ deprecated: true
description: |
- [Policy][permission-level] for which users can post messages to the channel.
+ A deprecated representation of a superset of the users who
+ have permission to post messages to the channel available
+ for backwards-compatibility. Clients should use
+ `can_send_message_group` instead.
+
+ It is an enum with the following possible values, corresponding
+ to roles/system groups:
- 1 = Any user can post.
- 2 = Only administrators can post.
- 3 = Only [full members][calc-full-member] can post.
- 4 = Only moderators can post.
- **Changes**: New in Zulip 3.0 (feature level 1), replacing the previous
+ **Changes**: Deprecated in Zulip 10.0 (feature level 333) and
+ replaced by `can_send_message_group`, which supports finer
+ resolution of configurations, resulting in this property being
+ inaccurate following that transition.
+
+ New in Zulip 3.0 (feature level 1), replacing the previous
`is_announcement_only` boolean.
- [permission-level]: /api/roles-and-permissions#permission-levels
[calc-full-member]: /api/roles-and-permissions#determining-if-a-user-is-a-full-member
message_retention_days:
type: integer
@@ -23073,18 +23084,29 @@ components:
The user's personal color for the channel.
stream_post_policy:
type: integer
+ deprecated: true
description: |
- [Policy][permission-level] for which users can post messages to the channel.
+ A deprecated representation of a superset of the users who
+ have permission to post messages to the channel available
+ for backwards-compatibility. Clients should use
+ `can_send_message_group` instead.
+
+ It is an enum with the following possible values, corresponding
+ to roles/system groups:
- 1 = Any user can post.
- 2 = Only administrators can post.
- 3 = Only [full members][calc-full-member] can post.
- 4 = Only moderators can post.
- **Changes**: New in Zulip 3.0 (feature level 1), replacing the previous
+ **Changes**: Deprecated in Zulip 10.0 (feature level 333) and
+ replaced by `can_send_message_group`, which supports finer
+ resolution of configurations, resulting in this property being
+ inaccurate following that transition.
+
+ New in Zulip 3.0 (feature level 1), replacing the previous
`is_announcement_only` boolean.
- [permission-level]: /api/roles-and-permissions#permission-levels
[calc-full-member]: /api/roles-and-permissions#determining-if-a-user-is-a-full-member
message_retention_days:
type: integer
diff --git a/zerver/tests/test_event_system.py b/zerver/tests/test_event_system.py
index ef4b8710456..c70b48f2cf4 100644
--- a/zerver/tests/test_event_system.py
+++ b/zerver/tests/test_event_system.py
@@ -1217,7 +1217,7 @@ class FetchQueriesTest(ZulipTestCase):
realm = get_realm_with_settings(realm_id=user.realm_id)
with (
- self.assert_database_query_count(44),
+ self.assert_database_query_count(45),
mock.patch("zerver.lib.events.always_want") as want_mock,
):
fetch_initial_state_data(user, realm=realm)
@@ -1251,7 +1251,7 @@ class FetchQueriesTest(ZulipTestCase):
starred_messages=1,
stream=5,
stop_words=0,
- subscription=6,
+ subscription=7,
update_display_settings=0,
update_global_notifications=0,
update_message_flags=5,
diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py
index a34d009976f..7c70279d179 100644
--- a/zerver/tests/test_events.py
+++ b/zerver/tests/test_events.py
@@ -99,7 +99,6 @@ from zerver.actions.streams import (
do_change_stream_group_based_setting,
do_change_stream_message_retention_days,
do_change_stream_permission,
- do_change_stream_post_policy,
do_change_subscription_property,
do_deactivate_stream,
do_rename_stream,
@@ -4541,14 +4540,6 @@ class SubscribeActionTest(BaseAction):
)
self.user_profile = self.example_user("hamlet")
- # Update stream stream_post_policy property
- with self.verify_action(include_subscribers=include_subscribers, num_events=3) as events:
- do_change_stream_post_policy(
- stream, Stream.STREAM_POST_POLICY_ADMINS, acting_user=self.example_user("hamlet")
- )
- check_stream_update("events[0]", events[0])
- check_message("events[2]", events[2])
-
with self.verify_action(include_subscribers=include_subscribers, num_events=2) as events:
do_change_stream_message_retention_days(stream, self.example_user("hamlet"), -1)
check_stream_update("events[0]", events[0])
@@ -4677,7 +4668,16 @@ class SubscribeActionTest(BaseAction):
is_system_group=True,
realm=self.user_profile.realm,
)
- with self.verify_action(include_subscribers=include_subscribers, num_events=1) as events:
+
+ num_events = 1
+ if setting_name == "can_send_message_group":
+ # Updating "can_send_message_group" also sends events
+ # for "stream_post_policy" and "is_announcement_value".
+ num_events = 3
+
+ with self.verify_action(
+ include_subscribers=include_subscribers, num_events=num_events
+ ) as events:
do_change_stream_group_based_setting(
stream,
setting_name,
@@ -4687,11 +4687,22 @@ class SubscribeActionTest(BaseAction):
check_stream_update("events[0]", events[0])
self.assertEqual(events[0]["value"], moderators_group.id)
+ if setting_name == "can_send_message_group":
+ check_stream_update("events[1]", events[1])
+ self.assertEqual(events[1]["property"], "stream_post_policy")
+ self.assertEqual(events[1]["value"], Stream.STREAM_POST_POLICY_MODERATORS)
+
+ check_stream_update("events[2]", events[2])
+ self.assertEqual(events[2]["property"], "is_announcement_only")
+ self.assertFalse(events[2]["value"])
+
setting_group = self.create_or_update_anonymous_group_for_setting(
[self.user_profile],
[moderators_group],
)
- with self.verify_action(include_subscribers=include_subscribers, num_events=1) as events:
+ with self.verify_action(
+ include_subscribers=include_subscribers, num_events=num_events
+ ) as events:
do_change_stream_group_based_setting(
stream,
setting_name,
@@ -4706,6 +4717,15 @@ class SubscribeActionTest(BaseAction):
),
)
+ if setting_name == "can_send_message_group":
+ check_stream_update("events[1]", events[1])
+ self.assertEqual(events[1]["property"], "stream_post_policy")
+ self.assertEqual(events[1]["value"], Stream.STREAM_POST_POLICY_EVERYONE)
+
+ check_stream_update("events[2]", events[2])
+ self.assertEqual(events[2]["property"], "is_announcement_only")
+ self.assertFalse(events[2]["value"])
+
def test_user_access_events_on_changing_subscriptions(self) -> None:
self.set_up_db_for_testing_user_access()
self.user_profile = self.example_user("polonius")
diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.py
index 82e8c1b0fef..02a5eed2bc4 100644
--- a/zerver/tests/test_home.py
+++ b/zerver/tests/test_home.py
@@ -274,7 +274,7 @@ class HomeTest(ZulipTestCase):
# Verify succeeds once logged-in
with (
- self.assert_database_query_count(53),
+ self.assert_database_query_count(54),
patch("zerver.lib.cache.cache_set") as cache_mock,
):
result = self._get_home_page(stream="Denmark")
@@ -579,7 +579,7 @@ class HomeTest(ZulipTestCase):
# Verify number of queries for Realm admin isn't much higher than for normal users.
self.login("iago")
with (
- self.assert_database_query_count(53),
+ self.assert_database_query_count(54),
patch("zerver.lib.cache.cache_set") as cache_mock,
):
result = self._get_home_page()
@@ -611,7 +611,7 @@ class HomeTest(ZulipTestCase):
self._get_home_page()
# Then for the second page load, measure the number of queries.
- with self.assert_database_query_count(48):
+ with self.assert_database_query_count(49):
result = self._get_home_page()
# Do a sanity check that our new streams were in the payload.
diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py
index 8506ab1af24..6ab10e4631c 100644
--- a/zerver/tests/test_subs.py
+++ b/zerver/tests/test_subs.py
@@ -3057,7 +3057,7 @@ class StreamAdminTest(ZulipTestCase):
are on.
"""
result = self.attempt_unsubscribe_of_principal(
- query_count=21,
+ query_count=19,
target_users=[self.example_user("cordelia")],
is_realm_admin=True,
is_subbed=True,
@@ -3074,7 +3074,7 @@ class StreamAdminTest(ZulipTestCase):
streams you aren't on.
"""
result = self.attempt_unsubscribe_of_principal(
- query_count=21,
+ query_count=19,
target_users=[self.example_user("cordelia")],
is_realm_admin=True,
is_subbed=False,
@@ -5105,7 +5105,7 @@ class SubscriptionAPITest(ZulipTestCase):
streams_to_sub = ["multi_user_stream"]
with (
self.capture_send_event_calls(expected_num_events=5) as events,
- self.assert_database_query_count(41),
+ self.assert_database_query_count(42),
):
self.common_subscribe_to_streams(
self.test_user,
@@ -5931,7 +5931,7 @@ class SubscriptionAPITest(ZulipTestCase):
]
# Test creating a public stream when realm does not have a notification stream.
- with self.assert_database_query_count(41):
+ with self.assert_database_query_count(42):
self.common_subscribe_to_streams(
self.test_user,
[new_streams[0]],
@@ -5939,7 +5939,7 @@ class SubscriptionAPITest(ZulipTestCase):
)
# Test creating private stream.
- with self.assert_database_query_count(45):
+ with self.assert_database_query_count(46):
self.common_subscribe_to_streams(
self.test_user,
[new_streams[1]],
@@ -5951,7 +5951,7 @@ class SubscriptionAPITest(ZulipTestCase):
new_stream_announcements_stream = get_stream(self.streams[0], self.test_realm)
self.test_realm.new_stream_announcements_stream_id = new_stream_announcements_stream.id
self.test_realm.save()
- with self.assert_database_query_count(52):
+ with self.assert_database_query_count(53):
self.common_subscribe_to_streams(
self.test_user,
[new_streams[2]],
@@ -6480,7 +6480,7 @@ class GetSubscribersTest(ZulipTestCase):
for user in [cordelia, othello, polonius]:
self.assert_user_got_subscription_notification(user, msg)
- with self.assert_database_query_count(6):
+ with self.assert_database_query_count(7):
subscribed_streams, _ = gather_subscriptions(
self.user_profile, include_subscribers=True
)
@@ -6515,7 +6515,7 @@ class GetSubscribersTest(ZulipTestCase):
acting_user=None,
)
- with self.assert_database_query_count(6):
+ with self.assert_database_query_count(7):
subscribed_streams, _ = gather_subscriptions(
self.user_profile, include_subscribers=True
)
@@ -6543,6 +6543,92 @@ class GetSubscribersTest(ZulipTestCase):
else:
self.assertEqual(sub["can_remove_subscribers_group"], admins_group.id)
+ def test_stream_post_policy_values_in_subscription_objects(self) -> None:
+ hamlet = self.example_user("hamlet")
+ cordelia = self.example_user("cordelia")
+ desdemona = self.example_user("desdemona")
+
+ streams = [f"stream_{i}" for i in range(6)]
+ for stream_name in streams:
+ self.make_stream(stream_name)
+
+ realm = hamlet.realm
+ self.common_subscribe_to_streams(
+ hamlet,
+ streams,
+ dict(principals=orjson.dumps([hamlet.id, cordelia.id]).decode()),
+ )
+
+ admins_group = NamedUserGroup.objects.get(
+ name=SystemGroups.ADMINISTRATORS, realm=realm, is_system_group=True
+ )
+ members_group = NamedUserGroup.objects.get(
+ name=SystemGroups.MEMBERS, realm=realm, is_system_group=True
+ )
+ full_members_group = NamedUserGroup.objects.get(
+ name=SystemGroups.FULL_MEMBERS, realm=realm, is_system_group=True
+ )
+
+ stream = get_stream("stream_1", realm)
+ do_change_stream_group_based_setting(
+ stream, "can_send_message_group", admins_group, acting_user=desdemona
+ )
+
+ stream = get_stream("stream_2", realm)
+ do_change_stream_group_based_setting(
+ stream, "can_send_message_group", members_group, acting_user=desdemona
+ )
+
+ stream = get_stream("stream_3", realm)
+ do_change_stream_group_based_setting(
+ stream, "can_send_message_group", full_members_group, acting_user=desdemona
+ )
+
+ hamletcharacters_group = NamedUserGroup.objects.get(name="hamletcharacters", realm=realm)
+ stream = get_stream("stream_4", realm)
+ do_change_stream_group_based_setting(
+ stream, "can_send_message_group", hamletcharacters_group, acting_user=desdemona
+ )
+
+ setting_group = self.create_or_update_anonymous_group_for_setting(
+ [cordelia], [admins_group]
+ )
+ stream = get_stream("stream_5", realm)
+ do_change_stream_group_based_setting(
+ stream, "can_send_message_group", setting_group, acting_user=desdemona
+ )
+
+ with self.assert_database_query_count(7):
+ subscribed_streams, _ = gather_subscriptions(hamlet, include_subscribers=True)
+
+ [stream_1_sub] = [sub for sub in subscribed_streams if sub["name"] == "stream_1"]
+ self.assertEqual(stream_1_sub["can_send_message_group"], admins_group.id)
+ self.assertEqual(stream_1_sub["stream_post_policy"], Stream.STREAM_POST_POLICY_ADMINS)
+
+ [stream_2_sub] = [sub for sub in subscribed_streams if sub["name"] == "stream_2"]
+ self.assertEqual(stream_2_sub["can_send_message_group"], members_group.id)
+ self.assertEqual(stream_2_sub["stream_post_policy"], Stream.STREAM_POST_POLICY_EVERYONE)
+
+ [stream_3_sub] = [sub for sub in subscribed_streams if sub["name"] == "stream_3"]
+ self.assertEqual(stream_3_sub["can_send_message_group"], full_members_group.id)
+ self.assertEqual(
+ stream_3_sub["stream_post_policy"], Stream.STREAM_POST_POLICY_RESTRICT_NEW_MEMBERS
+ )
+
+ [stream_4_sub] = [sub for sub in subscribed_streams if sub["name"] == "stream_4"]
+ self.assertEqual(stream_4_sub["can_send_message_group"], hamletcharacters_group.id)
+ self.assertEqual(stream_4_sub["stream_post_policy"], Stream.STREAM_POST_POLICY_EVERYONE)
+
+ [stream_5_sub] = [sub for sub in subscribed_streams if sub["name"] == "stream_5"]
+ self.assertEqual(
+ stream_5_sub["can_send_message_group"],
+ AnonymousSettingGroupDict(
+ direct_members=[cordelia.id],
+ direct_subgroups=[admins_group.id],
+ ),
+ )
+ self.assertEqual(stream_5_sub["stream_post_policy"], Stream.STREAM_POST_POLICY_EVERYONE)
+
def test_never_subscribed_streams(self) -> None:
"""
Check never_subscribed streams are fetched correctly and not include invite_only streams,
@@ -6608,7 +6694,7 @@ class GetSubscribersTest(ZulipTestCase):
create_private_streams()
def get_never_subscribed() -> list[NeverSubscribedStreamDict]:
- with self.assert_database_query_count(6):
+ with self.assert_database_query_count(7):
sub_data = gather_subscriptions_helper(self.user_profile)
self.verify_sub_fields(sub_data)
never_subscribed = sub_data.never_subscribed
@@ -6805,7 +6891,7 @@ class GetSubscribersTest(ZulipTestCase):
subdomain="zephyr",
)
- with self.assert_database_query_count(5):
+ with self.assert_database_query_count(6):
subscribed_streams, _ = gather_subscriptions(mit_user_profile, include_subscribers=True)
self.assertGreaterEqual(len(subscribed_streams), 2)