2014-12-22 19:12:26 -05:00
|
|
|
class Emoji
|
|
|
|
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 remove
|
|
|
|
return if path.blank?
|
2015-02-13 13:26:26 -05:00
|
|
|
if File.exists?(path)
|
|
|
|
File.delete(path) rescue nil
|
|
|
|
Emoji.clear_cache
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.all
|
2016-03-04 14:20:44 -05:00
|
|
|
Discourse.cache.fetch("all_emojis:v2") { standard | custom }
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.standard
|
2016-03-04 14:20:44 -05:00
|
|
|
Discourse.cache.fetch("standard_emojis:v2") { load_standard }
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|
|
|
|
|
2015-03-16 13:28:11 -04:00
|
|
|
def self.aliases
|
2016-03-04 14:20:44 -05:00
|
|
|
Discourse.cache.fetch("aliases_emojis:v2") { load_aliases }
|
2015-03-16 13:28:11 -04:00
|
|
|
end
|
|
|
|
|
2014-12-22 19:12:26 -05:00
|
|
|
def self.custom
|
2016-03-04 14:20:44 -05:00
|
|
|
Discourse.cache.fetch("custom_emojis:v2") { load_custom }
|
2014-12-22 19:12:26 -05:00
|
|
|
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_path(path)
|
|
|
|
extension = File.extname(path)
|
|
|
|
Emoji.new(path).tap do |e|
|
|
|
|
e.name = File.basename(path, ".*")
|
2015-08-21 16:42:37 -04:00
|
|
|
e.url = "#{base_url}/#{e.name}#{extension}"
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_from_db_item(emoji)
|
2016-03-04 14:20:44 -05:00
|
|
|
name = emoji["name"]
|
2014-12-22 19:12:26 -05:00
|
|
|
filename = "#{name}.png"
|
|
|
|
Emoji.new.tap do |e|
|
|
|
|
e.name = name
|
|
|
|
e.url = "/images/emoji/#{SiteSetting.emoji_set}/#{filename}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_for(file, name)
|
|
|
|
extension = File.extname(file.original_filename)
|
|
|
|
path = "#{Emoji.base_directory}/#{name}#{extension}"
|
2015-02-09 12:54:57 -05:00
|
|
|
|
2015-02-13 13:26:26 -05:00
|
|
|
# store the emoji
|
|
|
|
FileUtils.mkdir_p(Pathname.new(path).dirname)
|
|
|
|
File.open(path, "wb") { |f| f << file.tempfile.read }
|
|
|
|
# clear the cache
|
|
|
|
Emoji.clear_cache
|
2015-01-29 12:36:35 -05:00
|
|
|
# launch resize job
|
|
|
|
Jobs.enqueue(:resize_emoji, path: path)
|
2014-12-22 19:12:26 -05:00
|
|
|
# return created emoji
|
2015-02-09 12:54:57 -05:00
|
|
|
Emoji[name]
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_cache
|
2015-02-19 00:58:05 -05:00
|
|
|
Discourse.cache.delete("custom_emojis")
|
|
|
|
Discourse.cache.delete("standard_emojis")
|
2015-03-16 13:28:11 -04:00
|
|
|
Discourse.cache.delete("aliases_emojis")
|
2015-02-19 00:58:05 -05:00
|
|
|
Discourse.cache.delete("all_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
|
|
|
|
@db ||= File.open(db_file, "r:UTF-8") { |f| JSON.parse(f.read) }
|
|
|
|
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
|
|
|
|
|
|
|
|
def self.load_aliases
|
2016-03-04 14:20:44 -05:00
|
|
|
return @aliases if @aliases
|
2015-03-16 13:28:11 -04:00
|
|
|
|
2016-03-04 14:20:44 -05:00
|
|
|
@aliases ||= db['aliases']
|
2015-03-16 13:28:11 -04:00
|
|
|
|
2016-03-04 14:20:44 -05:00
|
|
|
# Fix how `slightly_smiling` was mislabeled
|
|
|
|
@aliases['slight_smile'] ||= []
|
|
|
|
@aliases['slight_smile'] << 'slightly_smiling'
|
|
|
|
|
|
|
|
@aliases
|
2014-12-22 19:12:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.load_custom
|
2015-11-06 09:02:40 -05:00
|
|
|
Dir.glob(File.join(Emoji.base_directory, "*.{png,gif}"))
|
|
|
|
.sort
|
|
|
|
.map { |emoji| Emoji.create_from_path(emoji) }
|
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
|
|
|
|
|
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 = {}
|
|
|
|
db['emojis'].each do |e|
|
|
|
|
hex = e['code'].hex
|
|
|
|
# Don't replace digits or letters
|
|
|
|
if hex > 128
|
|
|
|
@unicode_replacements[[hex].pack('U')] = e['name']
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
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
|