Files
upstream-zulip/zerver/models/realm_emoji.py
upstream-mirror 64898b2c79
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
snapshot of https://github.com/zulip/zulip.git @ 48c69911c7dd4e03b20552eb3bd77ad8151c1357 (2026-09-15)
2026-09-15 15:05:30 +05:00

139 lines
4.8 KiB
Python

from typing import TypedDict
from django.core.validators import MinLengthValidator, RegexValidator
from django.db import models
from django.db.models import CASCADE, Q
from django.db.models.signals import post_delete, post_save
from django.utils.translation import gettext_lazy
from typing_extensions import override
from zerver.lib.cache import cache_delete, cache_with_key
from zerver.models.realms import Realm
class EmojiInfo(TypedDict):
id: str
name: str
source_url: str
deactivated: bool
author_id: int | None
still_url: str | None
def get_all_custom_emoji_for_realm_cache_key(realm_id: int) -> str:
return f"realm_emoji:{realm_id}"
class RealmEmoji(models.Model):
author = models.ForeignKey(
"UserProfile",
on_delete=CASCADE,
)
realm = models.ForeignKey(Realm, on_delete=CASCADE)
name = models.TextField(
validators=[
MinLengthValidator(1),
# The second part of the regex (negative lookbehind) disallows names
# ending with one of the punctuation characters.
RegexValidator(
regex=r"^[0-9a-z.\-_]+(?<![.\-_])$",
message=gettext_lazy("Invalid characters in emoji name"),
),
]
)
# The basename of the custom emoji's filename; see PATH_ID_TEMPLATE for the full path.
file_name = models.TextField(db_index=True, null=True, blank=True)
# Whether this custom emoji is an animated image.
is_animated = models.BooleanField(default=False)
deactivated = models.BooleanField(default=False)
PATH_ID_TEMPLATE = "{realm_id}/emoji/images/{emoji_file_name}"
STILL_PATH_ID_TEMPLATE = "{realm_id}/emoji/images/still/{emoji_filename_without_extension}.png"
class Meta:
constraints = [
models.UniqueConstraint(
fields=["realm", "name"],
condition=Q(deactivated=False),
name="unique_realm_emoji_when_false_deactivated",
),
]
@override
def __str__(self) -> str:
return f"{self.realm.string_id}: {self.id} {self.name} {self.deactivated} {self.file_name}"
def emoji_as_emojiinfo(realm_emoji: RealmEmoji) -> EmojiInfo:
from zerver.lib.upload import get_emoji_url
author_id = realm_emoji.author_id
assert realm_emoji.file_name is not None
emoji_url = get_emoji_url(realm_emoji.file_name, realm_emoji.realm_id)
emoji_dict: EmojiInfo = dict(
id=str(realm_emoji.id),
name=realm_emoji.name,
source_url=emoji_url,
deactivated=realm_emoji.deactivated,
author_id=author_id,
still_url=None,
)
if realm_emoji.is_animated:
# For animated emoji, we include still_url with a static
# version of the image, so that clients can display the
# emoji in a less distracting (not animated) fashion when
# desired.
emoji_dict["still_url"] = get_emoji_url(
realm_emoji.file_name, realm_emoji.realm_id, still=True
)
return emoji_dict
def get_all_custom_emoji_for_realm_uncached(realm_id: int) -> dict[str, EmojiInfo]:
# RealmEmoji objects with file_name=None are still in the process
# of being uploaded, and we expect to be cleaned up by a
# try/finally block if the upload fails, so it's correct to
# exclude them.
query = RealmEmoji.objects.filter(realm_id=realm_id).exclude(file_name=None)
d = {}
for realm_emoji in query.iterator():
d[str(realm_emoji.id)] = emoji_as_emojiinfo(realm_emoji)
return d
@cache_with_key(get_all_custom_emoji_for_realm_cache_key, timeout=3600 * 24 * 7)
def get_all_custom_emoji_for_realm(realm_id: int) -> dict[str, EmojiInfo]:
return get_all_custom_emoji_for_realm_uncached(realm_id)
def get_name_keyed_dict_for_active_realm_emoji(realm_id: int) -> dict[str, EmojiInfo]:
# It's important to use the cached version here.
realm_emojis = get_all_custom_emoji_for_realm(realm_id)
return {row["name"]: row for row in realm_emojis.values() if not row["deactivated"]}
def flush_realm_emoji(*, instance: RealmEmoji, **kwargs: object) -> None:
if instance.file_name is None:
# Because we construct RealmEmoji.file_name using the ID for
# the RealmEmoji object, it will always have file_name=None,
# and then it'll be updated with the actual filename as soon
# as the upload completes successfully.
#
# Doing nothing when file_name=None is the best option, since
# such an object shouldn't have been cached yet, and this
# function will be called again when file_name is set.
return
cache_delete(get_all_custom_emoji_for_realm_cache_key(instance.realm_id))
post_save.connect(flush_realm_emoji, sender=RealmEmoji)
post_delete.connect(flush_realm_emoji, sender=RealmEmoji)