Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: allow blocking emojis (#7011)

Arpit Jalan99c6db21e632
diff --git a/app/services/word_watcher.rb b/app/services/word_watcher.rb
index ff82f36bd55..20614d23a7f 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 52264cf41af..48ce215e43a 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