Skip to content

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

FIX: Ensure queued posts with watched words are processed correctly. (#31641)

Gary Pendergaste4401e587e98
diff --git a/app/serializers/reviewable_score_serializer.rb b/app/serializers/reviewable_score_serializer.rb
index f7802e9719c..989456eb2c8 100644
--- a/app/serializers/reviewable_score_serializer.rb
+++ b/app/serializers/reviewable_score_serializer.rb
@@ -77,16 +77,20 @@ class ReviewableScoreSerializer < ApplicationSerializer
def watched_word_reason(link)
if object.context.nil?
# If the words weren't recorded, try to guess them based on current settings.
- words =
- WordWatcher.new(object.reviewable.post.raw).word_matches_for_action?(
- :flag,
- all_matches: true,
- )
+ if object.reviewable.respond_to?(:post)
+ s = object.reviewable.post.raw
+ s << " #{object.reviewable.post.topic.title}" if object.reviewable.post.post_number == 1
+ elsif object.reviewable.respond_to?(:payload)
+ s = object.reviewable.payload["raw"]
+ s << " #{object.reviewable.payload["title"]}" if object.reviewable.payload.key?("title")
+ end
+
+ words = WordWatcher.new(s).word_matches_across_all_actions
else
words = object.context.split(",")
end
- if words.nil?
+ if words.nil? || words.empty?
text =
I18n.t("reviewables.reasons.no_context.watched_word", link: link, default: "watched_word")
else
diff --git a/app/services/word_watcher.rb b/app/services/word_watcher.rb
index a0782130e49..87514aac598 100644
--- a/app/services/word_watcher.rb
+++ b/app/services/word_watcher.rb
@@ -223,6 +223,17 @@ class WordWatcher
match_list
end
+ def word_matches_across_all_actions
+ WatchedWord
+ .actions
+ .keys
+ .filter_map { |action| word_matches_for_action?(action, all_matches: true) }
+ .flatten
+ .compact
+ .uniq
+ .sort
+ end
+
def word_matches?(word, case_sensitive: false)
options = case_sensitive ? nil : Regexp::IGNORECASE
Regexp.new(WordWatcher.word_to_regexp(word), options).match?(@raw)
diff --git a/spec/serializers/reviewable_score_serializer_spec.rb b/spec/serializers/reviewable_score_serializer_spec.rb
index 7e2aa616f62..1f6bbc9fe08 100644
--- a/spec/serializers/reviewable_score_serializer_spec.rb
+++ b/spec/serializers/reviewable_score_serializer_spec.rb
@@ -80,6 +80,18 @@ RSpec.describe ReviewableScoreSerializer do
expect(score.reason).to include("bad, words")
end
+ it "handles guessing the watched words when the post hasn't been created yet" do
+ queued_reviewable = Fabricate(:reviewable_queued_post_topic)
+ reviewable_score =
+ ReviewableScore.new(reviewable: queued_reviewable, reason: "watched_word")
+
+ Fabricate(:watched_word, action: WatchedWord.actions[:flag], word: "contents")
+ Fabricate(:watched_word, action: WatchedWord.actions[:flag], word: "title")
+
+ result = described_class.new(reviewable_score, scope: Guardian.new(admin), root: nil)
+ expect(result.reason).to include("contents, title")
+ end
+
it "uses the no-context message if the post has no watched words" do
reviewable.target = Fabricate(:post, raw: "This post contains no bad words.")
diff --git a/spec/services/word_watcher_spec.rb b/spec/services/word_watcher_spec.rb
index a2911854ec4..f7be8d5fe91 100644
--- a/spec/services/word_watcher_spec.rb
+++ b/spec/services/word_watcher_spec.rb
@@ -368,6 +368,22 @@ RSpec.describe WordWatcher do
end
end
+ describe "#word_matches_across_all_actions" do
+ it("returns an array of words") do
+ Fabricate(:watched_word, action: WatchedWord.actions[:flag], word: "foo")
+ Fabricate(:watched_word, action: WatchedWord.actions[:block], word: "bar")
+ Fabricate(:watched_word, action: WatchedWord.actions[:silence], word: "baz")
+
+ contentful_check = described_class.new("Going to match the baz, the foo, and the bar.")
+
+ expect(contentful_check.word_matches_across_all_actions).to contain_exactly(
+ "foo",
+ "bar",
+ "baz",
+ )
+ end
+ end
+
describe "word replacement" do
fab!(:censored_word) do
Fabricate(:watched_word, word: "censored", action: WatchedWord.actions[:censor])