discourse/app/jobs/regular/delete_user_posts.rb
Keegan George fe36d9ae0d
FIX: return nil correctly for nil check (#37460)
## 🔍 Overview

This update is a follow-up to:
https://github.com/discourse/discourse/pull/36813. The PR had a
follow-up adding `nil` checks to the `DeleteUserPosts` job, but since
the look-ups were using `find` instead of `find_by` a `RecordNotFound`
error would be returned instead of `nil`. This update ensures that we
use `find_by` instead so that the `nil` checks work correctly.
Additionally, we add some tests to ensure the job isn't triggered when
the params are missing.
2026-02-02 15:43:56 -08:00

37 lines
896 B
Ruby
Vendored

# frozen_string_literal: true
module Jobs
class DeleteUserPosts < ::Jobs::Base
sidekiq_options queue: "critical"
def execute(args)
user = User.find_by(id: args[:user_id])
return if user.nil?
acting_user = User.find_by(id: args[:acting_user_id])
return if acting_user.nil?
guardian = Guardian.new(acting_user)
return unless guardian.can_delete_all_posts?(user)
deleted_count = 0
loop do
delete = user.delete_posts_in_batches(guardian)
break if delete.empty?
deleted_count += delete.size
end
post =
SystemMessage.create_from_system_user(
acting_user,
:user_posts_deleted,
user: user.username,
staff_user: acting_user.username,
count: deleted_count,
)
post&.topic&.invite_group(acting_user, Group[:admins])
end
end
end