Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

streams: Move can_access_stream_user_ids into streams.py.

Tim Abbotta1cfe25f8dd5
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py
index b4624c1af0a..9ce00b1938b 100644
--- a/zerver/lib/actions.py
+++ b/zerver/lib/actions.py
@@ -138,6 +138,7 @@ from zerver.lib.stream_subscription import (
from zerver.lib.stream_topic import StreamTopicTarget
from zerver.lib.streams import (
access_stream_for_send_message,
+ can_access_stream_user_ids,
check_stream_name,
create_stream_if_needed,
get_default_value_for_history_public_to_subscribers,
@@ -295,37 +296,6 @@ def subscriber_info(user_id: int) -> Dict[str, Any]:
return {"id": user_id, "flags": ["read"]}
-def can_access_stream_user_ids(stream: Stream) -> Set[int]:
- # return user ids of users who can access the attributes of
- # a stream, such as its name/description.
- if stream.is_public():
- # For a public stream, this is everyone in the realm
- # except unsubscribed guest users
- return public_stream_user_ids(stream)
- else:
- # for a private stream, it's subscribers plus realm admins.
- return private_stream_user_ids(stream.id) | {
- user.id for user in stream.realm.get_admin_users_and_bots()
- }
-
-
-def private_stream_user_ids(stream_id: int) -> Set[int]:
- subscriptions = get_active_subscriptions_for_stream_id(
- stream_id, include_deactivated_users=False
- )
- return {sub["user_profile_id"] for sub in subscriptions.values("user_profile_id")}
-
-
-def public_stream_user_ids(stream: Stream) -> Set[int]:
- guest_subscriptions = get_active_subscriptions_for_stream_id(
- stream.id, include_deactivated_users=False
- ).filter(user_profile__role=UserProfile.ROLE_GUEST)
- guest_subscriptions = {
- sub["user_profile_id"] for sub in guest_subscriptions.values("user_profile_id")
- }
- return set(active_non_guest_user_ids(stream.realm_id)) | guest_subscriptions
-
-
def bot_owner_user_ids(user_profile: UserProfile) -> Set[int]:
is_private_bot = (
user_profile.default_sending_stream
diff --git a/zerver/lib/streams.py b/zerver/lib/streams.py
index fb6490f5a95..b541c1563e3 100644
--- a/zerver/lib/streams.py
+++ b/zerver/lib/streams.py
@@ -1,4 +1,4 @@
-from typing import Iterable, List, Optional, Tuple, Union
+from typing import Iterable, List, Optional, Set, Tuple, Union
from django.db.models.query import QuerySet
from django.utils.timezone import now as timezone_now
@@ -8,6 +8,7 @@ from typing_extensions import TypedDict
from zerver.lib.exceptions import StreamAdministratorRequired
from zerver.lib.markdown import markdown_convert
from zerver.lib.request import JsonableError
+from zerver.lib.stream_subscription import get_active_subscriptions_for_stream_id
from zerver.models import (
DefaultStreamGroup,
Realm,
@@ -478,6 +479,38 @@ def access_stream_for_unmute_topic_by_id(
return stream
+def private_stream_user_ids(stream_id: int) -> Set[int]:
+ subscriptions = get_active_subscriptions_for_stream_id(
+ stream_id, include_deactivated_users=False
+ )
+ return {sub["user_profile_id"] for sub in subscriptions.values("user_profile_id")}
+
+
+def public_stream_user_ids(stream: Stream) -> Set[int]:
+ guest_subscriptions = get_active_subscriptions_for_stream_id(
+ stream.id, include_deactivated_users=False
+ ).filter(user_profile__role=UserProfile.ROLE_GUEST)
+ guest_subscriptions = {
+ sub["user_profile_id"] for sub in guest_subscriptions.values("user_profile_id")
+ }
+ return set(active_non_guest_user_ids(stream.realm_id)) | guest_subscriptions
+
+
+def can_access_stream_user_ids(stream: Stream) -> Set[int]:
+ # return user ids of users who can access the attributes of a
+ # stream, such as its name/description. Useful for sending events
+ # to all users with access to a stream's attributes.
+ if stream.is_public():
+ # For a public stream, this is everyone in the realm
+ # except unsubscribed guest users
+ return public_stream_user_ids(stream)
+ else:
+ # for a private stream, it's subscribers plus realm admins.
+ return private_stream_user_ids(stream.id) | {
+ user.id for user in stream.realm.get_admin_users_and_bots()
+ }
+
+
def can_access_stream_history(user_profile: UserProfile, stream: Stream) -> bool:
"""Determine whether the provided user is allowed to access the
history of the target stream. The stream is specified by name.