Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
events: Optimize computing can_create_streams and related fields.
Sahil Batrab44d30add729
diff --git a/zerver/lib/events.py b/zerver/lib/events.pyindex 5e79c43bb71..cccf2e14f5f 100644--- a/zerver/lib/events.py+++ b/zerver/lib/events.py@@ -58,6 +58,7 @@ from zerver.lib.timezone import canonicalize_timezonefrom zerver.lib.topic import TOPIC_NAMEfrom zerver.lib.user_groups import (get_group_setting_value_for_api,+ get_recursive_membership_groups,get_server_supported_permission_settings,user_groups_in_realm_serialized,)@@ -548,8 +549,17 @@ def fetch_initial_state_data(client_gravatar=False,)- state["can_create_private_streams"] = settings_user.can_create_private_streams(realm)- state["can_create_public_streams"] = settings_user.can_create_public_streams(realm)+ settings_user_recursive_group_ids = set(+ get_recursive_membership_groups(settings_user).values_list("id", flat=True)+ )++ state["can_create_private_streams"] = (+ realm.can_create_private_channel_group_id in settings_user_recursive_group_ids+ )+ state["can_create_public_streams"] = (+ realm.can_create_public_channel_group_id in settings_user_recursive_group_ids+ )+state["can_create_web_public_streams"] = settings_user.can_create_web_public_streams()# TODO/compatibility: Deprecated in Zulip 5.0 (feature level# 102); we can remove this once we no longer need to supportdiff --git a/zerver/tests/test_event_system.py b/zerver/tests/test_event_system.pyindex da279d58f8d..5ab5f9f01d2 100644--- a/zerver/tests/test_event_system.py+++ b/zerver/tests/test_event_system.py@@ -1170,7 +1170,7 @@ class FetchQueriesTest(ZulipTestCase):# count in production.realm = get_realm_with_settings(realm_id=user.realm_id)- with self.assert_database_query_count(44):+ with self.assert_database_query_count(43):with mock.patch("zerver.lib.events.always_want") as want_mock:fetch_initial_state_data(user, realm=realm)@@ -1194,7 +1194,7 @@ class FetchQueriesTest(ZulipTestCase):realm_filters=0,realm_linkifiers=0,realm_playgrounds=1,- realm_user=5,+ realm_user=4,realm_user_groups=5,realm_user_settings_defaults=1,recent_private_conversations=1,diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.pyindex 127a598fb23..d0c5aa3ed34 100644--- a/zerver/tests/test_home.py+++ b/zerver/tests/test_home.py@@ -260,7 +260,7 @@ class HomeTest(ZulipTestCase):self.client_post("/json/bots", bot_info)# Verify succeeds once logged-in- with self.assert_database_query_count(55):+ with self.assert_database_query_count(54):with patch("zerver.lib.cache.cache_set") as cache_mock:result = self._get_home_page(stream="Denmark")self.check_rendered_logged_in_app(result)@@ -564,7 +564,7 @@ class HomeTest(ZulipTestCase):def test_num_queries_for_realm_admin(self) -> None:# Verify number of queries for Realm admin isn't much higher than for normal users.self.login("iago")- with self.assert_database_query_count(55):+ with self.assert_database_query_count(54):with patch("zerver.lib.cache.cache_set") as cache_mock:result = self._get_home_page()self.check_rendered_logged_in_app(result)@@ -595,7 +595,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(50):+ with self.assert_database_query_count(49):result = self._get_home_page()# Do a sanity check that our new streams were in the payload.