streams: Optimize code for computing stream objects.

This commit updates code to optimize code for computing stream
objects to be sent with stream creation event and in response
for 'GET /streams/{stream_id}' endpoint by optimizing number
of queries while computing values for group permission settings.

This change does not benefit much currently as we only have one
stream group permission setting, but is important before we add
more stream permission settings.

There are a couple of places left where we can still optimize
the code and that would be done in further commits.
This commit is contained in:
Sahil Batra 2024-11-18 10:11:08 +05:30 committed by Tim Abbott
parent 2cc0f482e1
commit 7adc83d2a0
4 changed files with 28 additions and 6 deletions

View File

@ -234,7 +234,10 @@ def do_unarchive_stream(stream: Stream, new_name: str, *, acting_user: UserProfi
notify_users = admin_users_and_bots | subscribed_users notify_users = admin_users_and_bots | subscribed_users
send_stream_creation_event(realm, stream, [user.id for user in notify_users], recent_traffic) setting_groups_dict = get_group_setting_value_dict_for_streams([stream])
send_stream_creation_event(
realm, stream, [user.id for user in notify_users], recent_traffic, setting_groups_dict
)
sender = get_system_bot(settings.NOTIFICATION_BOT, stream.realm_id) sender = get_system_bot(settings.NOTIFICATION_BOT, stream.realm_id)
with override_language(stream.realm.default_language): with override_language(stream.realm.default_language):
@ -1235,7 +1238,10 @@ def do_change_stream_permission(
notify_stream_creation_ids = non_guest_user_ids - old_can_access_stream_user_ids notify_stream_creation_ids = non_guest_user_ids - old_can_access_stream_user_ids
recent_traffic = get_streams_traffic({stream.id}, realm) recent_traffic = get_streams_traffic({stream.id}, realm)
send_stream_creation_event(realm, stream, list(notify_stream_creation_ids), recent_traffic) setting_groups_dict = get_group_setting_value_dict_for_streams([stream])
send_stream_creation_event(
realm, stream, list(notify_stream_creation_ids), recent_traffic, setting_groups_dict
)
# Add subscribers info to the stream object. We need to send peer_add # Add subscribers info to the stream object. We need to send peer_add
# events to users who were previously subscribed to the streams as # events to users who were previously subscribed to the streams as

View File

@ -28,7 +28,11 @@ from zerver.lib.sessions import delete_user_sessions
from zerver.lib.soft_deactivation import queue_soft_reactivation from zerver.lib.soft_deactivation import queue_soft_reactivation
from zerver.lib.stream_subscription import bulk_get_subscriber_peer_info from zerver.lib.stream_subscription import bulk_get_subscriber_peer_info
from zerver.lib.stream_traffic import get_streams_traffic from zerver.lib.stream_traffic import get_streams_traffic
from zerver.lib.streams import get_streams_for_user, stream_to_dict from zerver.lib.streams import (
get_group_setting_value_dict_for_streams,
get_streams_for_user,
stream_to_dict,
)
from zerver.lib.types import AnonymousSettingGroupDict from zerver.lib.types import AnonymousSettingGroupDict
from zerver.lib.user_counts import realm_user_count_by_role from zerver.lib.user_counts import realm_user_count_by_role
from zerver.lib.user_groups import get_system_user_group_for_user from zerver.lib.user_groups import get_system_user_group_for_user
@ -555,10 +559,16 @@ def send_stream_events_for_role_update(
for stream in current_accessible_streams for stream in current_accessible_streams
if stream.id in now_accessible_stream_ids if stream.id in now_accessible_stream_ids
] ]
setting_groups_dict = get_group_setting_value_dict_for_streams(now_accessible_streams)
event = dict( event = dict(
type="stream", type="stream",
op="create", op="create",
streams=[stream_to_dict(stream, recent_traffic) for stream in now_accessible_streams], streams=[
stream_to_dict(stream, recent_traffic, setting_groups_dict)
for stream in now_accessible_streams
],
) )
send_event_on_commit(user_profile.realm, event, [user_profile.id]) send_event_on_commit(user_profile.realm, event, [user_profile.id])

View File

@ -928,7 +928,8 @@ def stream_to_dict(
def get_web_public_streams(realm: Realm) -> list[APIStreamDict]: # nocoverage def get_web_public_streams(realm: Realm) -> list[APIStreamDict]: # nocoverage
query = get_web_public_streams_queryset(realm) query = get_web_public_streams_queryset(realm)
streams = query.only(*Stream.API_FIELDS) streams = query.only(*Stream.API_FIELDS)
stream_dicts = [stream_to_dict(stream) for stream in streams] setting_groups_dict = get_group_setting_value_dict_for_streams(list(streams))
stream_dicts = [stream_to_dict(stream, None, setting_groups_dict) for stream in streams]
return stream_dicts return stream_dicts

View File

@ -62,6 +62,7 @@ from zerver.lib.streams import (
check_stream_name_available, check_stream_name_available,
do_get_streams, do_get_streams,
filter_stream_authorization, filter_stream_authorization,
get_group_setting_value_dict_for_streams,
get_stream_permission_policy_name, get_stream_permission_policy_name,
list_to_streams, list_to_streams,
stream_to_dict, stream_to_dict,
@ -895,7 +896,11 @@ def get_stream_backend(
(stream, sub) = access_stream_by_id(user_profile, stream_id, allow_realm_admin=True) (stream, sub) = access_stream_by_id(user_profile, stream_id, allow_realm_admin=True)
recent_traffic = get_streams_traffic({stream.id}, user_profile.realm) recent_traffic = get_streams_traffic({stream.id}, user_profile.realm)
return json_success(request, data={"stream": stream_to_dict(stream, recent_traffic)}) setting_groups_dict = get_group_setting_value_dict_for_streams([stream])
return json_success(
request, data={"stream": stream_to_dict(stream, recent_traffic, setting_groups_dict)}
)
@typed_endpoint @typed_endpoint