FEATURE: automatically delete replies on a topic after N days. (#9209)

This commit is contained in:
Vinoth Kannan
2020-03-19 21:06:31 +05:30
committed by GitHub
parent 0cd502a558
commit aad12822b7
16 changed files with 241 additions and 125 deletions
+11 -6
View File
@@ -1132,8 +1132,8 @@ class Topic < ActiveRecord::Base
# * by_user: User who is setting the topic's status update.
# * 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_timer(status_type, time, by_user: nil, based_on_last_post: false, category_id: SiteSetting.uncategorized_category_id)
return delete_topic_timer(status_type, by_user: by_user) if time.blank?
def set_or_create_timer(status_type, time, by_user: nil, based_on_last_post: false, category_id: SiteSetting.uncategorized_category_id, duration: nil)
return delete_topic_timer(status_type, by_user: by_user) if time.blank? && duration.blank?
public_topic_timer = !!TopicTimer.public_types[status_type]
topic_timer_options = { topic: self, public_type: public_topic_timer }
@@ -1143,19 +1143,24 @@ class Topic < ActiveRecord::Base
time_now = Time.zone.now
topic_timer.based_on_last_post = !based_on_last_post.blank?
topic_timer.duration = duration
if status_type == TopicTimer.types[:publish_to_category]
topic_timer.category = Category.find_by(id: category_id)
end
if topic_timer.based_on_last_post
num_hours = time.to_f
if num_hours > 0
if duration > 0
last_post_created_at = self.ordered_posts.last.present? ? self.ordered_posts.last.created_at : time_now
topic_timer.execute_at = last_post_created_at + num_hours.hours
topic_timer.execute_at = last_post_created_at + duration.hours
topic_timer.created_at = last_post_created_at
end
elsif topic_timer.status_type == TopicTimer.types[:delete_replies]
if duration > 0
first_reply_created_at = (self.ordered_posts.where("post_number > 1").minimum(:created_at) || time_now)
topic_timer.execute_at = first_reply_created_at + duration.days
topic_timer.created_at = first_reply_created_at
end
else
utc = Time.find_zone("UTC")
is_float = (Float(time) rescue nil)
+10 -9
View File
@@ -49,7 +49,8 @@ class TopicTimer < ActiveRecord::Base
publish_to_category: 3,
delete: 4,
reminder: 5,
bump: 6
bump: 6,
delete_replies: 7
)
end
@@ -73,14 +74,6 @@ class TopicTimer < ActiveRecord::Base
end
end
def duration
if (self.execute_at && self.created_at)
((self.execute_at - self.created_at) / 1.hour).round(2)
else
0
end
end
def public_type?
!!self.class.public_types[self.status_type]
end
@@ -120,6 +113,14 @@ class TopicTimer < ActiveRecord::Base
Jobs.cancel_scheduled_job(:bump_topic, topic_timer_id: id)
end
def cancel_auto_delete_replies_job
Jobs.cancel_scheduled_job(:delete_replies, topic_timer_id: id)
end
def schedule_auto_delete_replies_job(time)
Jobs.enqueue_at(time, :delete_replies, topic_timer_id: id)
end
def schedule_auto_bump_job(time)
Jobs.enqueue_at(time, :bump_topic, topic_timer_id: id)
end