mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: Add freeze_original option to PostMover (#29880)
* 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. * DEV: update tests to check raw instead of ids * DEV: Implement `freeze_original` option for `PostMover` update specs to use `*array` matcher * DEV: add tests to `MovedPost` model in post mover * DEV: Update `MovedPost` model rspec * DEV: add back empty line to `post_mover.rb`
This commit is contained in:
@@ -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)
|
||||
@@ -90,6 +93,14 @@ class PostMover
|
||||
@first_post_number_moved =
|
||||
posts.first.is_first_post? ? posts[1]&.post_number : posts.first.post_number
|
||||
|
||||
if @options[:freeze_original] # in this case we need to add the moderator post after the last copied post
|
||||
from_posts = @original_topic.posts.where("post_number > ?", posts.last.post_number)
|
||||
|
||||
shift_post_numbers(from_posts) if !moving_all_posts
|
||||
|
||||
@first_post_number_moved = posts.last.post_number + 1
|
||||
end
|
||||
|
||||
move_each_post
|
||||
handle_moved_references
|
||||
|
||||
@@ -281,15 +292,22 @@ class PostMover
|
||||
|
||||
update[:reply_to_user_id] = nil unless @move_map[post.reply_to_post_number]
|
||||
|
||||
post.attributes = update
|
||||
post.save(validate: false)
|
||||
moved_post =
|
||||
if @options[:freeze_original]
|
||||
post.dup
|
||||
else
|
||||
post
|
||||
end
|
||||
|
||||
DiscourseEvent.trigger(:post_moved, post, original_topic.id)
|
||||
moved_post.attributes = update
|
||||
moved_post.save(validate: false)
|
||||
|
||||
DiscourseEvent.trigger(:post_moved, moved_post, original_topic.id)
|
||||
|
||||
# Move any links from the post to the new topic
|
||||
post.topic_links.update_all(topic_id: destination_topic.id)
|
||||
moved_post.topic_links.update_all(topic_id: destination_topic.id)
|
||||
|
||||
post
|
||||
moved_post
|
||||
end
|
||||
|
||||
def move_same_topic(post)
|
||||
@@ -331,6 +349,10 @@ class PostMover
|
||||
SQL
|
||||
end
|
||||
|
||||
def shift_post_numbers(from_posts)
|
||||
from_posts.each { |post| post.update_columns(post_number: post.post_number + 1) }
|
||||
end
|
||||
|
||||
def move_incoming_emails
|
||||
DB.exec <<~SQL
|
||||
UPDATE incoming_emails ie
|
||||
@@ -683,6 +705,7 @@ class PostMover
|
||||
|
||||
def close_topic_and_schedule_deletion
|
||||
@original_topic.update_status("closed", true, @user)
|
||||
return if @options[:freeze_original] # we only close the topic when freezing it
|
||||
|
||||
days_to_deleting = SiteSetting.delete_merged_stub_topics_after_days
|
||||
if days_to_deleting == 0
|
||||
|
||||
Reference in New Issue
Block a user