discourse/app/services/post_owner_changer.rb
Robin Ward b58867b6e9 FEATURE: New 'Reviewable' model to make reviewable items generic
Includes support for flags, reviewable users and queued posts, with REST API
backwards compatibility.

Co-Authored-By: romanrizzi <romanalejandro@gmail.com>
Co-Authored-By: jjaffeux <j.jaffeux@gmail.com>
2019-03-28 12:45:10 -04:00

45 lines
1.3 KiB
Ruby

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