Class: PostOwnerChanger

Inherits:
Object
  • Object
show all
Defined in:
app/services/post_owner_changer.rb

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ PostOwnerChanger

Returns a new instance of PostOwnerChanger.



4
5
6
7
8
9
10
11
12
13
14
# File 'app/services/post_owner_changer.rb', line 4

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

  %i[post_ids topic new_owner acting_user].each do |arg|
    raise ArgumentError.new(arg) if self.instance_variable_get("@#{arg}").blank?
  end
end

Instance Method Details

#change_owner!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/post_owner_changer.rb', line 16

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, skip_delete_check: true)

    level = post.is_first_post? ? :watching : :tracking
    TopicUser.change(
      @new_owner.id,
      @topic.id,
      notification_level: NotificationLevels.topic_levels[level],
      posted: true,
    )

    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,
    )

    Post.where(topic_id: @topic.id, reply_to_post_number: post.post_number).update_all(
      reply_to_user_id: @new_owner.id,
    )

    @topic.save!(validate: false)
  end
end