FIX: Ensure hashtag_lookup falls back to system user if post user is deleted (#32466)

`hashtag_lookup` fails if called with a deleted user's ID. This change
defaults to system user if the user does not exist.
This commit is contained in:
Selase Krakani
2025-05-07 05:49:49 +00:00
committed by GitHub
parent dcc0eff0c7
commit b16fb6a60b
2 changed files with 21 additions and 5 deletions

View File

@ -111,11 +111,7 @@ module PrettyText
# categories, however if the suppress_secured_categories_from_admin
# site setting is activated then this user will not be able to access
# secure categories, so hashtags that are secure will not render.
if cooking_user_id.blank?
cooking_user = Discourse.system_user
else
cooking_user = User.find(cooking_user_id)
end
cooking_user = User.find_by(id: cooking_user_id) || Discourse.system_user
types_in_priority_order =
types_in_priority_order.select do |type|

View File

@ -162,5 +162,25 @@ RSpec.describe PrettyText::Helpers do
Guardian.expects(:new).with(Discourse.system_user).returns(guardian_system)
PrettyText::Helpers.hashtag_lookup("somecooltag", nil, %w[category tag])
end
it "falls back to system user when cooking_user is deleted" do
user.destroy
expect(
PrettyText::Helpers.hashtag_lookup("somecooltag::tag", user.id, %w[category tag]),
).to eq(
{
relative_url: tag.url,
text: "somecooltag",
description: "Coolest things ever",
colors: nil,
icon: "tag",
id: tag.id,
slug: "somecooltag",
ref: "somecooltag::tag",
type: "tag",
},
)
end
end
end