2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-11 18:23:18 -04:00
|
|
|
class TopicTimer < ActiveRecord::Base
|
2021-02-17 20:52:30 -05:00
|
|
|
MAX_DURATION_MINUTES = 20.years.to_i / 60
|
2021-02-11 18:05:14 -05:00
|
|
|
|
2021-02-04 19:12:56 -05:00
|
|
|
self.ignored_columns = [
|
|
|
|
"duration" # TODO(2021-06-01): remove
|
|
|
|
]
|
|
|
|
|
2017-03-21 23:12:02 -04:00
|
|
|
include Trashable
|
2013-05-27 21:00:53 -04:00
|
|
|
|
2017-03-21 23:12:02 -04:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :topic
|
2017-04-07 05:05:58 -04:00
|
|
|
belongs_to :category
|
2013-05-27 21:00:53 -04:00
|
|
|
|
2017-03-21 23:12:02 -04:00
|
|
|
validates :user_id, presence: true
|
|
|
|
validates :topic_id, presence: true
|
|
|
|
validates :execute_at, presence: true
|
|
|
|
validates :status_type, presence: true
|
2017-05-16 14:49:42 -04: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 03:26:15 -04:00
|
|
|
validates :category_id, presence: true, if: :publishing_to_category?
|
|
|
|
|
2021-01-12 17:49:29 -05:00
|
|
|
validate :executed_at_in_future?
|
2021-02-11 18:05:14 -05:00
|
|
|
validate :duration_in_range?
|
2017-03-21 23:12:02 -04:00
|
|
|
|
2019-11-19 12:10:14 -05:00
|
|
|
scope :scheduled_bump_topics, -> { where(status_type: TopicTimer.types[:bump], deleted_at: nil).pluck(:topic_id) }
|
2021-01-18 22:30:58 -05: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 09:24:18 -05:00
|
|
|
|
2017-03-21 23:12:02 -04:00
|
|
|
before_save do
|
|
|
|
self.created_at ||= Time.zone.now if execute_at
|
2017-05-16 14:49:42 -04:00
|
|
|
self.public_type = self.public_type?
|
2017-03-21 23:12:02 -04:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
if (will_save_change_to_execute_at? &&
|
|
|
|
!attribute_in_database(:execute_at).nil?) ||
|
|
|
|
will_save_change_to_user_id?
|
|
|
|
|
2021-01-18 22:30:58 -05: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-21 23:12:02 -04:00
|
|
|
self.send("cancel_auto_#{self.class.types[status_type]}_job")
|
2016-05-24 14:27:48 -04:00
|
|
|
end
|
2013-05-27 21:00:53 -04:00
|
|
|
end
|
|
|
|
|
2021-01-18 22:30:58 -05: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-21 23:12:02 -04:00
|
|
|
after_save do
|
2017-08-31 00:06:56 -04:00
|
|
|
if (saved_change_to_execute_at? || saved_change_to_user_id?)
|
2021-01-18 22:30:58 -05: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-03 21:47:12 -04:00
|
|
|
|
2021-01-18 22:30:58 -05: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-03 21:47:12 -04:00
|
|
|
end
|
2021-01-18 22:30:58 -05: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-27 21:00:53 -04:00
|
|
|
end
|
|
|
|
|
2017-03-21 23:12:02 -04:00
|
|
|
def self.types
|
|
|
|
@types ||= Enum.new(
|
|
|
|
close: 1,
|
2017-04-03 05:28:41 -04:00
|
|
|
open: 2,
|
2017-05-11 12:52:15 -04:00
|
|
|
publish_to_category: 3,
|
2017-05-16 14:49:42 -04:00
|
|
|
delete: 4,
|
2019-01-04 08:08:04 -05:00
|
|
|
reminder: 5,
|
2020-03-19 11:36:31 -04:00
|
|
|
bump: 6,
|
2020-12-02 18:43:19 -05:00
|
|
|
delete_replies: 7,
|
2020-12-14 12:06:50 -05:00
|
|
|
silent_close: 8,
|
|
|
|
clear_slow_mode: 9
|
2017-03-21 23:12:02 -04:00
|
|
|
)
|
2014-10-29 16:26:32 -04:00
|
|
|
end
|
|
|
|
|
2017-05-16 14:49:42 -04:00
|
|
|
def self.public_types
|
2020-12-14 12:06:50 -05:00
|
|
|
@_public_types ||= types.except(:reminder, :clear_slow_mode)
|
2017-05-16 14:49:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.private_types
|
2020-12-14 12:06:50 -05:00
|
|
|
@_private_types ||= types.only(:reminder, :clear_slow_mode)
|
2017-05-16 14:49:42 -04: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-12 17:49:29 -05:00
|
|
|
def runnable?
|
|
|
|
return false if deleted_at.present?
|
|
|
|
return false if execute_at > Time.zone.now
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-03-21 23:12:02 -04:00
|
|
|
private
|
2013-05-27 21:00:53 -04:00
|
|
|
|
2021-02-11 18:05:14 -05: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-12 17:49:29 -05: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 01:28:18 -04:00
|
|
|
end
|
2013-05-27 21:00:53 -04:00
|
|
|
|
2021-01-18 22:30:58 -05:00
|
|
|
def publishing_to_category?
|
|
|
|
self.status_type.to_i == TopicTimer.types[:publish_to_category]
|
|
|
|
end
|
|
|
|
|
2021-01-12 17:49:29 -05:00
|
|
|
# TODO(martin - 2021-05-01) - Remove cancels for toggle_topic_closed once topic timer revamp completed.
|
2017-03-21 23:12:02 -04:00
|
|
|
def cancel_auto_close_job
|
2017-05-11 18:23:18 -04:00
|
|
|
Jobs.cancel_scheduled_job(:toggle_topic_closed, topic_timer_id: id)
|
2021-01-12 17:49:29 -05: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-27 21:00:53 -04:00
|
|
|
end
|
|
|
|
|
2021-01-12 17:49:29 -05:00
|
|
|
# TODO(martin - 2021-05-01) - Remove cancels for toggle_topic_closed once topic timer revamp completed.
|
2020-12-02 18:43:19 -05:00
|
|
|
def cancel_auto_silent_close_job
|
|
|
|
Jobs.cancel_scheduled_job(:toggle_topic_closed, topic_timer_id: id)
|
2021-01-12 17:49:29 -05:00
|
|
|
Jobs.cancel_scheduled_job(:close_topic, topic_timer_id: id)
|
2020-12-02 18:43:19 -05:00
|
|
|
end
|
|
|
|
|
2017-04-03 05:28:41 -04:00
|
|
|
def cancel_auto_publish_to_category_job
|
2021-01-18 22:30:58 -05:00
|
|
|
Jobs.cancel_scheduled_job(TopicTimer.type_job_map[:publish_to_category], topic_timer_id: id)
|
2017-04-03 05:28:41 -04:00
|
|
|
end
|
|
|
|
|
2017-05-11 12:52:15 -04:00
|
|
|
def cancel_auto_delete_job
|
2021-01-18 22:30:58 -05:00
|
|
|
Jobs.cancel_scheduled_job(TopicTimer.type_job_map[:delete], topic_timer_id: id)
|
2017-05-11 12:52:15 -04:00
|
|
|
end
|
|
|
|
|
2017-05-16 14:49:42 -04:00
|
|
|
def cancel_auto_reminder_job
|
2021-01-18 22:30:58 -05:00
|
|
|
Jobs.cancel_scheduled_job(TopicTimer.type_job_map[:reminder], topic_timer_id: id)
|
2017-05-16 14:49:42 -04:00
|
|
|
end
|
|
|
|
|
2019-01-04 08:08:04 -05:00
|
|
|
def cancel_auto_bump_job
|
2021-01-18 22:30:58 -05:00
|
|
|
Jobs.cancel_scheduled_job(TopicTimer.type_job_map[:bump], topic_timer_id: id)
|
2019-01-04 08:08:04 -05:00
|
|
|
end
|
|
|
|
|
2020-03-19 11:36:31 -04:00
|
|
|
def cancel_auto_delete_replies_job
|
2021-01-18 22:30:58 -05:00
|
|
|
Jobs.cancel_scheduled_job(TopicTimer.type_job_map[:delete_replies], topic_timer_id: id)
|
2020-03-19 11:36:31 -04:00
|
|
|
end
|
|
|
|
|
2020-12-14 12:06:50 -05:00
|
|
|
def cancel_auto_clear_slow_mode_job
|
2021-01-18 22:30:58 -05:00
|
|
|
Jobs.cancel_scheduled_job(TopicTimer.type_job_map[:clear_slow_mode], topic_timer_id: id)
|
2020-12-14 12:06:50 -05:00
|
|
|
end
|
|
|
|
|
2021-01-18 22:30:58 -05:00
|
|
|
def schedule_auto_delete_replies_job
|
|
|
|
Jobs.enqueue(TopicTimer.type_job_map[:delete_replies], topic_timer_id: id)
|
2020-03-19 11:36:31 -04:00
|
|
|
end
|
|
|
|
|
2021-01-18 22:30:58 -05:00
|
|
|
def schedule_auto_bump_job
|
|
|
|
Jobs.enqueue(TopicTimer.type_job_map[:bump], topic_timer_id: id)
|
2019-01-04 08:08:04 -05:00
|
|
|
end
|
|
|
|
|
2021-01-18 22:30:58 -05:00
|
|
|
def schedule_auto_open_job
|
|
|
|
Jobs.enqueue(TopicTimer.type_job_map[:open], topic_timer_id: id)
|
2013-05-27 21:00:53 -04:00
|
|
|
end
|
|
|
|
|
2021-01-18 22:30:58 -05:00
|
|
|
def schedule_auto_close_job
|
|
|
|
Jobs.enqueue(TopicTimer.type_job_map[:close], topic_timer_id: id)
|
2020-12-02 18:43:19 -05:00
|
|
|
end
|
|
|
|
|
2021-01-18 22:30:58 -05:00
|
|
|
def schedule_auto_silent_close_job
|
|
|
|
Jobs.enqueue(TopicTimer.type_job_map[:close], topic_timer_id: id, silent: true)
|
2013-07-03 16:44:45 -04:00
|
|
|
end
|
2017-04-07 03:26:15 -04:00
|
|
|
|
2021-01-18 22:30:58 -05:00
|
|
|
def schedule_auto_publish_to_category_job
|
|
|
|
Jobs.enqueue(TopicTimer.type_job_map[:publish_to_category], topic_timer_id: id)
|
2017-04-07 03:26:15 -04:00
|
|
|
end
|
2017-05-11 12:52:15 -04:00
|
|
|
|
2021-01-18 22:30:58 -05:00
|
|
|
def schedule_auto_delete_job
|
|
|
|
Jobs.enqueue(TopicTimer.type_job_map[:delete], topic_timer_id: id)
|
2017-05-11 12:52:15 -04:00
|
|
|
end
|
2017-05-16 14:49:42 -04:00
|
|
|
|
2021-01-18 22:30:58 -05:00
|
|
|
def schedule_auto_reminder_job
|
2020-09-13 21:11:55 -04:00
|
|
|
# noop, TODO(martin 2021-03-11): Remove this after timers migrated and outstanding jobs cancelled
|
2017-05-16 14:49:42 -04:00
|
|
|
end
|
2020-12-14 12:06:50 -05:00
|
|
|
|
2021-01-18 22:30:58 -05:00
|
|
|
def schedule_auto_clear_slow_mode_job
|
|
|
|
Jobs.enqueue(TopicTimer.type_job_map[:clear_slow_mode], topic_timer_id: id)
|
2020-12-14 12:06:50 -05:00
|
|
|
end
|
2013-05-27 21:00:53 -04:00
|
|
|
end
|
2017-03-21 23:12:02 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
2017-05-11 18:23:18 -04:00
|
|
|
# Table name: topic_timers
|
2017-03-21 23:12:02 -04: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-11 14:29:56 -05:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2017-04-03 05:28:41 -04:00
|
|
|
# category_id :integer
|
2017-05-26 09:04:54 -04:00
|
|
|
# public_type :boolean default(TRUE)
|
2021-02-04 19:12:56 -05:00
|
|
|
# duration_minutes :integer
|
2017-03-21 23:12:02 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2018-07-16 02:18:07 -04:00
|
|
|
# idx_topic_id_public_type_deleted_at (topic_id) UNIQUE WHERE ((public_type = true) AND (deleted_at IS NULL))
|
2017-05-12 14:46:57 -04:00
|
|
|
# index_topic_timers_on_user_id (user_id)
|
2017-03-21 23:12:02 -04:00
|
|
|
#
|