FEATURE: Display the Watched Words that caused a post to be flagged. (#31435)

When a post is flagged due to matching watched words, it can be difficult to know what you're looking for, particularly if you have a lot of watched words built up over a long period of time.

This change stores the list of matched words, and later displays them in the review queue, listing which Watched Words were responsible for the flag. Because watched words can change, this is recorded at the time the post is flagged. For posts that were flagged prior to this feature landing, it tries to guess the relevant words based on the current Watched Words set.
This commit is contained in:
Gary Pendergast
2025-03-04 17:22:12 +11:00
committed by GitHub
parent b329eac79a
commit 9abeff460c
10 changed files with 137 additions and 8 deletions

View File

@ -12,6 +12,7 @@ RSpec.describe WatchedWord do
Fabricate(:watched_word, action: WatchedWord.actions[:require_approval])
end
let(:flag_word) { Fabricate(:watched_word, action: WatchedWord.actions[:flag]) }
let(:another_flag_word) { Fabricate(:watched_word, action: WatchedWord.actions[:flag]) }
let(:block_word) { Fabricate(:watched_word, action: WatchedWord.actions[:block]) }
let(:another_block_word) { Fabricate(:watched_word, action: WatchedWord.actions[:block]) }
@ -232,7 +233,43 @@ RSpec.describe WatchedWord do
).to eq(true)
reviewable = ReviewableFlaggedPost.where(target: post)
expect(reviewable).to be_present
expect(ReviewableScore.where(reviewable: reviewable, reason: "watched_word")).to be_present
expect(
ReviewableScore.where(
reviewable: reviewable,
reason: "watched_word",
context: flag_word.word,
),
).to be_present
end
it "should flag the post if it contains multiple flagged words" do
topic = Fabricate(:topic, user: tl2_user)
post =
Fabricate(
:post,
raw:
"I said.... #{flag_word.word} and #{another_flag_word.word} and #{flag_word.word} again",
topic: topic,
user: tl2_user,
)
expect { Jobs::ProcessPost.new.execute(post_id: post.id) }.to change { PostAction.count }.by(
1,
)
expect(
PostAction.where(
post_id: post.id,
post_action_type_id: PostActionType.types[:inappropriate],
).exists?,
).to eq(true)
reviewable = ReviewableFlaggedPost.where(target: post)
expect(reviewable).to be_present
expect(
ReviewableScore.where(
reviewable: reviewable,
reason: "watched_word",
context: "#{flag_word.word},#{another_flag_word.word}",
),
).to be_present
end
it "should look at the title too" do