From 079018daad0cc085422a7cf8d1c22c801c562ed5 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 22 Dec 2025 14:10:41 +1100 Subject: [PATCH] FEATURE: when moving posts to a PM also add acting user (#36789) Without acting user being allowed it can be very confusing, and the acting user can just remove themselves from the PM --------- Co-authored-by: Martin Brennan --- app/models/post_mover.rb | 11 +++++++++++ spec/models/post_mover_spec.rb | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/app/models/post_mover.rb b/app/models/post_mover.rb index c27de2c635f..a42b8ce7bb8 100644 --- a/app/models/post_mover.rb +++ b/app/models/post_mover.rb @@ -75,6 +75,7 @@ class PostMover def move_posts_to(topic) Guardian.new(user).ensure_can_see! topic @destination_topic = topic + ensure_acting_user_is_allowed_in_destination # when a topic contains some posts after moving posts to another topic we shouldn't close it # two types of posts should prevent a topic from closing: @@ -130,6 +131,16 @@ class PostMover destination_topic end + def ensure_acting_user_is_allowed_in_destination + return if !@move_to_pm + return if destination_topic.archetype != Archetype.private_message + return if user.id.blank? || user.bot? + return if destination_topic.topic_allowed_users.exists?(user_id: user.id) + + destination_topic.topic_allowed_users.create!(user_id: user.id) + destination_topic.notifier.watch!(user.id) + end + def create_temp_table DB.exec("DROP TABLE IF EXISTS temp_moved_posts") if Rails.env.test? diff --git a/spec/models/post_mover_spec.rb b/spec/models/post_mover_spec.rb index fa263469a91..0a3a761a1fa 100644 --- a/spec/models/post_mover_spec.rb +++ b/spec/models/post_mover_spec.rb @@ -2958,6 +2958,7 @@ RSpec.describe PostMover do end context "with freeze_original option" do + fab!(:admin) fab!(:original_topic, :topic) fab!(:destination_topic, :topic) fab!(:op) { Fabricate(:post, topic: original_topic, raw: "op of original topic") } @@ -3210,6 +3211,22 @@ RSpec.describe PostMover do expect(pm.posts.map(&:raw)).to include(*moving_posts.map(&:raw)) end + it "adds the acting user to the new PM" do + moving_posts = [first_post, second_post] + pm = + PostMover.new( + original_topic, + admin, + moving_posts.map(&:id), + move_to_pm: true, + options: { + freeze_original: true, + }, + ).to_new_topic("Hi I'm a new PM, with a copy of the old posts") + + expect(pm.topic_allowed_users.pluck(:user_id)).to include(admin.id) + end + it "keep all posts when moving to an existing PM" do pm = Fabricate(:private_message_topic) pm_with_posts = Fabricate(:private_message_topic)