stream creation: Avoid stream.realm references.

We want to avoid Django going back to the database to
get a realm object that the caller already has.

It's actually currently the case that we often
pre-fetch realm objects when we get stream objects
using get_stream (using a call to select_related() with
no arguments), but that is an expensive operation that
we want to avoid going forward.

This commit prepares us to just fetch slim objects.
This commit is contained in:
Steve Howell
2023-07-11 11:55:28 +00:00
committed by Tim Abbott
parent 445819e110
commit adb548c7a2
2 changed files with 11 additions and 9 deletions

View File

@@ -110,9 +110,9 @@ def render_stream_description(text: str, realm: Realm) -> str:
return markdown_convert(text, message_realm=realm, no_previews=True).rendered_content
def send_stream_creation_event(stream: Stream, user_ids: List[int]) -> None:
def send_stream_creation_event(realm: Realm, stream: Stream, user_ids: List[int]) -> None:
event = dict(type="stream", op="create", streams=[stream.to_dict()])
send_event(stream.realm, event, user_ids)
send_event(realm, event, user_ids)
def create_stream_if_needed(
@@ -172,10 +172,10 @@ def create_stream_if_needed(
)
if created:
if stream.is_public():
send_stream_creation_event(stream, active_non_guest_user_ids(stream.realm_id))
send_stream_creation_event(realm, stream, active_non_guest_user_ids(stream.realm_id))
else:
realm_admin_ids = [user.id for user in stream.realm.get_admin_users_and_bots()]
send_stream_creation_event(stream, realm_admin_ids)
send_stream_creation_event(realm, stream, realm_admin_ids)
return stream, created