diff --git a/app/models/post_action.rb b/app/models/post_action.rb index 7c0e7e21649..92310d30cde 100644 --- a/app/models/post_action.rb +++ b/app/models/post_action.rb @@ -308,6 +308,19 @@ SQL PostAction.where(where_attrs).first end + def self.copy(original_post, target_post) + cols_to_copy = (column_names - %w{id post_id}).join(', ') + + exec_sql <<~SQL + INSERT INTO post_actions(post_id, #{cols_to_copy}) + SELECT #{target_post.id}, #{cols_to_copy} + FROM post_actions + WHERE post_id = #{original_post.id} + SQL + + target_post.post_actions.each { |post_action| post_action.update_counters } + end + def self.remove_act(user, post, post_action_type_id) limit_action!(user,post,post_action_type_id) diff --git a/app/models/post_mover.rb b/app/models/post_mover.rb index 78a19ba1793..793bfcc59a0 100644 --- a/app/models/post_mover.rb +++ b/app/models/post_mover.rb @@ -85,14 +85,16 @@ class PostMover end def create_first_post(post) - p = PostCreator.create( + new_post = PostCreator.create( post.user, raw: post.raw, topic_id: destination_topic.id, acting_user: user, skip_validations: true ) - p.update_column(:reply_count, @reply_count[1] || 0) + + PostAction.copy(post, new_post) + new_post.update_column(:reply_count, @reply_count[1] || 0) end def move(post) diff --git a/spec/models/post_mover_spec.rb b/spec/models/post_mover_spec.rb index f67122fec49..16d3a4f4fe3 100644 --- a/spec/models/post_mover_spec.rb +++ b/spec/models/post_mover_spec.rb @@ -273,10 +273,7 @@ describe PostMover do new_topic = topic.move_posts(user, [p1.id, p2.id], title: "new testing topic name") expect(new_topic).to be_present - new_topic.posts.reload expect(new_topic.posts.by_post_number.first.raw).to eq(p1.raw) - - new_topic.reload expect(new_topic.posts_count).to eq(2) expect(new_topic.highest_post_number).to eq(2) @@ -284,7 +281,7 @@ describe PostMover do p1.reload expect(p1.sort_order).to eq(1) expect(p1.post_number).to eq(1) - p1.topic_id == topic.id + expect(p1.topic_id).to eq(topic.id) expect(p1.reply_count).to eq(0) # New first post @@ -295,7 +292,7 @@ describe PostMover do p2.reload expect(p2.post_number).to eq(2) expect(p2.sort_order).to eq(2) - p2.topic_id == new_topic.id + expect(p2.topic_id).to eq(new_topic.id) expect(p2.reply_to_post_number).to eq(1) expect(p2.reply_count).to eq(0) @@ -304,6 +301,17 @@ describe PostMover do expect(topic.highest_post_number).to eq(p4.post_number) end + it "preserves post actions in the new post" do + PostAction.act(another_user, p1, PostActionType.types[:like]) + + 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_topic.like_count).to eq(1) + expect(new_post.like_count).to eq(1) + expect(new_post.post_actions.size).to eq(1) + end + end context "to an existing topic with a deleted post" do