2017-03-13 05:45:50 +01:00
|
|
|
import os
|
2017-01-17 08:42:52 +01:00
|
|
|
import re
|
2023-07-14 14:25:57 +02:00
|
|
|
from dataclasses import dataclass
|
2017-01-17 08:42:52 +01:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2022-06-21 23:56:52 +02:00
|
|
|
from django.contrib.staticfiles.storage import staticfiles_storage
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2017-05-01 07:29:56 +02:00
|
|
|
|
2021-05-17 15:40:28 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2019-07-17 02:29:08 +02:00
|
|
|
from zerver.lib.storage import static_path
|
2017-03-13 05:45:50 +01:00
|
|
|
from zerver.lib.upload import upload_backend
|
2023-12-15 02:43:18 +01:00
|
|
|
from zerver.models import Reaction, Realm, RealmEmoji, UserProfile
|
|
|
|
from zerver.models.realm_emoji import (
|
2023-07-14 12:37:29 +02:00
|
|
|
get_all_custom_emoji_for_realm,
|
|
|
|
get_name_keyed_dict_for_active_realm_emoji,
|
|
|
|
)
|
2017-01-17 08:42:52 +01:00
|
|
|
|
2019-11-20 04:34:33 +01:00
|
|
|
emoji_codes_path = static_path("generated/emoji/emoji_codes.json")
|
|
|
|
if not os.path.exists(emoji_codes_path): # nocoverage
|
|
|
|
# During the collectstatic step of build-release-tarball,
|
|
|
|
# prod-static/serve/generated/emoji won't exist yet.
|
|
|
|
emoji_codes_path = os.path.join(
|
|
|
|
os.path.dirname(__file__),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
"../../static/generated/emoji/emoji_codes.json",
|
2019-11-20 04:34:33 +01:00
|
|
|
)
|
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
with open(emoji_codes_path, "rb") as fp:
|
|
|
|
emoji_codes = orjson.loads(fp.read())
|
2018-07-20 11:37:39 +02:00
|
|
|
|
2020-02-06 07:07:10 +01:00
|
|
|
name_to_codepoint = emoji_codes["name_to_codepoint"]
|
|
|
|
codepoint_to_name = emoji_codes["codepoint_to_name"]
|
|
|
|
EMOTICON_CONVERSIONS = emoji_codes["emoticon_conversions"]
|
2018-01-15 19:36:32 +01:00
|
|
|
|
|
|
|
possible_emoticons = EMOTICON_CONVERSIONS.keys()
|
2019-08-10 00:30:33 +02:00
|
|
|
possible_emoticon_regexes = (re.escape(emoticon) for emoticon in possible_emoticons)
|
2024-04-25 23:03:00 +02:00
|
|
|
terminal_symbols = r",.;?!()\[\] \"'\n\t" # from composebox_typeahead.ts
|
2021-05-15 12:02:50 +02:00
|
|
|
EMOTICON_RE = (
|
2024-04-26 20:30:22 +02:00
|
|
|
rf"(?<![^{terminal_symbols}])(?P<emoticon>("
|
|
|
|
+ r")|(".join(possible_emoticon_regexes)
|
|
|
|
+ rf"))(?![^{terminal_symbols}])"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2018-01-15 19:36:32 +01:00
|
|
|
|
2022-06-21 23:56:52 +02:00
|
|
|
|
|
|
|
def data_url() -> str:
|
|
|
|
# This bakes a hash into the URL, which looks something like
|
|
|
|
# static/webpack-bundles/files/64.0cdafdf0b6596657a9be.png
|
|
|
|
# This is how Django deals with serving static files in a cacheable way.
|
|
|
|
# See PR #22275 for details.
|
|
|
|
return staticfiles_storage.url("generated/emoji/emoji_api.json")
|
|
|
|
|
|
|
|
|
2018-01-15 19:36:32 +01:00
|
|
|
# Translates emoticons to their colon syntax, e.g. `:smiley:`.
|
2018-05-10 19:13:36 +02:00
|
|
|
def translate_emoticons(text: str) -> str:
|
2018-01-15 19:36:32 +01:00
|
|
|
translated = text
|
|
|
|
|
|
|
|
for emoticon in EMOTICON_CONVERSIONS:
|
|
|
|
translated = re.sub(re.escape(emoticon), EMOTICON_CONVERSIONS[emoticon], translated)
|
|
|
|
|
|
|
|
return translated
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2023-07-14 14:25:57 +02:00
|
|
|
@dataclass
|
|
|
|
class EmojiData:
|
|
|
|
emoji_code: str
|
|
|
|
reaction_type: str
|
|
|
|
|
|
|
|
|
|
|
|
def get_emoji_data(realm_id: int, emoji_name: str) -> EmojiData:
|
|
|
|
# Even if emoji_name is either in name_to_codepoint or named "zulip",
|
|
|
|
# we still need to call get_realm_active_emoji.
|
|
|
|
realm_emoji_dict = get_name_keyed_dict_for_active_realm_emoji(realm_id)
|
2023-07-14 12:37:29 +02:00
|
|
|
realm_emoji = realm_emoji_dict.get(emoji_name)
|
2023-07-14 14:25:57 +02:00
|
|
|
|
2018-03-18 17:22:07 +01:00
|
|
|
if realm_emoji is not None:
|
2023-07-14 14:25:57 +02:00
|
|
|
emoji_code = str(realm_emoji["id"])
|
|
|
|
return EmojiData(emoji_code=emoji_code, reaction_type=Reaction.REALM_EMOJI)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if emoji_name == "zulip":
|
2023-07-14 14:25:57 +02:00
|
|
|
return EmojiData(emoji_code=emoji_name, reaction_type=Reaction.ZULIP_EXTRA_EMOJI)
|
|
|
|
|
2017-10-02 23:47:45 +02:00
|
|
|
if emoji_name in name_to_codepoint:
|
2023-07-14 14:25:57 +02:00
|
|
|
emoji_code = name_to_codepoint[emoji_name]
|
|
|
|
return EmojiData(emoji_code=emoji_code, reaction_type=Reaction.UNICODE_EMOJI)
|
|
|
|
|
2023-07-17 22:40:33 +02:00
|
|
|
raise JsonableError(_("Emoji '{emoji_name}' does not exist").format(emoji_name=emoji_name))
|
2017-01-17 08:42:52 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def check_emoji_request(realm: Realm, emoji_name: str, emoji_code: str, emoji_type: str) -> None:
|
2017-11-19 07:07:29 +01:00
|
|
|
# For a given realm and emoji type, checks whether an emoji
|
|
|
|
# code is valid for new reactions, or not.
|
|
|
|
if emoji_type == "realm_emoji":
|
2023-07-14 12:37:29 +02:00
|
|
|
# We cache emoji, so this generally avoids a round trip,
|
|
|
|
# but it does require deserializing a bigger data structure
|
|
|
|
# than we need.
|
|
|
|
realm_emojis = get_all_custom_emoji_for_realm(realm.id)
|
2018-03-11 18:55:20 +01:00
|
|
|
realm_emoji = realm_emojis.get(emoji_code)
|
2017-12-06 02:58:20 +01:00
|
|
|
if realm_emoji is None:
|
2018-03-08 01:35:07 +01:00
|
|
|
raise JsonableError(_("Invalid custom emoji."))
|
2018-03-11 18:55:20 +01:00
|
|
|
if realm_emoji["name"] != emoji_name:
|
|
|
|
raise JsonableError(_("Invalid custom emoji name."))
|
2017-12-06 02:58:20 +01:00
|
|
|
if realm_emoji["deactivated"]:
|
2018-03-08 01:35:07 +01:00
|
|
|
raise JsonableError(_("This custom emoji has been deactivated."))
|
2017-11-19 07:07:29 +01:00
|
|
|
elif emoji_type == "zulip_extra_emoji":
|
|
|
|
if emoji_code not in ["zulip"]:
|
2018-03-08 01:35:07 +01:00
|
|
|
raise JsonableError(_("Invalid emoji code."))
|
2017-11-21 00:25:40 +01:00
|
|
|
if emoji_name != emoji_code:
|
|
|
|
raise JsonableError(_("Invalid emoji name."))
|
2017-11-19 07:07:29 +01:00
|
|
|
elif emoji_type == "unicode_emoji":
|
|
|
|
if emoji_code not in codepoint_to_name:
|
2018-03-08 01:35:07 +01:00
|
|
|
raise JsonableError(_("Invalid emoji code."))
|
2017-11-21 00:25:40 +01:00
|
|
|
if name_to_codepoint.get(emoji_name) != emoji_code:
|
|
|
|
raise JsonableError(_("Invalid emoji name."))
|
2017-11-19 07:07:29 +01:00
|
|
|
else:
|
|
|
|
# The above are the only valid emoji types
|
|
|
|
raise JsonableError(_("Invalid emoji type."))
|
2017-10-08 09:34:59 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-05-04 19:47:10 +02:00
|
|
|
def check_remove_custom_emoji(user_profile: UserProfile, emoji_name: str) -> None:
|
|
|
|
# normal users can remove emoji they themselves added
|
|
|
|
if user_profile.is_realm_admin:
|
2017-05-18 21:53:33 +02:00
|
|
|
return
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
emoji = RealmEmoji.objects.filter(
|
|
|
|
realm=user_profile.realm, name=emoji_name, deactivated=False
|
|
|
|
).first()
|
|
|
|
current_user_is_author = (
|
|
|
|
emoji is not None and emoji.author is not None and emoji.author.id == user_profile.id
|
|
|
|
)
|
2021-05-04 19:47:10 +02:00
|
|
|
if not current_user_is_author:
|
2018-03-08 01:47:17 +01:00
|
|
|
raise JsonableError(_("Must be an organization administrator or emoji author"))
|
2017-05-18 21:53:33 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def check_valid_emoji_name(emoji_name: str) -> None:
|
2020-04-08 21:49:55 +02:00
|
|
|
if emoji_name:
|
2023-02-01 02:28:33 +01:00
|
|
|
if re.match(r"^[0-9a-z\-_]+(?<![\-_])$", emoji_name):
|
2020-04-08 21:49:55 +02:00
|
|
|
return
|
2023-02-01 02:28:33 +01:00
|
|
|
if re.match(r"^[0-9a-z\-_]+$", emoji_name):
|
|
|
|
raise JsonableError(_("Emoji names must end with either a letter or digit."))
|
2022-07-02 05:16:18 +02:00
|
|
|
raise JsonableError(
|
|
|
|
_(
|
2023-02-01 02:28:33 +01:00
|
|
|
"Emoji names must contain only lowercase English letters, digits, spaces, dashes, and underscores.",
|
2022-07-02 05:16:18 +02:00
|
|
|
)
|
|
|
|
)
|
2020-04-08 21:49:55 +02:00
|
|
|
raise JsonableError(_("Emoji name is missing"))
|
2017-03-13 05:45:50 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-08-12 10:19:53 +02:00
|
|
|
def get_emoji_url(emoji_file_name: str, realm_id: int, still: bool = False) -> str:
|
|
|
|
return upload_backend.get_emoji_url(emoji_file_name, realm_id, still)
|
2017-03-13 05:45:50 +01:00
|
|
|
|
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def get_emoji_file_name(emoji_file_name: str, emoji_id: int) -> str:
|
2017-03-13 05:45:50 +01:00
|
|
|
_, image_ext = os.path.splitext(emoji_file_name)
|
2021-02-12 08:20:45 +01:00
|
|
|
return "".join((str(emoji_id), image_ext))
|