2023-07-27 16:42:21 +02:00
|
|
|
from zerver.lib.types import DefaultStreamDict
|
2023-07-08 21:20:28 +02:00
|
|
|
from zerver.models import DefaultStream, Stream
|
|
|
|
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_slim_realm_default_streams(realm_id: int) -> list[Stream]:
|
2023-07-09 13:30:19 +02:00
|
|
|
# We really want this query to be simple and just get "thin" Stream objects
|
|
|
|
# in one round trip.
|
|
|
|
#
|
|
|
|
# The above is enforced by at least three tests that verify query counts,
|
|
|
|
# and test_query_count in test_subs.py makes sure that the query itself is
|
|
|
|
# not like 11000 bytes, which is what we had in a prior version that used
|
|
|
|
# select_related() with not arguments (and thus joined to too many tables).
|
|
|
|
#
|
|
|
|
# Please be careful about modifying this code, as it has had a history
|
|
|
|
# of performance problems.
|
|
|
|
return list(Stream.objects.filter(defaultstream__realm_id=realm_id))
|
2023-07-08 21:20:28 +02:00
|
|
|
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_default_stream_ids_for_realm(realm_id: int) -> set[int]:
|
2023-07-08 21:20:28 +02:00
|
|
|
return set(DefaultStream.objects.filter(realm_id=realm_id).values_list("stream_id", flat=True))
|
|
|
|
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_default_streams_for_realm_as_dicts(realm_id: int) -> list[DefaultStreamDict]:
|
2023-07-08 22:47:27 +02:00
|
|
|
"""
|
|
|
|
Return all the default streams for a realm using a list of dictionaries sorted
|
|
|
|
by stream name.
|
|
|
|
"""
|
2023-07-09 13:30:19 +02:00
|
|
|
streams = get_slim_realm_default_streams(realm_id)
|
|
|
|
stream_dicts = [stream.to_dict() for stream in streams]
|
|
|
|
return sorted(stream_dicts, key=lambda stream: stream["name"])
|