Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
emoji: Allow emoji authors to delete the emoji always.
sahil83961acf68aa4f8
diff --git a/static/js/settings_emoji.js b/static/js/settings_emoji.jsindex 9d285866572..4dc06712ff0 100644--- a/static/js/settings_emoji.js+++ b/static/js/settings_emoji.js@@ -42,7 +42,7 @@ function can_admin_emoji(emoji) {// If we don't have the author information then only admin is allowed to disable that emoji.return false;}- if (!page_params.realm_add_emoji_by_admins_only && people.is_my_user_id(emoji.author_id)) {+ if (people.is_my_user_id(emoji.author_id)) {return true;}return false;diff --git a/zerver/lib/emoji.py b/zerver/lib/emoji.pyindex 63b4a282d50..f212c72c0b3 100644--- a/zerver/lib/emoji.py+++ b/zerver/lib/emoji.py@@ -1,6 +1,6 @@import osimport re-from typing import Optional, Tuple+from typing import Tupleimport orjsonfrom django.utils.translation import gettext as _@@ -85,28 +85,28 @@ def check_emoji_request(realm: Realm, emoji_name: str, emoji_code: str, emoji_tyraise JsonableError(_("Invalid emoji type."))-def check_emoji_admin(user_profile: UserProfile, emoji_name: Optional[str] = None) -> None:- """Raises an exception if the user cannot administer the target realm- emoji name in their organization."""+def check_add_emoji_admin(user_profile: UserProfile) -> None:+ """Raises an exception if the user cannot add the emoji in their organization."""- # Realm administrators can always administer emoji+ # Realm administrators can always add emojiif user_profile.is_realm_admin:returnif user_profile.realm.add_emoji_by_admins_only:raise OrganizationAdministratorRequired()- # Otherwise, normal users can add emoji- if emoji_name is None:++def check_remove_custom_emoji(user_profile: UserProfile, emoji_name: str) -> None:+ # normal users can remove emoji they themselves added+ if user_profile.is_realm_admin:return- # Additionally, normal users can remove emoji they themselves addedemoji = RealmEmoji.objects.filter(realm=user_profile.realm, name=emoji_name, deactivated=False).first()current_user_is_author = (emoji is not None and emoji.author is not None and emoji.author.id == user_profile.id)- if not user_profile.is_realm_admin and not current_user_is_author:+ if not current_user_is_author:raise JsonableError(_("Must be an organization administrator or emoji author"))diff --git a/zerver/tests/test_realm_emoji.py b/zerver/tests/test_realm_emoji.pyindex 7ad6dacf5c6..6fcd6d4eeff 100644--- a/zerver/tests/test_realm_emoji.py+++ b/zerver/tests/test_realm_emoji.py@@ -180,23 +180,10 @@ class RealmEmojiTest(ZulipTestCase):result = self.client_delete("/json/realm/emoji/my_emoji")self.assert_json_success(result)- def test_delete_admins_only(self) -> None:- emoji_author = self.example_user("othello")- self.login_user(emoji_author)- realm = get_realm("zulip")- realm.add_emoji_by_admins_only = True- realm.save()- self.create_test_emoji_with_no_author("my_emoji", realm)- result = self.client_delete("/json/realm/emoji/my_emoji")- self.assert_json_error(result, "Must be an organization administrator")-def test_delete_admin_or_author(self) -> None:- # If any user in a realm can upload the emoji then the user who- # uploaded it as well as the admin should be able to delete it.+ # Admins can delete emoji added by others also.+ # Non-admins can only delete emoji they added themselves.emoji_author = self.example_user("othello")- realm = get_realm("zulip")- realm.add_emoji_by_admins_only = False- realm.save()self.create_test_emoji("my_emoji_1", emoji_author)self.login_user(emoji_author)diff --git a/zerver/views/realm_emoji.py b/zerver/views/realm_emoji.pyindex 6b55503dc81..74f36b7d7b1 100644--- a/zerver/views/realm_emoji.py+++ b/zerver/views/realm_emoji.py@@ -4,7 +4,12 @@ from django.utils.translation import gettext as _from zerver.decorator import require_member_or_adminfrom zerver.lib.actions import check_add_realm_emoji, do_remove_realm_emoji-from zerver.lib.emoji import check_emoji_admin, check_valid_emoji_name, name_to_codepoint+from zerver.lib.emoji import (+ check_add_emoji_admin,+ check_remove_custom_emoji,+ check_valid_emoji_name,+ name_to_codepoint,+)from zerver.lib.request import REQ, JsonableError, has_request_variablesfrom zerver.lib.response import json_successfrom zerver.models import RealmEmoji, UserProfile@@ -12,7 +17,7 @@ from zerver.models import RealmEmoji, UserProfiledef list_emoji(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:- # We don't call check_emoji_admin here because the list of realm+ # We don't do any checks here because the list of realm# emoji is public.return json_success({"emoji": user_profile.realm.get_emoji()})@@ -25,7 +30,7 @@ def upload_emoji(emoji_name = emoji_name.strip().replace(" ", "_")valid_built_in_emoji = name_to_codepoint.keys()check_valid_emoji_name(emoji_name)- check_emoji_admin(user_profile)+ check_add_emoji_admin(user_profile)if RealmEmoji.objects.filter(realm=user_profile.realm, name=emoji_name, deactivated=False).exists():@@ -54,6 +59,6 @@ def delete_emoji(request: HttpRequest, user_profile: UserProfile, emoji_name: strealm=user_profile.realm, name=emoji_name, deactivated=False).exists():raise JsonableError(_("Emoji '{}' does not exist").format(emoji_name))- check_emoji_admin(user_profile, emoji_name)+ check_remove_custom_emoji(user_profile, emoji_name)do_remove_realm_emoji(user_profile.realm, emoji_name)return json_success()