FIX: deleted topic author in crawler view (#27788)

When a crawler visits a topic that has a deleted author, it would error because the `show.html.erb` view was expecting a user to be always present.

This ensure we don't render the "author" meta data when the author of the topic has been deleted.

Internal ref t/132508
This commit is contained in:
Régis Hanol 2024-07-09 10:44:03 +02:00 committed by GitHub
parent f44ec18fd2
commit 0846862cb5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 4 deletions

View File

@ -52,10 +52,12 @@
</div>
<% if @topic_view.crawler_posts&.first && !@topic_view.crawler_posts.first.is_first_post? %>
<span itemprop='author' itemscope itemtype="http://schema.org/Person">
<meta itemprop='name' content='<%= @topic_view.topic.user.username %>'>
<link itemprop='url' href='<%= Discourse.base_url %>/u/<%= @topic_view.topic.user.username %>'>
</span>
<% if @topic_view.topic.user.present? %>
<span itemprop='author' itemscope itemtype="http://schema.org/Person">
<meta itemprop='name' content='<%= @topic_view.topic.user.username %>'>
<link itemprop='url' href='<%= Discourse.base_url %>/u/<%= @topic_view.topic.user.username %>'>
</span>
<% end %>
<meta itemprop='text' content='<%= @topic_view.topic.excerpt %>'>
<% end %>

View File

@ -5382,6 +5382,12 @@ RSpec.describe TopicsController do
)
end
it "works even when the author has been deleted" do
topic.update!(user_id: nil)
get "#{topic.relative_url}/2"
end
context "with canonical_url" do
fab!(:topic_embed) { Fabricate(:topic_embed, embed_url: "https://markvanlan.com") }
let!(:user_agent) do