2017-01-17 08:42:52 +01:00
|
|
|
|
2017-03-13 05:45:50 +01:00
|
|
|
import os
|
2017-01-17 08:42:52 +01:00
|
|
|
import re
|
2017-05-01 07:29:56 +02:00
|
|
|
import ujson
|
2017-01-17 08:42:52 +01:00
|
|
|
|
2017-10-02 23:47:45 +02:00
|
|
|
from django.conf import settings
|
2017-01-17 08:42:52 +01:00
|
|
|
from django.utils.translation import ugettext as _
|
2018-05-10 19:13:36 +02:00
|
|
|
from typing import Optional, Tuple
|
2017-05-01 07:29:56 +02:00
|
|
|
|
2017-01-17 08:42:52 +01:00
|
|
|
from zerver.lib.request import JsonableError
|
2017-03-13 05:45:50 +01:00
|
|
|
from zerver.lib.upload import upload_backend
|
2017-05-01 07:29:56 +02:00
|
|
|
from zerver.models import Reaction, Realm, RealmEmoji, UserProfile
|
2017-01-17 08:42:52 +01:00
|
|
|
|
2018-07-20 11:37:39 +02:00
|
|
|
EMOJI_PATH = os.path.join(settings.STATIC_ROOT, "generated", "emoji")
|
|
|
|
NAME_TO_CODEPOINT_PATH = os.path.join(EMOJI_PATH, "name_to_codepoint.json")
|
|
|
|
CODEPOINT_TO_NAME_PATH = os.path.join(EMOJI_PATH, "codepoint_to_name.json")
|
|
|
|
EMOTICON_CONVERSIONS_PATH = os.path.join(EMOJI_PATH, "emoticon_conversions.json")
|
|
|
|
|
|
|
|
with open(NAME_TO_CODEPOINT_PATH) as fp:
|
|
|
|
name_to_codepoint = ujson.load(fp)
|
|
|
|
|
|
|
|
with open(CODEPOINT_TO_NAME_PATH) as fp:
|
|
|
|
codepoint_to_name = ujson.load(fp)
|
|
|
|
|
|
|
|
with open(EMOTICON_CONVERSIONS_PATH) as fp:
|
|
|
|
EMOTICON_CONVERSIONS = ujson.load(fp)
|
2018-01-15 19:36:32 +01:00
|
|
|
|
|
|
|
possible_emoticons = EMOTICON_CONVERSIONS.keys()
|
|
|
|
possible_emoticon_regexes = map(re.escape, possible_emoticons) # type: ignore # AnyStr/str issues
|
2018-03-30 00:34:25 +02:00
|
|
|
terminal_symbols = ',.;?!()\\[\\] "\'\\n\\t' # type: str # from composebox_typeahead.js
|
|
|
|
emoticon_regex = ('(?<![^{0}])(?P<emoticon>('.format(terminal_symbols)
|
|
|
|
+ ')|('.join(possible_emoticon_regexes) # type: ignore # AnyStr/str issues
|
|
|
|
+ '))(?![^{0}])'.format(terminal_symbols))
|
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
|
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def emoji_name_to_emoji_code(realm: Realm, emoji_name: str) -> Tuple[str, str]:
|
2018-03-18 17:22:07 +01:00
|
|
|
realm_emojis = realm.get_active_emoji()
|
2017-12-06 02:58:20 +01:00
|
|
|
realm_emoji = realm_emojis.get(emoji_name)
|
2018-03-18 17:22:07 +01:00
|
|
|
if realm_emoji is not None:
|
2017-12-15 16:54:07 +01:00
|
|
|
return str(realm_emojis[emoji_name]['id']), Reaction.REALM_EMOJI
|
2017-05-01 01:13:28 +02:00
|
|
|
if emoji_name == 'zulip':
|
2017-05-01 07:29:56 +02:00
|
|
|
return emoji_name, Reaction.ZULIP_EXTRA_EMOJI
|
2017-10-02 23:47:45 +02:00
|
|
|
if emoji_name in name_to_codepoint:
|
|
|
|
return name_to_codepoint[emoji_name], Reaction.UNICODE_EMOJI
|
2019-04-20 03:49:03 +02:00
|
|
|
raise JsonableError(_("Emoji '%s' does not exist") % (emoji_name,))
|
2017-01-17 08:42:52 +01:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def check_valid_emoji(realm: Realm, emoji_name: str) -> None:
|
2017-05-01 07:29:56 +02:00
|
|
|
emoji_name_to_emoji_code(realm, emoji_name)
|
|
|
|
|
2017-11-21 00:25:40 +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":
|
|
|
|
realm_emojis = realm.get_emoji()
|
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
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def check_emoji_admin(user_profile: UserProfile, emoji_name: Optional[str]=None) -> None:
|
2017-05-18 21:53:33 +02:00
|
|
|
"""Raises an exception if the user cannot administer the target realm
|
|
|
|
emoji name in their organization."""
|
|
|
|
|
|
|
|
# Realm administrators can always administer emoji
|
|
|
|
if user_profile.is_realm_admin:
|
|
|
|
return
|
|
|
|
if user_profile.realm.add_emoji_by_admins_only:
|
2018-03-08 01:47:17 +01:00
|
|
|
raise JsonableError(_("Must be an organization administrator"))
|
2017-01-17 08:42:52 +01:00
|
|
|
|
2017-05-18 21:53:33 +02:00
|
|
|
# Otherwise, normal users can add emoji
|
|
|
|
if emoji_name is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Additionally, normal users can remove emoji they themselves added
|
2018-03-18 17:33:10 +01:00
|
|
|
emoji = RealmEmoji.objects.filter(realm=user_profile.realm,
|
|
|
|
name=emoji_name,
|
|
|
|
deactivated=False).first()
|
2017-05-18 21:53:33 +02:00
|
|
|
current_user_is_author = (emoji is not None and
|
|
|
|
emoji.author is not None and
|
|
|
|
emoji.author.id == user_profile.id)
|
|
|
|
if not user_profile.is_realm_admin and 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
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def check_valid_emoji_name(emoji_name: str) -> None:
|
2018-07-02 00:05:24 +02:00
|
|
|
if re.match(r'^[0-9a-z.\-_]+(?<![.\-_])$', emoji_name):
|
2017-01-17 08:42:52 +01:00
|
|
|
return
|
|
|
|
raise JsonableError(_("Invalid characters in emoji name"))
|
2017-03-13 05:45:50 +01:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def get_emoji_url(emoji_file_name: str, realm_id: int) -> str:
|
2017-03-13 05:45:50 +01:00
|
|
|
return upload_backend.get_emoji_url(emoji_file_name, realm_id)
|
|
|
|
|
|
|
|
|
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)
|
2018-03-11 18:55:20 +01:00
|
|
|
return ''.join((str(emoji_id), image_ext))
|