2014-12-22 19:12:26 -05:00
|
|
|
class Emoji
|
2016-03-07 13:47:40 -05:00
|
|
|
# update this to clear the cache
|
2017-07-11 11:51:53 -04:00
|
|
|
EMOJI_VERSION = "5"
|
2016-03-07 13:47:40 -05:00
|
|
|
|
2017-06-13 14:03:59 -04:00
|
|
|
FITZPATRICK_SCALE ||= [ "1f3fb", "1f3fc", "1f3fd", "1f3fe", "1f3ff" ]
|
|
|
|
|
2014-12-22 19:12:26 -05:00
|
|
|
include ActiveModel::SerializerSupport
|
|
|
|
|
|
|
|
attr_reader :path
|
|
|
|
attr_accessor :name, :url
|
|
|
|
|
|
|
|
# whitelist emojis so that new user can post emojis
|
|
|
|
Post::white_listed_image_classes << "emoji"
|
|
|
|
|
|
|
|
def initialize(path = nil)
|
|
|
|
@path = path
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-03-16 13:28:11 -04:00
|
|
|
def self.aliases
|
2017-06-05 08:06:23 -04:00
|
|
|
Discourse.cache.fetch(cache_key("aliases_emojis")) { db['aliases'] }
|
2015-03-16 13:28:11 -04:00
|
|
|
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
|
|
|
|
Discourse.cache.fetch(cache_key("tonable_emojis")) { db['tonableEmojis'] }
|
|
|
|
end
|
|
|
|
|
2015-02-09 12:54:57 -05:00
|
|
|
def self.exists?(name)
|
|
|
|
Emoji[name].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.[](name)
|
|
|
|
Emoji.custom.detect { |e| e.name == name }
|
|
|
|
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"]
|
2017-07-11 11:51:53 -04:00
|
|
|
filename = emoji['filename'] || name
|
2014-12-22 19:12:26 -05:00
|
|
|
Emoji.new.tap do |e|
|
|
|
|
e.name = name
|
2017-07-11 11:51:53 -04:00
|
|
|
e.url = Emoji.url_for(filename)
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-11 11:51:53 -04:00
|
|
|
def self.url_for(name)
|
2017-07-21 13:04:43 -04:00
|
|
|
"#{Discourse.base_uri}/images/emoji/#{SiteSetting.emoji_set}/#{name}.png?v=#{EMOJI_VERSION}"
|
2017-07-11 11:51:53 -04:00
|
|
|
end
|
|
|
|
|
2016-07-22 12:59:43 -04:00
|
|
|
def self.cache_key(name)
|
2017-07-11 11:51:53 -04:00
|
|
|
"#{name}: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
|
2016-07-22 12:59:43 -04:00
|
|
|
Discourse.cache.delete(cache_key("custom_emojis"))
|
|
|
|
Discourse.cache.delete(cache_key("standard_emojis"))
|
|
|
|
Discourse.cache.delete(cache_key("aliases_emojis"))
|
|
|
|
Discourse.cache.delete(cache_key("all_emojis"))
|
2017-06-14 05:11:02 -04:00
|
|
|
Discourse.cache.delete(cache_key("tonable_emojis"))
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.db_file
|
2015-01-23 18:07:39 -05:00
|
|
|
"#{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
|
2016-03-04 14:20:44 -05:00
|
|
|
db['emojis'].map {|e| Emoji.create_from_db_item(e) }
|
2015-03-16 13:28:11 -04:00
|
|
|
end
|
|
|
|
|
2014-12-22 19:12:26 -05:00
|
|
|
def self.load_custom
|
2016-07-22 12:59:43 -04:00
|
|
|
result = []
|
|
|
|
|
2017-03-30 01:24:18 -04:00
|
|
|
CustomEmoji.order(:name).all.each do |emoji|
|
2017-02-02 04:41:57 -05:00
|
|
|
result << Emoji.new.tap do |e|
|
|
|
|
e.name = emoji.name
|
2017-05-05 02:53:38 -04:00
|
|
|
e.url = emoji.upload&.url
|
2017-02-02 04:41:57 -05:00
|
|
|
end
|
|
|
|
end
|
2016-07-22 12:59:43 -04:00
|
|
|
|
|
|
|
Plugin::CustomEmoji.emojis.each do |name, url|
|
|
|
|
result << Emoji.new.tap do |e|
|
|
|
|
e.name = name
|
2017-03-23 21:09:39 -04:00
|
|
|
url = (Discourse.base_uri + url) if url[/^\/[^\/]/]
|
2016-07-22 12:59:43 -04:00
|
|
|
e.url = url
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2015-08-21 16:42:37 -04:00
|
|
|
"#{Discourse.base_uri}/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)
|
2017-06-20 00:18:24 -04:00
|
|
|
hexes = code.split('-'.freeze).map!(&:hex)
|
2016-07-05 13:55:21 -04:00
|
|
|
# Don't replace digits, letters and some symbols
|
2017-06-20 00:18:24 -04:00
|
|
|
hexes.pack("U*".freeze) if hexes[0] > 255
|
2016-07-05 13:55:21 -04:00
|
|
|
end
|
|
|
|
|
2015-12-30 14:35:25 -05:00
|
|
|
def self.unicode_replacements
|
2015-12-30 14:46:52 -05:00
|
|
|
return @unicode_replacements if @unicode_replacements
|
|
|
|
|
2016-03-04 14:20:44 -05:00
|
|
|
@unicode_replacements = {}
|
2017-06-20 00:02:39 -04:00
|
|
|
is_tonable_emojis = Emoji.tonable_emojis
|
2017-06-20 00:18:24 -04:00
|
|
|
fitzpatrick_scales = FITZPATRICK_SCALE.map { |scale| scale.to_i(16) }
|
2017-06-14 09:35:37 -04:00
|
|
|
|
2016-03-04 14:20:44 -05:00
|
|
|
db['emojis'].each do |e|
|
2017-06-20 00:18:24 -04:00
|
|
|
name = e['name']
|
|
|
|
next if name == 'tm'.freeze
|
2017-06-14 09:35:37 -04:00
|
|
|
|
2016-07-05 13:55:21 -04:00
|
|
|
code = replacement_code(e['code'])
|
2017-06-14 09:35:37 -04:00
|
|
|
next unless code
|
|
|
|
|
2017-06-20 00:18:24 -04:00
|
|
|
@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)
|
|
|
|
@unicode_replacements[toned_code] = "#{name}:t#{index+2}"
|
2017-06-14 09:35:37 -04:00
|
|
|
end
|
|
|
|
end
|
2016-03-04 14:20:44 -05:00
|
|
|
end
|
|
|
|
|
2015-12-30 14:46:52 -05:00
|
|
|
@unicode_replacements["\u{2639}"] = 'frowning'
|
2016-03-04 14:20:44 -05:00
|
|
|
@unicode_replacements["\u{263A}"] = 'slight_smile'
|
|
|
|
@unicode_replacements["\u{263B}"] = 'slight_smile'
|
2015-12-30 14:46:52 -05:00
|
|
|
@unicode_replacements["\u{2661}"] = 'heart'
|
|
|
|
@unicode_replacements["\u{2665}"] = 'heart'
|
|
|
|
|
|
|
|
@unicode_replacements
|
2015-12-30 14:35:25 -05:00
|
|
|
end
|
|
|
|
|
2017-05-15 10:27:54 -04:00
|
|
|
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-21 14:24:28 -04: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-10 22:03:21 -04:00
|
|
|
def self.lookup_unicode(name)
|
|
|
|
@reverse_map ||= begin
|
|
|
|
map = {}
|
2017-06-20 00:02:39 -04:00
|
|
|
is_tonable_emojis = Emoji.tonable_emojis
|
2017-06-05 12:53:11 -04:00
|
|
|
|
2016-10-10 22:03:21 -04:00
|
|
|
db['emojis'].each do |e|
|
|
|
|
next if e['name'] == 'tm'
|
2017-06-13 14:03:59 -04: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
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
2016-10-10 22:03:21 -04:00
|
|
|
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 do |alias_name|
|
|
|
|
map[alias_name] = alias_code
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|