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
|
|
|
|
|
|
|
raise ArgumentError unless @post_ids && @topic && @new_owner && @acting_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_owner!
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
@post_ids.each do |post_id|
|
2016-06-17 14:14:52 -04:00
|
|
|
post = Post.with_deleted.where(id: post_id, topic_id: @topic.id).first
|
|
|
|
next if post.blank?
|
2015-03-02 11:17:11 -05:00
|
|
|
@topic.user = @new_owner if post.is_first_post?
|
2015-07-15 18:22:01 -04:00
|
|
|
|
|
|
|
if post.user == nil
|
|
|
|
@topic.recover! if post.is_first_post?
|
|
|
|
end
|
|
|
|
post.topic = @topic
|
2016-08-19 13:13:22 -04:00
|
|
|
post.set_owner(@new_owner, @acting_user, @skip_revision)
|
2017-08-11 03:55:00 -04:00
|
|
|
PostAction.remove_act(@new_owner, post, PostActionType.types[:like])
|
2015-03-02 11:17:11 -05:00
|
|
|
end
|
|
|
|
|
2015-03-11 15:54:11 -04:00
|
|
|
@topic.update_statistics
|
|
|
|
@new_owner.user_stat.update(first_post_created_at: @new_owner.posts(true).order('created_at ASC').first.try(:created_at))
|
2015-04-06 03:27:05 -04:00
|
|
|
@topic.save!
|
2015-03-11 15:54:11 -04:00
|
|
|
end
|
2015-03-02 11:17:11 -05:00
|
|
|
end
|
|
|
|
end
|