From 53e47e69919e51fede46487ee8e24e4c5441db5c Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Wed, 4 Apr 2018 16:12:30 -0700 Subject: [PATCH] messages: Modify access_message for is_history_public_to_subscribers. This completes the Message side of #2745. --- zerver/lib/message.py | 25 ++++++++++++++++++------- zerver/tests/test_messages.py | 13 +++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/zerver/lib/message.py b/zerver/lib/message.py index 0c4295a9f7..8253bdf033 100644 --- a/zerver/lib/message.py +++ b/zerver/lib/message.py @@ -482,17 +482,28 @@ def access_message(user_profile: UserProfile, message_id: int) -> Tuple[Message, # You can't access private messages you didn't receive raise JsonableError(_("Invalid message(s)")) stream = Stream.objects.get(id=message.recipient.type_id) - if not stream.is_public(): - # You can't access messages sent to invite-only streams - # that you didn't receive - raise JsonableError(_("Invalid message(s)")) - # So the message is to a public stream if stream.realm != user_profile.realm: # You can't access public stream messages in other realms raise JsonableError(_("Invalid message(s)")) - # Otherwise, the message must have been sent to a public - # stream in your realm, so return the message, user_message pair + if not stream.is_public(): + if not stream.is_history_public_to_subscribers(): + # You can't access messages sent to invite-only streams + # that you didn't receive + raise JsonableError(_("Invalid message(s)")) + + # This stream is an invite-only stream where message + # history is available to subscribers. So we check if + # you're subscribed. + if not Subscription.objects.filter(user_profile=user_profile, active=True, + recipient=message.recipient).exists(): + raise JsonableError(_("Invalid message(s)")) + + # You are subscribed, so let this fall through to the public stream case. + else: + # Otherwise, the message was sent to a public stream in + # your realm, so return the message, user_message pair + pass return (message, user_message) def render_markdown(message: Message, diff --git a/zerver/tests/test_messages.py b/zerver/tests/test_messages.py index 016bc056a4..341559de82 100644 --- a/zerver/tests/test_messages.py +++ b/zerver/tests/test_messages.py @@ -2262,6 +2262,7 @@ class MirroredMessageUsersTest(ZulipTestCase): self.assertTrue(bob.is_mirror_dummy) class StarTests(ZulipTestCase): + """This is also the main test for access_message""" def change_star(self, messages: List[int], add: bool=True, **kwargs: Any) -> HttpResponse: return self.client_post("/json/messages/flags", @@ -2405,6 +2406,18 @@ class StarTests(ZulipTestCase): result = self.change_star(message_ids) self.assert_json_error(result, 'Invalid message(s)') + with self.settings(PRIVATE_STREAM_HISTORY_FOR_SUBSCRIBERS=True): + # With PRIVATE_STREAM_HISTORY_FOR_SUBSCRIBERS, you still + # can't see it if you didn't receive the message and are + # not subscribed. + result = self.change_star(message_ids) + self.assert_json_error(result, 'Invalid message(s)') + + # But if you subscribe, then you can star the message + self.subscribe(self.example_user("cordelia"), stream_name) + result = self.change_star(message_ids) + self.assert_json_success(result) + def test_new_message(self) -> None: """ New messages aren't starred.