FIX: Plugin Custom emoji weren't working correctly on the server side

This commit is contained in:
Robin Ward 2016-07-22 12:59:43 -04:00
parent 2a257190e7
commit af266acac1
2 changed files with 42 additions and 40 deletions

View File

@ -23,19 +23,19 @@ class Emoji
end
def self.all
Discourse.cache.fetch("all_emojis:#{EMOJI_VERSION}") { standard | custom }
Discourse.cache.fetch(cache_key("all_emojis")) { standard | custom }
end
def self.standard
Discourse.cache.fetch("standard_emojis:#{EMOJI_VERSION}") { load_standard }
Discourse.cache.fetch(cache_key("standard_emojis")) { load_standard }
end
def self.aliases
Discourse.cache.fetch("aliases_emojis:#{EMOJI_VERSION}") { load_aliases }
Discourse.cache.fetch(cache_key("aliases_emojis")) { load_aliases }
end
def self.custom
Discourse.cache.fetch("custom_emojis:#{EMOJI_VERSION}") { load_custom }
Discourse.cache.fetch(cache_key("custom_emojis")) { load_custom }
end
def self.exists?(name)
@ -78,11 +78,15 @@ class Emoji
Emoji[name]
end
def self.cache_key(name)
"#{name}:#{EMOJI_VERSION}:#{Plugin::CustomEmoji.cache_key}"
end
def self.clear_cache
Discourse.cache.delete("custom_emojis:#{EMOJI_VERSION}")
Discourse.cache.delete("standard_emojis:#{EMOJI_VERSION}")
Discourse.cache.delete("aliases_emojis:#{EMOJI_VERSION}")
Discourse.cache.delete("all_emojis:#{EMOJI_VERSION}")
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"))
end
def self.db_file
@ -117,9 +121,20 @@ class Emoji
end
def self.load_custom
result = []
Dir.glob(File.join(Emoji.base_directory, "*.{png,gif}"))
.sort
.map { |emoji| Emoji.create_from_path(emoji) }
.each { |emoji| result << Emoji.create_from_path(emoji) }
Plugin::CustomEmoji.emojis.each do |name, url|
result << Emoji.new.tap do |e|
e.name = name
e.url = url
end
end
result
end
def self.base_directory

View File

@ -3,6 +3,21 @@ require 'fileutils'
require_dependency 'plugin/metadata'
require_dependency 'plugin/auth_provider'
class Plugin::CustomEmoji
def self.cache_key
@@cache_key ||= "plugin-emoji"
end
def self.emojis
@@emojis ||= {}
end
def self.register(name, url)
@@cache_key = Digest::SHA1.hexdigest(cache_key + name)[0..10]
emojis[name] = url
end
end
class Plugin::Instance
attr_accessor :path, :metadata
@ -17,13 +32,8 @@ class Plugin::Instance
}
end
# Memoized hash readers
[:seed_data, :emojis].each do |att|
class_eval %Q{
def #{att}
@#{att} ||= HashWithIndifferentAccess.new({})
end
}
def seed_data
@seed_data ||= HashWithIndifferentAccess.new({})
end
def self.find_all(parent_path)
@ -225,7 +235,7 @@ class Plugin::Instance
end
def register_emoji(name, url)
emojis[name] = url
Plugin::CustomEmoji.register(name, url)
end
def automatic_assets
@ -264,29 +274,6 @@ JS
end
end
if emojis.present?
emoji_registrations = ""
emojis.each do |name, url|
emoji_registrations << "emoji.registerEmoji(#{name.inspect}, #{url.inspect});\n"
end
js << <<~JS
define("discourse/initializers/custom-emoji",
["pretty-text/emoji", "exports"],
function(emoji, __exports__) {
"use strict";
__exports__["default"] = {
name: "custom-emoji",
after: "inject-objects",
initialize: function(container) {
#{emoji_registrations}
}
};
});
JS
end
# Generate an IIFE for the JS
js = "(function(){#{js}})();" if js.present?