From 7adc83d2a0e726cafc9f61796723e0d2f964197c Mon Sep 17 00:00:00 2001 From: Sahil Batra Date: Mon, 18 Nov 2024 10:11:08 +0530 Subject: [PATCH] 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. --- zerver/actions/streams.py | 10 ++++++++-- zerver/actions/users.py | 14 ++++++++++++-- zerver/lib/streams.py | 3 ++- zerver/views/streams.py | 7 ++++++- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/zerver/actions/streams.py b/zerver/actions/streams.py index e90f192ef3..665443c4be 100644 --- a/zerver/actions/streams.py +++ b/zerver/actions/streams.py @@ -234,7 +234,10 @@ def do_unarchive_stream(stream: Stream, new_name: str, *, acting_user: UserProfi 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) 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 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 # events to users who were previously subscribed to the streams as diff --git a/zerver/actions/users.py b/zerver/actions/users.py index c5220a02db..306ebc361f 100644 --- a/zerver/actions/users.py +++ b/zerver/actions/users.py @@ -28,7 +28,11 @@ from zerver.lib.sessions import delete_user_sessions from zerver.lib.soft_deactivation import queue_soft_reactivation from zerver.lib.stream_subscription import bulk_get_subscriber_peer_info 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.user_counts import realm_user_count_by_role 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 if stream.id in now_accessible_stream_ids ] + + setting_groups_dict = get_group_setting_value_dict_for_streams(now_accessible_streams) + event = dict( type="stream", 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]) diff --git a/zerver/lib/streams.py b/zerver/lib/streams.py index 9cf555c861..624833d165 100644 --- a/zerver/lib/streams.py +++ b/zerver/lib/streams.py @@ -928,7 +928,8 @@ def stream_to_dict( def get_web_public_streams(realm: Realm) -> list[APIStreamDict]: # nocoverage query = get_web_public_streams_queryset(realm) 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 diff --git a/zerver/views/streams.py b/zerver/views/streams.py index 0feb4d9cd9..7cf45cb89c 100644 --- a/zerver/views/streams.py +++ b/zerver/views/streams.py @@ -62,6 +62,7 @@ from zerver.lib.streams import ( check_stream_name_available, do_get_streams, filter_stream_authorization, + get_group_setting_value_dict_for_streams, get_stream_permission_policy_name, list_to_streams, 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) 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