From bd78b1ff90b79a7b14139031d2a34130a1d1f049 Mon Sep 17 00:00:00 2001 From: sahil839 Date: Tue, 20 Apr 2021 00:26:15 +0530 Subject: [PATCH] events: Add 'is_moderator' field to the page_params object sent to clients. --- templates/zerver/api/changelog.md | 3 +++ version.py | 2 +- zerver/lib/events.py | 2 ++ zerver/openapi/zulip.yaml | 8 ++++++++ zerver/tests/test_events.py | 16 ++++++++++++++++ zerver/tests/test_home.py | 1 + 6 files changed, 31 insertions(+), 1 deletion(-) diff --git a/templates/zerver/api/changelog.md b/templates/zerver/api/changelog.md index 76bb546d9e..c86b38446b 100644 --- a/templates/zerver/api/changelog.md +++ b/templates/zerver/api/changelog.md @@ -12,6 +12,9 @@ below features are supported. **Feature level 60** +* [`POST /register`](/api/register-queue): Added a new boolean field + `is_moderator`, similar to the existing `is_admin`, `is_owner` and + `is_guest` fields, to the response. * [`PATCH /users/{user_id}`](/api/update-user): Added support for changing a user's organization-level role to moderator. * API endpoints that return `role` values can now return `300`, the diff --git a/version.py b/version.py index 5589ccbe2e..ee9f9816f9 100644 --- a/version.py +++ b/version.py @@ -30,7 +30,7 @@ DESKTOP_WARNING_VERSION = "5.2.0" # # Changes should be accompanied by documentation explaining what the # new level means in templates/zerver/api/changelog.md. -API_FEATURE_LEVEL = 59 +API_FEATURE_LEVEL = 60 # Bump the minor PROVISION_VERSION to indicate that folks should provision # only when going from an old version of the code to a newer version. Bump diff --git a/zerver/lib/events.py b/zerver/lib/events.py index 6607606a0e..dc674582e4 100644 --- a/zerver/lib/events.py +++ b/zerver/lib/events.py @@ -337,6 +337,7 @@ def fetch_initial_state_data( state["can_invite_others_to_realm"] = settings_user.can_invite_others_to_realm() state["is_admin"] = settings_user.is_realm_admin state["is_owner"] = settings_user.is_realm_owner + state["is_moderator"] = settings_user.is_moderator state["is_guest"] = settings_user.is_guest state["user_id"] = settings_user.id state["enter_sends"] = settings_user.enter_sends @@ -609,6 +610,7 @@ def apply_event( if "role" in person: state["is_admin"] = is_administrator_role(person["role"]) state["is_owner"] = person["role"] == UserProfile.ROLE_REALM_OWNER + state["is_moderator"] = person["role"] == UserProfile.ROLE_MODERATOR state["is_guest"] = person["role"] == UserProfile.ROLE_GUEST # Recompute properties based on is_admin/is_guest state["can_create_streams"] = user_profile.can_create_streams() diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml index f9d622029a..e594b33bd5 100644 --- a/zerver/openapi/zulip.yaml +++ b/zerver/openapi/zulip.yaml @@ -8935,6 +8935,14 @@ paths: Present if `realm_user` is present in `fetch_event_types`. Whether the current user is an [organization owner](/help/roles-and-permissions). + is_moderator: + type: boolean + description: | + Present if `realm_user` is present in `fetch_event_types`. + + Whether the current user is an [organization moderator](/help/roles-and-permissions). + + **Changes**: New in Zulip 4.0 (feature level 60). is_guest: type: boolean description: | diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index e33daf5e67..6b8e4c6cc4 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -1303,6 +1303,22 @@ class NormalActionsTest(BaseAction): check_realm_user_update("events[0]", events[0], "role") self.assertEqual(events[0]["person"]["role"], role) + def test_change_is_moderator(self) -> None: + reset_emails_in_zulip_realm() + + # Important: We need to refresh from the database here so that + # we don't have a stale UserProfile object with an old value + # for email being passed into this next function. + self.user_profile.refresh_from_db() + + do_change_user_role(self.user_profile, UserProfile.ROLE_MEMBER, acting_user=None) + for role in [UserProfile.ROLE_MODERATOR, UserProfile.ROLE_MEMBER]: + events = self.verify_action( + lambda: do_change_user_role(self.user_profile, role, acting_user=None) + ) + check_realm_user_update("events[0]", events[0], "role") + self.assertEqual(events[0]["person"]["role"], role) + def test_change_is_guest(self) -> None: reset_emails_in_zulip_realm() diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.py index d5b16d09ba..1bea988b92 100644 --- a/zerver/tests/test_home.py +++ b/zerver/tests/test_home.py @@ -97,6 +97,7 @@ class HomeTest(ZulipTestCase): "insecure_desktop_app", "is_admin", "is_guest", + "is_moderator", "is_owner", "is_web_public_visitor", "jitsi_server_url",