discourse/app/services/post_owner_changer.rb
Sam Saffron 30990006a9 DEV: enable frozen string literal on all files
This reduces chances of errors where consumers of strings mutate inputs
and reduces memory usage of the app.

Test suite passes now, but there may be some stuff left, so we will run
a few sites on a branch prior to merging
2019-05-13 09:31:32 +08:00

47 lines
1.4 KiB
Ruby

# frozen_string_literal: true
require_dependency 'post_action_destroyer'
class PostOwnerChanger
def initialize(params)
@post_ids = params[:post_ids]
@topic = Topic.with_deleted.find_by(id: params[:topic_id].to_i)
@new_owner = params[:new_owner]
@acting_user = params[:acting_user]
@skip_revision = params[:skip_revision] || false
raise ArgumentError unless @post_ids && @topic && @new_owner && @acting_user
end
def change_owner!
@post_ids.each do |post_id|
next unless post = Post.with_deleted.find_by(id: post_id, topic_id: @topic.id)
if post.is_first_post?
@topic.user = @new_owner
@topic.recover! if post.user.nil?
end
post.topic = @topic
post.set_owner(@new_owner, @acting_user, @skip_revision)
PostActionDestroyer.destroy(@new_owner, post, :like)
level = post.is_first_post? ? :watching : :tracking
TopicUser.change(@new_owner.id, @topic.id, notification_level: NotificationLevels.topic_levels[level])
if post == @topic.posts.order("post_number DESC").where("NOT hidden AND posts.deleted_at IS NULL").first
@topic.last_poster = @new_owner
end
@topic.update_statistics
@new_owner.user_stat.update(
first_post_created_at: @new_owner.reload.posts.order('created_at ASC').first&.created_at
)
@topic.save!(validate: false)
end
end
end