2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-12-23 08:12:26 +08:00
|
|
|
class Emoji
|
2016-03-08 02:47:40 +08:00
|
|
|
# update this to clear the cache
|
2022-01-15 04:51:13 +08:00
|
|
|
EMOJI_VERSION = "12"
|
2016-03-08 02:47:40 +08:00
|
|
|
|
2017-06-14 02:03:59 +08:00
|
|
|
FITZPATRICK_SCALE ||= [ "1f3fb", "1f3fc", "1f3fd", "1f3fe", "1f3ff" ]
|
|
|
|
|
2020-03-31 02:16:10 +08:00
|
|
|
DEFAULT_GROUP ||= "default"
|
|
|
|
|
2014-12-23 08:12:26 +08:00
|
|
|
include ActiveModel::SerializerSupport
|
|
|
|
|
2020-03-31 02:16:10 +08:00
|
|
|
attr_accessor :name, :url, :tonable, :group
|
2014-12-23 08:12:26 +08:00
|
|
|
|
|
|
|
def self.all
|
2016-07-23 00:59:43 +08:00
|
|
|
Discourse.cache.fetch(cache_key("all_emojis")) { standard | custom }
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.standard
|
2016-07-23 00:59:43 +08:00
|
|
|
Discourse.cache.fetch(cache_key("standard_emojis")) { load_standard }
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
|
2015-03-17 01:28:11 +08:00
|
|
|
def self.aliases
|
2017-06-05 20:06:23 +08:00
|
|
|
Discourse.cache.fetch(cache_key("aliases_emojis")) { db['aliases'] }
|
2015-03-17 01:28:11 +08:00
|
|
|
end
|
|
|
|
|
2019-01-04 22:14:16 +08:00
|
|
|
def self.search_aliases
|
2018-05-01 21:43:49 +08:00
|
|
|
Discourse.cache.fetch(cache_key("search_aliases_emojis")) { db['searchAliases'] }
|
|
|
|
end
|
|
|
|
|
2019-01-04 22:14:16 +08:00
|
|
|
def self.translations
|
|
|
|
Discourse.cache.fetch(cache_key("translations_emojis")) { load_translations }
|
|
|
|
end
|
|
|
|
|
2014-12-23 08:12:26 +08:00
|
|
|
def self.custom
|
2016-07-23 00:59:43 +08:00
|
|
|
Discourse.cache.fetch(cache_key("custom_emojis")) { load_custom }
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
|
2017-06-05 20:06:23 +08:00
|
|
|
def self.tonable_emojis
|
|
|
|
Discourse.cache.fetch(cache_key("tonable_emojis")) { db['tonableEmojis'] }
|
|
|
|
end
|
|
|
|
|
2019-07-03 15:23:40 +08:00
|
|
|
def self.custom?(name)
|
|
|
|
name = name.delete_prefix(':').delete_suffix(':')
|
|
|
|
Emoji.custom.detect { |e| e.name == name }.present?
|
|
|
|
end
|
|
|
|
|
2015-02-10 01:54:57 +08:00
|
|
|
def self.exists?(name)
|
|
|
|
Emoji[name].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.[](name)
|
2019-06-26 21:11:04 +08:00
|
|
|
name = name.delete_prefix(':').delete_suffix(':')
|
|
|
|
is_toned = name.match?(/.+:t[1-6]/)
|
|
|
|
normalized_name = name.gsub(/(.+):t[1-6]/, '\1')
|
|
|
|
|
|
|
|
Emoji.all.detect do |e|
|
|
|
|
e.name == normalized_name &&
|
|
|
|
(!is_toned || (is_toned && e.tonable))
|
|
|
|
end
|
2015-02-10 01:54:57 +08:00
|
|
|
end
|
|
|
|
|
2014-12-23 08:12:26 +08:00
|
|
|
def self.create_from_db_item(emoji)
|
2016-03-05 03:20:44 +08:00
|
|
|
name = emoji["name"]
|
2017-07-11 23:51:53 +08:00
|
|
|
filename = emoji['filename'] || name
|
2019-06-26 21:11:04 +08:00
|
|
|
|
2014-12-23 08:12:26 +08:00
|
|
|
Emoji.new.tap do |e|
|
|
|
|
e.name = name
|
2019-06-26 21:11:04 +08:00
|
|
|
e.tonable = Emoji.tonable_emojis.include?(name)
|
2017-07-11 23:51:53 +08:00
|
|
|
e.url = Emoji.url_for(filename)
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-11 23:51:53 +08:00
|
|
|
def self.url_for(name)
|
2019-06-26 21:11:04 +08:00
|
|
|
name = name.delete_prefix(':').delete_suffix(':').gsub(/(.+):t([1-6])/, '\1/\2')
|
2021-03-03 03:04:16 +08:00
|
|
|
if SiteSetting.external_emoji_url.blank?
|
|
|
|
"#{Discourse.base_path}/images/emoji/#{SiteSetting.emoji_set}/#{name}.png?v=#{EMOJI_VERSION}"
|
|
|
|
else
|
|
|
|
"#{SiteSetting.external_emoji_url}/#{SiteSetting.emoji_set}/#{name}.png?v=#{EMOJI_VERSION}"
|
|
|
|
end
|
2017-07-11 23:51:53 +08:00
|
|
|
end
|
|
|
|
|
2016-07-23 00:59:43 +08:00
|
|
|
def self.cache_key(name)
|
2017-07-11 23:51:53 +08:00
|
|
|
"#{name}:v#{EMOJI_VERSION}:#{Plugin::CustomEmoji.cache_key}"
|
2016-07-23 00:59:43 +08:00
|
|
|
end
|
|
|
|
|
2014-12-23 08:12:26 +08:00
|
|
|
def self.clear_cache
|
2019-01-04 22:14:16 +08:00
|
|
|
%w{custom standard aliases search_aliases translations all tonable}.each do |key|
|
2017-11-21 06:50:23 +08:00
|
|
|
Discourse.cache.delete(cache_key("#{key}_emojis"))
|
|
|
|
end
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.db_file
|
2019-01-04 22:14:16 +08:00
|
|
|
@db_file ||= "#{Rails.root}/lib/emoji/db.json"
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
|
2015-03-17 01:28:11 +08:00
|
|
|
def self.db
|
2017-06-05 20:06:23 +08:00
|
|
|
@db ||= File.open(db_file, "r:UTF-8") { |f| JSON.parse(f.read) }
|
2015-03-17 01:28:11 +08:00
|
|
|
end
|
|
|
|
|
2014-12-23 08:12:26 +08:00
|
|
|
def self.load_standard
|
2016-03-05 03:20:44 +08:00
|
|
|
db['emojis'].map { |e| Emoji.create_from_db_item(e) }
|
2015-03-17 01:28:11 +08:00
|
|
|
end
|
|
|
|
|
2014-12-23 08:12:26 +08:00
|
|
|
def self.load_custom
|
2016-07-23 00:59:43 +08:00
|
|
|
result = []
|
|
|
|
|
2019-06-13 10:58:27 +08:00
|
|
|
if !GlobalSetting.skip_db?
|
|
|
|
CustomEmoji.includes(:upload).order(:name).each do |emoji|
|
|
|
|
result << Emoji.new.tap do |e|
|
|
|
|
e.name = emoji.name
|
|
|
|
e.url = emoji.upload&.url
|
2020-03-31 02:16:10 +08:00
|
|
|
e.group = emoji.group || DEFAULT_GROUP
|
2019-06-13 10:58:27 +08:00
|
|
|
end
|
2017-02-02 17:41:57 +08:00
|
|
|
end
|
|
|
|
end
|
2016-07-23 00:59:43 +08:00
|
|
|
|
2020-03-31 02:16:10 +08:00
|
|
|
Plugin::CustomEmoji.emojis.each do |group, emojis|
|
|
|
|
emojis.each do |name, url|
|
|
|
|
result << Emoji.new.tap do |e|
|
|
|
|
e.name = name
|
2020-10-09 19:51:24 +08:00
|
|
|
url = (Discourse.base_path + url) if url[/^\/[^\/]/]
|
2020-03-31 02:16:10 +08:00
|
|
|
e.url = url
|
|
|
|
e.group = group || DEFAULT_GROUP
|
|
|
|
end
|
2016-07-23 00:59:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
|
2019-01-04 22:14:16 +08:00
|
|
|
def self.load_translations
|
2020-05-28 02:11:52 +08:00
|
|
|
db["translations"]
|
2019-01-04 22:14:16 +08:00
|
|
|
end
|
|
|
|
|
2014-12-23 08:12:26 +08:00
|
|
|
def self.base_directory
|
2015-08-22 04:42:37 +08:00
|
|
|
"public#{base_url}"
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.base_url
|
|
|
|
db = RailsMultisite::ConnectionManagement.current_db
|
2020-10-09 19:51:24 +08:00
|
|
|
"#{Discourse.base_path}/uploads/#{db}/_emoji"
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
|
2016-07-06 01:55:21 +08:00
|
|
|
def self.replacement_code(code)
|
2019-08-07 17:38:58 +08:00
|
|
|
code
|
2020-04-30 14:48:34 +08:00
|
|
|
.split('-')
|
2019-08-07 17:38:58 +08:00
|
|
|
.map!(&:hex)
|
2020-04-30 14:48:34 +08:00
|
|
|
.pack("U*")
|
2016-07-06 01:55:21 +08:00
|
|
|
end
|
|
|
|
|
2015-12-31 03:35:25 +08:00
|
|
|
def self.unicode_replacements
|
2019-01-04 22:14:16 +08:00
|
|
|
@unicode_replacements ||= begin
|
|
|
|
replacements = {}
|
|
|
|
is_tonable_emojis = Emoji.tonable_emojis
|
|
|
|
fitzpatrick_scales = FITZPATRICK_SCALE.map { |scale| scale.to_i(16) }
|
|
|
|
|
|
|
|
db['emojis'].each do |e|
|
|
|
|
name = e['name']
|
2019-08-07 17:38:58 +08:00
|
|
|
|
|
|
|
# special cased as we prefer to keep these as symbols
|
2019-08-30 13:06:23 +08:00
|
|
|
next if name == 'registered'
|
|
|
|
next if name == 'copyright'
|
|
|
|
next if name == 'tm'
|
|
|
|
next if name == 'left_right_arrow'
|
2019-01-04 22:14:16 +08:00
|
|
|
|
|
|
|
code = replacement_code(e['code'])
|
|
|
|
next unless code
|
|
|
|
|
|
|
|
replacements[code] = name
|
|
|
|
if is_tonable_emojis.include?(name)
|
|
|
|
fitzpatrick_scales.each_with_index do |scale, index|
|
2020-04-30 14:48:34 +08:00
|
|
|
toned_code = code.codepoints.insert(1, scale).pack("U*")
|
2019-01-04 22:14:16 +08:00
|
|
|
replacements[toned_code] = "#{name}:t#{index + 2}"
|
|
|
|
end
|
2017-06-14 21:35:37 +08:00
|
|
|
end
|
|
|
|
end
|
2016-03-05 03:20:44 +08:00
|
|
|
|
2019-01-04 22:14:16 +08:00
|
|
|
replacements["\u{2639}"] = 'frowning'
|
|
|
|
replacements["\u{263B}"] = 'slight_smile'
|
|
|
|
replacements["\u{2661}"] = 'heart'
|
|
|
|
replacements["\u{2665}"] = 'heart'
|
2022-01-15 04:51:13 +08:00
|
|
|
replacements["\u{263A}"] = 'relaxed'
|
2015-12-31 03:46:52 +08:00
|
|
|
|
2019-01-04 22:14:16 +08:00
|
|
|
replacements
|
|
|
|
end
|
2015-12-31 03:35:25 +08:00
|
|
|
end
|
|
|
|
|
2017-05-15 22:27:54 +08:00
|
|
|
def self.unicode_unescape(string)
|
2019-03-21 16:11:33 +08:00
|
|
|
PrettyText.escape_emoji(string)
|
2017-05-15 22:27:54 +08:00
|
|
|
end
|
|
|
|
|
2017-07-22 02:24:28 +08:00
|
|
|
def self.gsub_emoji_to_unicode(str)
|
|
|
|
if str
|
|
|
|
str.gsub(/:([\w\-+]*(?::t\d)?):/) { |name| Emoji.lookup_unicode($1) || name }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-11 10:03:21 +08:00
|
|
|
def self.lookup_unicode(name)
|
|
|
|
@reverse_map ||= begin
|
|
|
|
map = {}
|
2017-06-20 12:02:39 +08:00
|
|
|
is_tonable_emojis = Emoji.tonable_emojis
|
2017-06-06 00:53:11 +08:00
|
|
|
|
2016-10-11 10:03:21 +08:00
|
|
|
db['emojis'].each do |e|
|
|
|
|
next if e['name'] == 'tm'
|
2017-06-14 02:03:59 +08:00
|
|
|
|
2016-10-11 10:03:21 +08:00
|
|
|
code = replacement_code(e['code'])
|
2017-06-14 02:03:59 +08:00
|
|
|
next unless code
|
|
|
|
|
|
|
|
map[e['name']] = code
|
2017-06-20 12:02:39 +08:00
|
|
|
if is_tonable_emojis.include?(e['name'])
|
2017-06-14 02:03:59 +08:00
|
|
|
FITZPATRICK_SCALE.each_with_index do |scale, index|
|
|
|
|
toned_code = (code.codepoints.insert(1, scale.to_i(16))).pack("U*")
|
|
|
|
map["#{e['name']}:t#{index + 2}"] = toned_code
|
|
|
|
end
|
|
|
|
end
|
2016-10-11 10:03:21 +08:00
|
|
|
end
|
2017-06-06 00:53:11 +08:00
|
|
|
|
|
|
|
Emoji.aliases.each do |key, alias_names|
|
|
|
|
next unless alias_code = map[key]
|
|
|
|
alias_names.each do |alias_name|
|
|
|
|
map[alias_name] = alias_code
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-11 10:03:21 +08:00
|
|
|
map
|
|
|
|
end
|
|
|
|
@reverse_map[name]
|
|
|
|
end
|
|
|
|
|
2016-03-03 03:31:32 +08:00
|
|
|
def self.unicode_replacements_json
|
|
|
|
@unicode_replacements_json ||= unicode_replacements.to_json
|
2015-12-31 03:35:25 +08:00
|
|
|
end
|
|
|
|
|
2021-06-26 01:48:36 +08:00
|
|
|
def self.codes_to_img(str)
|
|
|
|
return if str.blank?
|
|
|
|
|
|
|
|
str = str.gsub(/:([\w\-+]*(?::t\d)?):/) do |name|
|
|
|
|
code = $1
|
|
|
|
|
|
|
|
if code && Emoji.custom?(code)
|
|
|
|
emoji = Emoji[code]
|
|
|
|
"<img src=\"#{emoji.url}\" title=\"#{code}\" class=\"emoji\" alt=\"#{code}\">"
|
|
|
|
elsif code && Emoji.exists?(code)
|
|
|
|
"<img src=\"#{Emoji.url_for(code)}\" title=\"#{code}\" class=\"emoji\" alt=\"#{code}\">"
|
|
|
|
else
|
|
|
|
name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|