From 228f41e916b907c5e30c188fcd28de2531e925c0 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Wed, 4 Apr 2018 15:10:01 -0700 Subject: [PATCH] messages: Pass UserProfile to is_public_stream_by_name and rename. The new name can_access_stream_history_by_name gets to the point of what this function actually does. And passing in a user object lets us define what this does based on the user subscribed. --- zerver/lib/streams.py | 12 +++++++----- zerver/views/messages.py | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/zerver/lib/streams.py b/zerver/lib/streams.py index 564344d9d3..1c278764d6 100644 --- a/zerver/lib/streams.py +++ b/zerver/lib/streams.py @@ -127,10 +127,12 @@ def access_stream_for_unmute_topic(user_profile: UserProfile, stream_name: Text, raise JsonableError(error) return stream -def is_public_stream_by_name(stream_name: Text, realm: Realm) -> bool: - """Determine whether a stream is public, so that - our caller can decide whether we can get - historical messages for a narrowing search. +def can_access_stream_history_by_name(user_profile: UserProfile, stream_name: Text) -> 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 @@ -142,7 +144,7 @@ def is_public_stream_by_name(stream_name: Text, realm: Realm) -> bool: can actually see this stream. """ try: - stream = get_stream(stream_name, realm) + stream = get_stream(stream_name, user_profile.realm) except Stream.DoesNotExist: return False return stream.is_public() diff --git a/zerver/views/messages.py b/zerver/views/messages.py index 643ce2d6f7..0f3742a3b9 100644 --- a/zerver/views/messages.py +++ b/zerver/views/messages.py @@ -29,7 +29,7 @@ from zerver.lib.message import ( ) from zerver.lib.response import json_success, json_error from zerver.lib.sqlalchemy_utils import get_sqlalchemy_connection -from zerver.lib.streams import access_stream_by_id, is_public_stream_by_name +from zerver.lib.streams import access_stream_by_id, can_access_stream_history_by_name from zerver.lib.timestamp import datetime_to_timestamp, convert_to_UTC from zerver.lib.timezone import get_timezone from zerver.lib.topic_mutes import exclude_topic_mutes @@ -506,7 +506,7 @@ def ok_to_include_history(narrow: Optional[Iterable[Dict[str, Any]]], user_profi if narrow is not None: for term in narrow: if term['operator'] == "stream" and not term.get('negated', False): - if is_public_stream_by_name(term['operand'], user_profile.realm): + if can_access_stream_history_by_name(user_profile, term['operand']): include_history = True # Disable historical messages if the user is narrowing on anything # that's a property on the UserMessage table. There cannot be