From 73546365028a21bab90d7fd3bf5975a577bb5e7d Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Tue, 9 Feb 2021 11:17:06 +0530 Subject: [PATCH] FIX: return 404 `not found` error if a topic is deleted. (#11987) Currently, it's returning 403 invalid access error which causes issue in Google webmaster tools. --- app/controllers/topics_controller.rb | 19 ++++++++++++++++++- spec/requests/topics_controller_spec.rb | 6 ++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index aa3c6c57bb7..54f29248b65 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -848,7 +848,24 @@ class TopicsController < ApplicationController def feed raise Discourse::NotFound if !Post.exists?(topic_id: params[:topic_id]) - @topic_view = TopicView.new(params[:topic_id]) + begin + @topic_view = TopicView.new(params[:topic_id]) + rescue Discourse::NotLoggedIn + raise Discourse::NotFound + rescue Discourse::InvalidAccess => ex + + deleted = guardian.can_see_topic?(ex.obj, false) || + (!guardian.can_see_topic?(ex.obj) && + ex.obj&.access_topic_via_group && + ex.obj.deleted_at) + + raise Discourse::NotFound.new( + nil, + check_permalinks: deleted, + original_path: ex.obj.relative_url + ) + end + discourse_expires_in 1.minute render 'topics/show', formats: [:rss] end diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb index 71a7b9ba9e9..ee3c2ddde75 100644 --- a/spec/requests/topics_controller_spec.rb +++ b/spec/requests/topics_controller_spec.rb @@ -2540,6 +2540,12 @@ RSpec.describe TopicsController do get "/t/foo/#{topic.id}.rss" expect(response.status).to eq(404) end + + it 'returns 404 when the topic is deleted' do + topic.trash! + get "/t/foo/#{topic.id}.rss" + expect(response.status).to eq(404) + end end describe '#invite_group' do