2013-05-26 08:40:33 +08:00
|
|
|
class PostMover
|
|
|
|
attr_reader :original_topic, :destination_topic, :user, :post_ids
|
|
|
|
|
2013-07-03 03:47:15 +08:00
|
|
|
def self.move_types
|
|
|
|
@move_types ||= Enum.new(:new_topic, :existing_topic)
|
|
|
|
end
|
|
|
|
|
2013-05-26 08:40:33 +08:00
|
|
|
def initialize(original_topic, user, post_ids)
|
|
|
|
@original_topic = original_topic
|
|
|
|
@user = user
|
|
|
|
@post_ids = post_ids
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_topic(id)
|
2013-07-03 03:47:15 +08:00
|
|
|
@move_type = PostMover.move_types[:existing_topic]
|
|
|
|
|
2013-05-26 08:40:33 +08:00
|
|
|
Topic.transaction do
|
|
|
|
move_posts_to Topic.find_by_id(id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def to_new_topic(title, category_id = nil)
|
2013-07-03 03:47:15 +08:00
|
|
|
@move_type = PostMover.move_types[:new_topic]
|
|
|
|
|
2015-11-07 12:17:47 +08:00
|
|
|
post = Post.find_by(id: post_ids.first)
|
|
|
|
raise Discourse::InvalidParameters unless post
|
|
|
|
|
2013-05-26 08:40:33 +08:00
|
|
|
Topic.transaction do
|
|
|
|
move_posts_to Topic.create!(
|
2015-11-07 12:17:47 +08:00
|
|
|
user: post.user,
|
2013-05-26 08:40:33 +08:00
|
|
|
title: title,
|
2015-11-07 12:17:47 +08:00
|
|
|
category_id: category_id,
|
|
|
|
created_at: post.created_at
|
2013-05-26 08:40:33 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def move_posts_to(topic)
|
|
|
|
Guardian.new(user).ensure_can_see! topic
|
|
|
|
@destination_topic = topic
|
|
|
|
|
2015-12-30 05:01:49 +08:00
|
|
|
moving_all_posts = (@original_topic.posts.pluck(:id).sort == @post_ids.sort)
|
|
|
|
|
2013-05-26 08:40:33 +08:00
|
|
|
move_each_post
|
|
|
|
notify_users_that_posts_have_moved
|
|
|
|
update_statistics
|
2013-07-17 14:40:15 +08:00
|
|
|
update_user_actions
|
2014-10-17 03:38:26 +08:00
|
|
|
set_last_post_user_id(destination_topic)
|
2013-07-17 14:40:15 +08:00
|
|
|
|
2015-12-30 05:01:49 +08:00
|
|
|
if moving_all_posts
|
|
|
|
@original_topic.update_status('closed', true, @user)
|
|
|
|
end
|
|
|
|
|
2015-04-06 15:27:05 +08:00
|
|
|
destination_topic.reload
|
2013-07-17 14:40:15 +08:00
|
|
|
destination_topic
|
2013-05-26 08:40:33 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def move_each_post
|
2014-08-21 02:14:56 +08:00
|
|
|
max_post_number = destination_topic.max_post_number + 1
|
|
|
|
|
|
|
|
@move_map = {}
|
|
|
|
@reply_count = {}
|
|
|
|
posts.each_with_index do |post, offset|
|
|
|
|
unless post.is_first_post?
|
|
|
|
@move_map[post.post_number] = offset + max_post_number
|
|
|
|
else
|
|
|
|
@move_map[post.post_number] = 1
|
2013-05-26 08:40:33 +08:00
|
|
|
end
|
2014-08-21 02:14:56 +08:00
|
|
|
if post.reply_to_post_number.present?
|
|
|
|
@reply_count[post.reply_to_post_number] = (@reply_count[post.reply_to_post_number] || 0) + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
posts.each do |post|
|
|
|
|
post.is_first_post? ? create_first_post(post) : move(post)
|
2013-05-26 08:40:33 +08:00
|
|
|
end
|
2016-07-13 23:34:21 +08:00
|
|
|
|
2017-04-25 05:14:01 +08:00
|
|
|
PostReply.where("reply_id IN (:post_ids) OR post_id IN (:post_ids)", post_ids: post_ids).each do |post_reply|
|
2016-08-04 23:49:44 +08:00
|
|
|
if post_reply.post && post_reply.reply && post_reply.reply.topic_id != post_reply.post.topic_id
|
2016-07-13 23:34:21 +08:00
|
|
|
PostReply.delete_all(reply_id: post_reply.reply.id, post_id: post_reply.post.id)
|
|
|
|
end
|
|
|
|
end
|
2013-05-26 08:40:33 +08:00
|
|
|
end
|
|
|
|
|
2014-08-21 02:14:56 +08:00
|
|
|
def create_first_post(post)
|
2017-02-10 22:35:04 +08:00
|
|
|
new_post = PostCreator.create(
|
2013-05-26 08:40:33 +08:00
|
|
|
post.user,
|
|
|
|
raw: post.raw,
|
|
|
|
topic_id: destination_topic.id,
|
2016-07-06 04:48:14 +08:00
|
|
|
acting_user: user,
|
2017-07-03 23:51:20 +08:00
|
|
|
skip_validations: true,
|
|
|
|
guardian: Guardian.new(user)
|
2013-05-26 08:40:33 +08:00
|
|
|
)
|
2017-02-10 22:35:04 +08:00
|
|
|
|
|
|
|
PostAction.copy(post, new_post)
|
|
|
|
new_post.update_column(:reply_count, @reply_count[1] || 0)
|
2017-06-07 18:04:48 +08:00
|
|
|
new_post.custom_fields = post.custom_fields
|
|
|
|
new_post.save_custom_fields
|
2017-08-12 10:12:09 +08:00
|
|
|
|
|
|
|
DiscourseEvent.trigger(:post_moved, new_post, original_topic.id)
|
|
|
|
|
2017-06-07 18:04:48 +08:00
|
|
|
new_post
|
2013-05-26 08:40:33 +08:00
|
|
|
end
|
|
|
|
|
2014-08-21 02:14:56 +08:00
|
|
|
def move(post)
|
2013-05-26 08:40:33 +08:00
|
|
|
@first_post_number_moved ||= post.post_number
|
|
|
|
|
2017-06-02 06:00:04 +08:00
|
|
|
update = {
|
|
|
|
reply_count: @reply_count[post.post_number] || 0,
|
|
|
|
post_number: @move_map[post.post_number],
|
|
|
|
reply_to_post_number: @move_map[post.reply_to_post_number],
|
|
|
|
topic_id: destination_topic.id,
|
|
|
|
sort_order: @move_map[post.post_number]
|
|
|
|
}
|
|
|
|
|
|
|
|
unless @move_map[post.reply_to_post_number]
|
|
|
|
update[:reply_to_user_id] = nil
|
|
|
|
end
|
|
|
|
|
2017-08-12 10:12:09 +08:00
|
|
|
post.update(update)
|
|
|
|
|
|
|
|
DiscourseEvent.trigger(:post_moved, post, original_topic.id)
|
2013-07-03 04:42:25 +08:00
|
|
|
|
|
|
|
# Move any links from the post to the new topic
|
|
|
|
post.topic_links.update_all(topic_id: destination_topic.id)
|
2013-05-26 08:40:33 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_statistics
|
|
|
|
destination_topic.update_statistics
|
|
|
|
original_topic.update_statistics
|
2016-03-16 15:15:54 +08:00
|
|
|
TopicUser.update_post_action_cache(topic_id: original_topic.id, post_action_type: :bookmark)
|
|
|
|
TopicUser.update_post_action_cache(topic_id: destination_topic.id, post_action_type: :bookmark)
|
2013-05-26 08:40:33 +08:00
|
|
|
end
|
|
|
|
|
2013-07-17 14:40:15 +08:00
|
|
|
def update_user_actions
|
|
|
|
UserAction.synchronize_target_topic_ids(posts.map(&:id))
|
|
|
|
end
|
|
|
|
|
2013-05-26 08:40:33 +08:00
|
|
|
def notify_users_that_posts_have_moved
|
|
|
|
enqueue_notification_job
|
|
|
|
create_moderator_post_in_original_topic
|
|
|
|
end
|
|
|
|
|
|
|
|
def enqueue_notification_job
|
|
|
|
Jobs.enqueue(
|
|
|
|
:notify_moved_posts,
|
|
|
|
post_ids: post_ids,
|
|
|
|
moved_by_id: user.id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_moderator_post_in_original_topic
|
2015-08-01 04:30:18 +08:00
|
|
|
move_type_str = PostMover.move_types[@move_type].to_s
|
|
|
|
|
2017-09-14 21:52:09 +08:00
|
|
|
message = I18n.with_locale(SiteSetting.default_locale) do
|
2015-08-01 04:30:18 +08:00
|
|
|
I18n.t("move_posts.#{move_type_str}_moderator_post",
|
2013-07-03 03:47:15 +08:00
|
|
|
count: post_ids.count,
|
2017-09-14 21:52:09 +08:00
|
|
|
topic_link: "[#{destination_topic.title}](#{destination_topic.relative_url})")
|
|
|
|
end
|
|
|
|
|
|
|
|
original_topic.add_moderator_post(
|
|
|
|
user, message,
|
2015-08-01 04:30:18 +08:00
|
|
|
post_type: Post.types[:small_action],
|
|
|
|
action_code: "split_topic",
|
2013-05-26 08:40:33 +08:00
|
|
|
post_number: @first_post_number_moved
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def posts
|
|
|
|
@posts ||= begin
|
2017-03-06 15:04:10 +08:00
|
|
|
Post.where(topic: @original_topic, id: post_ids).order(:created_at).tap do |posts|
|
2013-05-26 08:40:33 +08:00
|
|
|
raise Discourse::InvalidParameters.new(:post_ids) if posts.empty?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-10-17 03:38:26 +08:00
|
|
|
|
|
|
|
def set_last_post_user_id(topic)
|
|
|
|
user_id = topic.posts.last.user_id rescue nil
|
|
|
|
return if user_id.nil?
|
|
|
|
topic.update_attribute :last_post_user_id, user_id
|
|
|
|
end
|
2013-05-26 08:40:33 +08:00
|
|
|
end
|