Class: Jobs::NotifyMovedPosts

Inherits:
Base
  • Object
show all
Defined in:
app/jobs/regular/notify_moved_posts.rb

Instance Method Summary collapse

Methods inherited from Base

acquire_cluster_concurrency_lock!, clear_cluster_concurrency_lock!, cluster_concurrency, cluster_concurrency_redis_key, delayed_perform, #error_context, get_cluster_concurrency, #last_db_duration, #log, #perform, #perform_immediately

Instance Method Details

#execute(args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/jobs/regular/notify_moved_posts.rb', line 5

def execute(args)
  raise Discourse::InvalidParameters.new(:post_ids) if args[:post_ids].blank?
  raise Discourse::InvalidParameters.new(:moved_by_id) if args[:moved_by_id].blank?

  posts =
    Post
      .includes(:user, :topic)
      .where(id: args[:post_ids])
      .where("user_id <> ?", args[:moved_by_id])
      .order(post_number: :asc)
  return if posts.blank?

  moved_by = User.find_by(id: args[:moved_by_id])

  # Make sure we don't notify the same user twice (in case multiple posts were moved at once.)
  users_notified = Set.new
  posts.each do |p|
    next if users_notified.include?(p.user_id)

    p.user.notifications.create(
      notification_type: Notification.types[:moved_post],
      topic_id: p.topic_id,
      post_number: p.post_number,
      data: { topic_title: p.topic.title, display_username: moved_by.username }.to_json,
    )
    users_notified << p.user_id
  end
end