From 28c7a04734e619b2e9e8911bd61e45885c44bec2 Mon Sep 17 00:00:00 2001 From: Vector73 Date: Wed, 11 Sep 2024 22:43:37 +0530 Subject: [PATCH] settings: Add `can_delete_own_message_group` realm setting. Added `can_delete_message_group` realm setting to replace `delete_own_message_policy` property. --- api_docs/changelog.md | 8 ++ version.py | 3 +- web/src/admin.js | 4 +- web/src/server_events_dispatch.js | 1 + web/src/settings_components.ts | 3 + web/src/settings_data.ts | 8 +- web/src/settings_org.js | 63 +++------ web/src/state_data.ts | 2 +- .../organization_permissions_admin.hbs | 12 +- web/tests/popover_menus_data.test.js | 4 +- web/tests/settings_data.test.js | 5 +- zerver/lib/event_schema.py | 1 + zerver/lib/server_initialization.py | 1 - ...0579_realm_can_delete_own_message_group.py | 23 ++++ ..._value_for_can_delete_own_message_group.py | 46 +++++++ ...lter_realm_can_delete_own_message_group.py | 22 +++ zerver/models/realms.py | 17 +++ zerver/models/users.py | 4 +- zerver/openapi/zulip.yaml | 32 +++++ zerver/tests/test_home.py | 1 + zerver/tests/test_message_delete.py | 128 ++++++++---------- zerver/tests/test_message_move_topic.py | 13 +- zerver/views/message_edit.py | 2 +- zerver/views/realm.py | 1 + 24 files changed, 264 insertions(+), 140 deletions(-) create mode 100644 zerver/migrations/0579_realm_can_delete_own_message_group.py create mode 100644 zerver/migrations/0580_set_default_value_for_can_delete_own_message_group.py create mode 100644 zerver/migrations/0581_alter_realm_can_delete_own_message_group.py diff --git a/api_docs/changelog.md b/api_docs/changelog.md index 90e791731b..5cf265a5b4 100644 --- a/api_docs/changelog.md +++ b/api_docs/changelog.md @@ -20,6 +20,14 @@ format used by the Zulip server that they are interacting with. ## Changes in Zulip 10.0 +**Feature level 291** + +* `PATCH /realm`, [`GET /events`](/api/get-events), + [`POST /register`](/api/register-queue): + Added `can_delete_own_message_group` realm setting which is a + [group-setting value](/api/group-setting-values) describing the set of users + with permission to delete the messages that they have sent in the organization. + **Feature level 290** * [`POST /user_groups/{user_group_id}/deactivate`](/api/deactivate-user-group): diff --git a/version.py b/version.py index 3a19db9b2b..eb66ba1072 100644 --- a/version.py +++ b/version.py @@ -34,7 +34,8 @@ DESKTOP_WARNING_VERSION = "5.9.3" # new level means in api_docs/changelog.md, as well as "**Changes**" # entries in the endpoint's documentation in `zulip.yaml`. -API_FEATURE_LEVEL = 290 # Last bumped for UserGroup.deactivated + +API_FEATURE_LEVEL = 291 # Last bumped for can_delete_own_message_group # 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/web/src/admin.js b/web/src/admin.js index b2385120ae..2db7ce4cf3 100644 --- a/web/src/admin.js +++ b/web/src/admin.js @@ -185,9 +185,7 @@ export function build_page() { can_create_user_groups: settings_data.user_can_create_user_groups(), policy_values: settings_config.common_policy_values, realm_can_delete_any_message_group: realm.realm_can_delete_any_message_group, - realm_delete_own_message_policy: realm.realm_delete_own_message_policy, - DELETE_OWN_MESSAGE_POLICY_ADMINS_ONLY: - settings_config.common_message_policy_values.by_admins_only.code, + realm_can_delete_own_message_group: realm.realm_can_delete_own_message_group, ...settings_org.get_organization_settings_options(), demote_inactive_streams_values: settings_config.demote_inactive_streams_values, web_mark_read_on_scroll_policy_values: diff --git a/web/src/server_events_dispatch.js b/web/src/server_events_dispatch.js index f23366248a..96ffd12bf5 100644 --- a/web/src/server_events_dispatch.js +++ b/web/src/server_events_dispatch.js @@ -210,6 +210,7 @@ export function dispatch_normal_event(event) { avatar_changes_disabled: settings_account.update_avatar_change_display, bot_creation_policy: settings_bots.update_bot_permissions_ui, can_delete_any_message_group: noop, + can_delete_own_message_group: noop, create_multiuse_invite_group: noop, invite_to_stream_policy: noop, default_code_block_language: noop, diff --git a/web/src/settings_components.ts b/web/src/settings_components.ts index ee8e137776..a6ad79c33d 100644 --- a/web/src/settings_components.ts +++ b/web/src/settings_components.ts @@ -482,6 +482,7 @@ const dropdown_widget_map = new Map([ ["realm_can_create_private_channel_group", null], ["realm_can_create_web_public_channel_group", null], ["realm_can_delete_any_message_group", null], + ["realm_can_delete_own_message_group", null], ["realm_direct_message_initiator_group", null], ["realm_direct_message_permission_group", null], ]); @@ -802,6 +803,7 @@ export function check_realm_settings_property_changed(elem: HTMLElement): boolea case "realm_can_create_private_channel_group": case "realm_can_create_web_public_channel_group": case "realm_can_delete_any_message_group": + case "realm_can_delete_own_message_group": case "realm_direct_message_initiator_group": case "realm_direct_message_permission_group": proposed_val = get_dropdown_list_widget_setting_value($elem); @@ -1000,6 +1002,7 @@ export function populate_data_for_realm_settings_request( "can_create_public_channel_group", "can_create_web_public_channel_group", "can_delete_any_message_group", + "can_delete_own_message_group", "direct_message_initiator_group", "direct_message_permission_group", ]); diff --git a/web/src/settings_data.ts b/web/src/settings_data.ts index fa6440fd81..29457b938a 100644 --- a/web/src/settings_data.ts +++ b/web/src/settings_data.ts @@ -212,7 +212,13 @@ export function user_can_delete_any_message(): boolean { } export function user_can_delete_own_message(): boolean { - return user_has_permission(realm.realm_delete_own_message_policy); + if (page_params.is_spectator) { + return false; + } + return user_groups.is_user_in_group( + realm.realm_can_delete_own_message_group, + current_user.user_id, + ); } export function should_mask_unread_count(sub_muted: boolean): boolean { diff --git a/web/src/settings_org.js b/web/src/settings_org.js index 8e8a85db29..88409f4a33 100644 --- a/web/src/settings_org.js +++ b/web/src/settings_org.js @@ -266,41 +266,19 @@ function message_delete_limit_setting_enabled() { // should be enabled. The setting is disabled when every user // who is allowed to delete their own messages is also allowed // to delete any message in the organization. - const realm_delete_own_message_policy = Number.parseInt( - $("#id_realm_delete_own_message_policy").val(), - 10, - ); + const realm_can_delete_own_message_group_id = + settings_components.get_dropdown_list_widget_setting_value( + $("#id_realm_can_delete_own_message_group"), + ); const realm_can_delete_any_message_group_id = settings_components.get_dropdown_list_widget_setting_value( $("#id_realm_can_delete_any_message_group"), ); - const realm_can_delete_any_message_group_name = user_groups.get_user_group_from_id( - realm_can_delete_any_message_group_id, - ).name; - const common_message_policy_values = settings_config.common_message_policy_values; - - if (realm_delete_own_message_policy === common_message_policy_values.by_admins_only.code) { - return false; - } - if (realm_can_delete_any_message_group_name === "role:administrators") { - return true; - } - if (realm_delete_own_message_policy === common_message_policy_values.by_moderators_only.code) { - return false; - } - if (realm_can_delete_any_message_group_name === "role:moderators") { - return true; - } - if (realm_delete_own_message_policy === common_message_policy_values.by_full_members.code) { - return false; - } - if (realm_can_delete_any_message_group_name === "role:fullmembers") { - return true; - } - if (realm_delete_own_message_policy === common_message_policy_values.by_members.code) { - return false; - } - return true; + const can_delete_any_message_subgroups = user_groups.get_recursive_subgroups( + user_groups.get_user_group_from_id(realm_can_delete_any_message_group_id), + ); + can_delete_any_message_subgroups.add(realm_can_delete_any_message_group_id); + return !can_delete_any_message_subgroups.has(realm_can_delete_own_message_group_id); } function check_disable_message_delete_limit_setting_dropdown() { @@ -318,11 +296,6 @@ function check_disable_message_delete_limit_setting_dropdown() { } } -function set_delete_own_message_policy_dropdown(setting_value) { - $("#id_realm_delete_own_message_policy").val(setting_value); - check_disable_message_delete_limit_setting_dropdown(); -} - function set_msg_delete_limit_dropdown() { settings_components.set_time_limit_setting("realm_message_content_delete_limit_seconds"); } @@ -488,8 +461,8 @@ function update_dependent_subsettings(property_name) { case "realm_can_delete_any_message_group": check_disable_message_delete_limit_setting_dropdown(); break; - case "realm_delete_own_message_policy": - set_delete_own_message_policy_dropdown(realm.realm_delete_own_message_policy); + case "realm_can_delete_own_message_group": + check_disable_message_delete_limit_setting_dropdown(); break; case "realm_org_join_restrictions": set_org_join_restrictions_dropdown(); @@ -542,6 +515,7 @@ export function discard_realm_property_element_changes(elem) { case "realm_can_create_private_channel_group": case "realm_can_create_web_public_channel_group": case "realm_can_delete_any_message_group": + case "realm_can_delete_own_message_group": settings_components.set_dropdown_list_widget_setting_value( property_name, property_value, @@ -861,7 +835,10 @@ export function set_up_dropdown_widget_for_realm_group_settings() { if (setting_name === "direct_message_permission_group") { dropdown_list_item_click_callback = check_disable_direct_message_initiator_group_dropdown; - } else if (setting_name === "can_delete_any_message_group") { + } else if ( + setting_name === "can_delete_any_message_group" || + setting_name === "can_delete_own_message_group" + ) { dropdown_list_item_click_callback = check_disable_message_delete_limit_setting_dropdown; } set_up_dropdown_widget( @@ -1044,7 +1021,6 @@ export function build_page() { set_msg_move_limit_setting("realm_move_messages_within_stream_limit_seconds"); set_msg_move_limit_setting("realm_move_messages_between_streams_limit_seconds"); set_msg_delete_limit_dropdown(); - set_delete_own_message_policy_dropdown(realm.realm_delete_own_message_policy); set_message_retention_setting_dropdown(); set_org_join_restrictions_dropdown(); set_message_content_in_email_notifications_visibility(); @@ -1153,11 +1129,6 @@ export function build_page() { ); }); - $("#id_realm_delete_own_message_policy").on("change", (e) => { - const setting_value = Number.parseInt($(e.target).val(), 10); - set_delete_own_message_policy_dropdown(setting_value); - }); - $("#id_realm_org_join_restrictions").on("click", (e) => { // This prevents the disappearance of modal when there are // no allowed domains otherwise it gets closed due to @@ -1225,6 +1196,8 @@ export function build_page() { }); } + check_disable_message_delete_limit_setting_dropdown(); + realm_icon.build_realm_icon_widget(upload_realm_logo_or_icon, null, true); if (realm.zulip_plan_is_not_limited) { realm_logo.build_realm_logo_widget(upload_realm_logo_or_icon, false); diff --git a/web/src/state_data.ts b/web/src/state_data.ts index 60aef3a63e..50bfb697bb 100644 --- a/web/src/state_data.ts +++ b/web/src/state_data.ts @@ -269,6 +269,7 @@ const realm_schema = z.object({ realm_can_create_private_channel_group: z.number(), realm_can_create_web_public_channel_group: z.number(), realm_can_delete_any_message_group: z.number(), + realm_can_delete_own_message_group: z.number(), realm_create_multiuse_invite_group: z.number(), realm_create_private_stream_policy: z.number(), realm_date_created: z.number(), @@ -283,7 +284,6 @@ const realm_schema = z.object({ }), ), realm_default_language: z.string(), - realm_delete_own_message_policy: z.number(), realm_description: z.string(), realm_digest_emails_enabled: NOT_TYPED_YET, realm_digest_weekday: NOT_TYPED_YET, diff --git a/web/templates/settings/organization_permissions_admin.hbs b/web/templates/settings/organization_permissions_admin.hbs index a711531034..70f42bf4cd 100644 --- a/web/templates/settings/organization_permissions_admin.hbs +++ b/web/templates/settings/organization_permissions_admin.hbs @@ -241,14 +241,10 @@ label=(t 'Who can delete any message') value_type="number" }} -
- - -
+ {{> ../dropdown_widget_with_label + widget_name="realm_can_delete_own_message_group" + label=(t 'Who can delete their own messages') + value_type="number" }}