FEATURE: Add freeze_original option to PostMover

This option will allow the api user to specify if the original topic should be `frozen`(locked and posts not deleted neither moved)

With this option when moving topic posts your posts will be `copied` to the new topic and the original topic will be kept there.
This commit is contained in:
Gabriel Grubba 2024-11-21 15:34:55 -03:00
parent 250a145361
commit b82a12def7
No known key found for this signature in database
GPG Key ID: 5FE41764F62D556C
2 changed files with 104 additions and 1 deletions

View File

@ -7,11 +7,14 @@ class PostMover
@move_types ||= Enum.new(:new_topic, :existing_topic)
end
def initialize(original_topic, user, post_ids, move_to_pm: false)
# options:
# freeze_original: :boolean - if true, the original topic will be frozen but not deleted and posts will be "copied" to topic
def initialize(original_topic, user, post_ids, move_to_pm: false, options: {})
@original_topic = original_topic
@user = user
@post_ids = post_ids
@move_to_pm = move_to_pm
@options = options
end
def to_topic(id, participants: nil, chronological_order: false)

View File

@ -2637,5 +2637,105 @@ RSpec.describe PostMover do
expect(topic_1.deleted_at).not_to be_nil
end
end
context "with freeze_original option" do
fab!(:original_topic) { Fabricate(:topic) }
fab!(:destination_topic) { Fabricate(:topic) }
fab!(:post_1) { Fabricate(:post, topic: original_topic) }
fab!(:post_2) { Fabricate(:post, topic: original_topic) }
it "keeps all posts when moving to a new topic" do
new_topic =
PostMover.new(
original_topic,
Discourse.system_user,
[post_1.id, post_2.id],
options: {
freeze_original: true,
},
).to_new_topic("Hi I'm a new topic, with a copy of the old posts")
expect(original_topic.posts.count).to eq(2)
expect(new_topic.posts.count).to eq(2)
end
it "does not get deleted when moved all posts to topic" do
SiteSetting.delete_merged_stub_topics_after_days = 0
PostMover.new(
original_topic,
Discourse.system_user,
[post_1.id, post_2.id],
options: {
freeze_original: true,
},
).to_topic(destination_topic.id)
expect(original_topic.deleted_at).to be_nil
end
it "keeps all posts when moving to an existing topic" do
PostMover.new(
original_topic,
Discourse.system_user,
[post_1.id, post_2.id],
options: {
freeze_original: true,
},
).to_topic(destination_topic.id)
expect(original_topic.posts.count).to eq(2)
expect(destination_topic.posts.count).to eq(2)
end
it "lets you move multiple times to the same topic" do
mover =
PostMover.new(
original_topic,
Discourse.system_user,
[post_1.id, post_2.id],
options: {
freeze_original: true,
},
)
mover.to_topic(destination_topic.id)
mover.to_topic(destination_topic.id)
expect(original_topic.posts.count).to eq(2)
expect(destination_topic.posts.count).to eq(4)
end
it "keeps all posts when moving to a new PM" do
pm =
PostMover.new(
original_topic,
Discourse.system_user,
[post_1.id, post_2.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(original_topic.posts.count).to eq(2)
expect(pm.posts.count).to eq(2)
end
it "keep all posts when moving to an existing PM" do
pm = Fabricate(:private_message_topic)
PostMover.new(
original_topic,
Discourse.system_user,
[post_1.id, post_2.id],
move_to_pm: true,
options: {
freeze_original: true,
},
).to_topic(pm.id)
expect(original_topic.posts.count).to eq(2)
expect(pm.posts.count).to eq(2)
end
end
end
end