mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 21:43:21 +00:00
performance: Remove recipient from access_stream_by_id.
The Recipient table is now kind of useless for stream-related operations, since we have recipient_id on Stream now.
This commit is contained in:
@@ -319,7 +319,7 @@ def access_stream_common(
|
||||
def access_stream_by_id(user_profile: UserProfile,
|
||||
stream_id: int,
|
||||
require_active: bool=True,
|
||||
allow_realm_admin: bool=False) -> Tuple[Stream, Recipient, Optional[Subscription]]:
|
||||
allow_realm_admin: bool=False) -> Tuple[Stream, Optional[Subscription]]:
|
||||
stream = get_stream_by_id(stream_id)
|
||||
|
||||
error = _("Invalid stream id")
|
||||
@@ -330,7 +330,7 @@ def access_stream_by_id(user_profile: UserProfile,
|
||||
require_active=require_active,
|
||||
allow_realm_admin=allow_realm_admin,
|
||||
)
|
||||
return (stream, stream.recipient, sub)
|
||||
return (stream, sub)
|
||||
|
||||
def get_public_streams_queryset(realm: Realm) -> 'QuerySet[Stream]':
|
||||
return Stream.objects.filter(realm=realm, invite_only=False,
|
||||
|
||||
@@ -4593,15 +4593,13 @@ class AccessStreamTest(ZulipTestCase):
|
||||
access_stream_by_name(hamlet, "invalid stream")
|
||||
|
||||
# Hamlet can access the private stream
|
||||
(stream_ret, rec_ret, sub_ret) = access_stream_by_id(hamlet, stream.id)
|
||||
(stream_ret, sub_ret) = access_stream_by_id(hamlet, stream.id)
|
||||
self.assertEqual(stream.id, stream_ret.id)
|
||||
assert sub_ret is not None
|
||||
self.assertEqual(sub_ret.recipient, rec_ret)
|
||||
self.assertEqual(sub_ret.recipient.type_id, stream.id)
|
||||
(stream_ret2, rec_ret2, sub_ret2) = access_stream_by_name(hamlet, stream.name)
|
||||
self.assertEqual(stream_ret.id, stream_ret2.id)
|
||||
self.assertEqual(sub_ret, sub_ret2)
|
||||
self.assertEqual(rec_ret, rec_ret2)
|
||||
|
||||
# Othello cannot access the private stream
|
||||
with self.assertRaisesRegex(JsonableError, "Invalid stream id"):
|
||||
@@ -4656,10 +4654,9 @@ class AccessStreamTest(ZulipTestCase):
|
||||
|
||||
# Guest user have access to subscribed public streams
|
||||
self.subscribe(guest_user_profile, stream_name)
|
||||
(stream_ret, rec_ret, sub_ret) = access_stream_by_id(guest_user_profile, stream.id)
|
||||
(stream_ret, sub_ret) = access_stream_by_id(guest_user_profile, stream.id)
|
||||
assert sub_ret is not None
|
||||
self.assertEqual(stream.id, stream_ret.id)
|
||||
self.assertEqual(sub_ret.recipient, rec_ret)
|
||||
self.assertEqual(sub_ret.recipient.type_id, stream.id)
|
||||
|
||||
stream_name = "private_stream_1"
|
||||
@@ -4670,16 +4667,15 @@ class AccessStreamTest(ZulipTestCase):
|
||||
|
||||
# Guest user have access to subscribed private streams
|
||||
self.subscribe(guest_user_profile, stream_name)
|
||||
(stream_ret, rec_ret, sub_ret) = access_stream_by_id(guest_user_profile, stream.id)
|
||||
(stream_ret, sub_ret) = access_stream_by_id(guest_user_profile, stream.id)
|
||||
assert sub_ret is not None
|
||||
self.assertEqual(stream.id, stream_ret.id)
|
||||
self.assertEqual(sub_ret.recipient, rec_ret)
|
||||
self.assertEqual(sub_ret.recipient.type_id, stream.id)
|
||||
|
||||
stream_name = "web_public_stream"
|
||||
stream = self.make_stream(stream_name, guest_user_profile.realm, is_web_public=True)
|
||||
# Guest users have access to web public streams even if they aren't subscribed.
|
||||
(stream_ret, rec_ret, sub_ret) = access_stream_by_id(guest_user_profile, stream.id)
|
||||
(stream_ret, sub_ret) = access_stream_by_id(guest_user_profile, stream.id)
|
||||
self.assertTrue(can_access_stream_history(guest_user_profile, stream))
|
||||
assert sub_ret is None
|
||||
self.assertEqual(stream.id, stream_ret.id)
|
||||
|
||||
@@ -1142,7 +1142,7 @@ class UserProfileTest(ZulipTestCase):
|
||||
with queries_captured() as queries:
|
||||
result = orjson.loads(self.client_get(f"/json/users/{iago.id}/subscriptions/{stream.id}").content)
|
||||
|
||||
self.assert_length(queries, 7)
|
||||
self.assert_length(queries, 6)
|
||||
self.assertTrue(result['is_subscribed'])
|
||||
|
||||
# Logging in with a Guest user.
|
||||
|
||||
@@ -69,7 +69,8 @@ def further_validated_draft_dict(draft_dict: Dict[str, Any],
|
||||
raise JsonableError(_("Topic must not contain null bytes"))
|
||||
if len(to) != 1:
|
||||
raise JsonableError(_("Must specify exactly 1 stream ID for stream messages"))
|
||||
stream, recipient, sub = access_stream_by_id(user_profile, to[0])
|
||||
stream, sub = access_stream_by_id(user_profile, to[0])
|
||||
recipient = stream.recipient
|
||||
elif draft_dict["type"] == "private" and len(to) != 0:
|
||||
to_users = get_user_profiles_by_ids(set(to), user_profile.realm)
|
||||
try:
|
||||
|
||||
@@ -51,7 +51,7 @@ def invite_users_backend(request: HttpRequest, user_profile: UserProfile,
|
||||
streams: List[Stream] = []
|
||||
for stream_id in stream_ids:
|
||||
try:
|
||||
(stream, recipient, sub) = access_stream_by_id(user_profile, stream_id)
|
||||
(stream, sub) = access_stream_by_id(user_profile, stream_id)
|
||||
except JsonableError:
|
||||
return json_error(
|
||||
_("Stream does not exist with id: {}. No invites were sent.").format(stream_id))
|
||||
@@ -146,7 +146,7 @@ def generate_multiuse_invite_backend(
|
||||
streams = []
|
||||
for stream_id in stream_ids:
|
||||
try:
|
||||
(stream, recipient, sub) = access_stream_by_id(user_profile, stream_id)
|
||||
(stream, sub) = access_stream_by_id(user_profile, stream_id)
|
||||
except JsonableError:
|
||||
return json_error(_("Invalid stream id {}. No invites were sent.").format(stream_id))
|
||||
streams.append(stream)
|
||||
|
||||
@@ -52,8 +52,8 @@ def mark_all_as_read(request: HttpRequest, user_profile: UserProfile) -> HttpRes
|
||||
def mark_stream_as_read(request: HttpRequest,
|
||||
user_profile: UserProfile,
|
||||
stream_id: int=REQ(validator=check_int)) -> HttpResponse:
|
||||
stream, recipient, sub = access_stream_by_id(user_profile, stream_id)
|
||||
count = do_mark_stream_messages_as_read(user_profile, recipient.id)
|
||||
stream, sub = access_stream_by_id(user_profile, stream_id)
|
||||
count = do_mark_stream_messages_as_read(user_profile, stream.recipient_id)
|
||||
|
||||
log_data_str = f"[{count} updated]"
|
||||
request._log_data["extra"] = log_data_str
|
||||
@@ -66,7 +66,7 @@ def mark_topic_as_read(request: HttpRequest,
|
||||
user_profile: UserProfile,
|
||||
stream_id: int=REQ(validator=check_int),
|
||||
topic_name: str=REQ()) -> HttpResponse:
|
||||
stream, recipient, sub = access_stream_by_id(user_profile, stream_id)
|
||||
stream, sub = access_stream_by_id(user_profile, stream_id)
|
||||
|
||||
if topic_name:
|
||||
topic_exists = user_message_exists_for_topic(
|
||||
|
||||
@@ -29,7 +29,7 @@ def mute_topic(user_profile: UserProfile,
|
||||
(stream, recipient, sub) = access_stream_by_name(user_profile, stream_name)
|
||||
else:
|
||||
assert stream_id is not None
|
||||
(stream, recipient, sub) = access_stream_by_id(user_profile, stream_id)
|
||||
(stream, sub) = access_stream_by_id(user_profile, stream_id)
|
||||
|
||||
if topic_is_muted(user_profile, stream.id, topic_name):
|
||||
return json_error(_("Topic already muted"))
|
||||
|
||||
@@ -168,7 +168,7 @@ def update_realm(
|
||||
notifications_stream_id):
|
||||
new_notifications_stream = None
|
||||
if notifications_stream_id >= 0:
|
||||
(new_notifications_stream, recipient, sub) = access_stream_by_id(
|
||||
(new_notifications_stream, sub) = access_stream_by_id(
|
||||
user_profile, notifications_stream_id)
|
||||
do_set_realm_notifications_stream(realm, new_notifications_stream,
|
||||
notifications_stream_id,
|
||||
@@ -180,7 +180,7 @@ def update_realm(
|
||||
signup_notifications_stream_id):
|
||||
new_signup_notifications_stream = None
|
||||
if signup_notifications_stream_id >= 0:
|
||||
(new_signup_notifications_stream, recipient, sub) = access_stream_by_id(
|
||||
(new_signup_notifications_stream, sub) = access_stream_by_id(
|
||||
user_profile, signup_notifications_stream_id)
|
||||
do_set_realm_signup_notifications_stream(realm, new_signup_notifications_stream,
|
||||
signup_notifications_stream_id,
|
||||
|
||||
@@ -152,7 +152,7 @@ def deactivate_stream_backend(request: HttpRequest,
|
||||
def add_default_stream(request: HttpRequest,
|
||||
user_profile: UserProfile,
|
||||
stream_id: int=REQ(validator=check_int)) -> HttpResponse:
|
||||
(stream, recipient, sub) = access_stream_by_id(user_profile, stream_id)
|
||||
(stream, sub) = access_stream_by_id(user_profile, stream_id)
|
||||
do_add_default_stream(stream)
|
||||
return json_success()
|
||||
|
||||
@@ -217,7 +217,7 @@ def remove_default_stream_group(request: HttpRequest, user_profile: UserProfile,
|
||||
def remove_default_stream(request: HttpRequest,
|
||||
user_profile: UserProfile,
|
||||
stream_id: int=REQ(validator=check_int)) -> HttpResponse:
|
||||
(stream, recipient, sub) = access_stream_by_id(
|
||||
(stream, sub) = access_stream_by_id(
|
||||
user_profile,
|
||||
stream_id,
|
||||
allow_realm_admin=True,
|
||||
@@ -279,7 +279,7 @@ def update_stream_backend(
|
||||
# But we require even realm administrators to be actually
|
||||
# subscribed to make a private stream public.
|
||||
if is_private is not None:
|
||||
(stream, recipient, sub) = access_stream_by_id(user_profile, stream_id)
|
||||
(stream, sub) = access_stream_by_id(user_profile, stream_id)
|
||||
do_change_stream_invite_only(stream, is_private, history_public_to_subscribers)
|
||||
return json_success()
|
||||
|
||||
@@ -621,8 +621,11 @@ def send_messages_for_new_subscribers(
|
||||
@has_request_variables
|
||||
def get_subscribers_backend(request: HttpRequest, user_profile: UserProfile,
|
||||
stream_id: int=REQ('stream', converter=to_non_negative_int)) -> HttpResponse:
|
||||
(stream, recipient, sub) = access_stream_by_id(user_profile, stream_id,
|
||||
allow_realm_admin=True)
|
||||
(stream, sub) = access_stream_by_id(
|
||||
user_profile,
|
||||
stream_id,
|
||||
allow_realm_admin=True,
|
||||
)
|
||||
subscribers = get_subscriber_emails(stream, user_profile)
|
||||
|
||||
return json_success({'subscribers': subscribers})
|
||||
@@ -671,7 +674,7 @@ def get_topics_backend(
|
||||
else:
|
||||
assert user_profile is not None
|
||||
|
||||
(stream, recipient, sub) = access_stream_by_id(user_profile, stream_id)
|
||||
(stream, sub) = access_stream_by_id(user_profile, stream_id)
|
||||
|
||||
result = get_topic_history_for_stream(
|
||||
user_profile=user_profile,
|
||||
@@ -686,7 +689,7 @@ def get_topics_backend(
|
||||
def delete_in_topic(request: HttpRequest, user_profile: UserProfile,
|
||||
stream_id: int=REQ(converter=to_non_negative_int),
|
||||
topic_name: str=REQ("topic_name")) -> HttpResponse:
|
||||
(stream, recipient, sub) = access_stream_by_id(user_profile, stream_id)
|
||||
(stream, sub) = access_stream_by_id(user_profile, stream_id)
|
||||
|
||||
messages = messages_for_topic(stream.recipient_id, topic_name)
|
||||
if not stream.is_history_public_to_subscribers():
|
||||
@@ -783,7 +786,7 @@ def update_subscription_properties_backend(
|
||||
if property not in property_converters:
|
||||
return json_error(_("Unknown subscription property: {}").format(property))
|
||||
|
||||
(stream, recipient, sub) = access_stream_by_id(user_profile, stream_id)
|
||||
(stream, sub) = access_stream_by_id(user_profile, stream_id)
|
||||
if sub is None:
|
||||
return json_error(_("Not subscribed to stream id {}").format(stream_id))
|
||||
|
||||
|
||||
@@ -546,7 +546,7 @@ def get_subscription_backend(request: HttpRequest, user_profile: UserProfile,
|
||||
stream_id: int=REQ(validator=check_int, path_only=True),
|
||||
) -> HttpResponse:
|
||||
target_user = access_user_by_id(user_profile, user_id, read_only=True)
|
||||
(stream, recipient, sub) = access_stream_by_id(user_profile, stream_id)
|
||||
(stream, sub) = access_stream_by_id(user_profile, stream_id)
|
||||
|
||||
subscription_status = {'is_subscribed': subscribed_to_stream(target_user, stream_id)}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user