events: Optimize computing can_create_streams and related fields.

Currently, for computing fields like can_create_public_streams
and can_create_private_steams fields, is_user_in_group is called
to check whether the user is part of the group which has the
permission. This means that there will be one DB query for each
field.

To optimize this, we now first fetch all the groups that the
user is member of, including the anonymous groups which are
used for settings, such that we can then just check whether
the user is part of the group which has the permission meaning
we would need only one query to compute all the fields.

This would be helpful when settings for other similar fields
will also be migrated to groups framework.
This commit is contained in:
Sahil Batra
2024-06-24 15:56:17 +05:30
committed by Tim Abbott
parent 3aaf25e42d
commit b44d30add7
3 changed files with 17 additions and 7 deletions

View File

@@ -58,6 +58,7 @@ from zerver.lib.timezone import canonicalize_timezone
from zerver.lib.topic import TOPIC_NAME
from 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 support