mirror of
https://github.com/discourse/discourse.git
synced 2025-05-31 22:09:12 +08:00
FEATURE: TL4 & category moderators can merge posts (#12843)
This gives TL4 users and category moderators the ability to merge posts in topics they have moderation privileges.
This commit is contained in:
@ -9,12 +9,13 @@ class PostMerger
|
||||
end
|
||||
|
||||
def merge
|
||||
return unless ensure_at_least_two_posts
|
||||
return if @posts.count < 2
|
||||
|
||||
ensure_same_topic!
|
||||
ensure_same_user!
|
||||
|
||||
guardian = Guardian.new(@user)
|
||||
ensure_staff_user!(guardian)
|
||||
ensure_can_merge!(guardian)
|
||||
|
||||
posts = @posts.sort_by do |post|
|
||||
guardian.ensure_can_delete!(post)
|
||||
@ -38,29 +39,25 @@ class PostMerger
|
||||
|
||||
private
|
||||
|
||||
def ensure_at_least_two_posts
|
||||
@posts.count >= 2
|
||||
end
|
||||
|
||||
def ensure_same_topic!
|
||||
unless @posts.map(&:topic_id).uniq.length == 1
|
||||
if @posts.map(&:topic_id).uniq.size != 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
|
||||
if @posts.map(&:user_id).uniq.size != 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?
|
||||
def ensure_can_merge!(guardian)
|
||||
raise Discourse::InvalidAccess unless guardian.can_moderate_topic?(@posts[0].topic)
|
||||
end
|
||||
|
||||
def ensure_max_post_length!(raw)
|
||||
value = StrippedLengthValidator.get_sanitized_value(raw)
|
||||
if value.length > SiteSetting.max_post_length
|
||||
if value.size > SiteSetting.max_post_length
|
||||
raise CannotMergeError.new(I18n.t("merge_posts.errors.max_post_length"))
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user