discourse/app/models/emoji.rb

143 lines
3.3 KiB
Ruby
Raw Normal View History

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?
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
Discourse.cache.fetch("all_emojis") { standard | custom }
2014-12-22 19:12:26 -05:00
end
def self.standard
Discourse.cache.fetch("standard_emojis") { load_standard }
2014-12-22 19:12:26 -05:00
end
2015-03-16 13:28:11 -04:00
def self.aliases
Discourse.cache.fetch("aliases_emojis") { load_aliases }
end
2014-12-22 19:12:26 -05:00
def self.custom
Discourse.cache.fetch("custom_emojis") { load_custom }
2014-12-22 19:12:26 -05:00
end
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)
name = emoji["aliases"].first
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}"
# 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
# launch resize job
Jobs.enqueue(:resize_emoji, path: path)
2014-12-22 19:12:26 -05:00
# return created emoji
Emoji[name]
2014-12-22 19:12:26 -05:00
end
def self.clear_cache
Discourse.cache.delete("custom_emojis")
Discourse.cache.delete("standard_emojis")
2015-03-16 13:28:11 -04:00
Discourse.cache.delete("aliases_emojis")
Discourse.cache.delete("all_emojis")
2014-12-22 19:12:26 -05:00
end
def self.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
@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
2015-03-16 13:28:11 -04:00
db.map { |emoji| Emoji.create_from_db_item(emoji) }
end
def self.load_aliases
aliases = {}
db.select { |emoji| emoji["aliases"].count > 1 }
.each { |emoji| aliases[emoji["aliases"][0]] = emoji["aliases"][1..-1] }
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
def self.unicode_replacements
return @unicode_replacements if @unicode_replacements
@unicode_replacements = Hash[db.map {|e| [e['emoji'], e['aliases'][0]] }]
@unicode_replacements["\u{2639}"] = 'frowning'
@unicode_replacements["\u{263A}"] = 'slightly_smiling'
@unicode_replacements["\u{263B}"] = 'slightly_smiling'
@unicode_replacements["\u{2661}"] = 'heart'
@unicode_replacements["\u{2665}"] = 'heart'
@unicode_replacements
end
def self.unicode_regexp
@unicode_regexp ||= Regexp.union(unicode_replacements.keys)
end
def self.sub_unicode!(text)
text.gsub!(unicode_regexp) {|m| ":#{unicode_replacements[m]}:"}
end
2014-12-22 19:12:26 -05:00
end