models: Replace add_emoji_by_admins_only with add_custom_emoji_policy.

This commit replaces boolean field add_emoji_by_admins_only with an
integer field add_custom_emoji_policy as we would also add full members
and moderators option for this setting in further commits.
This commit is contained in:
sahil839 2021-05-04 22:32:24 +05:30 committed by Tim Abbott
parent 17749cb608
commit 4b1313a92b
22 changed files with 167 additions and 50 deletions

View File

@ -165,7 +165,8 @@ function test_submit_settings_form(override, submit_form) {
realm_bot_creation_policy: settings_bots.bot_creation_policy_values.restricted.code, realm_bot_creation_policy: settings_bots.bot_creation_policy_values.restricted.code,
realm_email_address_visibility: realm_email_address_visibility:
settings_config.email_address_visibility_values.admins_only.code, settings_config.email_address_visibility_values.admins_only.code,
realm_add_emoji_by_admins_only: true, realm_add_custom_emoji_policy:
settings_config.add_custom_emoji_policy_values.by_admins_only.code,
realm_create_stream_by_admins_only: true, realm_create_stream_by_admins_only: true,
realm_waiting_period_threshold: 1, realm_waiting_period_threshold: 1,
realm_default_language: '"es"', realm_default_language: '"es"',
@ -210,9 +211,10 @@ function test_submit_settings_form(override, submit_form) {
create_stream_policy_elem.attr("id", "id_realm_create_stream_policy"); create_stream_policy_elem.attr("id", "id_realm_create_stream_policy");
create_stream_policy_elem.data = () => "number"; create_stream_policy_elem.data = () => "number";
const add_emoji_by_admins_only_elem = $("#id_realm_add_emoji_by_admins_only"); const add_custom_emoji_policy_elem = $("#id_realm_add_custom_emoji_policy");
add_emoji_by_admins_only_elem.val("by_anyone"); add_custom_emoji_policy_elem.val("1");
add_emoji_by_admins_only_elem.attr("id", "id_realm_add_emoji_by_admins_only"); add_custom_emoji_policy_elem.attr("id", "id_realm_add_custom_emoji_policy");
add_custom_emoji_policy_elem.data = () => "number";
const bot_creation_policy_elem = $("#id_realm_bot_creation_policy"); const bot_creation_policy_elem = $("#id_realm_bot_creation_policy");
bot_creation_policy_elem.val("1"); bot_creation_policy_elem.val("1");
@ -233,7 +235,7 @@ function test_submit_settings_form(override, submit_form) {
subsection_elem.set_find_results(".prop-element", [ subsection_elem.set_find_results(".prop-element", [
bot_creation_policy_elem, bot_creation_policy_elem,
email_address_visibility_elem, email_address_visibility_elem,
add_emoji_by_admins_only_elem, add_custom_emoji_policy_elem,
create_stream_policy_elem, create_stream_policy_elem,
invite_to_stream_policy_elem, invite_to_stream_policy_elem,
]); ]);
@ -246,7 +248,7 @@ function test_submit_settings_form(override, submit_form) {
bot_creation_policy: 1, bot_creation_policy: 1,
invite_to_stream_policy: 1, invite_to_stream_policy: 1,
email_address_visibility: 1, email_address_visibility: 1,
add_emoji_by_admins_only: false, add_custom_emoji_policy: 1,
create_stream_policy: 2, create_stream_policy: 2,
}; };
assert.deepEqual(data, expected_value); assert.deepEqual(data, expected_value);

View File

@ -76,8 +76,10 @@ export function build_page() {
realm_name_changes_disabled: page_params.realm_name_changes_disabled, realm_name_changes_disabled: page_params.realm_name_changes_disabled,
realm_email_changes_disabled: page_params.realm_email_changes_disabled, realm_email_changes_disabled: page_params.realm_email_changes_disabled,
realm_avatar_changes_disabled: page_params.realm_avatar_changes_disabled, realm_avatar_changes_disabled: page_params.realm_avatar_changes_disabled,
realm_add_emoji_by_admins_only: page_params.realm_add_emoji_by_admins_only, realm_add_custom_emoji_policy: page_params.realm_add_custom_emoji_policy,
can_add_emojis: settings_emoji.can_add_emoji(), can_add_emojis: settings_emoji.can_add_emoji(),
ADD_CUSTOM_EMOJI_POLICY_ADMINS_ONLY:
settings_config.add_custom_emoji_policy_values.by_admins_only.code,
realm_message_content_edit_limit_minutes: settings_org.get_realm_time_limits_in_minutes( realm_message_content_edit_limit_minutes: settings_org.get_realm_time_limits_in_minutes(
"realm_message_content_edit_limit_seconds", "realm_message_content_edit_limit_seconds",
), ),

View File

@ -176,7 +176,7 @@ export function dispatch_normal_event(event) {
case "realm": { case "realm": {
const realm_settings = { const realm_settings = {
add_emoji_by_admins_only: settings_emoji.update_custom_emoji_ui, add_custom_emoji_policy: settings_emoji.update_custom_emoji_ui,
allow_edit_history: noop, allow_edit_history: noop,
allow_message_deleting: noop, allow_message_deleting: noop,
allow_message_editing: noop, allow_message_editing: noop,

View File

@ -158,6 +158,19 @@ export const invite_to_realm_policy_values = {
}, },
}; };
export const add_custom_emoji_policy_values = {
by_admins_only: {
order: 1,
code: 2,
description: $t({defaultMessage: "Admins"}),
},
by_members: {
order: 2,
code: 1,
description: $t({defaultMessage: "Admins and members"}),
},
};
export const private_message_policy_values = { export const private_message_policy_values = {
by_anyone: { by_anyone: {
order: 1, order: 1,

View File

@ -13,6 +13,7 @@ import * as ListWidget from "./list_widget";
import * as loading from "./loading"; import * as loading from "./loading";
import {page_params} from "./page_params"; import {page_params} from "./page_params";
import * as people from "./people"; import * as people from "./people";
import * as settings_config from "./settings_config";
import * as ui from "./ui"; import * as ui from "./ui";
import * as ui_report from "./ui_report"; import * as ui_report from "./ui_report";
import * as upload_widget from "./upload_widget"; import * as upload_widget from "./upload_widget";
@ -31,7 +32,10 @@ export function can_add_emoji() {
} }
// for normal users, we depend on the setting // for normal users, we depend on the setting
return !page_params.realm_add_emoji_by_admins_only; return (
page_params.realm_add_custom_emoji_policy ===
settings_config.add_custom_emoji_policy_values.by_members.code
);
} }
function can_delete_emoji(emoji) { function can_delete_emoji(emoji) {
@ -50,10 +54,15 @@ function can_delete_emoji(emoji) {
export function update_custom_emoji_ui() { export function update_custom_emoji_ui() {
const rendered_tip = render_settings_emoji_settings_tip({ const rendered_tip = render_settings_emoji_settings_tip({
realm_add_emoji_by_admins_only: page_params.realm_add_emoji_by_admins_only, ADD_CUSTOM_EMOJI_POLICY_ADMINS_ONLY:
settings_config.add_custom_emoji_policy_values.by_admins_only.code,
}); });
$("#emoji-settings").find(".emoji-settings-tip-container").html(rendered_tip); $("#emoji-settings").find(".emoji-settings-tip-container").html(rendered_tip);
if (page_params.realm_add_emoji_by_admins_only && !page_params.is_admin) { if (
page_params.realm_add_custom_emoji_policy ===
settings_config.add_custom_emoji_policy_values.by_admins_only.code &&
!page_params.is_admin
) {
$(".add-emoji-text").hide(); $(".add-emoji-text").hide();
$(".admin-emoji-form").hide(); $(".admin-emoji-form").hide();
} else { } else {

View File

@ -108,6 +108,9 @@ export function get_organization_settings_options() {
options.invite_to_realm_policy_values = get_sorted_options_list( options.invite_to_realm_policy_values = get_sorted_options_list(
settings_config.invite_to_realm_policy_values, settings_config.invite_to_realm_policy_values,
); );
options.add_custom_emoji_policy_values = get_sorted_options_list(
settings_config.add_custom_emoji_policy_values,
);
return options; return options;
} }
@ -138,13 +141,6 @@ function get_property_value(property_name) {
return "custom_days"; return "custom_days";
} }
if (property_name === "realm_add_emoji_by_admins_only") {
if (page_params.realm_add_emoji_by_admins_only) {
return "by_admins_only";
}
return "by_anyone";
}
if (property_name === "realm_msg_edit_limit_setting") { if (property_name === "realm_msg_edit_limit_setting") {
if (!page_params.realm_allow_message_editing) { if (!page_params.realm_allow_message_editing) {
return "never"; return "never";
@ -207,7 +203,7 @@ const simple_dropdown_properties = [
"realm_invite_to_stream_policy", "realm_invite_to_stream_policy",
"realm_user_group_edit_policy", "realm_user_group_edit_policy",
"realm_private_message_policy", "realm_private_message_policy",
"realm_add_emoji_by_admins_only", "realm_add_custom_emoji_policy",
"realm_invite_to_realm_policy", "realm_invite_to_realm_policy",
"realm_wildcard_mention_policy", "realm_wildcard_mention_policy",
"realm_move_messages_between_streams_policy", "realm_move_messages_between_streams_policy",
@ -831,18 +827,6 @@ export function build_page() {
data.default_code_block_language = code_block_language_value; data.default_code_block_language = code_block_language_value;
break; break;
} }
case "other_permissions": {
const add_emoji_permission = $("#id_realm_add_emoji_by_admins_only").val();
switch (add_emoji_permission) {
case "by_admins_only":
data.add_emoji_by_admins_only = true;
break;
case "by_anyone":
data.add_emoji_by_admins_only = false;
break;
}
break;
}
case "org_join": { case "org_join": {
const org_join_restrictions = $("#id_realm_org_join_restrictions").val(); const org_join_restrictions = $("#id_realm_org_join_restrictions").val();
switch (org_join_restrictions) { switch (org_join_restrictions) {

View File

@ -1566,7 +1566,7 @@ body:not(.night-mode) #settings_page .custom_user_field .datepicker {
#id_realm_create_stream_policy, #id_realm_create_stream_policy,
#id_realm_invite_to_stream_policy, #id_realm_invite_to_stream_policy,
#id_realm_private_message_policy, #id_realm_private_message_policy,
#id_realm_add_emoji_by_admins_only, #id_realm_add_custom_emoji_policy,
#id_realm_user_group_edit_policy, #id_realm_user_group_edit_policy,
#id_realm_email_address_visibility, #id_realm_email_address_visibility,
#id_realm_wildcard_mention_policy, #id_realm_wildcard_mention_policy,

View File

@ -1,7 +1,7 @@
{{#if is_guest}} {{#if is_guest}}
<div class='tip'>{{t "Guests cannot edit custom emoji." }}</div> <div class='tip'>{{t "Guests cannot edit custom emoji." }}</div>
{{else}} {{else}}
{{#if realm_add_emoji_by_admins_only}} {{#if (eq realm_add_custom_emoji_policy ADD_CUSTOM_EMOJI_POLICY_ADMINS_ONLY) }}
<div class='tip'>{{t "Only organization administrators can add custom emoji in this organization." }}</div> <div class='tip'>{{t "Only organization administrators can add custom emoji in this organization." }}</div>
{{else}} {{else}}
<div class='tip'>{{t "Any member of this organization can add custom emoji." }}</div> <div class='tip'>{{t "Any member of this organization can add custom emoji." }}</div>

View File

@ -212,10 +212,9 @@
</div> </div>
<div class="input-group"> <div class="input-group">
<label for="realm_add_emoji_by_admins_only" class="dropdown-title">{{t "Who can add custom emoji" }}</label> <label for="realm_add_custom_emoji_policy" class="dropdown-title">{{t "Who can add custom emoji" }}</label>
<select name="realm_add_emoji_by_admins_only" id="id_realm_add_emoji_by_admins_only" class="prop-element"> <select name="realm_add_cutsom_emoji_policy" class="setting-widget prop-element" id="id_realm_add_custom_emoji_policy" data-setting-widget-type="number">
<option value="by_admins_only">{{t "Admins" }}</option> {{> dropdown_options_widget option_values=add_custom_emoji_policy_values}}
<option value="by_anyone">{{t "Admins and members" }}</option>
</select> </select>
</div> </div>

View File

@ -11,6 +11,11 @@ below features are supported.
## Changes in Zulip 5.0 ## Changes in Zulip 5.0
**Feature level 85**
* [`POST /register`](/api/register-queue), `PATCH /realm`: Replaced `add_emoji_by_admins_only`
field with an integer field `add_custom_emoji_policy`.
**Feature level 84** **Feature level 84**
* [`POST /register`](/api/register-queue): The `enter_sends` setting * [`POST /register`](/api/register-queue): The `enter_sends` setting

View File

@ -33,7 +33,7 @@ DESKTOP_WARNING_VERSION = "5.4.3"
# Changes should be accompanied by documentation explaining what the # Changes should be accompanied by documentation explaining what the
# new level means in templates/zerver/api/changelog.md, as well as # new level means in templates/zerver/api/changelog.md, as well as
# "**Changes**" entries in the endpoint's documentation in `zulip.yaml`. # "**Changes**" entries in the endpoint's documentation in `zulip.yaml`.
API_FEATURE_LEVEL = 84 API_FEATURE_LEVEL = 85
# Bump the minor PROVISION_VERSION to indicate that folks should provision # 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 # only when going from an old version of the code to a newer version. Bump

View File

@ -90,7 +90,7 @@ def check_add_emoji_admin(user_profile: UserProfile) -> None:
# Realm administrators can always add emoji # Realm administrators can always add emoji
if user_profile.is_realm_admin: if user_profile.is_realm_admin:
return return
if user_profile.realm.add_emoji_by_admins_only: if user_profile.realm.add_custom_emoji_policy == Realm.ADD_CUSTOM_EMOJI_ADMINS_ONLY:
raise OrganizationAdministratorRequired() raise OrganizationAdministratorRequired()

View File

@ -0,0 +1,18 @@
# Generated by Django 3.2.2 on 2021-05-15 18:00
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("zerver", "0336_userstatus_status_emoji"),
]
operations = [
migrations.AddField(
model_name="realm",
name="add_custom_emoji_policy",
field=models.PositiveSmallIntegerField(default=1),
),
]

View File

@ -0,0 +1,48 @@
# Generated by Django 3.2.2 on 2021-05-15 18:01
from django.db import migrations
from django.db.backends.postgresql.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps
def migrate_to_add_custom_emoji_policy(
apps: StateApps, schema_editor: DatabaseSchemaEditor
) -> None:
Realm = apps.get_model("zerver", "Realm")
Realm.ADD_CUSTOM_EMOJI_MEMBERS_ONLY = 1
Realm.ADD_CUSTOM_EMOJI_ADMINS_ONLY = 2
Realm.objects.filter(add_emoji_by_admins_only=False).update(
add_custom_emoji_policy=Realm.ADD_CUSTOM_EMOJI_MEMBERS_ONLY
)
Realm.objects.filter(add_emoji_by_admins_only=True).update(
add_custom_emoji_policy=Realm.ADD_CUSTOM_EMOJI_ADMINS_ONLY
)
def reverse_migrate_to_add_custom_emoji_policy(
apps: StateApps, schema_editor: DatabaseSchemaEditor
) -> None:
Realm = apps.get_model("zerver", "Realm")
Realm.ADD_CUSTOM_EMOJI_MEMBERS_ONLY = 1
Realm.ADD_CUSTOM_EMOJI_ADMINS_ONLY = 2
Realm.objects.filter(add_custom_emoji_policy=Realm.ADD_CUSTOM_EMOJI_MEMBERS_ONLY).update(
add_emoji_by_admins_only=False
)
Realm.objects.filter(add_custom_emoji_policy=Realm.ADD_CUSTOM_EMOJI_ADMINS_ONLY).update(
add_emoji_by_admins_only=True
)
class Migration(migrations.Migration):
dependencies = [
("zerver", "0337_realm_add_custom_emoji_policy"),
]
operations = [
migrations.RunPython(
migrate_to_add_custom_emoji_policy,
reverse_code=reverse_migrate_to_add_custom_emoji_policy,
elidable=True,
),
]

View File

@ -0,0 +1,17 @@
# Generated by Django 3.2.2 on 2021-05-15 18:05
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("zerver", "0338_migrate_to_add_custom_emoji_policy"),
]
operations = [
migrations.RemoveField(
model_name="realm",
name="add_emoji_by_admins_only",
),
]

View File

@ -245,8 +245,17 @@ class Realm(models.Model):
send_welcome_emails: bool = models.BooleanField(default=True) send_welcome_emails: bool = models.BooleanField(default=True)
message_content_allowed_in_email_notifications: bool = models.BooleanField(default=True) message_content_allowed_in_email_notifications: bool = models.BooleanField(default=True)
ADD_CUSTOM_EMOJI_MEMBERS_ONLY = 1
ADD_CUSTOM_EMOJI_ADMINS_ONLY = 2
ADD_CUSTOM_EMOJI_POLICY_TYPES = [
ADD_CUSTOM_EMOJI_MEMBERS_ONLY,
ADD_CUSTOM_EMOJI_ADMINS_ONLY,
]
mandatory_topics: bool = models.BooleanField(default=False) mandatory_topics: bool = models.BooleanField(default=False)
add_emoji_by_admins_only: bool = models.BooleanField(default=False) add_custom_emoji_policy: int = models.PositiveSmallIntegerField(
default=ADD_CUSTOM_EMOJI_MEMBERS_ONLY
)
name_changes_disabled: bool = models.BooleanField(default=False) name_changes_disabled: bool = models.BooleanField(default=False)
email_changes_disabled: bool = models.BooleanField(default=False) email_changes_disabled: bool = models.BooleanField(default=False)
avatar_changes_disabled: bool = models.BooleanField(default=False) avatar_changes_disabled: bool = models.BooleanField(default=False)
@ -590,7 +599,7 @@ class Realm(models.Model):
# Define the types of the various automatically managed properties # Define the types of the various automatically managed properties
property_types: Dict[str, Union[type, Tuple[type, ...]]] = dict( property_types: Dict[str, Union[type, Tuple[type, ...]]] = dict(
add_emoji_by_admins_only=bool, add_custom_emoji_policy=int,
allow_edit_history=bool, allow_edit_history=bool,
allow_message_deleting=bool, allow_message_deleting=bool,
bot_creation_policy=int, bot_creation_policy=int,

View File

@ -8729,13 +8729,19 @@ paths:
type: string type: string
description: | description: |
The text describing the emojiset. The text describing the emojiset.
realm_add_emoji_by_admins_only: realm_add_custom_emoji_policy:
type: boolean type: integer
description: | description: |
Present if `realm` is present in `fetch_event_types`. Present if `realm` is present in `fetch_event_types`.
Whether the organization is configured to only allow administrators The policy for which users can upload new custom emoji in this
to upload new custom emoji. organization.
* 1 = Members only
* 2 = Administrators only
**Changes**: New in Zulip 5.0 (feature level 85) replacing the
previous `realm_add_emoji_by_admins_only` boolean.
realm_allow_edit_history: realm_allow_edit_history:
type: boolean type: boolean
description: | description: |

View File

@ -2047,6 +2047,7 @@ class RealmPropertyActionTest(BaseAction):
message_content_delete_limit_seconds=[1000, 1100, 1200], message_content_delete_limit_seconds=[1000, 1100, 1200],
invite_to_realm_policy=[6, 4, 3, 2, 1], invite_to_realm_policy=[6, 4, 3, 2, 1],
move_messages_between_streams_policy=[4, 3, 2, 1], move_messages_between_streams_policy=[4, 3, 2, 1],
add_custom_emoji_policy=[2, 1],
) )
vals = test_values.get(name) vals = test_values.get(name)

View File

@ -129,7 +129,7 @@ class HomeTest(ZulipTestCase):
"promote_sponsoring_zulip", "promote_sponsoring_zulip",
"prompt_for_invites", "prompt_for_invites",
"queue_id", "queue_id",
"realm_add_emoji_by_admins_only", "realm_add_custom_emoji_policy",
"realm_allow_edit_history", "realm_allow_edit_history",
"realm_allow_message_deleting", "realm_allow_message_deleting",
"realm_allow_message_editing", "realm_allow_message_editing",

View File

@ -466,6 +466,7 @@ class RealmTest(ZulipTestCase):
wildcard_mention_policy=10, wildcard_mention_policy=10,
invite_to_realm_policy=10, invite_to_realm_policy=10,
move_messages_between_streams_policy=10, move_messages_between_streams_policy=10,
add_custom_emoji_policy=10,
) )
# We need an admin user. # We need an admin user.
@ -780,6 +781,7 @@ class RealmAPITest(ZulipTestCase):
message_content_delete_limit_seconds=[1000, 1100, 1200], message_content_delete_limit_seconds=[1000, 1100, 1200],
invite_to_realm_policy=Realm.INVITE_TO_REALM_POLICY_TYPES, invite_to_realm_policy=Realm.INVITE_TO_REALM_POLICY_TYPES,
move_messages_between_streams_policy=Realm.COMMON_POLICY_TYPES, move_messages_between_streams_policy=Realm.COMMON_POLICY_TYPES,
add_custom_emoji_policy=Realm.ADD_CUSTOM_EMOJI_POLICY_TYPES,
) )
vals = test_values.get(name) vals = test_values.get(name)

View File

@ -47,7 +47,7 @@ class RealmEmojiTest(ZulipTestCase):
# having no author are also there in the list. # having no author are also there in the list.
self.login("othello") self.login("othello")
realm = get_realm("zulip") realm = get_realm("zulip")
realm.add_emoji_by_admins_only = True realm.add_custom_emoji_policy = Realm.ADD_CUSTOM_EMOJI_ADMINS_ONLY
realm.save() realm.save()
realm_emoji = self.create_test_emoji_with_no_author("my_emoji", realm) realm_emoji = self.create_test_emoji_with_no_author("my_emoji", realm)
@ -135,7 +135,7 @@ class RealmEmojiTest(ZulipTestCase):
def test_upload_admins_only(self) -> None: def test_upload_admins_only(self) -> None:
self.login("othello") self.login("othello")
realm = get_realm("zulip") realm = get_realm("zulip")
realm.add_emoji_by_admins_only = True realm.add_custom_emoji_policy = Realm.ADD_CUSTOM_EMOJI_ADMINS_ONLY
realm.save() realm.save()
with get_test_image_file("img.png") as fp1: with get_test_image_file("img.png") as fp1:
emoji_data = {"f1": fp1} emoji_data = {"f1": fp1}
@ -145,7 +145,7 @@ class RealmEmojiTest(ZulipTestCase):
def test_upload_anyone(self) -> None: def test_upload_anyone(self) -> None:
self.login("othello") self.login("othello")
realm = get_realm("zulip") realm = get_realm("zulip")
realm.add_emoji_by_admins_only = False realm.add_custom_emoji_policy = Realm.ADD_CUSTOM_EMOJI_MEMBERS_ONLY
realm.save() realm.save()
with get_test_image_file("img.png") as fp1: with get_test_image_file("img.png") as fp1:
emoji_data = {"f1": fp1} emoji_data = {"f1": fp1}

View File

@ -60,7 +60,9 @@ def update_realm(
avatar_changes_disabled: Optional[bool] = REQ(json_validator=check_bool, default=None), avatar_changes_disabled: Optional[bool] = REQ(json_validator=check_bool, default=None),
inline_image_preview: Optional[bool] = REQ(json_validator=check_bool, default=None), inline_image_preview: Optional[bool] = REQ(json_validator=check_bool, default=None),
inline_url_embed_preview: Optional[bool] = REQ(json_validator=check_bool, default=None), inline_url_embed_preview: Optional[bool] = REQ(json_validator=check_bool, default=None),
add_emoji_by_admins_only: Optional[bool] = REQ(json_validator=check_bool, default=None), add_custom_emoji_policy: Optional[int] = REQ(
json_validator=check_int_in(Realm.ADD_CUSTOM_EMOJI_POLICY_TYPES), default=None
),
allow_message_deleting: Optional[bool] = REQ(json_validator=check_bool, default=None), allow_message_deleting: Optional[bool] = REQ(json_validator=check_bool, default=None),
message_content_delete_limit_seconds: Optional[int] = REQ( message_content_delete_limit_seconds: Optional[int] = REQ(
converter=to_non_negative_int, default=None converter=to_non_negative_int, default=None