2014-12-22 18:12:26 -06:00
|
|
|
class Emoji
|
2016-03-07 12:47:40 -06:00
|
|
|
# update this to clear the cache
|
2017-03-14 02:31:01 -05:00
|
|
|
EMOJI_VERSION = "v3"
|
2016-03-07 12:47:40 -06:00
|
|
|
|
2014-12-22 18:12:26 -06: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 11:59:43 -05:00
|
|
|
Discourse.cache.fetch(cache_key("all_emojis")) { standard | custom }
|
2014-12-22 18:12:26 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.standard
|
2016-07-22 11:59:43 -05:00
|
|
|
Discourse.cache.fetch(cache_key("standard_emojis")) { load_standard }
|
2014-12-22 18:12:26 -06:00
|
|
|
end
|
|
|
|
|
2015-03-16 12:28:11 -05:00
|
|
|
def self.aliases
|
2016-07-22 11:59:43 -05:00
|
|
|
Discourse.cache.fetch(cache_key("aliases_emojis")) { load_aliases }
|
2015-03-16 12:28:11 -05:00
|
|
|
end
|
|
|
|
|
2014-12-22 18:12:26 -06:00
|
|
|
def self.custom
|
2016-07-22 11:59:43 -05:00
|
|
|
Discourse.cache.fetch(cache_key("custom_emojis")) { load_custom }
|
2014-12-22 18:12:26 -06:00
|
|
|
end
|
|
|
|
|
2015-02-09 11:54:57 -06:00
|
|
|
def self.exists?(name)
|
|
|
|
Emoji[name].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.[](name)
|
|
|
|
Emoji.custom.detect { |e| e.name == name }
|
|
|
|
end
|
|
|
|
|
2014-12-22 18:12:26 -06:00
|
|
|
def self.create_from_db_item(emoji)
|
2016-03-04 13:20:44 -06:00
|
|
|
name = emoji["name"]
|
2016-04-27 13:56:04 -05:00
|
|
|
filename = "#{emoji['filename'] || name}.png"
|
2014-12-22 18:12:26 -06:00
|
|
|
Emoji.new.tap do |e|
|
|
|
|
e.name = name
|
2017-03-23 20:09:39 -05:00
|
|
|
e.url = "#{Discourse.base_uri}/images/emoji/#{SiteSetting.emoji_set}/#{filename}"
|
2014-12-22 18:12:26 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-22 11:59:43 -05:00
|
|
|
def self.cache_key(name)
|
|
|
|
"#{name}:#{EMOJI_VERSION}:#{Plugin::CustomEmoji.cache_key}"
|
|
|
|
end
|
|
|
|
|
2014-12-22 18:12:26 -06:00
|
|
|
def self.clear_cache
|
2016-07-22 11:59:43 -05: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"))
|
2014-12-22 18:12:26 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.db_file
|
2015-01-23 17:07:39 -06:00
|
|
|
"#{Rails.root}/lib/emoji/db.json"
|
2014-12-22 18:12:26 -06:00
|
|
|
end
|
|
|
|
|
2015-03-16 12:28:11 -05:00
|
|
|
def self.db
|
2016-04-27 13:56:04 -05:00
|
|
|
return @db if @db
|
|
|
|
@db = File.open(db_file, "r:UTF-8") { |f| JSON.parse(f.read) }
|
|
|
|
|
|
|
|
# Small tweak to `emoji.json` from Emoji one
|
|
|
|
@db['emojis'] << {"code" => "1f44d", "name" => "+1", "filename" => "thumbsup"}
|
|
|
|
@db['emojis'] << {"code" => "1f44e", "name" => "-1", "filename" => "thumbsdown"}
|
|
|
|
|
|
|
|
@db
|
2015-03-16 12:28:11 -05:00
|
|
|
end
|
|
|
|
|
2014-12-22 18:12:26 -06:00
|
|
|
def self.load_standard
|
2016-03-04 13:20:44 -06:00
|
|
|
db['emojis'].map {|e| Emoji.create_from_db_item(e) }
|
2015-03-16 12:28:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.load_aliases
|
2016-03-04 13:20:44 -06:00
|
|
|
return @aliases if @aliases
|
2015-03-16 12:28:11 -05:00
|
|
|
|
2016-03-04 13:20:44 -06:00
|
|
|
@aliases ||= db['aliases']
|
2015-03-16 12:28:11 -05:00
|
|
|
|
2016-03-04 13:20:44 -06:00
|
|
|
# Fix how `slightly_smiling` was mislabeled
|
|
|
|
@aliases['slight_smile'] ||= []
|
|
|
|
@aliases['slight_smile'] << 'slightly_smiling'
|
|
|
|
|
|
|
|
@aliases
|
2014-12-22 18:12:26 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.load_custom
|
2016-07-22 11:59:43 -05:00
|
|
|
result = []
|
|
|
|
|
2017-03-30 00:24:18 -05:00
|
|
|
CustomEmoji.order(:name).all.each do |emoji|
|
2017-02-02 03:41:57 -06:00
|
|
|
result << Emoji.new.tap do |e|
|
|
|
|
e.name = emoji.name
|
|
|
|
e.url = emoji.upload.url
|
|
|
|
end
|
|
|
|
end
|
2016-07-22 11:59:43 -05:00
|
|
|
|
|
|
|
Plugin::CustomEmoji.emojis.each do |name, url|
|
|
|
|
result << Emoji.new.tap do |e|
|
|
|
|
e.name = name
|
2017-03-23 20:09:39 -05:00
|
|
|
url = (Discourse.base_uri + url) if url[/^\/[^\/]/]
|
2016-07-22 11:59:43 -05:00
|
|
|
e.url = url
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
2014-12-22 18:12:26 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.base_directory
|
2015-08-21 15:42:37 -05:00
|
|
|
"public#{base_url}"
|
2014-12-22 18:12:26 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.base_url
|
|
|
|
db = RailsMultisite::ConnectionManagement.current_db
|
2015-08-21 15:42:37 -05:00
|
|
|
"#{Discourse.base_uri}/uploads/#{db}/_emoji"
|
2014-12-22 18:12:26 -06:00
|
|
|
end
|
|
|
|
|
2016-07-05 12:55:21 -05:00
|
|
|
def self.replacement_code(code)
|
|
|
|
hexes = code.split('-').map(&:hex)
|
|
|
|
|
|
|
|
# Don't replace digits, letters and some symbols
|
|
|
|
return hexes.pack("U" * hexes.size) if hexes[0] > 255
|
|
|
|
end
|
|
|
|
|
2015-12-30 13:35:25 -06:00
|
|
|
def self.unicode_replacements
|
2015-12-30 13:46:52 -06:00
|
|
|
return @unicode_replacements if @unicode_replacements
|
|
|
|
|
2016-03-04 13:20:44 -06:00
|
|
|
|
|
|
|
@unicode_replacements = {}
|
|
|
|
db['emojis'].each do |e|
|
2016-07-05 12:55:21 -05:00
|
|
|
next if e['name'] == 'tm'
|
|
|
|
code = replacement_code(e['code'])
|
|
|
|
@unicode_replacements[code] = e['name'] if code
|
2016-03-04 13:20:44 -06:00
|
|
|
end
|
|
|
|
|
2015-12-30 13:46:52 -06:00
|
|
|
@unicode_replacements["\u{2639}"] = 'frowning'
|
2016-03-04 13:20:44 -06:00
|
|
|
@unicode_replacements["\u{263A}"] = 'slight_smile'
|
|
|
|
@unicode_replacements["\u{263B}"] = 'slight_smile'
|
2015-12-30 13:46:52 -06:00
|
|
|
@unicode_replacements["\u{2661}"] = 'heart'
|
|
|
|
@unicode_replacements["\u{2665}"] = 'heart'
|
|
|
|
|
|
|
|
@unicode_replacements
|
2015-12-30 13:35:25 -06:00
|
|
|
end
|
|
|
|
|
2016-10-10 21:03:21 -05:00
|
|
|
def self.lookup_unicode(name)
|
|
|
|
@reverse_map ||= begin
|
|
|
|
map = {}
|
|
|
|
db['emojis'].each do |e|
|
|
|
|
next if e['name'] == 'tm'
|
|
|
|
code = replacement_code(e['code'])
|
|
|
|
map[e['name']] = code if code
|
|
|
|
end
|
|
|
|
map
|
|
|
|
end
|
|
|
|
@reverse_map[name]
|
|
|
|
end
|
|
|
|
|
2016-03-02 13:31:32 -06:00
|
|
|
def self.unicode_replacements_json
|
|
|
|
@unicode_replacements_json ||= unicode_replacements.to_json
|
2015-12-30 13:35:25 -06:00
|
|
|
end
|
|
|
|
|
2014-12-22 18:12:26 -06:00
|
|
|
end
|