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. This is used by the caller to determine whether this user can get historical messages before they joined for a narrowing search. Because of the way our search is currently structured, we may be passed an invalid stream here. We return False in that situation, and subsequent code will do validation and raise the appropriate JsonableError. Note that this function should only be used in contexts where access_stream is being called elsewhere to confirm that the user can actually see this stream. """ if stream.is_history_realm_public() and not user_profile.is_guest: return True if stream.is_history_public_to_subscribers(): # In this case, we check if the user is subscribed. error = _("Invalid stream name '%s'") % (stream.name,) try: (recipient, sub) = access_stream_common(user_profile, stream, error) except JsonableError: return False return True return False
def subscriber_ids_with_stream_history_access(stream: Stream) -> Set[int]: """Returns the set of active user IDs who can access any message history on this stream (regardless of whether they have a UserMessage) based on the stream's configuration. 1. if !history_public_to_subscribers: History is not available to anyone 2. if history_public_to_subscribers and is_web_public: All subscribers can access the history including guests 3. if history_public_to_subscribers and !is_web_public: All subscribers can access the history excluding guests """ if not stream.is_history_public_to_subscribers(): return set() subscriptions = get_active_subscriptions_for_stream_id(stream.id) if stream.is_web_public: return set( subscriptions.filter(user_profile__is_active=True).values_list( "user_profile__id", flat=True)) return set( subscriptions.filter(user_profile__is_active=True).exclude( user_profile__role=600).values_list("user_profile__id", flat=True))
def subscriber_ids_with_stream_history_access(stream: Stream) -> Set[int]: """Returns the set of active user IDs who can access any message history on this stream (regardless of whether they have a UserMessage) based on the stream's configuration. 1. if !history_public_to_subscribers: History is not available to anyone 2. if history_public_to_subscribers: All subscribers can access the history including guests The results of this function need to be kept consistent with what can_access_stream_history would dictate. """ if not stream.is_history_public_to_subscribers(): return set() return set( get_active_subscriptions_for_stream_id( stream.id, include_deactivated_users=False).values_list("user_profile_id", flat=True))