Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
streams: Include users reachable via multiple permission groups.
Evy Kassirerd0bc89001f68
diff --git a/zerver/lib/streams.py b/zerver/lib/streams.pyindex 977d7dac982..8e3d1780546 100644--- a/zerver/lib/streams.py+++ b/zerver/lib/streams.py@@ -308,15 +308,15 @@ def get_users_dict_with_metadata_access_to_streams_via_permission_groups(recursive_subgroups = get_root_id_annotated_recursive_subgroups_for_groups(all_permission_group_ids, realm_id)- subgroup_root_id_dict = {}- all_subgroup_ids = set()+ # A group can be a subgroup of several permission groups, so map+ # each subgroup to every root it is reachable from.+ subgroup_root_ids_dict: dict[int, set[int]] = defaultdict(set)for group in recursive_subgroups:- subgroup_root_id_dict[group.id] = group.root_id # type: ignore[attr-defined] # root_id is an annotated field.- all_subgroup_ids.add(group.id)+ subgroup_root_ids_dict[group.id].add(group.root_id) # type: ignore[attr-defined] # root_id is an annotated field.group_members = (UserGroupMembership.objects.filter(- user_group_id__in=list(all_subgroup_ids), user_profile__is_active=True+ user_group_id__in=list(subgroup_root_ids_dict), user_profile__is_active=True).exclude(# allow_everyone_group=False is false for both@@ -329,8 +329,8 @@ def get_users_dict_with_metadata_access_to_streams_via_permission_groups()group_members_dict = defaultdict(set)for user_group_id, user_profile_id in group_members:- root_id = subgroup_root_id_dict[user_group_id]- group_members_dict[root_id].add(user_profile_id)+ for root_id in subgroup_root_ids_dict[user_group_id]:+ group_members_dict[root_id].add(user_profile_id)users_with_metadata_access_dict = defaultdict(set)for stream in streams:diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.pyindex ab509246bb5..eb6cb71fcda 100644--- a/zerver/tests/test_subs.py+++ b/zerver/tests/test_subs.py@@ -4765,6 +4765,45 @@ class SubscriptionAPITest(ZulipTestCase):self.assert_length(event_stream_objects, 1)self.assertEqual(event_stream_objects[0]["stream_id"], private.id)+ def test_users_getting_remove_peer_event_via_nested_permission_groups(self) -> None:+ # Moderators have metadata access to the first channel because+ # the moderators group is a subgroup of the members group, and+ # to the second channel via the moderators group directly. When+ # both channels are processed together, moderators must be+ # notified about both.+ hamlet = self.example_user("hamlet")+ shiva = self.example_user("shiva")+ realm = hamlet.realm+ members_group = NamedUserGroup.objects.get(+ name=SystemGroups.MEMBERS, realm_for_sharding=realm, is_system_group=True+ )+ moderators_group = NamedUserGroup.objects.get(+ name=SystemGroups.MODERATORS, realm_for_sharding=realm, is_system_group=True+ )++ members_can_subscribe = self.make_stream("members_can_subscribe", invite_only=True)+ do_change_stream_group_based_setting(+ members_can_subscribe, "can_subscribe_group", members_group, acting_user=hamlet+ )+ moderators_can_add = self.make_stream("moderators_can_add", invite_only=True)+ do_change_stream_group_based_setting(+ moderators_can_add, "can_add_subscribers_group", moderators_group, acting_user=hamlet+ )+ self.subscribe(hamlet, members_can_subscribe.name)+ self.subscribe(hamlet, moderators_can_add.name)++ with self.capture_send_event_calls(expected_num_events=6) as events:+ do_deactivate_user(hamlet, acting_user=None)++ peer_events = [e for e in events if e["event"].get("op") == "peer_remove"]+ for stream in [members_can_subscribe, moderators_can_add]:+ notified_user_ids: set[int] = set()+ for event in peer_events:+ if stream.id in event["event"]["stream_ids"]:+ self.assertEqual(event["event"]["user_ids"], [hamlet.id])+ notified_user_ids |= set(event["users"])+ self.assertIn(shiva.id, notified_user_ids)+def test_bulk_subscribe_many(self) -> None:# Create a whole bunch of streamsstreams = [f"stream_{i}" for i in range(30)]