api: Allow realm_admins to make a stream announcement_only.

This commit is contained in:
Shubham Padia
2018-05-12 10:55:42 +05:30
committed by Tim Abbott
parent bb8577ba94
commit 897ed17f0c
3 changed files with 34 additions and 1 deletions

View File

@@ -2977,6 +2977,10 @@ def do_change_stream_web_public(stream: Stream, is_web_public: bool) -> None:
stream.is_web_public = is_web_public
stream.save(update_fields=['is_web_public'])
def do_change_stream_announcement_only(stream: Stream, is_announcement_only: bool) -> None:
stream.is_announcement_only = is_announcement_only
stream.save(update_fields=['is_announcement_only'])
def do_rename_stream(stream: Stream, new_name: str, log: bool=True) -> Dict[str, str]:
old_name = stream.name
stream.name = new_name

View File

@@ -661,6 +661,32 @@ class StreamAdminTest(ZulipTestCase):
{'description': ujson.dumps('Test description')})
self.assert_json_error(result, 'Must be an organization administrator')
def test_change_stream_announcement_only(self) -> None:
user_profile = self.example_user('hamlet')
self.login(user_profile.email)
self.subscribe(user_profile, 'stream_name1')
do_change_is_admin(user_profile, True)
stream_id = get_stream('stream_name1', user_profile.realm).id
result = self.client_patch('/json/streams/%d' % (stream_id,),
{'is_announcement_only': ujson.dumps(True)})
self.assert_json_success(result)
stream = get_stream('stream_name1', user_profile.realm)
self.assertEqual(True, stream.is_announcement_only)
def test_change_stream_announcement_only_requires_realm_admin(self) -> None:
user_profile = self.example_user('hamlet')
self.login(user_profile.email)
self.subscribe(user_profile, 'stream_name1')
do_change_is_admin(user_profile, False)
stream_id = get_stream('stream_name1', user_profile.realm).id
result = self.client_patch('/json/streams/%d' % (stream_id,),
{'is_announcement_only': ujson.dumps(True)})
self.assert_json_error(result, 'Must be an organization administrator')
def set_up_stream_for_deletion(self, stream_name: str, invite_only: bool=False,
subscribed: bool=True) -> Stream:
"""

View File

@@ -21,7 +21,7 @@ from zerver.lib.actions import bulk_remove_subscriptions, \
do_create_default_stream_group, do_add_streams_to_default_stream_group, \
do_remove_streams_from_default_stream_group, do_remove_default_stream_group, \
do_change_default_stream_group_description, do_change_default_stream_group_name, \
prep_stream_welcome_message
prep_stream_welcome_message, do_change_stream_announcement_only
from zerver.lib.response import json_success, json_error, json_response
from zerver.lib.streams import access_stream_by_id, access_stream_by_name, \
check_stream_name, check_stream_name_available, filter_stream_authorization, \
@@ -147,6 +147,7 @@ def update_stream_backend(
description: Optional[str]=REQ(validator=check_capped_string(
Stream.MAX_DESCRIPTION_LENGTH), default=None),
is_private: Optional[bool]=REQ(validator=check_bool, default=None),
is_announcement_only: Optional[bool]=REQ(validator=check_bool, default=None),
history_public_to_subscribers: Optional[bool]=REQ(validator=check_bool, default=None),
new_name: Optional[str]=REQ(validator=check_string, default=None),
) -> HttpResponse:
@@ -164,6 +165,8 @@ def update_stream_backend(
# are only changing the casing of the stream name).
check_stream_name_available(user_profile.realm, new_name)
do_rename_stream(stream, new_name)
if is_announcement_only is not None:
do_change_stream_announcement_only(stream, is_announcement_only)
# But we require even realm administrators to be actually
# subscribed to make a private stream public.