2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-12 21:10:47 +08:00

rename topic_status_update to topic_timer

This commit is contained in:
Neil Lalonde 2017-05-11 18:23:18 -04:00
parent 92d63b59a7
commit 55b61e9bea
39 changed files with 297 additions and 287 deletions

View file

@ -202,7 +202,7 @@ SQL
t = Topic.new(title: I18n.t("category.topic_prefix", category: name), user: user, pinned_at: Time.now, category_id: id)
t.skip_callbacks = true
t.ignore_category_auto_close = true
t.set_or_create_status_update(TopicStatusUpdate.types[:close], nil)
t.set_or_create_timer(TopicTimer.types[:close], nil)
t.save!(validate: false)
update_column(:topic_id, t.id)
t.posts.create(raw: post_template, user: user)

View file

@ -521,8 +521,8 @@ SQL
)
)

topic.set_or_create_status_update(
TopicStatusUpdate.types[:open],
topic.set_or_create_timer(
TopicTimer.types[:open],
SiteSetting.num_hours_to_close_topic,
by_user: Discourse.system_user
)

View file

@ -121,7 +121,7 @@ class Topic < ActiveRecord::Base
has_many :topic_links
has_many :topic_invites
has_many :invites, through: :topic_invites, source: :invite
has_many :topic_status_updates, dependent: :destroy
has_many :topic_timers, dependent: :destroy

has_one :user_warning
has_one :first_post, -> {where post_number: 1}, class_name: Post
@ -215,10 +215,10 @@ class Topic < ActiveRecord::Base
if !@ignore_category_auto_close &&
self.category &&
self.category.auto_close_hours &&
!topic_status_update&.execute_at
!topic_timer&.execute_at

self.set_or_create_status_update(
TopicStatusUpdate.types[:close],
self.set_or_create_timer(
TopicTimer.types[:close],
self.category.auto_close_hours,
based_on_last_post: self.category.auto_close_based_on_last_post
)
@ -952,8 +952,12 @@ SQL
Topic.where("pinned_until < now()").update_all(pinned_at: nil, pinned_globally: false, pinned_until: nil)
end

def topic_timer
@topic_timer ||= topic_timers.where('deleted_at IS NULL').first
end

def topic_status_update
@topic_status_update ||= topic_status_updates.where('deleted_at IS NULL').first
topic_timer # will be used to filter timers unrelated to topic status
end

# Valid arguments for the time:
@ -968,31 +972,31 @@ SQL
# * timezone_offset: (Integer) offset from UTC in minutes of the given argument.
# * based_on_last_post: True if time should be based on timestamp of the last post.
# * category_id: Category that the update will apply to.
def set_or_create_status_update(status_type, time, by_user: nil, timezone_offset: 0, based_on_last_post: false, category_id: SiteSetting.uncategorized_category_id)
topic_status_update = TopicStatusUpdate.find_or_initialize_by(
def set_or_create_timer(status_type, time, by_user: nil, timezone_offset: 0, based_on_last_post: false, category_id: SiteSetting.uncategorized_category_id)
topic_timer = TopicTimer.find_or_initialize_by(
status_type: status_type,
topic: self
)

if time.blank?
topic_status_update.trash!(trashed_by: by_user || Discourse.system_user)
topic_timer.trash!(trashed_by: by_user || Discourse.system_user)
return
end

time_now = Time.zone.now
topic_status_update.based_on_last_post = !based_on_last_post.blank?
topic_timer.based_on_last_post = !based_on_last_post.blank?

if status_type == TopicStatusUpdate.types[:publish_to_category]
topic_status_update.category = Category.find_by(id: category_id)
if status_type == TopicTimer.types[:publish_to_category]
topic_timer.category = Category.find_by(id: category_id)
end

if topic_status_update.based_on_last_post
if topic_timer.based_on_last_post
num_hours = time.to_f

if num_hours > 0
last_post_created_at = self.ordered_posts.last.present? ? self.ordered_posts.last.created_at : time_now
topic_status_update.execute_at = last_post_created_at + num_hours.hours
topic_status_update.created_at = last_post_created_at
topic_timer.execute_at = last_post_created_at + num_hours.hours
topic_timer.created_at = last_post_created_at
end
else
utc = Time.find_zone("UTC")
@ -1001,37 +1005,37 @@ SQL

if is_timestamp && m = /^(\d{1,2}):(\d{2})(?:\s*[AP]M)?$/i.match(time.strip)
# a time of day in client's time zone, like "15:00"
topic_status_update.execute_at = utc.local(now.year, now.month, now.day, m[1].to_i, m[2].to_i)
topic_status_update.execute_at += timezone_offset * 60 if timezone_offset
topic_status_update.execute_at += 1.day if topic_status_update.execute_at < now
topic_timer.execute_at = utc.local(now.year, now.month, now.day, m[1].to_i, m[2].to_i)
topic_timer.execute_at += timezone_offset * 60 if timezone_offset
topic_timer.execute_at += 1.day if topic_timer.execute_at < now
elsif is_timestamp && time.include?("-") && timestamp = utc.parse(time)
# a timestamp in client's time zone, like "2015-5-27 12:00"
topic_status_update.execute_at = timestamp
topic_status_update.execute_at += timezone_offset * 60 if timezone_offset
topic_status_update.errors.add(:execute_at, :invalid) if timestamp < now
topic_timer.execute_at = timestamp
topic_timer.execute_at += timezone_offset * 60 if timezone_offset
topic_timer.errors.add(:execute_at, :invalid) if timestamp < now
else
num_hours = time.to_f

if num_hours > 0
topic_status_update.execute_at = num_hours.hours.from_now
topic_timer.execute_at = num_hours.hours.from_now
end
end
end

if topic_status_update.execute_at
if topic_timer.execute_at
if by_user&.staff? || by_user&.trust_level == TrustLevel[4]
topic_status_update.user = by_user
topic_timer.user = by_user
else
topic_status_update.user ||= (self.user.staff? || self.user.trust_level == TrustLevel[4] ? self.user : Discourse.system_user)
topic_timer.user ||= (self.user.staff? || self.user.trust_level == TrustLevel[4] ? self.user : Discourse.system_user)
end

if self.persisted?
topic_status_update.save!
topic_timer.save!
else
self.topic_status_updates << topic_status_update
self.topic_timers << topic_timer
end

topic_status_update
topic_timer
end
end


View file

@ -1,4 +1,4 @@
class TopicStatusUpdate < ActiveRecord::Base
class TopicTimer < ActiveRecord::Base
include Trashable

belongs_to :user
@ -41,12 +41,12 @@ class TopicStatusUpdate < ActiveRecord::Base
end

def self.ensure_consistency!
TopicStatusUpdate.where("topic_status_updates.execute_at < ?", Time.zone.now)
.find_each do |topic_status_update|
TopicTimer.where("topic_timers.execute_at < ?", Time.zone.now)
.find_each do |topic_timer|

topic_status_update.send(
"schedule_auto_#{self.types[topic_status_update.status_type]}_job",
topic_status_update.execute_at
topic_timer.send(
"schedule_auto_#{self.types[topic_timer.status_type]}_job",
topic_timer.execute_at
)
end
end
@ -64,22 +64,22 @@ class TopicStatusUpdate < ActiveRecord::Base
def ensure_update_will_happen
if created_at && (execute_at < created_at)
errors.add(:execute_at, I18n.t(
'activerecord.errors.models.topic_status_update.attributes.execute_at.in_the_past'
'activerecord.errors.models.topic_timer.attributes.execute_at.in_the_past'
))
end
end

def cancel_auto_close_job
Jobs.cancel_scheduled_job(:toggle_topic_closed, topic_status_update_id: id)
Jobs.cancel_scheduled_job(:toggle_topic_closed, topic_timer_id: id)
end
alias_method :cancel_auto_open_job, :cancel_auto_close_job

def cancel_auto_publish_to_category_job
Jobs.cancel_scheduled_job(:publish_topic_to_category, topic_status_update_id: id)
Jobs.cancel_scheduled_job(:publish_topic_to_category, topic_timer_id: id)
end

def cancel_auto_delete_job
Jobs.cancel_scheduled_job(:delete_topic, topic_status_update_id: id)
Jobs.cancel_scheduled_job(:delete_topic, topic_timer_id: id)
end

def schedule_auto_open_job(time)
@ -87,7 +87,7 @@ class TopicStatusUpdate < ActiveRecord::Base
topic.update_status('closed', true, user) if !topic.closed

Jobs.enqueue_at(time, :toggle_topic_closed,
topic_status_update_id: id,
topic_timer_id: id,
state: false
)
end
@ -97,27 +97,27 @@ class TopicStatusUpdate < ActiveRecord::Base
topic.update_status('closed', false, user) if topic.closed

Jobs.enqueue_at(time, :toggle_topic_closed,
topic_status_update_id: id,
topic_timer_id: id,
state: true
)
end

def schedule_auto_publish_to_category_job(time)
Jobs.enqueue_at(time, :publish_topic_to_category, topic_status_update_id: id)
Jobs.enqueue_at(time, :publish_topic_to_category, topic_timer_id: id)
end

def publishing_to_category?
self.status_type.to_i == TopicStatusUpdate.types[:publish_to_category]
self.status_type.to_i == TopicTimer.types[:publish_to_category]
end

def schedule_auto_delete_job(time)
Jobs.enqueue_at(time, :delete_topic, topic_status_update_id: id)
Jobs.enqueue_at(time, :delete_topic, topic_timer_id: id)
end
end

# == Schema Information
#
# Table name: topic_status_updates
# Table name: topic_timers
#
# id :integer not null, primary key
# execute_at :datetime not null