emoji: Move `EMOTICON_CONVERSIONS` mapping to build_emoji infra.

This commit closes a long pending issue which involved moving the
`EMOTICON_CONVERSION` mapping to build_emoji infrastructure so
that there is only one source of truth. This was pending from the
time when this feature was implemented.
This commit is contained in:
Harshit Bansal 2018-07-20 09:37:39 +00:00 committed by Tim Abbott
parent 565fd75661
commit c0b0fb7cce
6 changed files with 39 additions and 38 deletions

View File

@ -50,6 +50,7 @@ run_test('get_canonical_name', () => {
var canonical_name = emoji.get_canonical_name('realm_emoji'); var canonical_name = emoji.get_canonical_name('realm_emoji');
assert.equal(canonical_name, 'realm_emoji'); assert.equal(canonical_name, 'realm_emoji');
var orig_emoji_codes = global.emoji_codes;
global.emoji_codes = { global.emoji_codes = {
name_to_codepoint: { name_to_codepoint: {
'+1': '1f44d', '+1': '1f44d',
@ -71,6 +72,7 @@ run_test('get_canonical_name', () => {
emoji.get_canonical_name('non_existent'); emoji.get_canonical_name('non_existent');
assert.equal(blueslip.get_test_logs('error').length, 1); assert.equal(blueslip.get_test_logs('error').length, 1);
blueslip.clear_test_data(); blueslip.clear_test_data();
global.emoji_codes = orig_emoji_codes;
}); });
run_test('translate_emoticons_to_names', () => { run_test('translate_emoticons_to_names', () => {
@ -95,9 +97,9 @@ run_test('translate_emoticons_to_names', () => {
{name: 'between symbols', original: 'Hello.<original>! World.', expected: 'Hello.<original>! World.'}, {name: 'between symbols', original: 'Hello.<original>! World.', expected: 'Hello.<original>! World.'},
{name: 'before end of sentence', original: 'Hello <original>!', expected: 'Hello <converted>!'}, {name: 'before end of sentence', original: 'Hello <original>!', expected: 'Hello <converted>!'},
]; ];
_.each(emoji.EMOTICON_CONVERSIONS, (full_name, shortcut) => { _.each(emoji_codes.emoticon_conversions, (full_name, shortcut) => {
_.each(testcases, (t) => { _.each(testcases, (t) => {
var converted_value = ':' + full_name + ':'; var converted_value = full_name;
var original = t.original; var original = t.original;
var expected = t.expected; var expected = t.expected;
original = original.replace(/(<original>)/g, shortcut); original = original.replace(/(<original>)/g, shortcut);

View File

@ -18,18 +18,6 @@ var zulip_emoji = {
deactivated: false, deactivated: false,
}; };
// Emoticons, and which emoji they should become (without colons). Duplicate
// emoji are allowed. Changes here should be mimicked in `zerver/lib/emoji.py`
// and `templates/zerver/help/enable-emoticon-translations.md`.
exports.EMOTICON_CONVERSIONS = {
':)': 'smiley',
'(:': 'smiley',
':(': 'slight_frown',
'<3': 'heart',
':|': 'expressionless',
':/': 'confused',
};
exports.update_emojis = function update_emojis(realm_emojis) { exports.update_emojis = function update_emojis(realm_emojis) {
// exports.all_realm_emojis is emptied before adding the realm-specific emoji // exports.all_realm_emojis is emptied before adding the realm-specific emoji
// to it. This makes sure that in case of deletion, the deleted realm_emojis // to it. This makes sure that in case of deletion, the deleted realm_emojis
@ -148,9 +136,9 @@ exports.translate_emoticons_to_names = function translate_emoticons_to_names(tex
return match; return match;
}; };
for (var emoticon in exports.EMOTICON_CONVERSIONS) { for (var emoticon in emoji_codes.emoticon_conversions) {
if (exports.EMOTICON_CONVERSIONS.hasOwnProperty(emoticon)) { if (emoji_codes.emoticon_conversions.hasOwnProperty(emoticon)) {
replacement_text = ':' + exports.EMOTICON_CONVERSIONS[emoticon] + ':'; replacement_text = emoji_codes.emoticon_conversions[emoticon];
var emoticon_regex = new RegExp('(' + util.escape_regexp(emoticon) + ')', 'g'); var emoticon_regex = new RegExp('(' + util.escape_regexp(emoticon) + ')', 'g');
translated = translated.replace(emoticon_regex, emoticon_replacer); translated = translated.replace(emoticon_regex, emoticon_replacer);
} }

View File

@ -10,7 +10,7 @@ from typing import Any, Dict, List
from emoji_setup_utils import generate_emoji_catalog, generate_codepoint_to_name_map, \ from emoji_setup_utils import generate_emoji_catalog, generate_codepoint_to_name_map, \
get_emoji_code, generate_name_to_codepoint_map, get_remapped_emojis_map, \ get_emoji_code, generate_name_to_codepoint_map, get_remapped_emojis_map, \
emoji_names_for_picker, EMOJISETS emoji_names_for_picker, EMOJISETS, EMOTICON_CONVERSIONS
from emoji_names import EMOJI_NAME_MAPS from emoji_names import EMOJI_NAME_MAPS
ZULIP_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../../') ZULIP_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../../')
@ -35,6 +35,8 @@ exports.codepoint_to_name = %(codepoint_to_name)s;
exports.emoji_catalog = %(emoji_catalog)s; exports.emoji_catalog = %(emoji_catalog)s;
exports.emoticon_conversions = %(emoticon_conversions)s;
return exports; return exports;
}()); }());
if (typeof module !== 'undefined') { if (typeof module !== 'undefined') {
@ -200,6 +202,7 @@ def generate_map_files(cache_path: str, emoji_catalog: Dict[str, List[str]]) ->
'name_to_codepoint': name_to_codepoint, 'name_to_codepoint': name_to_codepoint,
'codepoint_to_name': codepoint_to_name, 'codepoint_to_name': codepoint_to_name,
'emoji_catalog': emoji_catalog, 'emoji_catalog': emoji_catalog,
'emoticon_conversions': EMOTICON_CONVERSIONS,
}) })
NAME_TO_CODEPOINT_PATH = os.path.join(cache_path, 'name_to_codepoint.json') NAME_TO_CODEPOINT_PATH = os.path.join(cache_path, 'name_to_codepoint.json')
@ -210,6 +213,10 @@ def generate_map_files(cache_path: str, emoji_catalog: Dict[str, List[str]]) ->
with open(CODEPOINT_TO_NAME_PATH, 'w') as codepoint_to_name_file: with open(CODEPOINT_TO_NAME_PATH, 'w') as codepoint_to_name_file:
codepoint_to_name_file.write(ujson.dumps(codepoint_to_name)) codepoint_to_name_file.write(ujson.dumps(codepoint_to_name))
EMOTICON_CONVERSIONS_PATH = os.path.join(cache_path, 'emoticon_conversions.json')
with open(EMOTICON_CONVERSIONS_PATH, 'w') as emoticon_conversions_file:
emoticon_conversions_file.write(ujson.dumps(EMOTICON_CONVERSIONS))
def dump_emojis(cache_path: str) -> None: def dump_emojis(cache_path: str) -> None:
with open('emoji_map.json') as emoji_map_file: with open('emoji_map.json') as emoji_map_file:
emoji_map = ujson.load(emoji_map_file) emoji_map = ujson.load(emoji_map_file)

View File

@ -35,6 +35,17 @@ remapped_emojis = {
"1f1fa": "1f1fa-1f1f8", # us "1f1fa": "1f1fa-1f1f8", # us
} }
# Emoticons and which emoji they should become. Duplicate emoji are allowed.
# Changes here should be mimicked in `templates/zerver/help/enable-emoticon-translations.md`.
EMOTICON_CONVERSIONS = {
':)': ':smiley:',
'(:': ':smiley:',
':(': ':slight_frown:',
'<3': ':heart:',
':|': ':expressionless:',
':/': ':confused:',
}
def emoji_names_for_picker(emoji_name_maps: Dict[str, Dict[str, Any]]) -> List[str]: def emoji_names_for_picker(emoji_name_maps: Dict[str, Dict[str, Any]]) -> List[str]:
emoji_names = [] # type: List[str] emoji_names = [] # type: List[str]
for emoji_code, name_info in emoji_name_maps.items(): for emoji_code, name_info in emoji_name_maps.items():

View File

@ -8,4 +8,4 @@ ZULIP_VERSION = "1.8.1+git"
# Typically, adding a dependency only requires a minor version bump, and # Typically, adding a dependency only requires a minor version bump, and
# removing a dependency requires a major version bump. # removing a dependency requires a major version bump.
PROVISION_VERSION = '24.0' PROVISION_VERSION = '24.1'

View File

@ -11,20 +11,19 @@ from zerver.lib.request import JsonableError
from zerver.lib.upload import upload_backend from zerver.lib.upload import upload_backend
from zerver.models import Reaction, Realm, RealmEmoji, UserProfile from zerver.models import Reaction, Realm, RealmEmoji, UserProfile
NAME_TO_CODEPOINT_PATH = os.path.join(settings.STATIC_ROOT, "generated", "emoji", "name_to_codepoint.json") EMOJI_PATH = os.path.join(settings.STATIC_ROOT, "generated", "emoji")
CODEPOINT_TO_NAME_PATH = os.path.join(settings.STATIC_ROOT, "generated", "emoji", "codepoint_to_name.json") 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")
# Emoticons and which emoji they should become. Duplicate emoji are allowed. with open(NAME_TO_CODEPOINT_PATH) as fp:
# Changes here should be mimicked in `static/js/emoji.js` name_to_codepoint = ujson.load(fp)
# and `templates/zerver/help/enable-emoticon-translations.md`.
EMOTICON_CONVERSIONS = { with open(CODEPOINT_TO_NAME_PATH) as fp:
':)': ':smiley:', codepoint_to_name = ujson.load(fp)
'(:': ':smiley:',
':(': ':slight_frown:', with open(EMOTICON_CONVERSIONS_PATH) as fp:
'<3': ':heart:', EMOTICON_CONVERSIONS = ujson.load(fp)
':|': ':expressionless:',
':/': ':confused:',
}
possible_emoticons = EMOTICON_CONVERSIONS.keys() possible_emoticons = EMOTICON_CONVERSIONS.keys()
possible_emoticon_regexes = map(re.escape, possible_emoticons) # type: ignore # AnyStr/str issues possible_emoticon_regexes = map(re.escape, possible_emoticons) # type: ignore # AnyStr/str issues
@ -42,12 +41,6 @@ def translate_emoticons(text: str) -> str:
return translated return translated
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)
def emoji_name_to_emoji_code(realm: Realm, emoji_name: str) -> Tuple[str, str]: def emoji_name_to_emoji_code(realm: Realm, emoji_name: str) -> Tuple[str, str]:
realm_emojis = realm.get_active_emoji() realm_emojis = realm.get_active_emoji()
realm_emoji = realm_emojis.get(emoji_name) realm_emoji = realm_emojis.get(emoji_name)