2015-03-03 00:17:11 +08:00
|
|
|
class PostOwnerChanger
|
|
|
|
|
|
|
|
def initialize(params)
|
|
|
|
@post_ids = params[:post_ids]
|
2015-07-15 12:15:34 +08:00
|
|
|
@topic = Topic.with_deleted.find_by(id: params[:topic_id].to_i)
|
2015-03-03 00:17:11 +08:00
|
|
|
@new_owner = params[:new_owner]
|
|
|
|
@acting_user = params[:acting_user]
|
2016-08-20 01:13:22 +08:00
|
|
|
@skip_revision = params[:skip_revision] || false
|
2015-03-03 00:17:11 +08:00
|
|
|
|
|
|
|
raise ArgumentError unless @post_ids && @topic && @new_owner && @acting_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_owner!
|
2018-03-01 06:31:44 +08:00
|
|
|
@post_ids.each do |post_id|
|
|
|
|
post = Post.with_deleted.where(id: post_id, topic_id: @topic.id).first
|
|
|
|
next if post.blank?
|
|
|
|
@topic.user = @new_owner if post.is_first_post?
|
2015-07-16 06:22:01 +08:00
|
|
|
|
2018-03-01 06:31:44 +08:00
|
|
|
if post.user == nil
|
|
|
|
@topic.recover! if post.is_first_post?
|
2015-03-03 00:17:11 +08:00
|
|
|
end
|
2018-03-01 06:31:44 +08:00
|
|
|
post.topic = @topic
|
|
|
|
post.set_owner(@new_owner, @acting_user, @skip_revision)
|
|
|
|
PostAction.remove_act(@new_owner, post, PostActionType.types[:like])
|
2015-03-03 00:17:11 +08:00
|
|
|
|
2015-03-12 03:54:11 +08:00
|
|
|
@topic.update_statistics
|
2017-08-31 12:06:56 +08:00
|
|
|
|
|
|
|
@new_owner.user_stat.update(
|
|
|
|
first_post_created_at: @new_owner.reload.posts.order('created_at ASC').first&.created_at
|
|
|
|
)
|
|
|
|
|
2018-03-02 00:31:58 +08:00
|
|
|
@topic.save!(validate: false)
|
2015-03-12 03:54:11 +08:00
|
|
|
end
|
2015-03-03 00:17:11 +08:00
|
|
|
end
|
|
|
|
end
|