2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-02 11:17:11 -05:00
|
|
|
class PostOwnerChanger
|
|
|
|
def initialize(params)
|
|
|
|
@post_ids = params[:post_ids]
|
2015-07-15 00:15:34 -04:00
|
|
|
@topic = Topic.with_deleted.find_by(id: params[:topic_id].to_i)
|
2015-03-02 11:17:11 -05:00
|
|
|
@new_owner = params[:new_owner]
|
|
|
|
@acting_user = params[:acting_user]
|
2016-08-19 13:13:22 -04:00
|
|
|
@skip_revision = params[:skip_revision] || false
|
2015-03-02 11:17:11 -05:00
|
|
|
|
2023-01-09 07:20:10 -05:00
|
|
|
%i[post_ids topic new_owner acting_user].each do |arg|
|
2022-02-15 20:52:20 -05:00
|
|
|
raise ArgumentError.new(arg) if self.instance_variable_get("@#{arg}").blank?
|
|
|
|
end
|
2015-03-02 11:17:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def change_owner!
|
2018-02-28 17:31:44 -05:00
|
|
|
@post_ids.each do |post_id|
|
2018-05-16 13:48:04 -04:00
|
|
|
next unless post = Post.with_deleted.find_by(id: post_id, topic_id: @topic.id)
|
2015-07-15 18:22:01 -04:00
|
|
|
|
2018-05-16 13:48:04 -04:00
|
|
|
if post.is_first_post?
|
|
|
|
@topic.user = @new_owner
|
|
|
|
@topic.recover! if post.user.nil?
|
2015-03-02 11:17:11 -05:00
|
|
|
end
|
2018-05-16 13:48:04 -04:00
|
|
|
|
2018-02-28 17:31:44 -05:00
|
|
|
post.topic = @topic
|
|
|
|
post.set_owner(@new_owner, @acting_user, @skip_revision)
|
2020-08-19 11:21:02 -04:00
|
|
|
PostActionDestroyer.destroy(@new_owner, post, :like, skip_delete_check: true)
|
2015-03-02 11:17:11 -05:00
|
|
|
|
2018-05-16 13:48:04 -04:00
|
|
|
level = post.is_first_post? ? :watching : :tracking
|
2023-01-09 07:20:10 -05:00
|
|
|
TopicUser.change(
|
|
|
|
@new_owner.id,
|
|
|
|
@topic.id,
|
|
|
|
notification_level: NotificationLevels.topic_levels[level],
|
|
|
|
posted: true,
|
|
|
|
)
|
2018-05-16 13:48:04 -04:00
|
|
|
|
2023-01-09 07:20:10 -05:00
|
|
|
if post ==
|
|
|
|
@topic
|
|
|
|
.posts
|
|
|
|
.order("post_number DESC")
|
|
|
|
.where("NOT hidden AND posts.deleted_at IS NULL")
|
|
|
|
.first
|
2018-05-16 13:48:04 -04:00
|
|
|
@topic.last_poster = @new_owner
|
2018-04-16 05:48:06 -04:00
|
|
|
end
|
|
|
|
|
2015-03-11 15:54:11 -04:00
|
|
|
@topic.update_statistics
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
@new_owner.user_stat.update(
|
2023-01-09 07:20:10 -05:00
|
|
|
first_post_created_at: @new_owner.reload.posts.order("created_at ASC").first&.created_at,
|
2017-08-31 00:06:56 -04:00
|
|
|
)
|
|
|
|
|
2023-01-09 07:20:10 -05:00
|
|
|
Post.where(topic_id: @topic.id, reply_to_post_number: post.post_number).update_all(
|
|
|
|
reply_to_user_id: @new_owner.id,
|
|
|
|
)
|
2021-07-27 14:49:08 -04:00
|
|
|
|
2018-03-01 11:31:58 -05:00
|
|
|
@topic.save!(validate: false)
|
2015-03-11 15:54:11 -04:00
|
|
|
end
|
2015-03-02 11:17:11 -05:00
|
|
|
end
|
|
|
|
end
|