mirror of
https://github.com/discourse/discourse.git
synced 2026-08-04 10:23:17 -05:00
Allows different types of topic-timer to be automatically configured on topics. It uses the 'post_created_edited' trigger, which provides all sorts of filtering options including category/author/etc. <img width="804" height="270" alt="SCR-20251212-mzrg" src="https://github.com/user-attachments/assets/90cca3f8-1516-4adf-830a-c2f43125795d" /> --------- Co-authored-by: Régis Hanol <regis@hanol.fr>
60 lines
1.7 KiB
Ruby
60 lines
1.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
DiscourseAutomation::Scriptable.add(DiscourseAutomation::Scripts::SET_TOPIC_TIMER) do
|
|
field :type,
|
|
component: :choices,
|
|
extra: {
|
|
content: [
|
|
{ id: "auto_close", name: "topic.auto_close.title" },
|
|
{ id: "auto_close_after_last_post", name: "topic.auto_close_after_last_post.title" },
|
|
{ id: "auto_delete", name: "topic.auto_delete.title" },
|
|
{ id: "auto_delete_replies", name: "topic.auto_delete_replies.title" },
|
|
{ id: "auto_bump", name: "topic.auto_bump.title" },
|
|
],
|
|
},
|
|
required: true
|
|
|
|
field :duration, component: :relative_time, required: true
|
|
|
|
version 1
|
|
|
|
triggerables %i[post_created_edited]
|
|
|
|
script do |context, fields|
|
|
post = context["post"]
|
|
timer_type = fields.dig("type", "value")
|
|
|
|
next if !post.topic
|
|
next unless topic = Topic.find_by(id: post.topic_id)
|
|
|
|
duration_minutes = fields.dig("duration", "value")
|
|
|
|
case timer_type
|
|
when "auto_close"
|
|
topic.set_or_create_timer(
|
|
TopicTimer.types[:close],
|
|
(Time.now + duration_minutes.minutes).iso8601,
|
|
)
|
|
when "auto_close_after_last_post"
|
|
topic.set_or_create_timer(
|
|
TopicTimer.types[:close],
|
|
nil,
|
|
based_on_last_post: true,
|
|
duration_minutes:,
|
|
)
|
|
when "auto_delete"
|
|
topic.set_or_create_timer(
|
|
TopicTimer.types[:delete],
|
|
(Time.now + duration_minutes.minutes).iso8601,
|
|
)
|
|
when "auto_delete_replies"
|
|
topic.set_or_create_timer(TopicTimer.types[:delete_replies], nil, duration_minutes:)
|
|
when "auto_bump"
|
|
topic.set_or_create_timer(
|
|
TopicTimer.types[:bump],
|
|
(Time.now + duration_minutes.minutes).iso8601,
|
|
)
|
|
end
|
|
end
|
|
end
|