discourse/app/jobs/regular/toggle_topic_closed.rb
Martin Brennan 2404fa7a23
DEV: Split toggle topic close job (#11679)
Splits the `ToggleTopicClosed` job into two distinct `OpenTopic` and `CloseTopic` jobs to make the code clearer. The old job cannot be deleted yet because of outstanding sidekiq schedules, so a todo has been added to do so later this year.

Also replaced mentions of `topic_status_update` with `topic_timer` in some files, because the `topic_status_update` model is obsolete and replaced by topic timer.

Added some shortcut methods for checking if a topic is open/whether a user can change an open topic.
2021-01-13 08:49:29 +10:00

49 lines
1.4 KiB
Ruby

# frozen_string_literal: true
module Jobs
# TODO: DEPRECATED - Use OpenTopic and CloseTopic instead.
# (martin - 2021-05-01) - Delete once topic timer revamp is completed.
class ToggleTopicClosed < ::Jobs::Base
def execute(args)
topic_timer = TopicTimer.find_by(id: args[:topic_timer_id] || args[:topic_status_update_id])
# state false is Open Topic
# state true is Close Topic
state = !!args[:state]
timer_type = args[:silent] ? :silent_close : :close
if topic_timer.blank? || topic_timer.execute_at > Time.zone.now
return
end
if (topic = topic_timer.topic).blank? || topic.closed == state
topic_timer.destroy!
return
end
user = topic_timer.user
if Guardian.new(user).can_close_topic?(topic)
if state == false && topic.auto_close_threshold_reached?
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, { silent: args[:silent] })
end
topic.inherit_auto_close_from_category(timer_type: timer_type) if state == false
else
topic_timer.destroy!
topic.reload
if topic_timer.based_on_last_post
topic.inherit_auto_close_from_category(timer_type: timer_type)
end
end
end
end
end