streams: Refactor code to compute setting group values.

This commit adds a new function to compute setting group
values for a list of streams, so we can avoid having duplicate
code for computing setting group IDs from streams.
This commit is contained in:
Sahil Batra 2024-11-17 14:38:25 +05:30 committed by Tim Abbott
parent b20c24c09d
commit 2cc0f482e1
2 changed files with 17 additions and 21 deletions

View File

@ -39,8 +39,8 @@ from zerver.lib.stream_traffic import get_streams_traffic
from zerver.lib.streams import (
can_access_stream_user_ids,
check_basic_stream_access,
get_group_setting_value_dict_for_streams,
get_occupied_streams,
get_setting_values_for_group_settings,
get_stream_permission_policy_name,
render_stream_description,
send_stream_creation_event,
@ -354,13 +354,8 @@ def send_subscription_add_events(
subscribers = list(subscriber_dict[stream.id])
stream_subscribers_dict[stream.id] = subscribers
setting_group_ids = set()
for sub_info in sub_info_list:
stream = sub_info.stream
for setting_name in Stream.stream_permission_group_settings:
setting_group_ids.add(getattr(stream, setting_name + "_id"))
setting_groups_dict = get_setting_values_for_group_settings(list(setting_group_ids))
streams = [sub_info.stream for sub_info in sub_info_list]
setting_groups_dict = get_group_setting_value_dict_for_streams(streams)
for user_id, sub_infos in info_by_user.items():
sub_dicts: list[APISubscriptionDict] = []
@ -460,12 +455,7 @@ def send_stream_creation_events_for_previously_inaccessible_streams(
stream_ids = set(altered_user_dict.keys())
recent_traffic = get_streams_traffic(stream_ids, realm)
setting_group_ids = set()
for stream_id in stream_ids:
stream = stream_dict[stream_id]
for setting_name in Stream.stream_permission_group_settings:
setting_group_ids.add(getattr(stream, setting_name + "_id"))
streams = [stream_dict[stream_id] for stream_id in stream_ids]
setting_groups_dict: dict[int, int | AnonymousSettingGroupDict] | None = None
for stream_id, stream_users_ids in altered_user_dict.items():
@ -489,7 +479,7 @@ def send_stream_creation_events_for_previously_inaccessible_streams(
if notify_user_ids:
if setting_groups_dict is None:
setting_groups_dict = get_setting_values_for_group_settings(list(setting_group_ids))
setting_groups_dict = get_group_setting_value_dict_for_streams(streams)
send_stream_creation_event(
realm, stream, notify_user_ids, recent_traffic, setting_groups_dict

View File

@ -999,6 +999,17 @@ def get_streams_for_user(
return list(streams)
def get_group_setting_value_dict_for_streams(
streams: list[Stream],
) -> dict[int, int | AnonymousSettingGroupDict]:
setting_group_ids = set()
for stream in streams:
for setting_name in Stream.stream_permission_group_settings:
setting_group_ids.add(getattr(stream, setting_name + "_id"))
return get_setting_values_for_group_settings(list(setting_group_ids))
def get_setting_values_for_group_settings(
group_ids: list[int],
) -> dict[int, int | AnonymousSettingGroupDict]:
@ -1056,12 +1067,7 @@ def do_get_streams(
stream_ids = {stream.id for stream in streams}
recent_traffic = get_streams_traffic(stream_ids, user_profile.realm)
setting_group_ids = set()
for stream in streams:
for setting_name in Stream.stream_permission_group_settings:
setting_group_ids.add(getattr(stream, setting_name + "_id"))
setting_groups_dict = get_setting_values_for_group_settings(list(setting_group_ids))
setting_groups_dict = get_group_setting_value_dict_for_streams(streams)
stream_dicts = sorted(
(stream_to_dict(stream, recent_traffic, setting_groups_dict) for stream in streams),