Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
streams: Send stream deletion events on unsubscribing users.
Sahil Batradc9813634675
diff --git a/zerver/actions/streams.py b/zerver/actions/streams.pyindex 6a25509df31..869e593451f 100644--- a/zerver/actions/streams.py+++ b/zerver/actions/streams.py@@ -38,6 +38,7 @@ from zerver.lib.stream_subscription import (from zerver.lib.stream_traffic import get_streams_trafficfrom zerver.lib.streams import (can_access_stream_user_ids,+ check_basic_stream_access,get_occupied_streams,get_stream_permission_policy_name,render_stream_description,@@ -793,6 +794,20 @@ def send_subscription_remove_events(}queue_event_on_commit("deferred_work", event)+ if not user_profile.is_realm_admin:+ inaccessible_streams = [+ stream+ for stream in streams_by_user[user_profile.id]+ if not check_basic_stream_access(+ user_profile, stream, sub=None, allow_realm_admin=True+ )+ ]++ if inaccessible_streams:+ payload = [stream_to_dict(stream) for stream in inaccessible_streams]+ event = dict(type="stream", op="delete", streams=payload)+ send_event_on_commit(realm, event, [user_profile.id])+send_peer_remove_events(realm=realm,streams=streams,diff --git a/zerver/lib/events.py b/zerver/lib/events.pyindex e4299f14617..8dde4835375 100644--- a/zerver/lib/events.py+++ b/zerver/lib/events.py@@ -1052,6 +1052,13 @@ def apply_event(state["streams"] = [s for s in state["streams"] if s["stream_id"] not in deleted_stream_ids]++ state["unsubscribed"] = [+ stream+ for stream in state["unsubscribed"]+ if stream["stream_id"] not in deleted_stream_ids+ ]+state["never_subscribed"] = [streamfor stream in state["never_subscribed"]diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yamlindex 57dacb95296..7a332fc810c 100644--- a/zerver/openapi/zulip.yaml+++ b/zerver/openapi/zulip.yaml@@ -1299,8 +1299,9 @@ paths:Event sent to all users who can see a stream when it is deactivated.This event is also sent when a user loses access to a stream they- previously [could access](/help/stream-permissions) because their- [role](/help/roles-and-permissions) has changed.+ previously [could access](/help/stream-permissions) because they+ are unsubscribed from a private stream or their [role](/help/roles-and-permissions)+ has changed.**Changes**: Prior to Zulip 8.0 (feature level 205), this event wasnot sent when a user lost access to a stream due to their rolediff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.pyindex 740f6cb0f24..202419b8abc 100644--- a/zerver/tests/test_events.py+++ b/zerver/tests/test_events.py@@ -3784,6 +3784,15 @@ class SubscribeActionTest(BaseAction):)self.assertIsNone(events[0]["streams"][0]["stream_weekly_traffic"])+ # Add this user to make sure the stream is not deleted on unsubscribing hamlet.+ self.subscribe(self.example_user("iago"), stream.name)++ # Unsubscribe from invite-only stream.+ action = lambda: bulk_remove_subscriptions(realm, [hamlet], [stream], acting_user=None)+ events = self.verify_action(action, include_subscribers=include_subscribers, num_events=2)+ check_subscription_remove("events[0]", events[0])+ check_stream_delete("events[1]", events[1])+stream.invite_only = Falsestream.save()@@ -3840,6 +3849,14 @@ class SubscribeActionTest(BaseAction):)check_subscription_peer_remove("events[0]", events[0])+ # Unsubscribe guest from public stream.+ action = lambda: bulk_remove_subscriptions(+ realm, [self.user_profile], [stream], acting_user=None+ )+ events = self.verify_action(action, include_subscribers=include_subscribers, num_events=2)+ check_subscription_remove("events[0]", events[0])+ check_stream_delete("events[1]", events[1])+stream = self.make_stream("web-public-stream", self.user_profile.realm, is_web_public=True)# Guest user receives peer_add/peer_remove events for unsubscribed# web-public streams.@@ -3869,6 +3886,14 @@ class SubscribeActionTest(BaseAction):events = self.verify_action(action, include_subscribers=include_subscribers, num_events=1)check_subscription_add("events[0]", events[0])+ # Unsubscribe as a guest to web-public stream. Guest does not receive stream deletion+ # event for web-public stream.+ action = lambda: bulk_remove_subscriptions(+ user_profile.realm, [self.user_profile], [stream], acting_user=None+ )+ events = self.verify_action(action, include_subscribers=include_subscribers, num_events=1)+ check_subscription_remove("events[0]", events[0])+class DraftActionTest(BaseAction):def do_enable_drafts_synchronization(self, user_profile: UserProfile) -> None:diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.pyindex 37e6553328a..16f64672ce3 100644--- a/zerver/tests/test_subs.py+++ b/zerver/tests/test_subs.py@@ -4912,10 +4912,11 @@ class SubscriptionAPITest(ZulipTestCase):self.subscribe(user2, "private_stream")self.subscribe(user3, "private_stream")- # Sends 3 peer-remove events and 2 unsubscribe events.+ # Sends 3 peer-remove events, 2 unsubscribe events+ # and 2 stream delete events for private streams.with self.assert_database_query_count(16):with self.assert_memcached_count(3):- with self.capture_send_event_calls(expected_num_events=5) as events:+ with self.capture_send_event_calls(expected_num_events=7) as events:bulk_remove_subscriptions(realm,[user1, user2],@@ -4924,6 +4925,11 @@ class SubscriptionAPITest(ZulipTestCase):)peer_events = [e for e in events if e["event"].get("op") == "peer_remove"]+ stream_delete_events = [+ e+ for e in events+ if e["event"].get("type") == "stream" and e["event"].get("op") == "delete"+ ]# We only care about a subset of users when we inspect# peer_remove events.@@ -4955,6 +4961,14 @@ class SubscriptionAPITest(ZulipTestCase):],)+ self.assert_length(stream_delete_events, 2)+ self.assertEqual(stream_delete_events[0]["users"], [user1.id])+ self.assertEqual(stream_delete_events[1]["users"], [user2.id])+ for event in stream_delete_events:+ event_streams = event["event"]["streams"]+ self.assert_length(event_streams, 1)+ self.assertEqual(event_streams[0]["name"], "private_stream")+def test_bulk_subscribe_MIT(self) -> None:mit_user = self.mit_user("starnine")num_streams = 15@@ -4986,7 +5000,7 @@ class SubscriptionAPITest(ZulipTestCase):# Followed by one subscription event:self.assertEqual(events[num_streams]["event"]["type"], "subscription")- with self.capture_send_event_calls(expected_num_events=1):+ with self.capture_send_event_calls(expected_num_events=2):bulk_remove_subscriptions(realm,users=[mit_user],