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
|
|
|
|
from zerver.lib.emoji import check_emoji_admin, check_valid_emoji_name
|
|
|
|
from zerver.lib.request import REQ, JsonableError, has_request_variables
|
|
|
|
from zerver.lib.response import json_error, json_success
|
|
|
|
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
|
|
|
|
|
|
|
# We don't call check_emoji_admin here because the list of realm
|
|
|
|
# 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(" ", "_")
|
2017-01-17 08:38:25 +01:00
|
|
|
check_valid_emoji_name(emoji_name)
|
2016-12-20 15:41:30 +01:00
|
|
|
check_emoji_admin(user_profile)
|
2021-02-12 08:19:30 +01:00
|
|
|
if RealmEmoji.objects.filter(
|
|
|
|
realm=user_profile.realm, name=emoji_name, deactivated=False
|
|
|
|
).exists():
|
2018-03-11 18:55:20 +01:00
|
|
|
return json_error(_("A custom emoji with this name already exists."))
|
2017-03-13 05:45:50 +01:00
|
|
|
if len(request.FILES) != 1:
|
|
|
|
return json_error(_("You must upload exactly one file."))
|
|
|
|
emoji_file = list(request.FILES.values())[0]
|
|
|
|
if (settings.MAX_EMOJI_FILE_SIZE * 1024 * 1024) < emoji_file.size:
|
2021-02-12 08:19:30 +01:00
|
|
|
return json_error(
|
|
|
|
_("Uploaded file is larger than the allowed limit of {} MiB").format(
|
|
|
|
settings.MAX_EMOJI_FILE_SIZE,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
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:
|
|
|
|
return json_error(_("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))
|
2017-05-18 21:53:33 +02:00
|
|
|
check_emoji_admin(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()
|