mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: keep the topic in closed status until the community flags are handled
This commit is contained in:
@@ -15,7 +15,16 @@ module Jobs
|
|||||||
user = topic_timer.user
|
user = topic_timer.user
|
||||||
|
|
||||||
if Guardian.new(user).can_close?(topic)
|
if Guardian.new(user).can_close?(topic)
|
||||||
|
if state == false && PostAction.auto_close_threshold_reached?(topic)
|
||||||
|
topic.set_or_create_timer(
|
||||||
|
TopicTimer.types[:open],
|
||||||
|
SiteSetting.num_hours_to_close_topic,
|
||||||
|
by_user: Discourse.system_user
|
||||||
|
)
|
||||||
|
else
|
||||||
topic.update_status('autoclosed', state, user)
|
topic.update_status('autoclosed', state, user)
|
||||||
|
end
|
||||||
|
|
||||||
topic.inherit_auto_close_from_category if state == false
|
topic.inherit_auto_close_from_category if state == false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -564,9 +564,7 @@ class PostAction < ActiveRecord::Base
|
|||||||
|
|
||||||
MAXIMUM_FLAGS_PER_POST = 3
|
MAXIMUM_FLAGS_PER_POST = 3
|
||||||
|
|
||||||
def self.auto_close_if_threshold_reached(topic)
|
def self.auto_close_threshold_reached?(topic)
|
||||||
return if topic.nil? || topic.closed?
|
|
||||||
|
|
||||||
flags = PostAction.active
|
flags = PostAction.active
|
||||||
.flags
|
.flags
|
||||||
.joins(:post)
|
.joins(:post)
|
||||||
@@ -580,6 +578,13 @@ class PostAction < ActiveRecord::Base
|
|||||||
# we need a minimum number of flags
|
# we need a minimum number of flags
|
||||||
return if flags.sum { |f| f[1] } < SiteSetting.num_flags_to_close_topic
|
return if flags.sum { |f| f[1] } < SiteSetting.num_flags_to_close_topic
|
||||||
|
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.auto_close_if_threshold_reached(topic)
|
||||||
|
return if topic.nil? || topic.closed?
|
||||||
|
return unless auto_close_threshold_reached?(topic)
|
||||||
|
|
||||||
# the threshold has been reached, we will close the topic waiting for intervention
|
# the threshold has been reached, we will close the topic waiting for intervention
|
||||||
topic.update_status("closed", true, Discourse.system_user,
|
topic.update_status("closed", true, Discourse.system_user,
|
||||||
message: I18n.t(
|
message: I18n.t(
|
||||||
|
|||||||
@@ -589,20 +589,23 @@ describe PostAction do
|
|||||||
expect(post.hidden).to eq(false)
|
expect(post.hidden).to eq(false)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "will automatically pause a topic due to large community flagging" do
|
context "topic auto closing" do
|
||||||
|
let(:topic) { Fabricate(:topic) }
|
||||||
|
let(:post1) { create_post(topic: topic) }
|
||||||
|
let(:post2) { create_post(topic: topic) }
|
||||||
|
let(:post3) { create_post(topic: topic) }
|
||||||
|
|
||||||
|
let(:flagger1) { Fabricate(:user) }
|
||||||
|
let(:flagger2) { Fabricate(:user) }
|
||||||
|
|
||||||
|
before do
|
||||||
SiteSetting.flags_required_to_hide_post = 0
|
SiteSetting.flags_required_to_hide_post = 0
|
||||||
SiteSetting.num_flags_to_close_topic = 3
|
SiteSetting.num_flags_to_close_topic = 3
|
||||||
SiteSetting.num_flaggers_to_close_topic = 2
|
SiteSetting.num_flaggers_to_close_topic = 2
|
||||||
SiteSetting.num_hours_to_close_topic = 1
|
SiteSetting.num_hours_to_close_topic = 1
|
||||||
|
end
|
||||||
|
|
||||||
topic = Fabricate(:topic)
|
it "will automatically pause a topic due to large community flagging" do
|
||||||
post1 = create_post(topic: topic)
|
|
||||||
post2 = create_post(topic: topic)
|
|
||||||
post3 = create_post(topic: topic)
|
|
||||||
|
|
||||||
flagger1 = Fabricate(:user)
|
|
||||||
flagger2 = Fabricate(:user)
|
|
||||||
|
|
||||||
# reaching `num_flaggers_to_close_topic` isn't enough
|
# reaching `num_flaggers_to_close_topic` isn't enough
|
||||||
[flagger1, flagger2].each do |flagger|
|
[flagger1, flagger2].each do |flagger|
|
||||||
PostAction.act(flagger, post1, PostActionType.types[:inappropriate])
|
PostAction.act(flagger, post1, PostActionType.types[:inappropriate])
|
||||||
@@ -639,6 +642,49 @@ describe PostAction do
|
|||||||
expect(topic_status_update.status_type).to eq(TopicTimer.types[:open])
|
expect(topic_status_update.status_type).to eq(TopicTimer.types[:open])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "will keep the topic in closed status until the community flags are handled" do
|
||||||
|
freeze_time
|
||||||
|
|
||||||
|
PostAction.stubs(:auto_close_threshold_reached?).returns(true)
|
||||||
|
PostAction.auto_close_if_threshold_reached(topic)
|
||||||
|
|
||||||
|
expect(topic.reload.closed).to eq(true)
|
||||||
|
|
||||||
|
timer = TopicTimer.last
|
||||||
|
expect(timer.execute_at).to eq(1.hour.from_now)
|
||||||
|
|
||||||
|
freeze_time timer.execute_at
|
||||||
|
Jobs.expects(:enqueue_in).with(1.hour.to_i, :toggle_topic_closed, topic_timer_id: timer.id, state: false).returns(true)
|
||||||
|
Jobs::ToggleTopicClosed.new.execute(topic_timer_id: timer.id, state: false)
|
||||||
|
|
||||||
|
expect(topic.reload.closed).to eq(true)
|
||||||
|
expect(timer.reload.execute_at).to eq(1.hour.from_now)
|
||||||
|
|
||||||
|
freeze_time timer.execute_at
|
||||||
|
PostAction.stubs(:auto_close_threshold_reached?).returns(false)
|
||||||
|
Jobs::ToggleTopicClosed.new.execute(topic_timer_id: timer.id, state: false)
|
||||||
|
|
||||||
|
expect(topic.reload.closed).to eq(false)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "will reopen topic after the flags are auto handled" do
|
||||||
|
freeze_time
|
||||||
|
[flagger1, flagger2].each do |flagger|
|
||||||
|
[post1, post2, post3].each do |post|
|
||||||
|
PostAction.act(flagger, post, PostActionType.types[:inappropriate])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(topic.reload.closed).to eq(true)
|
||||||
|
|
||||||
|
freeze_time 61.days.from_now
|
||||||
|
Jobs::AutoQueueHandler.new.execute({})
|
||||||
|
Jobs::ToggleTopicClosed.new.execute(topic_timer_id: TopicTimer.last.id, state: false)
|
||||||
|
|
||||||
|
expect(topic.reload.closed).to eq(false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "prevents user to act twice at the same time" do
|
it "prevents user to act twice at the same time" do
|
||||||
|
|||||||
Reference in New Issue
Block a user