discourse/app/jobs/regular/notify_moved_posts.rb
Mark VanLandingham df1fc5bca8
FIX: Consistently notify lowest post number if post_moved notification generation (#30448)
We currently query the posts table without an order when notifying users of moved posts. Generally the query will return the lowest post number post (b/c ID correlates with post_number in most cases) but not always. This adds an order to the post query in notify_moved_posts job.

Also I removed some if statement nesting with early returns / guard clauses.
2024-12-23 09:53:43 -06:00

35 lines
1.1 KiB
Ruby

# frozen_string_literal: true
module Jobs
class NotifyMovedPosts < ::Jobs::Base
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
end
end