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.
This commit is contained in:
Keegan George
2026-02-02 15:43:56 -08:00
committed by GitHub
parent 84e6257909
commit fe36d9ae0d
2 changed files with 14 additions and 2 deletions
+2 -2
View File
@@ -5,10 +5,10 @@ module Jobs
sidekiq_options queue: "critical"
def execute(args)
user = User.find(args[:user_id])
user = User.find_by(id: args[:user_id])
return if user.nil?
acting_user = User.find(args[:acting_user_id]) if args[:acting_user_id]
acting_user = User.find_by(id: args[:acting_user_id])
return if acting_user.nil?
guardian = Guardian.new(acting_user)
+12
View File
@@ -48,4 +48,16 @@ RSpec.describe Jobs::DeleteUserPosts do
user.reload
expect(user.posts.count).to eq(0)
end
it "does nothing if user does not exist" do
expect {
described_class.new.execute(user_id: -999, acting_user_id: admin.id)
}.not_to raise_error
end
it "does nothing if acting_user does not exist" do
expect {
described_class.new.execute(user_id: user.id, acting_user_id: -999)
}.not_to raise_error
end
end