2022-04-07 12:24:30 +02:00
|
|
|
from typing import IO, Dict, Optional
|
2022-04-14 23:40:49 +02:00
|
|
|
|
2022-09-29 23:09:47 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2022-04-07 12:33:44 +02:00
|
|
|
from django.db import transaction
|
2022-09-29 23:09:47 +02:00
|
|
|
from django.db.utils import IntegrityError
|
2022-04-06 18:34:07 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2022-04-14 23:40:49 +02:00
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
|
|
|
|
from zerver.lib.emoji import get_emoji_file_name
|
|
|
|
from zerver.lib.exceptions import JsonableError
|
|
|
|
from zerver.lib.pysa import mark_sanitized
|
|
|
|
from zerver.lib.upload import upload_emoji_image
|
2023-07-14 12:37:29 +02:00
|
|
|
from zerver.models import (
|
|
|
|
EmojiInfo,
|
|
|
|
Realm,
|
|
|
|
RealmAuditLog,
|
|
|
|
RealmEmoji,
|
|
|
|
UserProfile,
|
|
|
|
active_user_ids,
|
|
|
|
get_all_custom_emoji_for_realm,
|
|
|
|
)
|
django_api: Extract send_event_on_commit helper.
django-stubs 4.2.1 gives transaction.on_commit a more accurate type
annotation, but this exposed that mypy can’t handle the lambda default
parameters that we use to recapture loop variables such as
for stream_id in public_stream_ids:
peer_user_ids = …
event = …
transaction.on_commit(
lambda event=event, peer_user_ids=peer_user_ids: send_event(
realm, event, peer_user_ids
)
)
https://github.com/python/mypy/issues/15459
A workaround that mypy accepts is
transaction.on_commit(
(
lambda event, peer_user_ids: lambda: send_event(
realm, event, peer_user_ids
)
)(event, peer_user_ids)
)
But that’s kind of ugly and potentially error-prone, so let’s make a
helper function for this very common pattern.
send_event_on_commit(realm, event, peer_user_ids)
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2023-06-17 20:53:07 +02:00
|
|
|
from zerver.tornado.django_api import send_event_on_commit
|
2022-04-14 23:40:49 +02:00
|
|
|
|
|
|
|
|
2022-04-06 18:51:13 +02:00
|
|
|
def notify_realm_emoji(realm: Realm, realm_emoji: Dict[str, EmojiInfo]) -> None:
|
|
|
|
event = dict(type="realm_emoji", op="update", realm_emoji=realm_emoji)
|
django_api: Extract send_event_on_commit helper.
django-stubs 4.2.1 gives transaction.on_commit a more accurate type
annotation, but this exposed that mypy can’t handle the lambda default
parameters that we use to recapture loop variables such as
for stream_id in public_stream_ids:
peer_user_ids = …
event = …
transaction.on_commit(
lambda event=event, peer_user_ids=peer_user_ids: send_event(
realm, event, peer_user_ids
)
)
https://github.com/python/mypy/issues/15459
A workaround that mypy accepts is
transaction.on_commit(
(
lambda event, peer_user_ids: lambda: send_event(
realm, event, peer_user_ids
)
)(event, peer_user_ids)
)
But that’s kind of ugly and potentially error-prone, so let’s make a
helper function for this very common pattern.
send_event_on_commit(realm, event, peer_user_ids)
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2023-06-17 20:53:07 +02:00
|
|
|
send_event_on_commit(realm, event, active_user_ids(realm.id))
|
2022-04-14 23:40:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
def check_add_realm_emoji(
|
|
|
|
realm: Realm, name: str, author: UserProfile, image_file: IO[bytes]
|
|
|
|
) -> RealmEmoji:
|
|
|
|
try:
|
|
|
|
realm_emoji = RealmEmoji(realm=realm, name=name, author=author)
|
|
|
|
realm_emoji.full_clean()
|
|
|
|
realm_emoji.save()
|
2022-09-29 23:09:47 +02:00
|
|
|
except (IntegrityError, ValidationError):
|
2022-04-14 23:40:49 +02:00
|
|
|
# Match the string in upload_emoji.
|
|
|
|
raise JsonableError(_("A custom emoji with this name already exists."))
|
|
|
|
|
|
|
|
emoji_file_name = get_emoji_file_name(image_file.name, realm_emoji.id)
|
|
|
|
|
|
|
|
# The only user-controlled portion of 'emoji_file_name' is an extension,
|
|
|
|
# which can not contain '..' or '/' or '\', making it difficult to exploit
|
|
|
|
emoji_file_name = mark_sanitized(emoji_file_name)
|
|
|
|
|
|
|
|
emoji_uploaded_successfully = False
|
|
|
|
is_animated = False
|
|
|
|
try:
|
|
|
|
is_animated = upload_emoji_image(image_file, emoji_file_name, author)
|
|
|
|
emoji_uploaded_successfully = True
|
|
|
|
finally:
|
|
|
|
if not emoji_uploaded_successfully:
|
|
|
|
realm_emoji.delete()
|
|
|
|
realm_emoji.file_name = emoji_file_name
|
|
|
|
realm_emoji.is_animated = is_animated
|
|
|
|
realm_emoji.save(update_fields=["file_name", "is_animated"])
|
2022-04-06 18:34:07 +02:00
|
|
|
|
2023-07-14 12:37:29 +02:00
|
|
|
realm_emoji_dict = get_all_custom_emoji_for_realm(realm.id)
|
2022-04-06 18:34:07 +02:00
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=realm,
|
|
|
|
acting_user=author,
|
|
|
|
event_type=RealmAuditLog.REALM_EMOJI_ADDED,
|
|
|
|
event_time=timezone_now(),
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={
|
|
|
|
"realm_emoji": dict(sorted(realm_emoji_dict.items())),
|
|
|
|
"added_emoji": realm_emoji_dict[str(realm_emoji.id)],
|
|
|
|
},
|
2022-04-06 18:34:07 +02:00
|
|
|
)
|
|
|
|
notify_realm_emoji(realm_emoji.realm, realm_emoji_dict)
|
2022-04-14 23:40:49 +02:00
|
|
|
return realm_emoji
|
|
|
|
|
|
|
|
|
2022-04-07 12:33:44 +02:00
|
|
|
@transaction.atomic(durable=True)
|
2022-04-07 12:24:30 +02:00
|
|
|
def do_remove_realm_emoji(realm: Realm, name: str, *, acting_user: Optional[UserProfile]) -> None:
|
2022-04-14 23:40:49 +02:00
|
|
|
emoji = RealmEmoji.objects.get(realm=realm, name=name, deactivated=False)
|
|
|
|
emoji.deactivated = True
|
|
|
|
emoji.save(update_fields=["deactivated"])
|
2022-04-07 12:24:30 +02:00
|
|
|
|
2023-07-14 12:37:29 +02:00
|
|
|
realm_emoji_dict = get_all_custom_emoji_for_realm(realm.id)
|
2022-04-07 12:24:30 +02:00
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=realm,
|
|
|
|
acting_user=acting_user,
|
|
|
|
event_type=RealmAuditLog.REALM_EMOJI_REMOVED,
|
|
|
|
event_time=timezone_now(),
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={
|
|
|
|
"realm_emoji": dict(sorted(realm_emoji_dict.items())),
|
|
|
|
"deactivated_emoji": realm_emoji_dict[str(emoji.id)],
|
|
|
|
},
|
2022-04-07 12:24:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
notify_realm_emoji(realm, realm_emoji_dict)
|