2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-22 15:01:33 +08:00
|
|
|
class TopicTimestampChanger
|
2017-05-22 16:03:49 +08:00
|
|
|
class InvalidTimestampError < StandardError; end
|
|
|
|
|
|
|
|
def initialize(timestamp:, topic: nil, topic_id: nil)
|
|
|
|
@topic = topic || Topic.with_deleted.find(topic_id)
|
2015-07-26 00:06:49 +08:00
|
|
|
@posts = @topic.posts
|
2017-05-22 16:03:49 +08:00
|
|
|
@current_timestamp = Time.zone.now
|
|
|
|
@timestamp = Time.zone.at(timestamp)
|
|
|
|
|
|
|
|
raise InvalidTimestampError if @timestamp.to_f > @current_timestamp.to_f
|
|
|
|
|
2015-07-26 00:06:49 +08:00
|
|
|
@time_difference = calculate_time_difference
|
|
|
|
end
|
|
|
|
|
|
|
|
def change!
|
|
|
|
ActiveRecord::Base.transaction do
|
2015-10-20 10:12:52 +08:00
|
|
|
last_posted_at = @timestamp
|
2015-07-26 00:06:49 +08:00
|
|
|
|
|
|
|
@posts.each do |post|
|
|
|
|
if post.is_first_post?
|
|
|
|
update_post(post, @timestamp)
|
|
|
|
else
|
2015-10-20 10:12:52 +08:00
|
|
|
new_created_at = Time.at(post.created_at.to_f + @time_difference)
|
2017-05-22 16:03:49 +08:00
|
|
|
new_created_at = @current_timestamp if new_created_at > @current_timestamp
|
2015-10-20 10:12:52 +08:00
|
|
|
last_posted_at = new_created_at if new_created_at > last_posted_at
|
|
|
|
update_post(post, new_created_at)
|
2015-07-26 00:06:49 +08:00
|
|
|
end
|
|
|
|
end
|
2015-10-20 10:12:52 +08:00
|
|
|
|
2021-07-16 15:56:51 +08:00
|
|
|
@topic.reset_bumped_at
|
2015-10-20 10:12:52 +08:00
|
|
|
update_topic(last_posted_at)
|
2017-04-03 17:28:41 +08:00
|
|
|
|
|
|
|
yield(@topic) if block_given?
|
2015-07-26 00:06:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Burst the cache for stats
|
2019-12-03 17:05:53 +08:00
|
|
|
[AdminDashboardData, About].each { |klass| Discourse.redis.del klass.stats_cache_key }
|
2015-07-26 00:06:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def calculate_time_difference
|
|
|
|
@timestamp - @topic.created_at
|
|
|
|
end
|
|
|
|
|
2015-10-20 10:12:52 +08:00
|
|
|
def update_topic(last_posted_at)
|
2019-04-29 15:32:25 +08:00
|
|
|
@topic.update(
|
2015-07-26 00:06:49 +08:00
|
|
|
created_at: @timestamp,
|
|
|
|
updated_at: @timestamp,
|
2015-10-20 10:12:52 +08:00
|
|
|
last_posted_at: last_posted_at
|
2015-07-26 00:06:49 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_post(post, timestamp)
|
2019-04-29 15:32:25 +08:00
|
|
|
post.update(created_at: timestamp, updated_at: timestamp)
|
2015-07-26 00:06:49 +08:00
|
|
|
end
|
|
|
|
end
|