2017-03-13 05:45:50 +01:00
|
|
|
from django.conf import settings
|
2016-06-02 23:01:28 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2016-02-12 21:08:56 +01:00
|
|
|
|
2019-06-18 16:43:22 +02:00
|
|
|
from zerver.decorator import require_member_or_admin
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.actions import check_add_realm_emoji, do_remove_realm_emoji
|
2021-05-17 15:40:28 +02:00
|
|
|
from zerver.lib.emoji import check_remove_custom_emoji, check_valid_emoji_name, name_to_codepoint
|
2021-07-16 22:11:10 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.response import json_success
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import RealmEmoji, UserProfile
|
2016-02-12 21:08:56 +01:00
|
|
|
|
2017-03-13 05:45:50 +01:00
|
|
|
|
2017-10-27 02:18:49 +02:00
|
|
|
def list_emoji(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
|
2016-12-20 15:41:30 +01:00
|
|
|
|
2021-05-04 19:47:10 +02:00
|
|
|
# We don't do any checks here because the list of realm
|
2016-12-20 15:41:30 +01:00
|
|
|
# emoji is public.
|
2021-02-12 08:20:45 +01:00
|
|
|
return json_success({"emoji": user_profile.realm.get_emoji()})
|
2016-02-12 21:08:56 +01:00
|
|
|
|
2017-03-13 05:45:50 +01:00
|
|
|
|
2019-06-18 16:43:22 +02:00
|
|
|
@require_member_or_admin
|
2017-01-17 08:38:25 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def upload_emoji(
|
|
|
|
request: HttpRequest, user_profile: UserProfile, emoji_name: str = REQ(path_only=True)
|
|
|
|
) -> HttpResponse:
|
2021-02-12 08:20:45 +01:00
|
|
|
emoji_name = emoji_name.strip().replace(" ", "_")
|
2021-06-16 20:49:41 +02:00
|
|
|
valid_built_in_emoji = name_to_codepoint.keys()
|
2017-01-17 08:38:25 +01:00
|
|
|
check_valid_emoji_name(emoji_name)
|
2021-05-17 15:40:28 +02:00
|
|
|
|
|
|
|
if not user_profile.can_add_custom_emoji():
|
|
|
|
raise JsonableError(_("Insufficient permission"))
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
if RealmEmoji.objects.filter(
|
|
|
|
realm=user_profile.realm, name=emoji_name, deactivated=False
|
|
|
|
).exists():
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("A custom emoji with this name already exists."))
|
2017-03-13 05:45:50 +01:00
|
|
|
if len(request.FILES) != 1:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("You must upload exactly one file."))
|
2021-06-16 20:49:41 +02:00
|
|
|
if emoji_name in valid_built_in_emoji:
|
|
|
|
if not user_profile.is_realm_admin:
|
|
|
|
raise JsonableError(_("Only administrators can override built-in emoji."))
|
2017-03-13 05:45:50 +01:00
|
|
|
emoji_file = list(request.FILES.values())[0]
|
2021-05-29 09:04:19 +02:00
|
|
|
if (settings.MAX_EMOJI_FILE_SIZE_MIB * 1024 * 1024) < emoji_file.size:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(
|
2021-02-12 08:19:30 +01:00
|
|
|
_("Uploaded file is larger than the allowed limit of {} MiB").format(
|
2021-05-29 09:04:19 +02:00
|
|
|
settings.MAX_EMOJI_FILE_SIZE_MIB,
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
realm_emoji = check_add_realm_emoji(user_profile.realm, emoji_name, user_profile, emoji_file)
|
2018-03-11 18:55:20 +01:00
|
|
|
if realm_emoji is None:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Image file upload failed."))
|
2016-02-12 21:08:56 +01:00
|
|
|
return json_success()
|
|
|
|
|
2017-03-13 05:45:50 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
def delete_emoji(request: HttpRequest, user_profile: UserProfile, emoji_name: str) -> HttpResponse:
|
|
|
|
if not RealmEmoji.objects.filter(
|
|
|
|
realm=user_profile.realm, name=emoji_name, deactivated=False
|
|
|
|
).exists():
|
2020-06-15 23:22:24 +02:00
|
|
|
raise JsonableError(_("Emoji '{}' does not exist").format(emoji_name))
|
2021-05-04 19:47:10 +02:00
|
|
|
check_remove_custom_emoji(user_profile, emoji_name)
|
2016-02-12 21:08:56 +01:00
|
|
|
do_remove_realm_emoji(user_profile.realm, emoji_name)
|
2016-10-21 07:34:04 +02:00
|
|
|
return json_success()
|