streams: Make /streams endpoint return also web-public streams.

This commit modifies the /streams endpoint so that the web-public
streams are included in the default list of streams that users
have access to.

This is part of PR #14638 that aims to allow guest users to
browse and subscribe themselves to web public streams.
This commit is contained in:
Clara Dantas
2020-07-24 12:50:36 -03:00
committed by Tim Abbott
parent 0994b029d6
commit a9af80d7a2
4 changed files with 36 additions and 1 deletions

View File

@@ -5520,7 +5520,7 @@ def get_web_public_streams(realm: Realm) -> List[Dict[str, Any]]:
return streams
def do_get_streams(
user_profile: UserProfile, include_public: bool=True,
user_profile: UserProfile, include_public: bool=True, include_web_public: bool=False,
include_subscribed: bool=True, include_all_active: bool=False,
include_default: bool=False, include_owner_subscribed: bool=False,
) -> List[Dict[str, Any]]:
@@ -5552,6 +5552,9 @@ def do_get_streams(
if include_public:
invite_only_check = Q(invite_only=False)
add_filter_option(invite_only_check)
if include_web_public:
web_public_check = Q(is_web_public=True)
add_filter_option(web_public_check)
if include_owner_subscribed and user_profile.is_bot:
bot_owner = user_profile.bot_owner
assert bot_owner is not None

View File

@@ -4315,6 +4315,14 @@ paths:
type: boolean
default: true
example: false
- name: include_web_public
in: query
description: |
Include all web public streams.
schema:
type: boolean
default: false
example: true
- name: include_subscribed
in: query
description: |

View File

@@ -1508,6 +1508,28 @@ class DefaultStreamTest(ZulipTestCase):
self.assert_json_success(result)
self.assertFalse(stream_name in self.get_default_stream_names(user_profile.realm))
def test_guest_user_access_to_streams(self) -> None:
user_profile = self.example_user("polonius")
self.login_user(user_profile)
self.assertEqual(user_profile.role, UserProfile.ROLE_GUEST)
# Get all the streams that Polonius has access to (subscribed + web public streams)
result = self.client_get('/json/streams?include_web_public=true')
streams = result.json()['streams']
subscribed, unsubscribed, never_subscribed = gather_subscriptions_helper(user_profile)
self.assertEqual(len(streams),
len(subscribed) + len(unsubscribed) + len(never_subscribed))
expected_streams = subscribed + unsubscribed + never_subscribed
stream_names = [
stream['name']
for stream in streams
]
expected_stream_names = [
stream['name']
for stream in expected_streams
]
self.assertEqual(set(stream_names), set(expected_stream_names))
class DefaultStreamGroupTest(ZulipTestCase):
def test_create_update_and_remove_default_stream_group(self) -> None:
realm = get_realm("zulip")

View File

@@ -597,6 +597,7 @@ def get_subscribers_backend(request: HttpRequest, user_profile: UserProfile,
def get_streams_backend(
request: HttpRequest, user_profile: UserProfile,
include_public: bool=REQ(validator=check_bool, default=True),
include_web_public: bool=REQ(validator=check_bool, default=False),
include_subscribed: bool=REQ(validator=check_bool, default=True),
include_all_active: bool=REQ(validator=check_bool, default=False),
include_default: bool=REQ(validator=check_bool, default=False),
@@ -604,6 +605,7 @@ def get_streams_backend(
) -> HttpResponse:
streams = do_get_streams(user_profile, include_public=include_public,
include_web_public=include_web_public,
include_subscribed=include_subscribed,
include_all_active=include_all_active,
include_default=include_default,