2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-12-22 19:12:26 -05:00
|
|
|
class Emoji
|
2016-03-07 13:47:40 -05:00
|
|
|
# update this to clear the cache
|
2022-01-14 15:51:13 -05:00
|
|
|
EMOJI_VERSION = "12"
|
2016-03-07 13:47:40 -05:00
|
|
|
|
2024-10-15 22:09:07 -04:00
|
|
|
FITZPATRICK_SCALE = %w[1f3fb 1f3fc 1f3fd 1f3fe 1f3ff]
|
2017-06-13 14:03:59 -04:00
|
|
|
|
2024-10-15 22:09:07 -04:00
|
|
|
DEFAULT_GROUP = "default"
|
2020-03-30 14:16:10 -04:00
|
|
|
|
2014-12-22 19:12:26 -05:00
|
|
|
include ActiveModel::SerializerSupport
|
|
|
|
|
2024-11-01 11:32:59 -04:00
|
|
|
attr_accessor :name, :url, :tonable, :group, :search_aliases, :created_by
|
2014-12-22 19:12:26 -05:00
|
|
|
|
2022-02-15 20:46:17 -05:00
|
|
|
def self.global_emoji_cache
|
|
|
|
@global_emoji_cache ||= DistributedCache.new("global_emoji_cache", namespace: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.site_emoji_cache
|
|
|
|
@site_emoji_cache ||= DistributedCache.new("site_emoji_cache")
|
|
|
|
end
|
|
|
|
|
2014-12-22 19:12:26 -05:00
|
|
|
def self.all
|
2016-07-22 12:59:43 -04:00
|
|
|
Discourse.cache.fetch(cache_key("all_emojis")) { standard | custom }
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.standard
|
2016-07-22 12:59:43 -04:00
|
|
|
Discourse.cache.fetch(cache_key("standard_emojis")) { load_standard }
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|
|
|
|
|
2023-04-13 03:38:54 -04:00
|
|
|
def self.allowed
|
|
|
|
Discourse.cache.fetch(cache_key("allowed_emojis")) { load_allowed }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.denied
|
|
|
|
Discourse.cache.fetch(cache_key("denied_emojis")) { load_denied }
|
|
|
|
end
|
|
|
|
|
2015-03-16 13:28:11 -04:00
|
|
|
def self.aliases
|
2022-11-14 07:38:50 -05:00
|
|
|
db["aliases"]
|
2015-03-16 13:28:11 -04:00
|
|
|
end
|
|
|
|
|
2019-01-04 09:14:16 -05:00
|
|
|
def self.search_aliases
|
2022-11-14 07:38:50 -05:00
|
|
|
db["searchAliases"]
|
2018-05-01 09:43:49 -04:00
|
|
|
end
|
|
|
|
|
2019-01-04 09:14:16 -05:00
|
|
|
def self.translations
|
|
|
|
Discourse.cache.fetch(cache_key("translations_emojis")) { load_translations }
|
|
|
|
end
|
|
|
|
|
2014-12-22 19:12:26 -05:00
|
|
|
def self.custom
|
2016-07-22 12:59:43 -04:00
|
|
|
Discourse.cache.fetch(cache_key("custom_emojis")) { load_custom }
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|
|
|
|
|
2017-06-05 08:06:23 -04:00
|
|
|
def self.tonable_emojis
|
2022-11-14 07:38:50 -05:00
|
|
|
db["tonableEmojis"]
|
2017-06-05 08:06:23 -04:00
|
|
|
end
|
|
|
|
|
2019-07-03 03:23:40 -04:00
|
|
|
def self.custom?(name)
|
|
|
|
name = name.delete_prefix(":").delete_suffix(":")
|
|
|
|
Emoji.custom.detect { |e| e.name == name }.present?
|
|
|
|
end
|
|
|
|
|
2015-02-09 12:54:57 -05:00
|
|
|
def self.exists?(name)
|
|
|
|
Emoji[name].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.[](name)
|
2019-06-26 09:11:04 -04:00
|
|
|
name = name.delete_prefix(":").delete_suffix(":")
|
2023-03-21 12:48:55 -04:00
|
|
|
is_toned = name.match?(/\A.+:t[1-6]\z/)
|
|
|
|
normalized_name = name.gsub(/\A(.+):t[1-6]\z/, '\1')
|
2019-06-26 09:11:04 -04:00
|
|
|
|
2022-02-15 20:46:17 -05:00
|
|
|
found_emoji = nil
|
|
|
|
|
|
|
|
[[global_emoji_cache, :standard], [site_emoji_cache, :custom]].each do |cache, list_key|
|
2023-03-21 13:33:12 -04:00
|
|
|
found_emoji =
|
2022-02-15 20:46:17 -05:00
|
|
|
cache.defer_get_set(normalized_name) do
|
2023-04-10 11:44:11 -04:00
|
|
|
[
|
|
|
|
Emoji
|
|
|
|
.public_send(list_key)
|
|
|
|
.detect { |e| e.name == normalized_name && (!is_toned || (is_toned && e.tonable)) },
|
|
|
|
]
|
|
|
|
end[
|
|
|
|
0
|
|
|
|
]
|
2022-02-15 20:46:17 -05:00
|
|
|
|
|
|
|
break if found_emoji
|
2019-06-26 09:11:04 -04:00
|
|
|
end
|
2022-02-15 20:46:17 -05:00
|
|
|
|
|
|
|
found_emoji
|
2015-02-09 12:54:57 -05:00
|
|
|
end
|
|
|
|
|
2014-12-22 19:12:26 -05:00
|
|
|
def self.create_from_db_item(emoji)
|
2016-03-04 14:20:44 -05:00
|
|
|
name = emoji["name"]
|
2022-10-19 03:53:56 -04:00
|
|
|
return unless group = groups[name]
|
2017-07-11 11:51:53 -04:00
|
|
|
filename = emoji["filename"] || name
|
2019-06-26 09:11:04 -04:00
|
|
|
|
2014-12-22 19:12:26 -05:00
|
|
|
Emoji.new.tap do |e|
|
|
|
|
e.name = name
|
2019-06-26 09:11:04 -04:00
|
|
|
e.tonable = Emoji.tonable_emojis.include?(name)
|
2017-07-11 11:51:53 -04:00
|
|
|
e.url = Emoji.url_for(filename)
|
2022-10-19 03:53:56 -04:00
|
|
|
e.group = group
|
2022-10-15 08:09:00 -04:00
|
|
|
e.search_aliases = search_aliases[name] || []
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-11 11:51:53 -04:00
|
|
|
def self.url_for(name)
|
2019-06-26 09:11:04 -04:00
|
|
|
name = name.delete_prefix(":").delete_suffix(":").gsub(/(.+):t([1-6])/, '\1/\2')
|
2021-03-02 14:04:16 -05: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 11:51:53 -04:00
|
|
|
end
|
|
|
|
|
2016-07-22 12:59:43 -04:00
|
|
|
def self.cache_key(name)
|
2022-02-15 20:46:17 -05:00
|
|
|
"#{name}#{cache_postfix}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.cache_postfix
|
|
|
|
":v#{EMOJI_VERSION}:#{Plugin::CustomEmoji.cache_key}"
|
2016-07-22 12:59:43 -04:00
|
|
|
end
|
|
|
|
|
2014-12-22 19:12:26 -05:00
|
|
|
def self.clear_cache
|
2023-04-13 03:38:54 -04:00
|
|
|
%w[custom standard translations allowed denied all].each do |key|
|
2017-11-20 17:50:23 -05:00
|
|
|
Discourse.cache.delete(cache_key("#{key}_emojis"))
|
|
|
|
end
|
2022-02-15 20:46:17 -05:00
|
|
|
global_emoji_cache.clear
|
|
|
|
site_emoji_cache.clear
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|
|
|
|
|
2022-09-14 07:10:48 -04:00
|
|
|
def self.groups_file
|
|
|
|
@groups_file ||= "#{Rails.root}/lib/emoji/groups.json"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.groups
|
|
|
|
@groups ||=
|
|
|
|
begin
|
|
|
|
groups = {}
|
|
|
|
|
|
|
|
File
|
|
|
|
.open(groups_file, "r:UTF-8") { |f| JSON.parse(f.read) }
|
|
|
|
.each { |group| group["icons"].each { |icon| groups[icon["name"]] = group["name"] } }
|
|
|
|
|
|
|
|
groups
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-22 19:12:26 -05:00
|
|
|
def self.db_file
|
2019-01-04 09:14:16 -05:00
|
|
|
@db_file ||= "#{Rails.root}/lib/emoji/db.json"
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|
|
|
|
|
2015-03-16 13:28:11 -04:00
|
|
|
def self.db
|
2017-06-05 08:06:23 -04:00
|
|
|
@db ||= File.open(db_file, "r:UTF-8") { |f| JSON.parse(f.read) }
|
2015-03-16 13:28:11 -04:00
|
|
|
end
|
|
|
|
|
2014-12-22 19:12:26 -05:00
|
|
|
def self.load_standard
|
2022-10-19 03:53:56 -04:00
|
|
|
db["emojis"].map { |e| Emoji.create_from_db_item(e) }.compact
|
2015-03-16 13:28:11 -04:00
|
|
|
end
|
|
|
|
|
2023-04-13 03:38:54 -04:00
|
|
|
def self.load_allowed
|
|
|
|
denied_emojis = denied
|
|
|
|
all_emojis = load_standard + load_custom
|
|
|
|
|
|
|
|
if denied_emojis.present?
|
|
|
|
all_emojis.reject { |e| denied_emojis.include?(e.name) }
|
|
|
|
else
|
|
|
|
all_emojis
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.load_denied
|
|
|
|
if SiteSetting.emoji_deny_list.present?
|
|
|
|
denied_emoji = SiteSetting.emoji_deny_list.split("|")
|
|
|
|
if denied_emoji.size > 0
|
|
|
|
denied_emoji.concat(denied_emoji.flat_map { |e| Emoji.aliases[e] }.compact)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-22 19:12:26 -05:00
|
|
|
def self.load_custom
|
2016-07-22 12:59:43 -04:00
|
|
|
result = []
|
|
|
|
|
2019-06-12 22:58:27 -04: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-30 14:16:10 -04:00
|
|
|
e.group = emoji.group || DEFAULT_GROUP
|
2024-11-01 11:32:59 -04:00
|
|
|
e.created_by = User.where(id: emoji.user_id).pick(:username)
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2019-06-12 22:58:27 -04:00
|
|
|
end
|
2017-02-02 04:41:57 -05:00
|
|
|
end
|
2016-07-22 12:59:43 -04:00
|
|
|
|
2020-03-30 14:16:10 -04:00
|
|
|
Plugin::CustomEmoji.emojis.each do |group, emojis|
|
|
|
|
emojis.each do |name, url|
|
|
|
|
result << Emoji.new.tap do |e|
|
|
|
|
e.name = name
|
2023-01-20 13:52:49 -05:00
|
|
|
url = (Discourse.base_path + url) if url[%r{\A/[^/]}]
|
2020-03-30 14:16:10 -04:00
|
|
|
e.url = url
|
|
|
|
e.group = group || DEFAULT_GROUP
|
|
|
|
end
|
2016-07-22 12:59:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|
|
|
|
|
2019-01-04 09:14:16 -05:00
|
|
|
def self.load_translations
|
2020-05-27 14:11:52 -04:00
|
|
|
db["translations"]
|
2019-01-04 09:14:16 -05:00
|
|
|
end
|
|
|
|
|
2014-12-22 19:12:26 -05:00
|
|
|
def self.base_directory
|
2015-08-21 16:42:37 -04:00
|
|
|
"public#{base_url}"
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.base_url
|
|
|
|
db = RailsMultisite::ConnectionManagement.current_db
|
2020-10-09 07:51:24 -04:00
|
|
|
"#{Discourse.base_path}/uploads/#{db}/_emoji"
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|
|
|
|
|
2016-07-05 13:55:21 -04:00
|
|
|
def self.replacement_code(code)
|
2019-08-07 05:38:58 -04:00
|
|
|
code.split("-").map!(&:hex).pack("U*")
|
2016-07-05 13:55:21 -04:00
|
|
|
end
|
|
|
|
|
2015-12-30 14:35:25 -05:00
|
|
|
def self.unicode_replacements
|
2019-01-04 09:14:16 -05:00
|
|
|
@unicode_replacements ||=
|
|
|
|
begin
|
|
|
|
replacements = {}
|
|
|
|
is_tonable_emojis = Emoji.tonable_emojis
|
|
|
|
fitzpatrick_scales = FITZPATRICK_SCALE.map { |scale| scale.to_i(16) }
|
2023-01-09 07:20:10 -05:00
|
|
|
|
2019-01-04 09:14:16 -05:00
|
|
|
db["emojis"].each do |e|
|
|
|
|
name = e["name"]
|
2023-01-09 07:20:10 -05:00
|
|
|
|
2019-08-07 05:38:58 -04:00
|
|
|
# special cased as we prefer to keep these as symbols
|
2019-08-30 01:06:23 -04:00
|
|
|
next if name == "registered"
|
|
|
|
next if name == "copyright"
|
|
|
|
next if name == "tm"
|
|
|
|
next if name == "left_right_arrow"
|
2023-01-09 07:20:10 -05:00
|
|
|
|
2019-01-04 09:14:16 -05:00
|
|
|
code = replacement_code(e["code"])
|
|
|
|
next unless code
|
2023-01-09 07:20:10 -05:00
|
|
|
|
2019-01-04 09:14:16 -05:00
|
|
|
replacements[code] = name
|
|
|
|
if is_tonable_emojis.include?(name)
|
|
|
|
fitzpatrick_scales.each_with_index do |scale, index|
|
2020-04-30 02:48:34 -04:00
|
|
|
toned_code = code.codepoints.insert(1, scale).pack("U*")
|
2019-01-04 09:14:16 -05:00
|
|
|
replacements[toned_code] = "#{name}:t#{index + 2}"
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2019-01-04 09:14:16 -05:00
|
|
|
end
|
2017-06-14 09:35:37 -04:00
|
|
|
end
|
2016-03-04 14:20:44 -05:00
|
|
|
|
2019-01-04 09:14:16 -05:00
|
|
|
replacements["\u{2639}"] = "frowning"
|
|
|
|
replacements["\u{263B}"] = "slight_smile"
|
|
|
|
replacements["\u{2661}"] = "heart"
|
|
|
|
replacements["\u{2665}"] = "heart"
|
2015-12-30 14:46:52 -05:00
|
|
|
|
2019-01-04 09:14:16 -05:00
|
|
|
replacements
|
|
|
|
end
|
2015-12-30 14:35:25 -05:00
|
|
|
end
|
|
|
|
|
2017-05-15 10:27:54 -04:00
|
|
|
def self.unicode_unescape(string)
|
2019-03-21 04:11:33 -04:00
|
|
|
PrettyText.escape_emoji(string)
|
2017-05-15 10:27:54 -04:00
|
|
|
end
|
|
|
|
|
2017-07-21 14:24:28 -04:00
|
|
|
def self.gsub_emoji_to_unicode(str)
|
|
|
|
str.gsub(/:([\w\-+]*(?::t\d)?):/) { |name| Emoji.lookup_unicode($1) || name } if str
|
|
|
|
end
|
|
|
|
|
2016-10-10 22:03:21 -04:00
|
|
|
def self.lookup_unicode(name)
|
2023-04-13 03:38:54 -04:00
|
|
|
return "" if denied&.include?(name)
|
|
|
|
|
2016-10-10 22:03:21 -04:00
|
|
|
@reverse_map ||=
|
|
|
|
begin
|
|
|
|
map = {}
|
2017-06-20 00:02:39 -04:00
|
|
|
is_tonable_emojis = Emoji.tonable_emojis
|
2023-01-09 07:20:10 -05:00
|
|
|
|
2016-10-10 22:03:21 -04:00
|
|
|
db["emojis"].each do |e|
|
|
|
|
next if e["name"] == "tm"
|
2023-01-09 07:20:10 -05:00
|
|
|
|
2016-10-10 22:03:21 -04:00
|
|
|
code = replacement_code(e["code"])
|
2017-06-13 14:03:59 -04:00
|
|
|
next unless code
|
2023-01-09 07:20:10 -05:00
|
|
|
|
2017-06-13 14:03:59 -04:00
|
|
|
map[e["name"]] = code
|
2017-06-20 00:02:39 -04:00
|
|
|
if is_tonable_emojis.include?(e["name"])
|
2017-06-13 14:03:59 -04: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
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2017-06-13 14:03:59 -04:00
|
|
|
end
|
|
|
|
end
|
2017-06-05 12:53:11 -04:00
|
|
|
|
|
|
|
Emoji.aliases.each do |key, alias_names|
|
|
|
|
next unless alias_code = map[key]
|
|
|
|
alias_names.each { |alias_name| map[alias_name] = alias_code }
|
|
|
|
end
|
|
|
|
|
2016-10-10 22:03:21 -04:00
|
|
|
map
|
|
|
|
end
|
|
|
|
@reverse_map[name]
|
|
|
|
end
|
|
|
|
|
2016-03-02 14:31:32 -05:00
|
|
|
def self.unicode_replacements_json
|
|
|
|
@unicode_replacements_json ||= unicode_replacements.to_json
|
2015-12-30 14:35:25 -05:00
|
|
|
end
|
|
|
|
|
2021-06-25 13:48:36 -04:00
|
|
|
def self.codes_to_img(str)
|
|
|
|
return if str.blank?
|
|
|
|
|
|
|
|
str =
|
|
|
|
str.gsub(/:([\w\-+]*(?::t\d)?):/) do |name|
|
|
|
|
code = $1
|
2023-01-09 07:20:10 -05:00
|
|
|
|
2021-06-25 13:48:36 -04:00
|
|
|
if code && Emoji.custom?(code)
|
|
|
|
emoji = Emoji[code]
|
2022-02-09 06:18:59 -05:00
|
|
|
"<img src=\"#{emoji.url}\" title=\"#{code}\" class=\"emoji\" alt=\"#{code}\" loading=\"lazy\" width=\"20\" height=\"20\">"
|
2021-06-25 13:48:36 -04:00
|
|
|
elsif code && Emoji.exists?(code)
|
2022-02-09 06:18:59 -05:00
|
|
|
"<img src=\"#{Emoji.url_for(code)}\" title=\"#{code}\" class=\"emoji\" alt=\"#{code}\" loading=\"lazy\" width=\"20\" height=\"20\">"
|
2021-06-25 13:48:36 -04:00
|
|
|
else
|
|
|
|
name
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2021-06-25 13:48:36 -04:00
|
|
|
end
|
|
|
|
end
|
2024-05-27 09:24:55 -04:00
|
|
|
|
|
|
|
def self.sanitize_emoji_name(name)
|
2024-12-03 19:27:12 -05:00
|
|
|
name.gsub(/[^a-z0-9\+\-]+/i, "_").gsub(/_{2,}/, "_").downcase
|
2024-05-27 09:24:55 -04:00
|
|
|
end
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|