2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-22 07:31:56 +08:00
|
|
|
class PostMerger
|
|
|
|
class CannotMergeError < StandardError; end
|
|
|
|
|
|
|
|
def initialize(user, posts)
|
|
|
|
@user = user
|
|
|
|
@posts = posts
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge
|
|
|
|
return unless ensure_at_least_two_posts
|
|
|
|
ensure_same_topic!
|
|
|
|
ensure_same_user!
|
|
|
|
|
|
|
|
guardian = Guardian.new(@user)
|
|
|
|
ensure_staff_user!(guardian)
|
|
|
|
|
|
|
|
posts = @posts.sort_by do |post|
|
|
|
|
guardian.ensure_can_delete!(post)
|
|
|
|
post.post_number
|
|
|
|
end
|
|
|
|
|
|
|
|
post_content = posts.map(&:raw)
|
|
|
|
post = posts.pop
|
|
|
|
|
2021-04-01 09:16:18 +08:00
|
|
|
merged_post_raw = post_content.join("\n\n")
|
2016-03-22 07:31:56 +08:00
|
|
|
changes = {
|
2021-04-01 09:16:18 +08:00
|
|
|
raw: merged_post_raw,
|
2016-03-22 07:31:56 +08:00
|
|
|
edit_reason: I18n.t("merge_posts.edit_reason", count: posts.length, username: @user.username)
|
|
|
|
}
|
|
|
|
|
2021-04-01 09:16:18 +08:00
|
|
|
ensure_max_post_length!(merged_post_raw)
|
|
|
|
PostRevisor.new(post, post.topic).revise!(@user, changes) do
|
2016-03-22 07:31:56 +08:00
|
|
|
posts.each { |p| PostDestroyer.new(@user, p).destroy }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def ensure_at_least_two_posts
|
|
|
|
@posts.count >= 2
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_same_topic!
|
|
|
|
unless @posts.map(&:topic_id).uniq.length == 1
|
|
|
|
raise CannotMergeError.new(I18n.t("merge_posts.errors.different_topics"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_same_user!
|
|
|
|
unless @posts.map(&:user_id).uniq.length == 1
|
|
|
|
raise CannotMergeError.new(I18n.t("merge_posts.errors.different_users"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_staff_user!(guardian)
|
|
|
|
raise Discourse::InvalidAccess unless guardian.is_staff?
|
|
|
|
end
|
2021-04-01 09:16:18 +08:00
|
|
|
|
|
|
|
def ensure_max_post_length!(raw)
|
|
|
|
value = StrippedLengthValidator.get_sanitized_value(raw)
|
|
|
|
if value.length > SiteSetting.max_post_length
|
|
|
|
raise CannotMergeError.new(I18n.t("merge_posts.errors.max_post_length"))
|
|
|
|
end
|
|
|
|
end
|
2016-03-22 07:31:56 +08:00
|
|
|
end
|