emoji: Convert active_realm_emojis from object to Map.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
Anders Kaseorg 2020-02-05 15:17:30 -08:00 committed by Tim Abbott
parent 419666fc31
commit 362ab8838b
9 changed files with 29 additions and 27 deletions

View File

@ -174,7 +174,7 @@ set_global('compose', {
finish: noop, finish: noop,
}); });
emoji.active_realm_emojis = {}; emoji.active_realm_emojis = new Map();
emoji.emojis_by_name = emojis_by_name; emoji.emojis_by_name = emojis_by_name;
emoji.emojis = emoji_list; emoji.emojis = emoji_list;

View File

@ -61,9 +61,9 @@ run_test('initialize', () => {
}); });
run_test('get_canonical_name', () => { run_test('get_canonical_name', () => {
emoji.active_realm_emojis = { emoji.active_realm_emojis = new Map(Object.entries({
realm_emoji: 'TBD', realm_emoji: 'TBD',
}; }));
let canonical_name = emoji.get_canonical_name('realm_emoji'); let canonical_name = emoji.get_canonical_name('realm_emoji');
assert.equal(canonical_name, 'realm_emoji'); assert.equal(canonical_name, 'realm_emoji');
@ -79,9 +79,9 @@ run_test('get_canonical_name', () => {
canonical_name = emoji.get_canonical_name('+1'); canonical_name = emoji.get_canonical_name('+1');
assert.equal(canonical_name, 'thumbs_up'); assert.equal(canonical_name, 'thumbs_up');
emoji.active_realm_emojis = { emoji.active_realm_emojis = new Map(Object.entries({
'+1': 'TBD', '+1': 'TBD',
}; }));
canonical_name = emoji.get_canonical_name('+1'); canonical_name = emoji.get_canonical_name('+1');
assert.equal(canonical_name, '+1'); assert.equal(canonical_name, '+1');

View File

@ -25,7 +25,7 @@ set_global('emoji', {
deactivated: false, deactivated: false,
}, },
})), })),
active_realm_emojis: { active_realm_emojis: new Map(Object.entries({
realm_emoji: { realm_emoji: {
id: '991', id: '991',
emoji_name: 'realm_emoji', emoji_name: 'realm_emoji',
@ -36,7 +36,7 @@ set_global('emoji', {
emoji_name: 'zulip', emoji_name: 'zulip',
emoji_url: 'TBD', emoji_url: 'TBD',
}, },
}, })),
deactivated_realm_emojis: { deactivated_realm_emojis: {
inactive_realm_emoji: { inactive_realm_emoji: {
emoji_name: 'inactive_realm_emoji', emoji_name: 'inactive_realm_emoji',

View File

@ -652,9 +652,9 @@ run_test('render_emoji', () => {
emoji_name: 'thumbs_up', emoji_name: 'thumbs_up',
emoji_code: '1f44d', emoji_code: '1f44d',
}; };
emoji.active_realm_emojis = { emoji.active_realm_emojis = new Map(Object.entries({
realm_emoji: 'TBD', realm_emoji: 'TBD',
}; }));
global.stub_templates(function (template_name, args) { global.stub_templates(function (template_name, args) {
assert.equal(template_name, 'typeahead_list_item'); assert.equal(template_name, 'typeahead_list_item');

View File

@ -4,7 +4,7 @@
exports.emojis_by_name = new Map(); exports.emojis_by_name = new Map();
exports.all_realm_emojis = new Map(); exports.all_realm_emojis = new Map();
exports.active_realm_emojis = {}; exports.active_realm_emojis = new Map();
exports.default_emoji_aliases = {}; exports.default_emoji_aliases = {};
const zulip_emoji = { const zulip_emoji = {
@ -20,7 +20,7 @@ exports.update_emojis = function update_emojis(realm_emojis) {
// 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
// don't persist in exports.active_realm_emojis. // don't persist in exports.active_realm_emojis.
exports.all_realm_emojis.clear(); exports.all_realm_emojis.clear();
exports.active_realm_emojis = {}; exports.active_realm_emojis.clear();
_.each(realm_emojis, function (data) { _.each(realm_emojis, function (data) {
exports.all_realm_emojis.set(data.id, { exports.all_realm_emojis.set(data.id, {
@ -30,14 +30,16 @@ exports.update_emojis = function update_emojis(realm_emojis) {
deactivated: data.deactivated, deactivated: data.deactivated,
}); });
if (data.deactivated !== true) { if (data.deactivated !== true) {
exports.active_realm_emojis[data.name] = {id: data.id, exports.active_realm_emojis.set(data.name, {
emoji_name: data.name, id: data.id,
emoji_url: data.source_url}; emoji_name: data.name,
emoji_url: data.source_url,
});
} }
}); });
// Add the Zulip emoji to the realm emojis list // Add the Zulip emoji to the realm emojis list
exports.all_realm_emojis.zulip = zulip_emoji; exports.all_realm_emojis.set("zulip", zulip_emoji);
exports.active_realm_emojis.zulip = zulip_emoji; exports.active_realm_emojis.set("zulip", zulip_emoji);
exports.build_emoji_data(exports.active_realm_emojis); exports.build_emoji_data(exports.active_realm_emojis);
}; };
@ -76,7 +78,7 @@ exports.initialize = function initialize() {
exports.build_emoji_data = function (realm_emojis) { exports.build_emoji_data = function (realm_emojis) {
exports.emojis_by_name.clear(); exports.emojis_by_name.clear();
let emoji_dict; let emoji_dict;
_.each(realm_emojis, function (realm_emoji, realm_emoji_name) { for (const [realm_emoji_name, realm_emoji] of realm_emojis) {
emoji_dict = { emoji_dict = {
name: realm_emoji_name, name: realm_emoji_name,
display_name: realm_emoji_name, display_name: realm_emoji_name,
@ -86,7 +88,7 @@ exports.build_emoji_data = function (realm_emojis) {
has_reacted: false, has_reacted: false,
}; };
exports.emojis_by_name.set(realm_emoji_name, emoji_dict); exports.emojis_by_name.set(realm_emoji_name, emoji_dict);
}); }
_.each(emoji_codes.emoji_catalog, function (codepoints) { _.each(emoji_codes.emoji_catalog, function (codepoints) {
_.each(codepoints, function (codepoint) { _.each(codepoints, function (codepoint) {
@ -129,7 +131,7 @@ exports.build_emoji_upload_widget = function () {
}; };
exports.get_canonical_name = function (emoji_name) { exports.get_canonical_name = function (emoji_name) {
if (exports.active_realm_emojis.hasOwnProperty(emoji_name)) { if (exports.active_realm_emojis.has(emoji_name)) {
return emoji_name; return emoji_name;
} }
if (!emoji_codes.name_to_codepoint.hasOwnProperty(emoji_name)) { if (!emoji_codes.name_to_codepoint.hasOwnProperty(emoji_name)) {

View File

@ -102,9 +102,9 @@ function show_emoji_catalog() {
exports.generate_emoji_picker_data = function (realm_emojis) { exports.generate_emoji_picker_data = function (realm_emojis) {
exports.complete_emoji_catalog = {}; exports.complete_emoji_catalog = {};
exports.complete_emoji_catalog.Custom = []; exports.complete_emoji_catalog.Custom = [];
_.each(realm_emojis, function (realm_emoji, realm_emoji_name) { for (const realm_emoji_name of realm_emojis.keys()) {
exports.complete_emoji_catalog.Custom.push(emoji.emojis_by_name.get(realm_emoji_name)); exports.complete_emoji_catalog.Custom.push(emoji.emojis_by_name.get(realm_emoji_name));
}); }
_.each(emoji_codes.emoji_catalog, function (codepoints, category) { _.each(emoji_codes.emoji_catalog, function (codepoints, category) {
exports.complete_emoji_catalog[category] = []; exports.complete_emoji_catalog[category] = [];
@ -243,7 +243,7 @@ function get_alias_to_be_used(message_id, emoji_name) {
// the passed name as it is. // the passed name as it is.
const message = message_store.get(message_id); const message = message_store.get(message_id);
let aliases = [emoji_name]; let aliases = [emoji_name];
if (!emoji.active_realm_emojis.hasOwnProperty(emoji_name)) { if (!emoji.active_realm_emojis.has(emoji_name)) {
if (emoji_codes.name_to_codepoint.hasOwnProperty(emoji_name)) { if (emoji_codes.name_to_codepoint.hasOwnProperty(emoji_name)) {
const codepoint = emoji_codes.name_to_codepoint[emoji_name]; const codepoint = emoji_codes.name_to_codepoint[emoji_name];
aliases = emoji.default_emoji_aliases[codepoint]; aliases = emoji.default_emoji_aliases[codepoint];

View File

@ -189,8 +189,8 @@ function handleUnicodeEmoji(unicode_emoji) {
function handleEmoji(emoji_name) { function handleEmoji(emoji_name) {
const alt_text = ':' + emoji_name + ':'; const alt_text = ':' + emoji_name + ':';
const title = emoji_name.split("_").join(" "); const title = emoji_name.split("_").join(" ");
if (emoji.active_realm_emojis.hasOwnProperty(emoji_name)) { if (emoji.active_realm_emojis.has(emoji_name)) {
const emoji_url = emoji.active_realm_emojis[emoji_name].emoji_url; const emoji_url = emoji.active_realm_emojis.get(emoji_name).emoji_url;
return '<img alt="' + alt_text + '"' + return '<img alt="' + alt_text + '"' +
' class="emoji" src="' + emoji_url + '"' + ' class="emoji" src="' + emoji_url + '"' +
' title="' + title + '">'; ' title="' + title + '">';

View File

@ -119,13 +119,13 @@ exports.toggle_emoji_reaction = function (message_id, emoji_name) {
emoji_name: emoji_name, emoji_name: emoji_name,
}; };
if (emoji.active_realm_emojis.hasOwnProperty(emoji_name)) { if (emoji.active_realm_emojis.has(emoji_name)) {
if (emoji_name === 'zulip') { if (emoji_name === 'zulip') {
reaction_info.reaction_type = 'zulip_extra_emoji'; reaction_info.reaction_type = 'zulip_extra_emoji';
} else { } else {
reaction_info.reaction_type = 'realm_emoji'; reaction_info.reaction_type = 'realm_emoji';
} }
reaction_info.emoji_code = emoji.active_realm_emojis[emoji_name].id; reaction_info.emoji_code = emoji.active_realm_emojis.get(emoji_name).id;
} else if (emoji_codes.name_to_codepoint.hasOwnProperty(emoji_name)) { } else if (emoji_codes.name_to_codepoint.hasOwnProperty(emoji_name)) {
reaction_info.reaction_type = 'unicode_emoji'; reaction_info.reaction_type = 'unicode_emoji';
reaction_info.emoji_code = emoji_codes.name_to_codepoint[emoji_name]; reaction_info.emoji_code = emoji_codes.name_to_codepoint[emoji_name];

View File

@ -148,7 +148,7 @@ exports.render_emoji = function (item) {
is_emoji: true, is_emoji: true,
primary: item.emoji_name.split("_").join(" "), primary: item.emoji_name.split("_").join(" "),
}; };
if (emoji.active_realm_emojis.hasOwnProperty(item.emoji_name)) { if (emoji.active_realm_emojis.has(item.emoji_name)) {
args.img_src = item.emoji_url; args.img_src = item.emoji_url;
} else { } else {
args.emoji_code = item.emoji_code; args.emoji_code = item.emoji_code;