A platform for community discussion. Free, open, simple.
FIX: Moving first post sequentially doesn't bump the destination topic (#35559)
Chris Albertice23fea08604
diff --git a/app/models/post_mover.rb b/app/models/post_mover.rbindex 5655f506845..c27de2c635f 100644--- a/app/models/post_mover.rb+++ b/app/models/post_mover.rb@@ -302,12 +302,8 @@ class PostMovernew_post.custom_fields = post.custom_fieldsnew_post.save_custom_fields- # When freezing original, ensure the notification generated points- # to the newly created post, not the old OP- if @options[:freeze_original]- @post_ids_after_move =- @post_ids_after_move.map { |post_id| post_id == post.id ? new_post.id : post_id }- end+ @post_ids_after_move =+ @post_ids_after_move.map { |post_id| post_id == post.id ? new_post.id : post_id }DiscourseEvent.trigger(:first_post_moved, new_post, post)DiscourseEvent.trigger(:post_moved, new_post, original_topic.id, post)@@ -521,7 +517,7 @@ class PostMoverFROM post_timings ptJOIN temp_moved_posts mp ON (pt.topic_id = mp.old_topic_id AND pt.post_number = mp.old_post_number)WHERE mp.old_post_id <> mp.new_post_id- AND mp.old_post_id IN (:post_ids)+ AND mp.new_post_id IN (:post_ids)ON CONFLICT (topic_id, post_number, user_id) DO UPDATESET msecs = GREATEST(post_timings.msecs, excluded.msecs)SQL@@ -701,7 +697,7 @@ class PostMoverdef update_last_post_statspost = destination_topic.ordered_posts.where.not(post_type: Post.types[:whisper]).last- if post && post_ids.include?(post.id)+ if post && @post_ids_after_move.include?(post.id)attrs = {}attrs[:last_posted_at] = post.created_atattrs[:last_post_user_id] = post.user_iddiff --git a/spec/models/post_mover_spec.rb b/spec/models/post_mover_spec.rbindex 5fe7afb5b57..fa263469a91 100644--- a/spec/models/post_mover_spec.rb+++ b/spec/models/post_mover_spec.rb@@ -162,15 +162,6 @@ RSpec.describe PostMover doTopicLink.extract_from(p2)end- def create_post_timing(post, user, msecs)- PostTiming.create!(- topic_id: post.topic_id,- user_id: user.id,- post_number: post.post_number,- msecs: msecs,- )- end-context "with post replies" dodescribe "when a post with replies is moved" doit "should update post replies correctly" do@@ -1194,6 +1185,16 @@ RSpec.describe PostMover do)end+ fab!(:from_topic_with_newer_op) do+ Fabricate(+ :topic,+ user: user,+ created_at: 1.day.ago,+ bumped_at: 1.day.ago,+ updated_at: 1.day.ago,+ )+ end+fab!(:to_topic) doFabricate(:topic,@@ -1244,7 +1245,17 @@ RSpec.describe PostMover do)end- it "sets bumped_at correctly when first post gets moved" do+ fab!(:from_topic_with_newer_op_p1) do+ Fabricate(+ :post,+ topic: from_topic_with_newer_op,+ user: user,+ created_at: 1.day.ago,+ updated_at: 1.day.ago,+ )+ end++ it "does not change bumped_at when first post gets moved and is older" do# from_p1 was the first post in from_topicto_topic_bumped_at = to_topic.bumped_atmoved_to =@@ -1257,7 +1268,7 @@ RSpec.describe PostMover doexpect(moved_to.bumped_at).to eq_time(to_topic_bumped_at)end- it "sets bumped_at correctly when moved post becomes latest post" do+ it "sets bumped_at to now when moved post becomes latest post" do# from_p2 will be the latest post once movedmoved_to =from_topic.move_posts(@@ -1268,6 +1279,28 @@ RSpec.describe PostMover do)expect(moved_to.bumped_at).to eq_time(Time.zone.now)end++ it "sets bumped_at to now if first post gets moved and is newer (chronological)" do+ moved_to =+ from_topic_with_newer_op.move_posts(+ user,+ [from_topic_with_newer_op_p1.id],+ destination_topic_id: to_topic.id,+ chronological_order: true,+ )+ expect(moved_to.bumped_at).to eq_time(Time.zone.now)+ end++ it "sets bumped_at to now if first post gets moved and is newer (sequential)" do+ moved_to =+ from_topic_with_newer_op.move_posts(+ user,+ [from_topic_with_newer_op_p1.id],+ destination_topic_id: to_topic.id,+ chronological_order: false,+ )+ expect(moved_to.bumped_at).to eq_time(Time.zone.now)+ endendcontext "when moved to a message" do@@ -3063,6 +3096,18 @@ RSpec.describe PostMover doit "keeps posts when moving all posts to a new topic" doall_posts_from_original_topic = original_topic.ordered_posts.map(&:raw)+ create_post_timing(op, Discourse.system_user, 100)+ create_post_timing(first_post, Discourse.system_user, 200)+ create_post_timing(second_post, Discourse.system_user, 300)+ create_post_timing(third_post, Discourse.system_user, 400)++ expect(+ PostTiming.where(topic_id: original_topic.id, user_id: Discourse.system_user.id).pluck(+ :post_number,+ :msecs,+ ),+ ).to contain_exactly([1, 100], [2, 200], [3, 300], [4, 400])+new_topic =PostMover.new(original_topic,@@ -3078,12 +3123,46 @@ RSpec.describe PostMover doexpect(original_topic.posts.map(&:raw)).to include(*all_posts_from_original_topic)expect(new_topic.posts.map(&:raw)).to include(*all_posts_from_original_topic)++ expect(+ PostTiming.where(topic_id: original_topic.id, user_id: Discourse.system_user.id).pluck(+ :post_number,+ :msecs,+ ),+ ).to include([1, 100], [2, 200], [3, 300], [4, 400])++ expect(+ PostTiming.where(topic_id: new_topic.id, user_id: Discourse.system_user.id).pluck(+ :post_number,+ :msecs,+ ),+ ).to contain_exactly([1, 100], [2, 200], [3, 300], [4, 400])endit "does not get deleted when moved all posts to topic" doSiteSetting.delete_merged_stub_topics_after_days = 0all_posts_from_original_topic = original_topic.posts.map(&:raw)+ create_post_timing(op, Discourse.system_user, 100)+ create_post_timing(first_post, Discourse.system_user, 200)+ create_post_timing(second_post, Discourse.system_user, 300)+ create_post_timing(third_post, Discourse.system_user, 400)+ create_post_timing(op_of_destination, Discourse.system_user, 1000)++ expect(+ PostTiming.where(topic_id: original_topic.id, user_id: Discourse.system_user.id).pluck(+ :post_number,+ :msecs,+ ),+ ).to contain_exactly([1, 100], [2, 200], [3, 300], [4, 400])++ expect(+ PostTiming.where(topic_id: destination_topic.id, user_id: Discourse.system_user.id).pluck(+ :post_number,+ :msecs,+ ),+ ).to contain_exactly([1, 1000])+PostMover.new(original_topic,Discourse.system_user,@@ -3098,6 +3177,20 @@ RSpec.describe PostMover doexpect(original_topic.posts.map(&:raw)).to include(*all_posts_from_original_topic)expect(destination_topic.posts.map(&:raw)).to include(*all_posts_from_original_topic)++ expect(+ PostTiming.where(topic_id: original_topic.id, user_id: Discourse.system_user.id).pluck(+ :post_number,+ :msecs,+ ),+ ).to include([1, 100], [2, 200], [3, 300], [4, 400])++ expect(+ PostTiming.where(topic_id: destination_topic.id, user_id: Discourse.system_user.id).pluck(+ :post_number,+ :msecs,+ ),+ ).to contain_exactly([1, 1000], [2, 100], [3, 200], [4, 300], [5, 400])endit "keeps all posts when moving to a new PM" do@@ -3293,5 +3386,14 @@ RSpec.describe PostMover doendendend++ def create_post_timing(post, user, msecs)+ PostTiming.create!(+ topic_id: post.topic_id,+ user_id: user.id,+ post_number: post.post_number,+ msecs: msecs,+ )+ endendend