FEATURE: allow blocking emojis (#7011)

FEATURE: allow blocking emojis (#7011)

Blocking emojis won't work - feature - Discourse Meta

diff --git a/app/services/word_watcher.rb b/app/services/word_watcher.rb
index ff82f36..20614d2 100644
--- a/app/services/word_watcher.rb
+++ b/app/services/word_watcher.rb
@@ -19,7 +19,7 @@ class WordWatcher
         nil
       else
         regexp = '(' + words.map { |w| word_to_regexp(w) }.join('|'.freeze) + ')'
-        SiteSetting.watched_words_regular_expressions? ? regexp : "\\b(#{regexp})\\b"
+        SiteSetting.watched_words_regular_expressions? ? regexp : "(?<!\\w)(#{regexp})(?!\\w)"
       end
     end
     s.present? ? Regexp.new(s, Regexp::IGNORECASE) : nil
diff --git a/spec/services/word_watcher_spec.rb b/spec/services/word_watcher_spec.rb
index 52264cf..48ce215 100644
--- a/spec/services/word_watcher_spec.rb
+++ b/spec/services/word_watcher_spec.rb
@@ -48,6 +48,26 @@ describe WordWatcher do
         expect(m[1]).to eq("acknowledge")
       end
 
+      context "emojis" do
+        it "handles emoji" do
+          Fabricate(:watched_word, word: ":joy:", action: WatchedWord.actions[:require_approval])
+          m = WordWatcher.new("Lots of emojis here :joy:").word_matches_for_action?(:require_approval)
+          expect(m[1]).to eq(":joy:")
+        end
+
+        it "handles unicode emoji" do
+          Fabricate(:watched_word, word: "🎃", action: WatchedWord.actions[:require_approval])
+          m = WordWatcher.new("Halloween party! 🎃").word_matches_for_action?(:require_approval)
+          expect(m[1]).to eq("🎃")
+        end
+
+        it "handles emoji skin tone" do
+          Fabricate(:watched_word, word: ":woman:t5:", action: WatchedWord.actions[:require_approval])
+          m = WordWatcher.new("To Infinity and beyond! 🚀 :woman:t5:").word_matches_for_action?(:require_approval)
+          expect(m[1]).to eq(":woman:t5:")
+        end
+      end
+
       context "regular expressions" do
         before do
           SiteSetting.watched_words_regular_expressions = true

GitHub sha: 99c6db21

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there:

Nice simple change! I like :+1:

1 Like

That said, I would like us to ensure some other conditions are tested.

Say you watch the word love:

Do all of these still work?

  • I love, bananas
  • I love; stuff
  • love: is a thing.
  • peace ,love and understanding.
  • I love.

May need a bit more fancy to handle these, you may need to adjust regex etc.

2 Likes

All of the above examples works fine with the new regex. Added more tests in this commit:

2 Likes