Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
performance: Remove recipient from access_stream_by_id.
Steve Howella51b483f1a31
diff --git a/zerver/lib/streams.py b/zerver/lib/streams.pyindex 4bd969fcb78..da6661ba0f9 100644--- a/zerver/lib/streams.py+++ b/zerver/lib/streams.py@@ -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,diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.pyindex 35fd8461ead..3daac1531fa 100644--- a/zerver/tests/test_subs.py+++ b/zerver/tests/test_subs.py@@ -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 streamwith self.assertRaisesRegex(JsonableError, "Invalid stream id"):@@ -4656,10 +4654,9 @@ class AccessStreamTest(ZulipTestCase):# Guest user have access to subscribed public streamsself.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 Noneself.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 streamsself.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 Noneself.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 Noneself.assertEqual(stream.id, stream_ret.id)diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex 547646610e1..9d6237e5f3e 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -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.diff --git a/zerver/views/drafts.py b/zerver/views/drafts.pyindex 47b5c6fa242..63bcc8e7d1f 100644--- a/zerver/views/drafts.py+++ b/zerver/views/drafts.py@@ -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.recipientelif draft_dict["type"] == "private" and len(to) != 0:to_users = get_user_profiles_by_ids(set(to), user_profile.realm)try:diff --git a/zerver/views/invite.py b/zerver/views/invite.pyindex d23ce240577..e99f6d17c57 100644--- a/zerver/views/invite.py+++ b/zerver/views/invite.py@@ -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)diff --git a/zerver/views/message_flags.py b/zerver/views/message_flags.pyindex dfd415d8b96..0e639678f87 100644--- a/zerver/views/message_flags.py+++ b/zerver/views/message_flags.py@@ -52,8 +52,8 @@ def mark_all_as_read(request: HttpRequest, user_profile: UserProfile) -> HttpResdef 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(diff --git a/zerver/views/muting.py b/zerver/views/muting.pyindex 8887cd86da2..09220db6ad8 100644--- a/zerver/views/muting.py+++ b/zerver/views/muting.py@@ -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"))diff --git a/zerver/views/realm.py b/zerver/views/realm.pyindex bd5340e7aa1..df42e994c6c 100644--- a/zerver/views/realm.py+++ b/zerver/views/realm.py@@ -168,7 +168,7 @@ def update_realm(notifications_stream_id):new_notifications_stream = Noneif 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 = Noneif 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,diff --git a/zerver/views/streams.py b/zerver/views/streams.pyindex a8811a54e48..96e3c28ebb0 100644--- a/zerver/views/streams.py+++ b/zerver/views/streams.py@@ -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_variablesdef 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))diff --git a/zerver/views/users.py b/zerver/views/users.pyindex 2bf3c14cf19..1e64cb342bf 100644--- a/zerver/views/users.py+++ b/zerver/views/users.py@@ -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)}