Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
streams: Send different peer events for web & non-web public streams.
Shubham Padia1014de23a8e8
diff --git a/zerver/actions/streams.py b/zerver/actions/streams.pyindex d1827312898..22dd9c0abe0 100644--- a/zerver/actions/streams.py+++ b/zerver/actions/streams.py@@ -633,9 +633,10 @@ def send_peer_subscriber_events(# to multiple default public streams during# new-user registration.#- # This optimization depends on all public streams- # having the same peers for any single user, which- # isn't the case for private streams.+ # This optimization works on web public streams and+ # public streams with no guest users since they+ # will have the same peers for any single user,+ # which isn't the case for private streams.[altered_user_id] = altered_user_idsuser_streams[altered_user_id].add(stream_id)else:@@ -648,18 +649,46 @@ def send_peer_subscriber_events(send_event_on_commit(realm, event, peer_user_ids)for user_id, stream_ids in user_streams.items():- if stream_id in web_public_stream_ids:- peer_user_ids = web_public_peer_ids - altered_user_ids- else:- peer_user_ids = (non_guest_user_ids | subscriber_dict[stream_id]) - altered_user_ids-- event = dict(- type="subscription",- op=op,- stream_ids=sorted(stream_ids),- user_ids=[user_id],- )- send_event_on_commit(realm, event, peer_user_ids)+ web_public_user_stream_ids = []+ public_user_stream_ids_without_guest_users = []+ for stream_id in stream_ids:+ if stream_id in web_public_stream_ids:+ web_public_user_stream_ids.append(stream_id)+ else:+ if (non_guest_user_ids | subscriber_dict[stream_id]) == non_guest_user_ids:+ public_user_stream_ids_without_guest_users.append(stream_id)+ else:+ # If the stream has guest users, we will send+ # an event right away.+ peer_user_ids = (non_guest_user_ids | subscriber_dict[stream_id]) - {+ user_id+ }+ event = dict(+ type="subscription",+ op=op,+ stream_ids=[stream_id],+ user_ids=[user_id],+ )+ send_event_on_commit(realm, event, peer_user_ids)++ if len(web_public_user_stream_ids) > 0:+ web_public_streams_event = dict(+ type="subscription",+ op=op,+ stream_ids=sorted(web_public_user_stream_ids),+ user_ids=[user_id],+ )+ send_event_on_commit(+ realm, web_public_streams_event, web_public_peer_ids - {user_id}+ )+ if len(public_user_stream_ids_without_guest_users) > 0:+ public_streams_event = dict(+ type="subscription",+ op=op,+ stream_ids=sorted(public_user_stream_ids_without_guest_users),+ user_ids=[user_id],+ )+ send_event_on_commit(realm, public_streams_event, non_guest_user_ids - {user_id})def send_user_creation_events_on_adding_subscriptions(diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.pyindex 6c35126a0e9..2a82e83527c 100644--- a/zerver/tests/test_events.py+++ b/zerver/tests/test_events.py@@ -1834,20 +1834,21 @@ class NormalActionsTest(BaseAction):realm.signup_announcements_stream = get_stream("core team", realm)realm.save(update_fields=["signup_announcements_stream"])- with self.verify_action(num_events=5) as events:+ with self.verify_action(num_events=6) as events:self.register("[email protected]", "test1")- self.assert_length(events, 5)+ self.assert_length(events, 6)check_realm_user_add("events[0]", events[0])new_user_profile = get_user_by_delivery_email("[email protected]", self.user_profile.realm)self.assertEqual(new_user_profile.delivery_email, "[email protected]")check_subscription_peer_add("events[3]", events[3])+ check_subscription_peer_add("events[4]", events[4])- check_message("events[4]", events[4])+ check_message("events[5]", events[5])self.assertIn(f'data-user-id="{new_user_profile.id}">test1_zulip.com</span> joined this organization.',- events[4]["message"]["content"],+ events[5]["message"]["content"],)check_user_group_add_members("events[1]", events[1])@@ -1865,19 +1866,20 @@ class NormalActionsTest(BaseAction):realm.signup_announcements_stream = get_stream("core team", realm)realm.save(update_fields=["signup_announcements_stream"])- with self.verify_action(num_events=5) as events:+ with self.verify_action(num_events=6) as events:self.register("[email protected]", "test1")- self.assert_length(events, 5)+ self.assert_length(events, 6)check_realm_user_add("events[0]", events[0])new_user_profile = get_user_by_delivery_email("[email protected]", self.user_profile.realm)self.assertEqual(new_user_profile.email, f"user{new_user_profile.id}@zulip.testserver")check_subscription_peer_add("events[3]", events[3])+ check_subscription_peer_add("events[4]", events[4])- check_message("events[4]", events[4])+ check_message("events[5]", events[5])self.assertIn(f'data-user-id="{new_user_profile.id}">test1_zulip.com</span> joined this organization',- events[4]["message"]["content"],+ events[5]["message"]["content"],)check_user_group_add_members("events[1]", events[1])@@ -3479,6 +3481,7 @@ class NormalActionsTest(BaseAction):name="hamletcharacters", realm=self.user_profile.realm)hamlet = self.example_user("hamlet")+ self.user_profile = hamletsetting_group = self.create_or_update_anonymous_group_for_setting([user_profile, hamlet], [members_group])@@ -3575,16 +3578,17 @@ class NormalActionsTest(BaseAction):self.make_stream("Test new stream")self.subscribe(user_profile, "Test new stream")self.subscribe(self.user_profile, "Test new stream")- with self.verify_action(num_events=6) as events:+ with self.verify_action(num_events=7) as events:do_deactivate_user(user_profile, acting_user=None)check_subscription_peer_remove("events[0]", events[0])- check_user_group_remove_members("events[1]", events[1])+ check_subscription_peer_remove("events[1]", events[1])check_user_group_remove_members("events[2]", events[2])check_user_group_remove_members("events[3]", events[3])- check_user_group_update("events[4]", events[4], {"can_mention_group"})- check_realm_user_remove("events[5]]", events[5])+ check_user_group_remove_members("events[4]", events[4])+ check_user_group_update("events[5]", events[5], {"can_mention_group"})+ check_realm_user_remove("events[6]]", events[6])self.assertEqual(- events[4]["data"]["can_mention_group"],+ events[5]["data"]["can_mention_group"],UserGroupMembersDict(direct_members=[], direct_subgroups=[members_group.id]),)diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.pyindex d9d6ae18abd..b16a86be3f6 100644--- a/zerver/tests/test_subs.py+++ b/zerver/tests/test_subs.py@@ -6402,6 +6402,9 @@ class SubscriptionAPITest(ZulipTestCase):stream1 = self.make_stream("stream1")stream2 = self.make_stream("stream2")stream3 = self.make_stream("stream3")+ stream4 = self.make_stream("stream4")+ stream5 = self.make_stream("stream5", is_web_public=True)+ stream6 = self.make_stream("stream6", is_web_public=True)private = self.make_stream("private_stream", invite_only=True)self.subscribe(user1, "stream1")@@ -6410,6 +6413,11 @@ class SubscriptionAPITest(ZulipTestCase):self.subscribe(user2, "stream2")self.subscribe(user2, "stream3")+ self.subscribe(user2, "stream4")+ self.subscribe(user2, "stream5")+ self.subscribe(user2, "stream6")++ self.subscribe(guest, "stream4")self.subscribe(user1, "private_stream")self.subscribe(user2, "private_stream")@@ -6442,14 +6450,14 @@ class SubscriptionAPITest(ZulipTestCase):# Sends 3 peer-remove events, 2 unsubscribe events# and 2 stream delete events for private streams.with (- self.assert_database_query_count(19),- self.assert_memcached_count(3),- self.capture_send_event_calls(expected_num_events=7) as events,+ self.assert_database_query_count(23),+ self.assert_memcached_count(4),+ self.capture_send_event_calls(expected_num_events=9) as events,):bulk_remove_subscriptions(realm,[user1, user2],- [stream1, stream2, stream3, private],+ [stream1, stream2, stream3, stream4, stream5, stream6, private],acting_user=None,)@@ -6501,11 +6509,50 @@ class SubscriptionAPITest(ZulipTestCase):{user1.id, user2.id},{user3.id, user4.id, user5.id, user6.id, user7.id, user8.id},),+ # stream2 and stream3 are non-guest public streams,+ # remove peer events for them will be sent together as+ # a separate event since they will have the same peer+ # user ids.("stream2,stream3",{user2.id},{user1.id, user3.id, user4.id, user5.id, user6.id, user7.id, user8.id},),+ # stream4 has a guest user and a different set of peer+ # user ids than a non-guest public channel and it thus+ # gets its own event.+ (+ "stream4",+ {user2.id},+ {+ user1.id,+ user3.id,+ user4.id,+ user5.id,+ user6.id,+ user7.id,+ user8.id,+ guest.id,+ },+ ),+ # stream5 and stream6 are web public streams, remove+ # peer events for them will be sent together as a+ # separate event since they will have the same peer+ # user ids.+ (+ "stream5,stream6",+ {user2.id},+ {+ user1.id,+ user3.id,+ user4.id,+ user5.id,+ user6.id,+ user7.id,+ user8.id,+ guest.id,+ },+ ),],)diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex 35ad0e076b7..6ada4eb428d 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -1002,7 +1002,7 @@ class QueryCountTest(ZulipTestCase):with (self.assert_database_query_count(86),self.assert_memcached_count(20),- self.capture_send_event_calls(expected_num_events=10) as events,+ self.capture_send_event_calls(expected_num_events=11) as events,):fred = do_create_user(email="[email protected]",@@ -1023,7 +1023,7 @@ class QueryCountTest(ZulipTestCase):notifications.add(",".join(stream_names))self.assertEqual(- notifications, {"Denmark,Scotland,Verona", "private_stream1", "private_stream2"}+ notifications, {"private_stream1", "private_stream2", "Verona", "Denmark,Scotland"})