2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
module Jobs
|
2019-10-02 12:01:53 +08:00
|
|
|
class ToggleTopicClosed < ::Jobs::Base
|
2017-03-22 11:12:02 +08:00
|
|
|
def execute(args)
|
2023-07-18 10:13:40 +08:00
|
|
|
Discourse.deprecate(
|
|
|
|
"ToggleTopicClosed is deprecated. Use OpenTopic and CloseTopic instead.",
|
|
|
|
drop_from: "3.3.0",
|
|
|
|
)
|
|
|
|
|
2017-05-12 06:23:18 +08:00
|
|
|
topic_timer = TopicTimer.find_by(id: args[:topic_timer_id] || args[:topic_status_update_id])
|
2021-01-13 06:49:29 +08:00
|
|
|
|
|
|
|
# state false is Open Topic
|
|
|
|
# state true is Close Topic
|
2017-04-04 14:29:47 +08:00
|
|
|
state = !!args[:state]
|
2020-12-03 07:43:19 +08:00
|
|
|
timer_type = args[:silent] ? :silent_close : :close
|
2017-03-22 11:12:02 +08:00
|
|
|
|
2020-08-26 12:18:33 +08:00
|
|
|
return if topic_timer.blank? || topic_timer.execute_at > Time.zone.now
|
2017-03-22 11:12:02 +08:00
|
|
|
|
2020-08-26 12:18:33 +08:00
|
|
|
if (topic = topic_timer.topic).blank? || topic.closed == state
|
|
|
|
topic_timer.destroy!
|
2017-04-04 14:29:47 +08:00
|
|
|
return
|
|
|
|
end
|
2017-03-22 11:12:02 +08:00
|
|
|
|
2017-05-12 06:23:18 +08:00
|
|
|
user = topic_timer.user
|
2017-03-22 11:12:02 +08:00
|
|
|
|
2020-07-15 00:36:19 +08:00
|
|
|
if Guardian.new(user).can_close_topic?(topic)
|
2019-01-04 01:03:01 +08:00
|
|
|
if state == false && topic.auto_close_threshold_reached?
|
2019-01-08 18:43:10 +08:00
|
|
|
topic.set_or_create_timer(
|
|
|
|
TopicTimer.types[:open],
|
|
|
|
SiteSetting.num_hours_to_close_topic,
|
|
|
|
by_user: Discourse.system_user,
|
|
|
|
)
|
|
|
|
else
|
2020-12-03 07:43:19 +08:00
|
|
|
topic.update_status("autoclosed", state, user, { silent: args[:silent] })
|
2019-01-08 18:43:10 +08:00
|
|
|
end
|
|
|
|
|
2020-12-03 07:43:19 +08:00
|
|
|
topic.inherit_auto_close_from_category(timer_type: timer_type) if state == false
|
2020-08-26 12:59:05 +08:00
|
|
|
else
|
|
|
|
topic_timer.destroy!
|
|
|
|
topic.reload
|
|
|
|
|
|
|
|
if topic_timer.based_on_last_post
|
2020-12-03 07:43:19 +08:00
|
|
|
topic.inherit_auto_close_from_category(timer_type: timer_type)
|
2020-08-26 12:59:05 +08:00
|
|
|
end
|
2017-03-22 11:12:02 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|