From 654ce3e0c7722d11c3935b80058665029c93f93a Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Thu, 2 Jan 2014 17:20:33 -0500 Subject: [PATCH] Add make_stream_private endpoint. (imported from commit b504c24fd651ecf8814e64725a9f303dc085b14e) --- zerver/lib/actions.py | 11 +++++++++++ zerver/tests.py | 16 ++++++++++++++++ zerver/views/__init__.py | 9 ++++++++- zproject/urls.py | 1 + 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 73aefd7431..bca954d7eb 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -1116,6 +1116,17 @@ def do_make_stream_public(user_profile, realm, stream_name): stream.save(update_fields=['invite_only']) return {} +def do_make_stream_private(realm, stream_name): + stream_name = stream_name.strip() + stream = get_stream(stream_name, realm) + + if not stream: + raise JsonableError('Unknown stream "%s"' % (stream_name,)) + + stream.invite_only = True + stream.save(update_fields=['invite_only']) + return {} + def do_rename_stream(realm, old_name, new_name, log=True): old_name = old_name.strip() new_name = new_name.strip() diff --git a/zerver/tests.py b/zerver/tests.py index b3aff70b80..e9d8fe98c3 100644 --- a/zerver/tests.py +++ b/zerver/tests.py @@ -534,6 +534,22 @@ class StreamAdminTest(AuthedTestCase): stream = Stream.objects.get(name='private_stream', realm=realm) self.assertFalse(stream.invite_only) + def test_make_stream_private(self): + email = 'hamlet@zulip.com' + self.login(email) + user_profile = get_user_profile_by_email(email) + realm = user_profile.realm + stream, _ = create_stream_if_needed(realm, 'public_stream') + + assign_perm('administer', user_profile, realm) + params = { + 'stream_name': 'public_stream' + } + result = self.client.post("/json/make_stream_private", params) + self.assert_json_success(result) + stream = Stream.objects.get(name='public_stream', realm=realm) + self.assertTrue(stream.invite_only) + class PermissionTest(TestCase): def test_get_admin_users(self): user_profile = get_user_profile_by_email('hamlet@zulip.com') diff --git a/zerver/views/__init__.py b/zerver/views/__init__.py index 95fd1f4b95..894b6ed667 100644 --- a/zerver/views/__init__.py +++ b/zerver/views/__init__.py @@ -40,7 +40,8 @@ from zerver.lib.actions import bulk_remove_subscriptions, \ do_add_alert_words, do_remove_alert_words, do_set_alert_words, get_subscriber_emails, \ do_set_muted_topics, do_rename_stream, clear_followup_emails_queue, \ notify_for_streams_by_default, do_change_enable_offline_push_notifications, \ - do_deactivate_stream, do_change_autoscroll_forever, do_make_stream_public + do_deactivate_stream, do_change_autoscroll_forever, do_make_stream_public, \ + do_make_stream_private from zerver.lib.create_user import random_api_key from zerver.lib.push_notifications import num_push_devices_for_user from zerver.forms import RegistrationForm, HomepageForm, ToSForm, \ @@ -1585,6 +1586,12 @@ def json_rename_stream(request, user_profile, old_name=REQ, new_name=REQ): def json_make_stream_public(request, user_profile, stream_name=REQ): return json_success(do_make_stream_public(user_profile, user_profile.realm, stream_name)) +@authenticated_json_post_view +@require_realm_admin +@has_request_variables +def json_make_stream_private(request, user_profile, stream_name=REQ): + return json_success(do_make_stream_private(user_profile.realm, stream_name)) + def list_subscriptions_backend(request, user_profile): return json_success({"subscriptions": gather_subscriptions(user_profile)[0]}) diff --git a/zproject/urls.py b/zproject/urls.py index 3290531f0b..d85e5e9101 100644 --- a/zproject/urls.py +++ b/zproject/urls.py @@ -108,6 +108,7 @@ urlpatterns += patterns('zerver.views', url(r'^json/get_public_streams$', 'json_get_public_streams'), url(r'^json/rename_stream$', 'json_rename_stream'), url(r'^json/make_stream_public$', 'json_make_stream_public'), + url(r'^json/make_stream_private$', 'json_make_stream_private'), url(r'^json/send_message$', 'json_send_message'), url(r'^json/invite_users$', 'json_invite_users'), url(r'^json/bulk_invite_users$', 'json_bulk_invite_users'),