Rename PostTimestampChanger -> TopicTimestampChanger.

This commit is contained in:
Guo Xiang Tan 2017-05-22 15:01:33 +08:00
parent f57914ccd8
commit 4382a0bb07
4 changed files with 30 additions and 20 deletions

View File

@ -558,8 +558,10 @@ class TopicsController < ApplicationController
guardian.ensure_can_change_post_timestamps!
begin
PostTimestampChanger.new( topic_id: params[:topic_id].to_i,
timestamp: params[:timestamp].to_i ).change!
TopicTimestampChanger.new(
topic_id: params[:topic_id].to_i,
timestamp: params[:timestamp].to_i
).change!
render json: success_json
rescue ActiveRecord::RecordInvalid

View File

@ -7,7 +7,7 @@ module Jobs
topic = topic_timer.topic
return if topic.blank?
PostTimestampChanger.new(timestamp: Time.zone.now, topic: topic).change! do
TopicTimestampChanger.new(timestamp: Time.zone.now, topic: topic).change! do
if topic.private_message?
topic = TopicConverter.new(topic, Discourse.system_user)
.convert_to_public_topic(topic_timer.category_id)

View File

@ -1,4 +1,4 @@
class PostTimestampChanger
class TopicTimestampChanger
def initialize(params)
@topic = params[:topic] || Topic.with_deleted.find(params[:topic_id])
@posts = @topic.posts
@ -9,12 +9,14 @@ class PostTimestampChanger
def change!
ActiveRecord::Base.transaction do
last_posted_at = @timestamp
now = Time.zone.now
@posts.each do |post|
if post.is_first_post?
update_post(post, @timestamp)
else
new_created_at = Time.at(post.created_at.to_f + @time_difference)
new_created_at = now if new_created_at > now
last_posted_at = new_created_at if new_created_at > last_posted_at
update_post(post, new_created_at)
end

View File

@ -1,6 +1,6 @@
require 'rails_helper'
describe PostTimestampChanger do
describe TopicTimestampChanger do
describe "change!" do
let(:old_timestamp) { Time.zone.now }
let(:new_timestamp) { old_timestamp + 1.day }
@ -9,25 +9,31 @@ describe PostTimestampChanger do
let!(:p2) { Fabricate(:post, topic: topic, created_at: old_timestamp + 1.day) }
let(:params) { { topic_id: topic.id, timestamp: new_timestamp.to_f } }
it 'changes the timestamp of the topic and opening post' do
PostTimestampChanger.new(params).change!
context 'new timestamp is in the future' do
let(:new_timestamp) { old_timestamp + 2.day }
topic.reload
[:created_at, :updated_at, :bumped_at].each do |column|
expect(topic.public_send(column)).to be_within_one_second_of(new_timestamp)
it 'changes the timestamp of the topic and opening post' do
Timecop.freeze do
TopicTimestampChanger.new(params).change!
topic.reload
[:created_at, :updated_at, :bumped_at].each do |column|
expect(topic.public_send(column)).to be_within_one_second_of(new_timestamp)
end
p1.reload
[:created_at, :updated_at].each do |column|
expect(p1.public_send(column)).to be_within_one_second_of(new_timestamp)
end
expect(topic.last_posted_at).to be_within_one_second_of(p2.reload.created_at)
end
end
p1.reload
[:created_at, :updated_at].each do |column|
expect(p1.public_send(column)).to be_within_one_second_of(new_timestamp)
end
expect(topic.last_posted_at).to be_within_one_second_of(p2.reload.created_at)
end
describe 'predated timestamp' do
it 'updates the timestamp of posts in the topic with the time difference applied' do
PostTimestampChanger.new(params).change!
TopicTimestampChanger.new(params).change!
p2.reload
[:created_at, :updated_at].each do |column|
@ -40,7 +46,7 @@ describe PostTimestampChanger do
let(:new_timestamp) { old_timestamp - 1.day }
it 'updates the timestamp of posts in the topic with the time difference applied' do
PostTimestampChanger.new(params).change!
TopicTimestampChanger.new(params).change!
p2.reload
[:created_at, :updated_at].each do |column|
@ -53,7 +59,7 @@ describe PostTimestampChanger do
$redis.set AdminDashboardData.stats_cache_key, "X"
$redis.set About.stats_cache_key, "X"
PostTimestampChanger.new(params).change!
TopicTimestampChanger.new(params).change!
expect($redis.get(AdminDashboardData.stats_cache_key)).to eq(nil)
expect($redis.get(About.stats_cache_key)).to eq(nil)