From 9e09a240d77cb5f68dd5a0bf64a33d5c300b3d7e Mon Sep 17 00:00:00 2001 From: Shubham Padia Date: Thu, 6 Feb 2025 11:09:31 +0000 Subject: [PATCH] stream: Pass `is_subscribed` to `check_basic_stream_access`. Earlier, we were passing the whole subscription object to the function in order to check if the user was subscribed or not. In the future commits, we want to check that without fetching and passing the complete subscription object and this commit will help us do that. --- zerver/actions/streams.py | 2 +- zerver/lib/streams.py | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/zerver/actions/streams.py b/zerver/actions/streams.py index 57878bf758..2f4d7708ec 100644 --- a/zerver/actions/streams.py +++ b/zerver/actions/streams.py @@ -913,7 +913,7 @@ def send_subscription_remove_events( stream for stream in streams_by_user[user_profile.id] if not check_basic_stream_access( - user_profile, stream, sub=None, allow_realm_admin=True + user_profile, stream, is_subscribed=False, allow_realm_admin=True ) ] diff --git a/zerver/lib/streams.py b/zerver/lib/streams.py index 668dc7d7ca..4ac3a3f675 100644 --- a/zerver/lib/streams.py +++ b/zerver/lib/streams.py @@ -484,7 +484,8 @@ def access_stream_for_delete_or_update( def check_basic_stream_access( user_profile: UserProfile, stream: Stream, - sub: Subscription | None, + *, + is_subscribed: bool, allow_realm_admin: bool = False, ) -> bool: # Any realm user, even guests, can access web_public streams. @@ -496,7 +497,7 @@ def check_basic_stream_access( return True # Or if you are subscribed to the stream, you can access it. - if sub is not None: + if is_subscribed: return True # For some specific callers (e.g. getting list of subscribers, @@ -538,7 +539,7 @@ def access_stream_common( sub = None if not stream.deactivated and check_basic_stream_access( - user_profile, stream, sub, allow_realm_admin=allow_realm_admin + user_profile, stream, is_subscribed=sub is not None, allow_realm_admin=allow_realm_admin ): return sub @@ -777,7 +778,9 @@ def can_access_stream_history_by_id(user_profile: UserProfile, stream_id: int) - def can_remove_subscribers_from_stream( stream: Stream, user_profile: UserProfile, sub: Subscription | None ) -> bool: - if not check_basic_stream_access(user_profile, stream, sub, allow_realm_admin=True): + if not check_basic_stream_access( + user_profile, stream, is_subscribed=sub is not None, allow_realm_admin=True + ): return False # Optimization for the organization administrator code path. We