streams: Compute object only when needed.

This commit updates code to compute the values for group permission
settings in send_stream_creation_events_for_previously_inaccessible_streams
only when we need to send the events. This helps us in avoiding
unnecessary DB queries.
This commit is contained in:
Sahil Batra 2024-11-17 12:21:55 +05:30 committed by Tim Abbott
parent f2158c42a7
commit b20c24c09d
2 changed files with 9 additions and 6 deletions

View File

@ -466,7 +466,7 @@ def send_stream_creation_events_for_previously_inaccessible_streams(
for setting_name in Stream.stream_permission_group_settings: for setting_name in Stream.stream_permission_group_settings:
setting_group_ids.add(getattr(stream, setting_name + "_id")) 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: dict[int, int | AnonymousSettingGroupDict] | None = None
for stream_id, stream_users_ids in altered_user_dict.items(): for stream_id, stream_users_ids in altered_user_dict.items():
stream = stream_dict[stream_id] stream = stream_dict[stream_id]
@ -488,6 +488,9 @@ def send_stream_creation_events_for_previously_inaccessible_streams(
notify_user_ids = list(stream_users_ids & altered_guests) notify_user_ids = list(stream_users_ids & altered_guests)
if notify_user_ids: if notify_user_ids:
if setting_groups_dict is None:
setting_groups_dict = get_setting_values_for_group_settings(list(setting_group_ids))
send_stream_creation_event( send_stream_creation_event(
realm, stream, notify_user_ids, recent_traffic, setting_groups_dict realm, stream, notify_user_ids, recent_traffic, setting_groups_dict
) )

View File

@ -4801,7 +4801,7 @@ class SubscriptionAPITest(ZulipTestCase):
streams_to_sub = ["multi_user_stream"] streams_to_sub = ["multi_user_stream"]
with ( with (
self.capture_send_event_calls(expected_num_events=5) as events, self.capture_send_event_calls(expected_num_events=5) as events,
self.assert_database_query_count(40), self.assert_database_query_count(39),
): ):
self.common_subscribe_to_streams( self.common_subscribe_to_streams(
self.test_user, self.test_user,
@ -4827,7 +4827,7 @@ class SubscriptionAPITest(ZulipTestCase):
# Now add ourselves # Now add ourselves
with ( with (
self.capture_send_event_calls(expected_num_events=2) as events, self.capture_send_event_calls(expected_num_events=2) as events,
self.assert_database_query_count(16), self.assert_database_query_count(15),
): ):
self.common_subscribe_to_streams( self.common_subscribe_to_streams(
self.test_user, self.test_user,
@ -5273,7 +5273,7 @@ class SubscriptionAPITest(ZulipTestCase):
test_user_ids = [user.id for user in test_users] test_user_ids = [user.id for user in test_users]
with ( with (
self.assert_database_query_count(18), self.assert_database_query_count(17),
self.assert_memcached_count(3), self.assert_memcached_count(3),
mock.patch("zerver.views.streams.send_messages_for_new_subscribers"), mock.patch("zerver.views.streams.send_messages_for_new_subscribers"),
): ):
@ -5641,7 +5641,7 @@ class SubscriptionAPITest(ZulipTestCase):
] ]
# Test creating a public stream when realm does not have a notification stream. # Test creating a public stream when realm does not have a notification stream.
with self.assert_database_query_count(40): with self.assert_database_query_count(39):
self.common_subscribe_to_streams( self.common_subscribe_to_streams(
self.test_user, self.test_user,
[new_streams[0]], [new_streams[0]],
@ -5661,7 +5661,7 @@ class SubscriptionAPITest(ZulipTestCase):
new_stream_announcements_stream = get_stream(self.streams[0], self.test_realm) new_stream_announcements_stream = get_stream(self.streams[0], self.test_realm)
self.test_realm.new_stream_announcements_stream_id = new_stream_announcements_stream.id self.test_realm.new_stream_announcements_stream_id = new_stream_announcements_stream.id
self.test_realm.save() self.test_realm.save()
with self.assert_database_query_count(51): with self.assert_database_query_count(50):
self.common_subscribe_to_streams( self.common_subscribe_to_streams(
self.test_user, self.test_user,
[new_streams[2]], [new_streams[2]],