rename topic_status_update to topic_timer

This commit is contained in:
Neil Lalonde
2017-05-11 18:27:53 -04:00
parent 92d63b59a7
commit 55b61e9bea
39 changed files with 297 additions and 287 deletions
+31 -27
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