From af266acac150b16f23a9e9886311d6c9150bc936 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 22 Jul 2016 12:59:43 -0400 Subject: [PATCH] FIX: Plugin Custom emoji weren't working correctly on the server side --- app/models/emoji.rb | 33 ++++++++++++++++++++-------- lib/plugin/instance.rb | 49 ++++++++++++++++-------------------------- 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/app/models/emoji.rb b/app/models/emoji.rb index e7bab298546..51eae0c706a 100644 --- a/app/models/emoji.rb +++ b/app/models/emoji.rb @@ -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 diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb index 54a1405cd5f..a9de8e4698b 100644 --- a/lib/plugin/instance.rb +++ b/lib/plugin/instance.rb @@ -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?