discourse/app/models/emoji.rb

204 lines
4.8 KiB
Ruby
Raw Normal View History

2014-12-23 08:12:26 +08:00
class Emoji
# update this to clear the cache
EMOJI_VERSION = "6"
2017-06-14 02:03:59 +08:00
FITZPATRICK_SCALE ||= [ "1f3fb", "1f3fc", "1f3fd", "1f3fe", "1f3ff" ]
2014-12-23 08:12:26 +08:00
include ActiveModel::SerializerSupport
attr_reader :path
attr_accessor :name, :url
def initialize(path = nil)
@path = path
end
def self.all
Discourse.cache.fetch(cache_key("all_emojis")) { standard | custom }
2014-12-23 08:12:26 +08:00
end
def self.standard
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
2018-05-01 21:43:49 +08:00
def self.searchAliases
Discourse.cache.fetch(cache_key("search_aliases_emojis")) { db['searchAliases'] }
end
2014-12-23 08:12:26 +08:00
def self.custom
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
def self.exists?(name)
Emoji[name].present?
end
def self.[](name)
Emoji.custom.detect { |e| e.name == name }
end
2014-12-23 08:12:26 +08:00
def self.create_from_db_item(emoji)
name = emoji["name"]
2017-07-11 23:51:53 +08:00
filename = emoji['filename'] || name
2014-12-23 08:12:26 +08:00
Emoji.new.tap do |e|
e.name = 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)
2017-07-22 01:04:43 +08:00
"#{Discourse.base_uri}/images/emoji/#{SiteSetting.emoji_set}/#{name}.png?v=#{EMOJI_VERSION}"
2017-07-11 23:51:53 +08:00
end
def self.cache_key(name)
2017-07-11 23:51:53 +08:00
"#{name}:v#{EMOJI_VERSION}:#{Plugin::CustomEmoji.cache_key}"
end
2014-12-23 08:12:26 +08:00
def self.clear_cache
2018-05-01 21:43:49 +08:00
%w{custom standard aliases search_aliases all tonable}.each do |key|
Discourse.cache.delete(cache_key("#{key}_emojis"))
end
2014-12-23 08:12:26 +08:00
end
def self.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
2017-07-28 09:20:09 +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
result = []
CustomEmoji.includes(:upload).order(:name).each do |emoji|
result << Emoji.new.tap do |e|
e.name = emoji.name
e.url = emoji.upload&.url
end
end
Plugin::CustomEmoji.emojis.each do |name, url|
result << Emoji.new.tap do |e|
e.name = name
url = (Discourse.base_uri + url) if url[/^\/[^\/]/]
e.url = url
end
end
result
2014-12-23 08:12:26 +08:00
end
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
2015-08-22 04:42:37 +08:00
"#{Discourse.base_uri}/uploads/#{db}/_emoji"
2014-12-23 08:12:26 +08:00
end
def self.replacement_code(code)
hexes = code.split('-'.freeze).map!(&:hex)
# Don't replace digits, letters and some symbols
hexes.pack("U*".freeze) if hexes[0] > 255
end
def self.unicode_replacements
return @unicode_replacements if @unicode_replacements
@unicode_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']
next if name == 'tm'.freeze
code = replacement_code(e['code'])
next unless code
@unicode_replacements[code] = name
if is_tonable_emojis.include?(name)
fitzpatrick_scales.each_with_index do |scale, index|
toned_code = code.codepoints.insert(1, scale).pack("U*".freeze)
2017-07-28 09:20:09 +08:00
@unicode_replacements[toned_code] = "#{name}:t#{index + 2}"
end
end
end
@unicode_replacements["\u{2639}"] = 'frowning'
@unicode_replacements["\u{263A}"] = 'slight_smile'
@unicode_replacements["\u{263B}"] = 'slight_smile'
@unicode_replacements["\u{2661}"] = 'heart'
@unicode_replacements["\u{2665}"] = 'heart'
@unicode_replacements
end
def self.unicode_unescape(string)
string.each_char.map do |c|
if str = unicode_replacements[c]
":#{str}:"
else
c
end
end.join
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
def self.lookup_unicode(name)
@reverse_map ||= begin
map = {}
is_tonable_emojis = Emoji.tonable_emojis
db['emojis'].each do |e|
next if e['name'] == 'tm'
2017-06-14 02:03:59 +08:00
code = replacement_code(e['code'])
2017-06-14 02:03:59 +08:00
next unless code
map[e['name']] = code
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*")
2017-07-28 09:20:09 +08:00
map["#{e['name']}:t#{index + 2}"] = toned_code
2017-06-14 02:03:59 +08:00
end
end
end
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
map
end
@reverse_map[name]
end
def self.unicode_replacements_json
@unicode_replacements_json ||= unicode_replacements.to_json
end
2014-12-23 08:12:26 +08:00
end