From fe36d9ae0d3f14c0964a8f2b76e1953b616d2980 Mon Sep 17 00:00:00 2001 From: Keegan George Date: Mon, 2 Feb 2026 15:43:56 -0800 Subject: [PATCH] FIX: return nil correctly for nil check (#37460) ## :mag: 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. --- app/jobs/regular/delete_user_posts.rb | 4 ++-- spec/jobs/delete_user_posts_spec.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/jobs/regular/delete_user_posts.rb b/app/jobs/regular/delete_user_posts.rb index 19a6e06bc65..a6187bc0941 100644 --- a/app/jobs/regular/delete_user_posts.rb +++ b/app/jobs/regular/delete_user_posts.rb @@ -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) diff --git a/spec/jobs/delete_user_posts_spec.rb b/spec/jobs/delete_user_posts_spec.rb index 36a6b867aea..8db849e5b30 100644 --- a/spec/jobs/delete_user_posts_spec.rb +++ b/spec/jobs/delete_user_posts_spec.rb @@ -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