FIX: ensures emoji helper is working with custom emojis (#7843)

FIX: ensures emoji helper is working with custom emojis (#7843)

diff --git a/app/helpers/emoji_helper.rb b/app/helpers/emoji_helper.rb
index f1f13b1..fcf0462 100644
--- a/app/helpers/emoji_helper.rb
+++ b/app/helpers/emoji_helper.rb
@@ -7,7 +7,10 @@ module EmojiHelper
     str = str.gsub(/:([\w\-+]*(?::t\d)?):/) do |name|
       code = $1
 
-      if Emoji.exists?(code)
+      if code && Emoji.custom?(code)
+        emoji = Emoji[code]
+        "<img src=\"#{emoji.url}\" title=\"#{code}\" class=\"emoji\" alt=\"#{code}\">"
+      elsif code && Emoji.exists?(code)
         "<img src=\"#{Emoji.url_for(code)}\" title=\"#{code}\" class=\"emoji\" alt=\"#{code}\">"
       else
         name
diff --git a/app/models/emoji.rb b/app/models/emoji.rb
index 22076c0..94f5812 100644
--- a/app/models/emoji.rb
+++ b/app/models/emoji.rb
@@ -38,6 +38,11 @@ class Emoji
     Discourse.cache.fetch(cache_key("tonable_emojis")) { db['tonableEmojis'] }
   end
 
+  def self.custom?(name)
+    name = name.delete_prefix(':').delete_suffix(':')
+    Emoji.custom.detect { |e| e.name == name }.present?
+  end
+
   def self.exists?(name)
     Emoji[name].present?
   end
diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb
index dea2adb..33da7de 100644
--- a/lib/plugin/instance.rb
+++ b/lib/plugin/instance.rb
@@ -19,6 +19,10 @@ class Plugin::CustomEmoji
     emojis[name] = url
   end
 
+  def self.unregister(name)
+    emojis.delete(name)
+  end
+
   def self.translations
     @@translations ||= {}
   end
diff --git a/spec/helpers/emoji_helper_spec.rb b/spec/helpers/emoji_helper_spec.rb
index 7d7fa32..7dec3b3 100644
--- a/spec/helpers/emoji_helper_spec.rb
+++ b/spec/helpers/emoji_helper_spec.rb
@@ -7,10 +7,14 @@ describe EmojiHelper do
 
   describe "emoji_codes_to_img" do
     it "replaces emoji codes by images" do
-      str = "This is a good day :woman: :man:t4:"
+      Plugin::CustomEmoji.register("xxxxxx", "/public/xxxxxx.png")
+
+      str = "This is a good day :xxxxxx: :woman: :man:t4:"
       replaced_str = helper.emoji_codes_to_img(str)
 
-      expect(replaced_str).to eq("This is a good day <img src=\"/images/emoji/twitter/woman.png?v=#{Emoji::EMOJI_VERSION}\" title=\"woman\" class=\"emoji\" alt=\"woman\"> <img src=\"/images/emoji/twitter/man/4.png?v=#{Emoji::EMOJI_VERSION}\" title=\"man:t4\" class=\"emoji\" alt=\"man:t4\">")
+      expect(replaced_str).to eq("This is a good day <img src=\"/public/xxxxxx.png\" title=\"xxxxxx\" class=\"emoji\" alt=\"xxxxxx\"> <img src=\"/images/emoji/twitter/woman.png?v=#{Emoji::EMOJI_VERSION}\" title=\"woman\" class=\"emoji\" alt=\"woman\"> <img src=\"/images/emoji/twitter/man/4.png?v=#{Emoji::EMOJI_VERSION}\" title=\"man:t4\" class=\"emoji\" alt=\"man:t4\">")
+
+      Plugin::CustomEmoji.unregister("xxxxxx")
     end
 
     it "doesn't replace if code doesn't exist" do

GitHub sha: 9ee2c121