DEV: move post flags into database (#27125)

This is preparation for a feature that will allow admins to define their custom flags. Current behaviour should stay untouched.
This commit is contained in:
Krzysztof Kotlarek
2024-05-23 12:19:07 +10:00
committed by GitHub
parent 312a930ac8
commit cfbbfd177c
30 changed files with 380 additions and 104 deletions

View File

@@ -1,7 +1,3 @@
# frozen_string_literal: true
Fabricator(:flag, from: :post_action) do
user
post
post_action_type_id PostActionType.types[:spam]
end
Fabricator(:flag) { name "offtopic", applies_to { %w[Post Chat::Message] } }

View File

@@ -0,0 +1,7 @@
# frozen_string_literal: true
Fabricator(:flag_post_action, from: :post_action) do
user
post
post_action_type_id PostActionType.types[:spam]
end

View File

@@ -360,7 +360,7 @@ RSpec.describe ComposerMessagesFinder do
end
it "shows a message when the replier has already flagged the post" do
Fabricate(:flag, post: self_flagged_post, user: user)
Fabricate(:flag_post_action, post: self_flagged_post, user: user)
finder =
ComposerMessagesFinder.new(
user,
@@ -372,13 +372,13 @@ RSpec.describe ComposerMessagesFinder do
end
it "shows a message when replying to flagged topic (first post)" do
Fabricate(:flag, post: original_post, user: user)
Fabricate(:flag_post_action, post: original_post, user: user)
finder = ComposerMessagesFinder.new(user, composer_action: "reply", topic_id: topic.id)
expect(finder.check_dont_feed_the_trolls).to be_present
end
it "does not show a message when not enough others have flagged the post" do
Fabricate(:flag, post: under_flagged_post, user: other_user)
Fabricate(:flag_post_action, post: under_flagged_post, user: other_user)
finder =
ComposerMessagesFinder.new(
user,
@@ -392,7 +392,12 @@ RSpec.describe ComposerMessagesFinder do
it "does not show a message when the flag has already been resolved" do
SiteSetting.dont_feed_the_trolls_threshold = 1
Fabricate(:flag, post: resolved_flag_post, user: other_user, disagreed_at: 1.hour.ago)
Fabricate(
:flag_post_action,
post: resolved_flag_post,
user: other_user,
disagreed_at: 1.hour.ago,
)
finder =
ComposerMessagesFinder.new(
user,
@@ -404,8 +409,8 @@ RSpec.describe ComposerMessagesFinder do
end
it "shows a message when enough others have already flagged the post" do
Fabricate(:flag, post: over_flagged_post, user: other_user)
Fabricate(:flag, post: over_flagged_post, user: third_user)
Fabricate(:flag_post_action, post: over_flagged_post, user: other_user)
Fabricate(:flag_post_action, post: over_flagged_post, user: third_user)
finder =
ComposerMessagesFinder.new(
user,

View File

@@ -0,0 +1,28 @@
# frozen_string_literal: true
RSpec.describe FlagGuardian do
fab!(:user)
fab!(:admin)
fab!(:flag)
describe "#can_edit_flag?" do
it "returns true for admin and false for regular user" do
expect(Guardian.new(admin).can_edit_flag?(flag)).to eq(true)
expect(Guardian.new(user).can_edit_flag?(flag)).to eq(false)
end
it "returns false when flag is system" do
expect(Guardian.new(admin).can_edit_flag?(Flag.system.first)).to eq(false)
end
it "returns false when flag was already used with post action" do
Fabricate(:post_action, post_action_type_id: flag.id)
expect(Guardian.new(admin).can_edit_flag?(flag)).to eq(false)
end
it "returns false when flag was already used with reviewable" do
Fabricate(:reviewable_score, reviewable_score_type: flag.id)
expect(Guardian.new(admin).can_edit_flag?(flag)).to eq(false)
end
end
end

View File

@@ -673,10 +673,7 @@ TEXT
end
describe "#replace_flags" do
after do
PostActionType.replace_flag_settings(nil)
ReviewableScore.reload_types
end
after { PostActionType.replace_flag_settings(nil) }
let(:original_flags) { PostActionType.flag_settings }

View File

@@ -484,7 +484,7 @@ RSpec.describe PostRevisor do
post = Fabricate(:post, raw: "hello world")
Fabricate(:flag, post: post, user: user)
Fabricate(:flag_post_action, post: post, user: user)
revisor = PostRevisor.new(post)
revisor.revise!(

69
spec/models/flag_spec.rb Normal file
View File

@@ -0,0 +1,69 @@
# frozen_string_literal: true
RSpec.describe Flag, type: :model do
before { Flag.reset_flag_settings! }
it "has id lower than 1000 for system flags" do
flag = Fabricate(:flag, id: 1)
expect(flag.system?).to be true
end
it "has id greater than 1000 for non-system flags" do
flag = Fabricate(:flag)
expect(flag.system?).to be false
expect(flag.id).to be > 1000
end
it "has correct name key" do
flag = Fabricate(:flag, name: "CuStOm Flag!!!")
expect(flag.name_key).to eq("custom_flag")
flag.update!(name: "It's Illegal")
expect(flag.name_key).to eq("its_illegal")
flag.update!(name: "THIS IS SPaM!+)(*&^%$#@@@!)")
expect(flag.name_key).to eq("this_is_spam")
end
it "updates post action types when created, modified or destroyed" do
expect(PostActionType.flag_types.keys).to eq(
%i[notify_user notify_moderators off_topic inappropriate spam illegal],
)
expect(ReviewableScore.types.keys).to eq(
%i[notify_user notify_moderators off_topic inappropriate spam illegal needs_approval],
)
flag = Fabricate(:flag, name: "custom")
expect(PostActionType.flag_types.keys).to eq(
%i[notify_user notify_moderators off_topic inappropriate spam illegal custom],
)
expect(ReviewableScore.types.keys).to eq(
%i[notify_user notify_moderators off_topic inappropriate spam illegal custom needs_approval],
)
flag.update!(name: "edited_custom")
expect(PostActionType.flag_types.keys).to eq(
%i[notify_user notify_moderators off_topic inappropriate spam illegal edited_custom],
)
expect(ReviewableScore.types.keys).to eq(
%i[
notify_user
notify_moderators
off_topic
inappropriate
spam
illegal
edited_custom
needs_approval
],
)
flag.destroy!
expect(PostActionType.flag_types.keys).to eq(
%i[notify_user notify_moderators off_topic inappropriate spam illegal],
)
expect(ReviewableScore.types.keys).to eq(
%i[notify_user notify_moderators off_topic inappropriate spam illegal needs_approval],
)
end
end

View File

@@ -359,7 +359,7 @@ RSpec.describe TrustLevel3Requirements do
flags =
%i[off_topic inappropriate notify_user notify_moderators spam].map do |t|
Fabricate(
:flag,
:flag_post_action,
post: Fabricate(:post, user: user),
post_action_type_id: PostActionType.types[t],
agreed_at: 1.minute.ago,
@@ -369,7 +369,7 @@ RSpec.describe TrustLevel3Requirements do
_deferred_flags =
%i[off_topic inappropriate notify_user notify_moderators spam].map do |t|
Fabricate(
:flag,
:flag_post_action,
post: Fabricate(:post, user: user),
post_action_type_id: PostActionType.types[t],
deferred_at: 1.minute.ago,
@@ -379,7 +379,7 @@ RSpec.describe TrustLevel3Requirements do
_deleted_flags =
%i[off_topic inappropriate notify_user notify_moderators spam].map do |t|
Fabricate(
:flag,
:flag_post_action,
post: Fabricate(:post, user: user),
post_action_type_id: PostActionType.types[t],
deleted_at: 1.minute.ago,
@@ -388,7 +388,7 @@ RSpec.describe TrustLevel3Requirements do
# Same post, different user:
Fabricate(
:flag,
:flag_post_action,
post: flags[1].post,
post_action_type_id: PostActionType.types[:spam],
agreed_at: 1.minute.ago,
@@ -396,7 +396,7 @@ RSpec.describe TrustLevel3Requirements do
# Flagged their own post:
Fabricate(
:flag,
:flag_post_action,
user: user,
post: Fabricate(:post, user: user),
post_action_type_id: PostActionType.types[:spam],
@@ -405,7 +405,7 @@ RSpec.describe TrustLevel3Requirements do
# More than 100 days ago:
Fabricate(
:flag,
:flag_post_action,
post: Fabricate(:post, user: user, created_at: 101.days.ago),
post_action_type_id: PostActionType.types[:spam],
created_at: 101.days.ago,

View File

@@ -72,7 +72,11 @@ RSpec.describe SpamRule::AutoSilence do
end
it "returns 0 when there is one flag that has a reason other than spam" do
Fabricate(:flag, post: post, post_action_type_id: PostActionType.types[:off_topic])
Fabricate(
:flag_post_action,
post: post,
post_action_type_id: PostActionType.types[:off_topic],
)
expect(count).to eq(0)
end

View File

@@ -6,7 +6,7 @@ describe "Composer don't feed the trolls popup", type: :system do
fab!(:topic) { Fabricate(:topic, user: user) }
fab!(:post) { Fabricate(:post, user: user, topic: topic) }
fab!(:reply) { Fabricate(:post, user: troll, topic: topic) }
fab!(:flag) { Fabricate(:flag, post: reply, user: user) }
fab!(:flag) { Fabricate(:flag_post_action, post: reply, user: user) }
let(:topic_page) { PageObjects::Pages::Topic.new }
before { sign_in user }

View File

@@ -12,7 +12,7 @@ describe "Flagging post", type: :system do
describe "Using Take Action" do
it "can select the default action to hide the post, agree with other flags, and reach the flag threshold" do
other_flag = Fabricate(:flag, post: post_to_flag, user: Fabricate(:moderator))
other_flag = Fabricate(:flag_post_action, post: post_to_flag, user: Fabricate(:moderator))
other_flag_reviewable =
Fabricate(:reviewable_flagged_post, target: post_to_flag, created_by: other_flag.user)
expect(other_flag.reload.agreed_at).to be_nil