FIX: keep first post edit history when moving/merging (#22966)

This commit is contained in:
Renato Atilio 2023-08-03 22:04:35 -03:00 committed by GitHub
parent c91409e6fd
commit 701ae8764e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 1 deletions

View File

@ -260,7 +260,13 @@ class PostMover
PostAction.copy(post, new_post)
attrs_to_update = { reply_count: @reply_count[1] || 0 }
PostRevision.copy(post, new_post)
attrs_to_update = {
reply_count: @reply_count[1] || 0,
version: post.version,
public_version: post.public_version,
}
if new_post.post_number != @move_map[post.post_number]
attrs_to_update[:post_number] = @move_map[post.post_number]

View File

@ -39,6 +39,17 @@ class PostRevision < ActiveRecord::Base
def create_notification
PostActionNotifier.after_create_post_revision(self)
end
def self.copy(original_post, target_post)
cols_to_copy = (column_names - %w[id post_id]).join(", ")
DB.exec <<~SQL
INSERT INTO post_revisions(post_id, #{cols_to_copy})
SELECT #{target_post.id}, #{cols_to_copy}
FROM post_revisions
WHERE post_id = #{original_post.id}
SQL
end
end
# == Schema Information

View File

@ -875,6 +875,18 @@ RSpec.describe PostMover do
expect(TopicUser.find_by(topic: destination_topic, user: user).liked).to eq(true)
end
it "copies the post revisions from first post to the new post" do
p1.revise(another_user, { raw: "A different raw content" })
moved_to = topic.move_posts(user, [p1.id], destination_topic_id: destination_topic.id)
new_post = moved_to.posts.last
expect(new_post.id).not_to eq(p1.id)
expect(new_post.version).to eq(2)
expect(new_post.public_version).to eq(2)
expect(new_post.post_revisions.size).to eq(1)
end
context "with read state and other stats per user" do
def create_topic_user(user, topic, opts = {})
notification_level = opts.delete(:notification_level) || :regular
@ -1223,6 +1235,18 @@ RSpec.describe PostMover do
expect(new_topic.first_post.custom_fields).to eq(custom_fields)
end
it "preserves the post revisions in the new post" do
p1.revise(another_user, { raw: "A different raw content" })
new_topic = topic.move_posts(user, [p1.id], title: "new testing topic name")
new_post = new_topic.posts.where(post_number: 1).first
expect(new_post.id).not_to eq(p1.id)
expect(new_post.version).to eq(2)
expect(new_post.public_version).to eq(2)
expect(new_post.post_revisions.size).to eq(1)
end
include_examples "moves email related stuff" do
let!(:old_post) { p1 }
end