A platform for community discussion. Free, open, simple.
DEV: add full_move to MovedPost record small_action modifier (#30236)
Mark VanLandinghambbb31b05ca2b
diff --git a/app/models/moved_post.rb b/app/models/moved_post.rbindex 21f34e52f0b..fb621e8bd0c 100644--- a/app/models/moved_post.rb+++ b/app/models/moved_post.rb@@ -32,6 +32,7 @@ end# old_topic_title :string# post_user_id :integer# user_id :integer+# full_move :boolean## Indexes#diff --git a/app/models/post_mover.rb b/app/models/post_mover.rbindex 555b460a4ae..461be590174 100644--- a/app/models/post_mover.rb+++ b/app/models/post_mover.rb@@ -92,14 +92,14 @@ class PostMoverPost.types[:whisper],).count- moving_all_posts = original_topic_posts_count == posts.length+ @full_move = original_topic_posts_count == posts.length@first_post_number_moved =posts.first.is_first_post? ? posts[1]&.post_number : posts.first.post_numberif @options[:freeze_original] # in this case we need to add the moderator post after the last copied postfrom_posts = @original_topic.ordered_posts.where("post_number > ?", posts.last.post_number)- shift_post_numbers(from_posts) if !moving_all_posts+ shift_post_numbers(from_posts) if !@full_move@first_post_number_moved = posts.last.post_number + 1end@@ -114,7 +114,7 @@ class PostMoverupdate_upload_security_statusupdate_bookmarks- close_topic_and_schedule_deletion if moving_all_posts+ close_topic_and_schedule_deletion if @full_movedestination_topic.reloadDiscourseEvent.trigger(@@ -366,10 +366,11 @@ class PostMovermetadata[:created_new_topic] = @creating_new_topicmetadata[:old_topic_title] = @original_topic_titlemetadata[:user_id] = @user.id+ metadata[:full_move] = @full_moveDB.exec(<<~SQL, metadata)- INSERT INTO moved_posts(old_topic_id, old_topic_title, old_post_id, old_post_number, post_user_id, user_id, new_topic_id, new_topic_title, new_post_id, new_post_number, created_new_topic, created_at, updated_at)- VALUES (:old_topic_id, :old_topic_title, :old_post_id, :old_post_number, :post_user_id, :user_id, :new_topic_id, :new_topic_title, :new_post_id, :new_post_number, :created_new_topic, :now, :now)+ INSERT INTO moved_posts(old_topic_id, old_topic_title, old_post_id, old_post_number, post_user_id, user_id, full_move, new_topic_id, new_topic_title, new_post_id, new_post_number, created_new_topic, created_at, updated_at)+ VALUES (:old_topic_id, :old_topic_title, :old_post_id, :old_post_number, :post_user_id, :user_id, :full_move, :new_topic_id, :new_topic_title, :new_post_id, :new_post_number, :created_new_topic, :now, :now)SQLend@@ -613,23 +614,38 @@ class PostMovermove_type_str = PostMover.move_types[@move_type].to_smove_type_str.sub!("topic", "message") if @move_to_pm+ topic_link =+ if posts.first.is_first_post?+ "[#{destination_topic.title}](#{destination_topic.relative_url})"+ else+ "[#{destination_topic.title}](#{posts.first.relative_url})"+ end++ post_type = @move_to_pm ? Post.types[:whisper] : Post.types[:small_action]++ continue =+ DiscoursePluginRegistry.apply_modifier(+ :post_mover_create_moderator_post,+ true,+ user: user,+ post_type: post_type,+ post_ids: @post_ids_after_move,+ full_move: @full_move,+ original_topic: original_topic,+ destination_topic: destination_topic,+ first_post_number_moved: @first_post_number_moved,+ )+ return if !continue+message =I18n.with_locale(SiteSetting.default_locale) doI18n.t("move_posts.#{move_type_str}_moderator_post",count: posts.length,- topic_link:- (- if posts.first.is_first_post?- "[#{destination_topic.title}](#{destination_topic.relative_url})"- else- "[#{destination_topic.title}](#{posts.first.relative_url})"- end- ),+ topic_link: topic_link,)end- post_type = @move_to_pm ? Post.types[:whisper] : Post.types[:small_action]original_topic.add_moderator_post(user,message,diff --git a/db/migrate/20241211222608_add_full_move_to_moved_post_records.rb b/db/migrate/20241211222608_add_full_move_to_moved_post_records.rbnew file mode 100644index 00000000000..1650f2b1180--- /dev/null+++ b/db/migrate/20241211222608_add_full_move_to_moved_post_records.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++class AddFullMoveToMovedPostRecords < ActiveRecord::Migration[7.2]+ def change+ add_column :moved_posts, :full_move, :boolean+ end+enddiff --git a/spec/models/post_mover_spec.rb b/spec/models/post_mover_spec.rbindex 9002006eb2d..c98bbeb3280 100644--- a/spec/models/post_mover_spec.rb+++ b/spec/models/post_mover_spec.rb@@ -351,6 +351,42 @@ RSpec.describe PostMover doexpect(topic.closed).to eq(true)end+ it "records full_move=true in MovedPost records when all posts are moved" do+ post_ids = [p1.id, p2.id, p3.id, p4.id]+ new_topic =+ topic.move_posts(+ user,+ [p1.id, p2.id, p3.id, p4.id],+ title: "new testing topic name",+ category_id: category.id,+ )+ expect(+ MovedPost.where(+ old_post_id: post_ids,+ new_topic_id: new_topic.id,+ full_move: true,+ ).count,+ ).to eq(4)+ end++ it "records full_move=false in MovedPost records when only some posts" do+ post_ids = [p3.id, p4.id]+ new_topic =+ topic.move_posts(+ user,+ [p3.id, p4.id],+ title: "new testing topic name",+ category_id: category.id,+ )+ expect(+ MovedPost.where(+ old_post_id: post_ids,+ new_topic_id: new_topic.id,+ full_move: false,+ ).count,+ ).to eq(2)+ end+it "does not move posts that do not belong to the existing topic" donew_topic =topic.move_posts(user, [p2.id, p3.id, p5.id], title: "Logan is a pretty good movie")@@ -2854,6 +2890,43 @@ RSpec.describe PostMover doexpect(moderator_post.action_code).to eq("split_topic")end+ context "with `post_mover_create_moderator_post` modifier" do+ fab!(:topic_1) { Fabricate(:topic) }+ fab!(:topic_2) { Fabricate(:topic) }+ fab!(:post_1) { Fabricate(:post, topic: topic_1) }+ fab!(:user)++ before { SiteSetting.delete_merged_stub_topics_after_days = 0 }+ let(:modifier_block) { Proc.new { |continue, _| false } }++ it "does not create small action post when modifier returns false" do+ plugin_instance = Plugin::Instance.new+ plugin_instance.register_modifier(:post_mover_create_moderator_post, &modifier_block)++ expect {+ PostMover.new(+ original_topic,+ Discourse.system_user,+ [first_post.id, second_post.id],+ options: {+ freeze_original: true,+ },+ ).to_topic(destination_topic.id)+ }.not_to change {+ original_topic+ .posts+ .where(post_type: Post.types[:small_action], action_code: "split_topic")+ .count+ }+ ensure+ DiscoursePluginRegistry.unregister_modifier(+ plugin_instance,+ :post_mover_create_moderator_post,+ &modifier_block+ )+ end+ end+it "keeps posts when moving all posts to a new topic" doall_posts_from_original_topic = original_topic.ordered_posts.map(&:raw)