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: Send stream access events when privacy is changed.

Sahil Batraf4bfc05bef56
diff --git a/zerver/actions/streams.py b/zerver/actions/streams.py
index fc247e8c61b..864f3d381c4 100644
--- a/zerver/actions/streams.py
+++ b/zerver/actions/streams.py
@@ -42,7 +42,6 @@ from zerver.lib.streams import (
get_anonymous_group_membership_dict_for_streams,
get_stream_permission_policy_key,
get_stream_post_policy_value_based_on_group_setting,
- get_user_ids_with_metadata_access_via_permission_groups,
get_users_dict_with_metadata_access_to_streams_via_permission_groups,
render_stream_description,
send_stream_creation_event,
@@ -1277,6 +1276,10 @@ def do_change_stream_permission(
old_history_public_to_subscribers_value = stream.history_public_to_subscribers
old_is_web_public_value = stream.is_web_public
+ old_user_ids_with_metadata_access: set[int] = set()
+ if old_invite_only_value != invite_only or old_is_web_public_value != is_web_public:
+ old_user_ids_with_metadata_access = can_access_stream_metadata_user_ids(stream)
+
stream.is_web_public = is_web_public
stream.invite_only = invite_only
stream.history_public_to_subscribers = history_public_to_subscribers
@@ -1351,56 +1354,44 @@ def do_change_stream_permission(
},
)
- notify_stream_creation_ids = set()
- if old_invite_only_value and not stream.invite_only:
- # We need to send stream creation event to users who can access the
- # stream now but were not able to do so previously. So, we can exclude
- # subscribers and realm admins from the non-guest user list.
- stream_subscriber_user_ids = get_active_subscriptions_for_stream_id(
- stream.id, include_deactivated_users=False
- ).values_list("user_profile_id", flat=True)
-
- old_can_access_stream_metadata_user_ids = set(stream_subscriber_user_ids) | {
- user.id for user in stream.realm.get_admin_users_and_bots()
- }
- user_ids_with_metadata_access_via_permission_groups = (
- get_user_ids_with_metadata_access_via_permission_groups(stream)
- )
- non_guest_user_ids = set(active_non_guest_user_ids(stream.realm_id))
- notify_stream_creation_ids = (
- non_guest_user_ids
- - old_can_access_stream_metadata_user_ids
- - user_ids_with_metadata_access_via_permission_groups
+ current_user_ids_with_metadata_access = can_access_stream_metadata_user_ids(stream)
+ user_ids_gaining_metadata_access = set()
+ if (
+ old_invite_only_value != stream.invite_only
+ or old_is_web_public_value != stream.is_web_public
+ ):
+ user_ids_gaining_metadata_access = (
+ current_user_ids_with_metadata_access - old_user_ids_with_metadata_access
)
+ if user_ids_gaining_metadata_access:
+ recent_traffic = get_streams_traffic(realm, {stream.id})
+ anonymous_group_membership = get_anonymous_group_membership_dict_for_streams([stream])
+ send_stream_creation_event(
+ realm,
+ stream,
+ list(user_ids_gaining_metadata_access),
+ recent_traffic,
+ anonymous_group_membership,
+ )
- recent_traffic = get_streams_traffic(realm, {stream.id})
- anonymous_group_membership = get_anonymous_group_membership_dict_for_streams([stream])
- send_stream_creation_event(
- realm,
- stream,
- list(notify_stream_creation_ids),
- recent_traffic,
- anonymous_group_membership,
- )
+ # Add subscribers info to the stream object. We need to send peer_add
+ # events to users who did not have metadata access to the stream previously.
+ stream_subscriber_user_ids = get_active_subscriptions_for_stream_id(
+ stream.id, include_deactivated_users=False
+ ).values_list("user_profile_id", flat=True)
+ peer_add_event = dict(
+ type="subscription",
+ op="peer_add",
+ stream_ids=[stream.id],
+ user_ids=sorted(stream_subscriber_user_ids),
+ )
+ send_event_on_commit(stream.realm, peer_add_event, user_ids_gaining_metadata_access)
- # Add subscribers info to the stream object. We need to send peer_add
- # events to users who were previously subscribed to the streams as
- # they did not had subscribers data.
- old_subscribers_access_user_ids = set(stream_subscriber_user_ids) | {
- user.id for user in stream.realm.get_admin_users_and_bots()
- }
- peer_notify_user_ids = (
- non_guest_user_ids
- - old_subscribers_access_user_ids
- - user_ids_with_metadata_access_via_permission_groups
- )
- peer_add_event = dict(
- type="subscription",
- op="peer_add",
- stream_ids=[stream.id],
- user_ids=sorted(stream_subscriber_user_ids),
+ user_ids_losing_metadata_access = (
+ old_user_ids_with_metadata_access - current_user_ids_with_metadata_access
)
- send_event_on_commit(stream.realm, peer_add_event, peer_notify_user_ids)
+ if user_ids_losing_metadata_access:
+ send_stream_deletion_event(stream.realm, user_ids_losing_metadata_access, [stream])
event = dict(
op="update",
@@ -1415,7 +1406,7 @@ def do_change_stream_permission(
# we do not need to send update events to the users who received creation event
# since they already have the updated stream info.
notify_stream_update_ids = (
- can_access_stream_metadata_user_ids(stream) - notify_stream_creation_ids
+ current_user_ids_with_metadata_access - user_ids_gaining_metadata_access
)
send_event_on_commit(stream.realm, event, notify_stream_update_ids)
diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py
index 640d52f5ed7..922109dfc45 100644
--- a/zerver/tests/test_subs.py
+++ b/zerver/tests/test_subs.py
@@ -747,6 +747,172 @@ class StreamAdminTest(ZulipTestCase):
}
self.assertEqual(realm_audit_log.extra_data, expected_extra_data)
+ def test_events_when_changing_stream_privacy(self) -> None:
+ desdemona = self.example_user("desdemona")
+ iago = self.example_user("iago")
+ cordelia = self.example_user("cordelia")
+ othello = self.example_user("othello")
+ polonius = self.example_user("polonius")
+ hamlet = self.example_user("hamlet")
+
+ anonymous_group = UserGroupMembersData(
+ direct_members=[hamlet.id],
+ direct_subgroups=[],
+ )
+
+ realm = iago.realm
+ stream = self.make_stream("test_stream", realm)
+ self.subscribe(othello, stream.name)
+ self.subscribe(desdemona, stream.name)
+ do_change_stream_group_based_setting(
+ stream, "can_administer_channel_group", anonymous_group, acting_user=desdemona
+ )
+
+ # These users will always have metadata access to the stream - Iago (realm admin),
+ # Othello (subscriber) and Hamlet (member of can_administer_channel_group).
+ users_with_metadata_access_always = [othello.id, iago.id, hamlet.id]
+
+ # Test when changing stream from public to private.
+ params = {
+ "is_private": orjson.dumps(True).decode(),
+ }
+ self.login("desdemona")
+ with self.capture_send_event_calls(expected_num_events=3) as events:
+ result = self.client_patch(f"/json/streams/{stream.id}", params)
+ self.assert_json_success(result)
+ stream.refresh_from_db()
+ self.assertEqual(stream.invite_only, True)
+
+ delete_event = events[0]
+ self.assertEqual(delete_event["event"]["type"], "stream")
+ self.assertEqual(delete_event["event"]["op"], "delete")
+ update_event = events[1]
+ self.assertEqual(update_event["event"]["type"], "stream")
+ self.assertEqual(update_event["event"]["op"], "update")
+
+ # Cordelia loses access to the stream.
+ self.assertIn(cordelia.id, delete_event["users"])
+ self.assertNotIn(cordelia.id, update_event["users"])
+ # Polonius did not have access initially as well
+ # so does not receive any event.
+ self.assertNotIn(polonius.id, delete_event["users"])
+ self.assertNotIn(polonius.id, update_event["users"])
+
+ # Users having metadata access originally receive only
+ # update event.
+ for user_id in users_with_metadata_access_always:
+ self.assertNotIn(user_id, delete_event["users"])
+ self.assertIn(user_id, update_event["users"])
+
+ # Test when changing stream from private to public.
+ params = {
+ "is_private": orjson.dumps(False).decode(),
+ }
+ with self.capture_send_event_calls(expected_num_events=4) as events:
+ result = self.client_patch(f"/json/streams/{stream.id}", params)
+ self.assert_json_success(result)
+
+ create_event = events[0]
+ self.assertEqual(create_event["event"]["type"], "stream")
+ self.assertEqual(create_event["event"]["op"], "create")
+ peer_add_event = events[1]
+ self.assertEqual(peer_add_event["event"]["type"], "subscription")
+ self.assertEqual(peer_add_event["event"]["op"], "peer_add")
+ update_event = events[2]
+ self.assertEqual(update_event["event"]["type"], "stream")
+ self.assertEqual(update_event["event"]["op"], "update")
+
+ # Cordelia gains access to the stream and receives a peer_add
+ # event as well to obtain subscriber data, but does not
+ # receive the update event as they already receive the latest
+ # data from stream creation event.
+ self.assertIn(cordelia.id, create_event["users"])
+ self.assertIn(cordelia.id, peer_add_event["users"])
+ self.assertNotIn(cordelia.id, update_event["users"])
+ # Polonius still cannot access an unsubscribed public stream.
+ self.assertNotIn(polonius.id, create_event["users"])
+ self.assertNotIn(polonius.id, peer_add_event["users"])
+ self.assertNotIn(polonius.id, update_event["users"])
+
+ # Users having metadata access originally receive only
+ # update event.
+ for user_id in users_with_metadata_access_always:
+ self.assertNotIn(user_id, create_event["users"])
+ self.assertNotIn(user_id, peer_add_event["users"])
+ self.assertIn(user_id, update_event["users"])
+
+ # Test when changing stream from public to web-public.
+ params = {"is_web_public": orjson.dumps(True).decode()}
+ with self.capture_send_event_calls(expected_num_events=4) as events:
+ result = self.client_patch(f"/json/streams/{stream.id}", params)
+ self.assert_json_success(result)
+ stream.refresh_from_db()
+ self.assertEqual(stream.is_web_public, True)
+
+ create_event = events[0]
+ self.assertEqual(create_event["event"]["type"], "stream")
+ self.assertEqual(create_event["event"]["op"], "create")
+ peer_add_event = events[1]
+ self.assertEqual(peer_add_event["event"]["type"], "subscription")
+ self.assertEqual(peer_add_event["event"]["op"], "peer_add")
+ update_event = events[2]
+ self.assertEqual(update_event["event"]["type"], "stream")
+ self.assertEqual(update_event["event"]["op"], "update")
+
+ # Polonius gains access to the stream and receives a peer_add
+ # event as well to obtain subscriber data, but does not
+ # receive the update event as they already receive the latest
+ # data from stream creation event.
+ self.assertIn(polonius.id, create_event["users"])
+ self.assertIn(polonius.id, peer_add_event["users"])
+ self.assertNotIn(polonius.id, update_event["users"])
+
+ # Cordelia already had access to the stream so receives only
+ # update event.
+ self.assertNotIn(cordelia.id, create_event["users"])
+ self.assertNotIn(cordelia.id, peer_add_event["users"])
+ self.assertIn(cordelia.id, update_event["users"])
+
+ # Users having metadata access originally receive only
+ # update event.
+ for user_id in users_with_metadata_access_always:
+ self.assertNotIn(user_id, create_event["users"])
+ self.assertNotIn(user_id, peer_add_event["users"])
+ self.assertIn(user_id, update_event["users"])
+
+ # Test when changing stream from web-public to private.
+ params = {
+ "is_web_public": orjson.dumps(False).decode(),
+ "is_private": orjson.dumps(True).decode(),
+ }
+ with self.capture_send_event_calls(expected_num_events=3) as events:
+ result = self.client_patch(f"/json/streams/{stream.id}", params)
+ self.assert_json_success(result)
+ stream.refresh_from_db()
+ self.assertEqual(stream.is_web_public, False)
+ self.assertEqual(stream.invite_only, True)
+
+ delete_event = events[0]
+ self.assertEqual(delete_event["event"]["type"], "stream")
+ self.assertEqual(delete_event["event"]["op"], "delete")
+
+ update_event = events[1]
+ self.assertEqual(update_event["event"]["type"], "stream")
+ self.assertEqual(update_event["event"]["op"], "update")
+
+ # Both cordelia and polonius lose access to the stream.
+ self.assertIn(polonius.id, delete_event["users"])
+ self.assertIn(cordelia.id, delete_event["users"])
+ self.assertNotIn(polonius.id, update_event["users"])
+ self.assertNotIn(cordelia.id, update_event["users"])
+
+ # Users having metadata access originally receive only
+ # update event.
+ for user_id in users_with_metadata_access_always:
+ self.assertNotIn(user_id, create_event["users"])
+ self.assertNotIn(user_id, peer_add_event["users"])
+ self.assertIn(user_id, update_event["users"])
+
def test_updating_protected_history_and_can_create_topic_group_for_streams(self) -> None:
user_profile = self.example_user("iago")
self.login_user(user_profile)