Class: Jobs::ToggleTopicClosed

Inherits:
Base
  • Object
show all
Defined in:
app/jobs/regular/toggle_topic_closed.rb

Instance Method Summary collapse

Methods inherited from Base

acquire_cluster_concurrency_lock!, clear_cluster_concurrency_lock!, cluster_concurrency, cluster_concurrency_redis_key, delayed_perform, #error_context, get_cluster_concurrency, #last_db_duration, #log, #perform, #perform_immediately

Instance Method Details

#execute(args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/jobs/regular/toggle_topic_closed.rb', line 5

def execute(args)
  Discourse.deprecate(
    "ToggleTopicClosed is deprecated. Use OpenTopic and CloseTopic instead.",
    drop_from: "3.3.0",
  )

  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

  return if topic_timer.blank? || topic_timer.execute_at > Time.zone.now

  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