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.

topic_history: Allow anonymous access in web-public streams.

Amanc114447e115f
diff --git a/zerver/lib/streams.py b/zerver/lib/streams.py
index ef7755c0406..45d498d45d8 100644
--- a/zerver/lib/streams.py
+++ b/zerver/lib/streams.py
@@ -337,6 +337,17 @@ def access_stream_by_name(user_profile: UserProfile,
allow_realm_admin=allow_realm_admin)
return (stream, recipient, sub)
+def access_web_public_stream(stream_id: int, realm: Realm) -> Stream:
+ error = _("Invalid stream id")
+ try:
+ stream = get_stream_by_id_in_realm(stream_id, realm)
+ except Stream.DoesNotExist:
+ raise JsonableError(error)
+
+ if not stream.is_web_public:
+ raise JsonableError(error)
+ return stream
+
def access_stream_for_unmute_topic_by_name(user_profile: UserProfile,
stream_name: str,
error: str) -> Stream:
diff --git a/zerver/tests/test_message_topics.py b/zerver/tests/test_message_topics.py
index 4f342e58bc0..6986e0ee5cf 100644
--- a/zerver/tests/test_message_topics.py
+++ b/zerver/tests/test_message_topics.py
@@ -161,6 +161,34 @@ class TopicHistoryTest(ZulipTestCase):
result = self.client_get(endpoint, {})
self.assert_json_error(result, 'Invalid stream id')
+ def test_get_topics_web_public_stream_web_public_request(self) -> None:
+ stream = self.make_stream('web-public-steram', is_web_public=True)
+ for i in range(3):
+ self.send_stream_message(self.example_user('iago'),
+ stream.name, topic_name='topic' + str(i))
+
+ endpoint = f'/json/users/me/{stream.id}/topics'
+ result = self.client_get(endpoint)
+ self.assert_json_success(result)
+ history = result.json()['topics']
+ self.assertEqual([topic['name'] for topic in history], [
+ 'topic2',
+ 'topic1',
+ 'topic0',
+ ])
+
+ def test_get_topics_non_web_public_stream_web_public_request(self) -> None:
+ stream = get_stream('Verona', self.example_user('iago').realm)
+ endpoint = f'/json/users/me/{stream.id}/topics'
+ result = self.client_get(endpoint)
+ self.assert_json_error(result, 'Invalid stream id', 400)
+
+ def test_get_topics_non_existant_stream_web_public_request(self) -> None:
+ non_existant_stream_id = 10000000000000000000000
+ endpoint = f'/json/users/me/{non_existant_stream_id}/topics'
+ result = self.client_get(endpoint)
+ self.assert_json_error(result, 'Invalid stream id', 400)
+
class TopicDeleteTest(ZulipTestCase):
def test_topic_delete(self) -> None:
initial_last_msg_id = self.get_last_message().id
diff --git a/zerver/views/streams.py b/zerver/views/streams.py
index b2937aba3e2..4271e33cd53 100644
--- a/zerver/views/streams.py
+++ b/zerver/views/streams.py
@@ -15,12 +15,14 @@ from typing import (
import orjson
from django.conf import settings
+from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import ValidationError
from django.db import transaction
from django.http import HttpRequest, HttpResponse
from django.utils.translation import override as override_language
from django.utils.translation import ugettext as _
+from zerver.context_processors import get_valid_realm_from_request
from zerver.decorator import (
authenticated_json_view,
require_non_guest_user,
@@ -62,12 +64,17 @@ from zerver.lib.streams import (
access_stream_by_id,
access_stream_by_name,
access_stream_for_delete_or_update,
+ access_web_public_stream,
check_stream_name,
check_stream_name_available,
filter_stream_authorization,
list_to_streams,
)
-from zerver.lib.topic import get_topic_history_for_stream, messages_for_topic
+from zerver.lib.topic import (
+ get_topic_history_for_public_stream,
+ get_topic_history_for_stream,
+ messages_for_topic,
+)
from zerver.lib.types import Validator
from zerver.lib.validator import (
check_bool,
@@ -607,16 +614,35 @@ def get_streams_backend(
return json_success({"streams": streams})
@has_request_variables
-def get_topics_backend(request: HttpRequest, user_profile: UserProfile,
- stream_id: int=REQ(converter=to_non_negative_int,
- path_only=True)) -> HttpResponse:
- (stream, recipient, sub) = access_stream_by_id(user_profile, stream_id)
+def get_topics_backend(
+ request: HttpRequest, maybe_user_profile: Union[UserProfile, AnonymousUser],
+ stream_id: int=REQ(converter=to_non_negative_int,
+ path_only=True)) -> HttpResponse:
+
+ if not maybe_user_profile.is_authenticated:
+ is_web_public_query = True
+ user_profile: Optional[UserProfile] = None
+ else:
+ is_web_public_query = False
+ assert isinstance(maybe_user_profile, UserProfile)
+ user_profile = maybe_user_profile
+ assert user_profile is not None
- result = get_topic_history_for_stream(
- user_profile=user_profile,
- recipient=recipient,
- public_history=stream.is_history_public_to_subscribers(),
- )
+ if is_web_public_query:
+ realm = get_valid_realm_from_request(request)
+ stream = access_web_public_stream(stream_id, realm)
+ result = get_topic_history_for_public_stream(recipient=stream.recipient)
+
+ else:
+ assert user_profile is not None
+
+ (stream, recipient, sub) = access_stream_by_id(user_profile, stream_id)
+
+ result = get_topic_history_for_stream(
+ user_profile=user_profile,
+ recipient=recipient,
+ public_history=stream.is_history_public_to_subscribers(),
+ )
return json_success(dict(topics=result))
diff --git a/zproject/urls.py b/zproject/urls.py
index f433187d8b1..ac02c8936aa 100644
--- a/zproject/urls.py
+++ b/zproject/urls.py
@@ -339,7 +339,8 @@ v1_api_and_json_patterns = [
'DELETE': 'zerver.views.custom_profile_fields.remove_user_custom_profile_data'}),
path('users/me/<int:stream_id>/topics', rest_dispatch,
- {'GET': 'zerver.views.streams.get_topics_backend'}),
+ {'GET': ('zerver.views.streams.get_topics_backend',
+ {'allow_anonymous_user_web'})}),
# streams -> zerver.views.streams