2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-12 06:23:18 +08:00
|
|
|
class TopicTimer < ActiveRecord::Base
|
2021-02-12 07:05:14 +08:00
|
|
|
MAX_DURATION_MINUTES = 2.years.to_i / 60
|
|
|
|
|
2021-02-05 08:12:56 +08:00
|
|
|
self.ignored_columns = [
|
|
|
|
"duration" # TODO(2021-06-01): remove
|
|
|
|
]
|
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
include Trashable
|
2013-05-28 09:00:53 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :topic
|
2017-04-07 17:05:58 +08:00
|
|
|
belongs_to :category
|
2013-05-28 09:00:53 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
validates :user_id, presence: true
|
|
|
|
validates :topic_id, presence: true
|
|
|
|
validates :execute_at, presence: true
|
|
|
|
validates :status_type, presence: true
|
2017-05-17 02:49:42 +08:00
|
|
|
validates :status_type, uniqueness: { scope: [:topic_id, :deleted_at] }, if: :public_type?
|
|
|
|
validates :status_type, uniqueness: { scope: [:topic_id, :deleted_at, :user_id] }, if: :private_type?
|
2017-04-07 15:26:15 +08:00
|
|
|
validates :category_id, presence: true, if: :publishing_to_category?
|
|
|
|
|
2021-01-13 06:49:29 +08:00
|
|
|
validate :executed_at_in_future?
|
2021-02-12 07:05:14 +08:00
|
|
|
validate :duration_in_range?
|
2017-03-22 11:12:02 +08:00
|
|
|
|
2019-11-20 01:10:14 +08:00
|
|
|
scope :scheduled_bump_topics, -> { where(status_type: TopicTimer.types[:bump], deleted_at: nil).pluck(:topic_id) }
|
2021-01-19 11:30:58 +08:00
|
|
|
scope :pending_timers, ->(before_time = Time.now.utc) do
|
|
|
|
where("execute_at <= :before_time AND deleted_at IS NULL", before_time: before_time)
|
|
|
|
end
|
2019-11-19 22:24:18 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
before_save do
|
|
|
|
self.created_at ||= Time.zone.now if execute_at
|
2017-05-17 02:49:42 +08:00
|
|
|
self.public_type = self.public_type?
|
2017-03-22 11:12:02 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
if (will_save_change_to_execute_at? &&
|
|
|
|
!attribute_in_database(:execute_at).nil?) ||
|
|
|
|
will_save_change_to_user_id?
|
|
|
|
|
2021-01-19 11:30:58 +08:00
|
|
|
# TODO(martin - 2021-05-01) - Remove this backwards compatability for outstanding
|
|
|
|
# jobs once they have all been run and after Jobs::TopicTimerEnqueuer is in place
|
2017-03-22 11:12:02 +08:00
|
|
|
self.send("cancel_auto_#{self.class.types[status_type]}_job")
|
2016-05-25 02:27:48 +08:00
|
|
|
end
|
2013-05-28 09:00:53 +08:00
|
|
|
end
|
|
|
|
|
2021-01-19 11:30:58 +08:00
|
|
|
# These actions are in place to make sure the topic is in the correct
|
|
|
|
# state at the point in time where the timer is saved. It does not
|
|
|
|
# guarantee that the topic will be in the correct state when the timer
|
|
|
|
# job is executed, but each timer job handles deleted topics etc. gracefully.
|
|
|
|
#
|
|
|
|
# This is also important for the Open Temporarily and Close Temporarily timers,
|
|
|
|
# which change the topic's status straight away and set a timer to do the
|
|
|
|
# opposite action in the future.
|
2017-03-22 11:12:02 +08:00
|
|
|
after_save do
|
2017-08-31 12:06:56 +08:00
|
|
|
if (saved_change_to_execute_at? || saved_change_to_user_id?)
|
2021-01-19 11:30:58 +08:00
|
|
|
if status_type == TopicTimer.types[:silent_close] || status_type == TopicTimer.types[:close]
|
|
|
|
topic.update_status('closed', false, user) if topic.closed?
|
|
|
|
end
|
|
|
|
if status_type == TopicTimer.types[:open]
|
|
|
|
topic.update_status('closed', true, user) if topic.open?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def status_type_name
|
|
|
|
self.class.types[status_type]
|
|
|
|
end
|
|
|
|
|
|
|
|
def enqueue_typed_job(time: nil)
|
|
|
|
return if typed_job_scheduled?
|
|
|
|
self.send("schedule_auto_#{status_type_name}_job")
|
|
|
|
end
|
2013-07-04 09:47:12 +08:00
|
|
|
|
2021-01-19 11:30:58 +08:00
|
|
|
# TODO(martin - 2021-05-01) - Remove this backwards compatability for outstanding
|
|
|
|
# jobs once they have all been run and after Jobs::TopicTimerEnqueuer is in place
|
|
|
|
def typed_job_scheduled?
|
|
|
|
scheduled = Jobs.scheduled_for(
|
|
|
|
TopicTimer.type_job_map[status_type_name], topic_timer_id: id
|
|
|
|
).any?
|
|
|
|
|
|
|
|
if [:close, :silent_close, :open].include?(status_type_name)
|
|
|
|
return scheduled || Jobs.scheduled_for(:toggle_topic_closed, topic_timer_id: id).any?
|
2013-07-04 09:47:12 +08:00
|
|
|
end
|
2021-01-19 11:30:58 +08:00
|
|
|
|
|
|
|
scheduled
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.type_job_map
|
|
|
|
{
|
|
|
|
close: :close_topic,
|
|
|
|
open: :open_topic,
|
|
|
|
publish_to_category: :publish_topic_to_category,
|
|
|
|
delete: :delete_topic,
|
|
|
|
reminder: :topic_reminder,
|
|
|
|
bump: :bump_topic,
|
|
|
|
delete_replies: :delete_replies,
|
|
|
|
silent_close: :close_topic,
|
|
|
|
clear_slow_mode: :clear_slow_mode
|
|
|
|
}
|
2013-05-28 09:00:53 +08:00
|
|
|
end
|
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
def self.types
|
|
|
|
@types ||= Enum.new(
|
|
|
|
close: 1,
|
2017-04-03 17:28:41 +08:00
|
|
|
open: 2,
|
2017-05-12 00:52:15 +08:00
|
|
|
publish_to_category: 3,
|
2017-05-17 02:49:42 +08:00
|
|
|
delete: 4,
|
2019-01-04 21:08:04 +08:00
|
|
|
reminder: 5,
|
2020-03-19 23:36:31 +08:00
|
|
|
bump: 6,
|
2020-12-03 07:43:19 +08:00
|
|
|
delete_replies: 7,
|
2020-12-15 01:06:50 +08:00
|
|
|
silent_close: 8,
|
|
|
|
clear_slow_mode: 9
|
2017-03-22 11:12:02 +08:00
|
|
|
)
|
2014-10-30 04:26:32 +08:00
|
|
|
end
|
|
|
|
|
2017-05-17 02:49:42 +08:00
|
|
|
def self.public_types
|
2020-12-15 01:06:50 +08:00
|
|
|
@_public_types ||= types.except(:reminder, :clear_slow_mode)
|
2017-05-17 02:49:42 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.private_types
|
2020-12-15 01:06:50 +08:00
|
|
|
@_private_types ||= types.only(:reminder, :clear_slow_mode)
|
2017-05-17 02:49:42 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def public_type?
|
|
|
|
!!self.class.public_types[self.status_type]
|
|
|
|
end
|
|
|
|
|
|
|
|
def private_type?
|
|
|
|
!!self.class.private_types[self.status_type]
|
|
|
|
end
|
|
|
|
|
2021-01-13 06:49:29 +08:00
|
|
|
def runnable?
|
|
|
|
return false if deleted_at.present?
|
|
|
|
return false if execute_at > Time.zone.now
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
private
|
2013-05-28 09:00:53 +08:00
|
|
|
|
2021-02-12 07:05:14 +08:00
|
|
|
def duration_in_range?
|
|
|
|
return if duration_minutes.blank?
|
|
|
|
|
|
|
|
if duration_minutes.to_i <= 0
|
|
|
|
errors.add(:duration_minutes, I18n.t(
|
|
|
|
'activerecord.errors.models.topic_timer.attributes.duration_minutes.cannot_be_zero'
|
|
|
|
))
|
|
|
|
end
|
|
|
|
|
|
|
|
if duration_minutes.to_i > MAX_DURATION_MINUTES
|
|
|
|
errors.add(:duration_minutes, I18n.t(
|
|
|
|
'activerecord.errors.models.topic_timer.attributes.duration_minutes.exceeds_maximum'
|
|
|
|
))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-13 06:49:29 +08:00
|
|
|
def executed_at_in_future?
|
|
|
|
return if created_at.blank? || (execute_at > created_at)
|
|
|
|
|
|
|
|
errors.add(:execute_at, I18n.t(
|
|
|
|
'activerecord.errors.models.topic_timer.attributes.execute_at.in_the_past'
|
|
|
|
))
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
2013-05-28 09:00:53 +08:00
|
|
|
|
2021-01-19 11:30:58 +08:00
|
|
|
def publishing_to_category?
|
|
|
|
self.status_type.to_i == TopicTimer.types[:publish_to_category]
|
|
|
|
end
|
|
|
|
|
2021-01-13 06:49:29 +08:00
|
|
|
# TODO(martin - 2021-05-01) - Remove cancels for toggle_topic_closed once topic timer revamp completed.
|
2017-03-22 11:12:02 +08:00
|
|
|
def cancel_auto_close_job
|
2017-05-12 06:23:18 +08:00
|
|
|
Jobs.cancel_scheduled_job(:toggle_topic_closed, topic_timer_id: id)
|
2021-01-13 06:49:29 +08:00
|
|
|
Jobs.cancel_scheduled_job(:close_topic, topic_timer_id: id)
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO(martin - 2021-05-01) - Remove cancels for toggle_topic_closed once topic timer revamp completed.
|
|
|
|
def cancel_auto_open_job
|
|
|
|
Jobs.cancel_scheduled_job(:toggle_topic_closed, topic_timer_id: id)
|
|
|
|
Jobs.cancel_scheduled_job(:open_topic, topic_timer_id: id)
|
2013-05-28 09:00:53 +08:00
|
|
|
end
|
|
|
|
|
2021-01-13 06:49:29 +08:00
|
|
|
# TODO(martin - 2021-05-01) - Remove cancels for toggle_topic_closed once topic timer revamp completed.
|
2020-12-03 07:43:19 +08:00
|
|
|
def cancel_auto_silent_close_job
|
|
|
|
Jobs.cancel_scheduled_job(:toggle_topic_closed, topic_timer_id: id)
|
2021-01-13 06:49:29 +08:00
|
|
|
Jobs.cancel_scheduled_job(:close_topic, topic_timer_id: id)
|
2020-12-03 07:43:19 +08:00
|
|
|
end
|
|
|
|
|
2017-04-03 17:28:41 +08:00
|
|
|
def cancel_auto_publish_to_category_job
|
2021-01-19 11:30:58 +08:00
|
|
|
Jobs.cancel_scheduled_job(TopicTimer.type_job_map[:publish_to_category], topic_timer_id: id)
|
2017-04-03 17:28:41 +08:00
|
|
|
end
|
|
|
|
|
2017-05-12 00:52:15 +08:00
|
|
|
def cancel_auto_delete_job
|
2021-01-19 11:30:58 +08:00
|
|
|
Jobs.cancel_scheduled_job(TopicTimer.type_job_map[:delete], topic_timer_id: id)
|
2017-05-12 00:52:15 +08:00
|
|
|
end
|
|
|
|
|
2017-05-17 02:49:42 +08:00
|
|
|
def cancel_auto_reminder_job
|
2021-01-19 11:30:58 +08:00
|
|
|
Jobs.cancel_scheduled_job(TopicTimer.type_job_map[:reminder], topic_timer_id: id)
|
2017-05-17 02:49:42 +08:00
|
|
|
end
|
|
|
|
|
2019-01-04 21:08:04 +08:00
|
|
|
def cancel_auto_bump_job
|
2021-01-19 11:30:58 +08:00
|
|
|
Jobs.cancel_scheduled_job(TopicTimer.type_job_map[:bump], topic_timer_id: id)
|
2019-01-04 21:08:04 +08:00
|
|
|
end
|
|
|
|
|
2020-03-19 23:36:31 +08:00
|
|
|
def cancel_auto_delete_replies_job
|
2021-01-19 11:30:58 +08:00
|
|
|
Jobs.cancel_scheduled_job(TopicTimer.type_job_map[:delete_replies], topic_timer_id: id)
|
2020-03-19 23:36:31 +08:00
|
|
|
end
|
|
|
|
|
2020-12-15 01:06:50 +08:00
|
|
|
def cancel_auto_clear_slow_mode_job
|
2021-01-19 11:30:58 +08:00
|
|
|
Jobs.cancel_scheduled_job(TopicTimer.type_job_map[:clear_slow_mode], topic_timer_id: id)
|
2020-12-15 01:06:50 +08:00
|
|
|
end
|
|
|
|
|
2021-01-19 11:30:58 +08:00
|
|
|
def schedule_auto_delete_replies_job
|
|
|
|
Jobs.enqueue(TopicTimer.type_job_map[:delete_replies], topic_timer_id: id)
|
2020-03-19 23:36:31 +08:00
|
|
|
end
|
|
|
|
|
2021-01-19 11:30:58 +08:00
|
|
|
def schedule_auto_bump_job
|
|
|
|
Jobs.enqueue(TopicTimer.type_job_map[:bump], topic_timer_id: id)
|
2019-01-04 21:08:04 +08:00
|
|
|
end
|
|
|
|
|
2021-01-19 11:30:58 +08:00
|
|
|
def schedule_auto_open_job
|
|
|
|
Jobs.enqueue(TopicTimer.type_job_map[:open], topic_timer_id: id)
|
2013-05-28 09:00:53 +08:00
|
|
|
end
|
|
|
|
|
2021-01-19 11:30:58 +08:00
|
|
|
def schedule_auto_close_job
|
|
|
|
Jobs.enqueue(TopicTimer.type_job_map[:close], topic_timer_id: id)
|
2020-12-03 07:43:19 +08:00
|
|
|
end
|
|
|
|
|
2021-01-19 11:30:58 +08:00
|
|
|
def schedule_auto_silent_close_job
|
|
|
|
Jobs.enqueue(TopicTimer.type_job_map[:close], topic_timer_id: id, silent: true)
|
2013-07-04 04:44:45 +08:00
|
|
|
end
|
2017-04-07 15:26:15 +08:00
|
|
|
|
2021-01-19 11:30:58 +08:00
|
|
|
def schedule_auto_publish_to_category_job
|
|
|
|
Jobs.enqueue(TopicTimer.type_job_map[:publish_to_category], topic_timer_id: id)
|
2017-04-07 15:26:15 +08:00
|
|
|
end
|
2017-05-12 00:52:15 +08:00
|
|
|
|
2021-01-19 11:30:58 +08:00
|
|
|
def schedule_auto_delete_job
|
|
|
|
Jobs.enqueue(TopicTimer.type_job_map[:delete], topic_timer_id: id)
|
2017-05-12 00:52:15 +08:00
|
|
|
end
|
2017-05-17 02:49:42 +08:00
|
|
|
|
2021-01-19 11:30:58 +08:00
|
|
|
def schedule_auto_reminder_job
|
2020-09-14 09:11:55 +08:00
|
|
|
# noop, TODO(martin 2021-03-11): Remove this after timers migrated and outstanding jobs cancelled
|
2017-05-17 02:49:42 +08:00
|
|
|
end
|
2020-12-15 01:06:50 +08:00
|
|
|
|
2021-01-19 11:30:58 +08:00
|
|
|
def schedule_auto_clear_slow_mode_job
|
|
|
|
Jobs.enqueue(TopicTimer.type_job_map[:clear_slow_mode], topic_timer_id: id)
|
2020-12-15 01:06:50 +08:00
|
|
|
end
|
2013-05-28 09:00:53 +08:00
|
|
|
end
|
2017-03-22 11:12:02 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
2017-05-12 06:23:18 +08:00
|
|
|
# Table name: topic_timers
|
2017-03-22 11:12:02 +08:00
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# execute_at :datetime not null
|
|
|
|
# status_type :integer not null
|
|
|
|
# user_id :integer not null
|
|
|
|
# topic_id :integer not null
|
|
|
|
# based_on_last_post :boolean default(FALSE), not null
|
|
|
|
# deleted_at :datetime
|
|
|
|
# deleted_by_id :integer
|
2019-01-12 03:29:56 +08:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2017-04-03 17:28:41 +08:00
|
|
|
# category_id :integer
|
2017-05-26 21:04:54 +08:00
|
|
|
# public_type :boolean default(TRUE)
|
2021-02-05 08:12:56 +08:00
|
|
|
# duration_minutes :integer
|
2017-03-22 11:12:02 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2018-07-16 14:18:07 +08:00
|
|
|
# idx_topic_id_public_type_deleted_at (topic_id) UNIQUE WHERE ((public_type = true) AND (deleted_at IS NULL))
|
2017-05-13 02:46:57 +08:00
|
|
|
# index_topic_timers_on_user_id (user_id)
|
2017-03-22 11:12:02 +08:00
|
|
|
#
|