2017-03-13 05:45:50 +01:00
|
|
|
from django.conf import settings
|
2022-06-21 22:23:34 +02:00
|
|
|
from django.core.files.uploadedfile import UploadedFile
|
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
|
|
|
|
2022-04-14 23:40:49 +02:00
|
|
|
from zerver.actions.realm_emoji import check_add_realm_emoji, do_remove_realm_emoji
|
2019-06-18 16:43:22 +02:00
|
|
|
from zerver.decorator import require_member_or_admin
|
2021-05-17 15:40:28 +02:00
|
|
|
from zerver.lib.emoji import check_remove_custom_emoji, check_valid_emoji_name, name_to_codepoint
|
2023-01-25 08:29:51 +01:00
|
|
|
from zerver.lib.exceptions import JsonableError, ResourceNotFoundError
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.response import json_success
|
2024-07-12 17:50:11 +02:00
|
|
|
from zerver.lib.typed_endpoint import PathOnly, typed_endpoint
|
2024-07-11 20:46:33 +02:00
|
|
|
from zerver.lib.upload import get_file_info
|
2023-12-15 02:43:18 +01:00
|
|
|
from zerver.models import RealmEmoji, UserProfile
|
|
|
|
from zerver.models.realm_emoji import get_all_custom_emoji_for_realm
|
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:
|
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.
|
2023-07-14 12:37:29 +02:00
|
|
|
return json_success(
|
|
|
|
request, data=dict(emoji=get_all_custom_emoji_for_realm(user_profile.realm_id))
|
|
|
|
)
|
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
|
2024-07-12 17:50:11 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def upload_emoji(
|
2024-07-12 17:50:11 +02:00
|
|
|
request: HttpRequest, user_profile: UserProfile, *, emoji_name: PathOnly[str]
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> 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."))
|
2023-01-18 02:59:37 +01:00
|
|
|
if emoji_name in valid_built_in_emoji and not user_profile.is_realm_admin:
|
2023-01-25 16:47:51 +01:00
|
|
|
raise JsonableError(_("Only administrators can override default emoji."))
|
2023-07-22 00:34:11 +02:00
|
|
|
[emoji_file] = request.FILES.values()
|
2022-06-21 22:23:34 +02:00
|
|
|
assert isinstance(emoji_file, UploadedFile)
|
|
|
|
assert emoji_file.size is not None
|
2024-01-29 00:52:43 +01:00
|
|
|
if emoji_file.size > settings.MAX_EMOJI_FILE_SIZE_MIB * 1024 * 1024:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(
|
2023-07-17 22:40:33 +02:00
|
|
|
_("Uploaded file is larger than the allowed limit of {max_size} MiB").format(
|
|
|
|
max_size=settings.MAX_EMOJI_FILE_SIZE_MIB,
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2024-07-11 20:46:33 +02:00
|
|
|
_filename, content_type = get_file_info(emoji_file)
|
|
|
|
check_add_realm_emoji(user_profile.realm, emoji_name, user_profile, emoji_file, content_type)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2016-02-12 21:08:56 +01:00
|
|
|
|
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():
|
2023-07-17 22:40:33 +02:00
|
|
|
raise ResourceNotFoundError(
|
|
|
|
_("Emoji '{emoji_name}' does not exist").format(emoji_name=emoji_name)
|
|
|
|
)
|
2021-05-04 19:47:10 +02:00
|
|
|
check_remove_custom_emoji(user_profile, emoji_name)
|
2022-04-07 12:24:30 +02:00
|
|
|
do_remove_realm_emoji(user_profile.realm, emoji_name, acting_user=user_profile)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|