A platform for community discussion. Free, open, simple.
FIX: Redirect over-range topic page URLs instead of 404 (#40224)
Régis Hanol0a2a6b4871b9
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rbindex c35a609fd74..d02aaa63031 100644--- a/app/controllers/topics_controller.rb+++ b/app/controllers/topics_controller.rb@@ -169,8 +169,18 @@ class TopicsController < ApplicationControllerendpage = params[:page]- if page < 0 || page > 1 && (page - 1) * @topic_view.chunk_size >= @topic_view.topic.posts_count- raise Discourse::NotFound+ raise Discourse::NotFound if page < 0++ if page > 1+ visible_posts_count = @topic_view.filtered_posts.count+ chunk_size = @topic_view.chunk_size+ if (page - 1) * chunk_size >= visible_posts_count+ last_page = visible_posts_count > 0 ? ((visible_posts_count - 1) / chunk_size) + 1 : 1+ url = @topic_view.topic.relative_url+ url += ".json" if request.format.json?+ url += "?page=#{last_page}" if last_page > 1+ return redirect_to url, status: :moved_permanently+ endenddiscourse_expires_in 1.minutediff --git a/plugins/discourse-post-voting/spec/requests/topics_controller_spec.rb b/plugins/discourse-post-voting/spec/requests/topics_controller_spec.rbindex e907d202d9c..d08eb8b6714 100644--- a/plugins/discourse-post-voting/spec/requests/topics_controller_spec.rb+++ b/plugins/discourse-post-voting/spec/requests/topics_controller_spec.rb@@ -33,10 +33,10 @@ describe TopicsController do)end- it "does not error for topic views without any posts" do+ it "redirects an over-range page to the last valid page" doget "/t/#{topic.id}.json?page=2"- expect(response.status).to eq(404)+ expect(response).to redirect_to("/t/#{topic.slug}/#{topic.id}.json")endit "orders posts by date of creation when 'activity' filter is provided" dodiff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 76e5f9fa730..3b5dd2a65ec 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -3138,29 +3138,34 @@ RSpec.describe TopicsController doexpect(response.status).to eq(200)end- it "return 404 for an invalid page" do+ it "redirects an over-range page to the last valid page" doget "/t/#{topic.slug}/#{topic.id}.json", params: { page: 2 }- expect(response.status).to eq(404)+ expect(response).to redirect_to("/t/#{topic.slug}/#{topic.id}.json")end- it "handles pagination correctly with deleted posts" do+ it "redirects over-range pages to the last multi-page page" dotopic_with_posts = Fabricate(:topic)+ Fabricate.times(25, :post, topic: topic_with_posts)+ Topic.reset_highest(topic_with_posts.id)- 24.times do |i|- Fabricate(:post, topic: topic_with_posts, deleted_at: i.even? ? DateTime.now : nil)- end+ get "/t/#{topic_with_posts.slug}/#{topic_with_posts.id}", params: { page: 5 }+ expect(response).to redirect_to("/t/#{topic_with_posts.slug}/#{topic_with_posts.id}?page=2")+ end- Topic.reset_highest(topic_with_posts.id)- topic_with_posts.reload+ it "uses viewer-visible post count when deciding the last valid page (whispers)" do+ SiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}"- expect(topic_with_posts.posts_count).to eq(12)- expect(topic_with_posts.highest_post_number).to eq(24)+ topic_with_posts = Fabricate(:topic)+ Fabricate.times(20, :post, topic: topic_with_posts)+ Fabricate(:post, topic: topic_with_posts, post_type: Post.types[:whisper])+ Topic.reset_highest(topic_with_posts.id)- get "/t/#{topic_with_posts.slug}/#{topic_with_posts.id}.json", params: { page: 1 }- expect(response.status).to eq(200)+ get "/t/#{topic_with_posts.slug}/#{topic_with_posts.id}.json", params: { page: 2 }+ expect(response).to redirect_to("/t/#{topic_with_posts.slug}/#{topic_with_posts.id}.json")+ sign_in(admin)get "/t/#{topic_with_posts.slug}/#{topic_with_posts.id}.json", params: { page: 2 }- expect(response.status).to eq(404)+ expect(response.status).to eq(200)endit "can find a topic given a slug in the id param" do@@ -3851,7 +3856,7 @@ RSpec.describe TopicsController doexpect(extract_post_stream).to eq(@post_ids[3..3])get "/t/#{topic.slug}/#{topic.id}.json", params: { page: 3 }- expect(response.status).to eq(404)+ expect(response).to redirect_to("/t/#{topic.slug}/#{topic.id}.json?page=2")TopicView.stubs(:chunk_size).returns(4)@@ -3860,7 +3865,7 @@ RSpec.describe TopicsController doexpect(extract_post_stream).to eq(@post_ids[0..3])get "/t/#{topic.slug}/#{topic.id}.json", params: { page: 2 }- expect(response.status).to eq(404)+ expect(response).to redirect_to("/t/#{topic.slug}/#{topic.id}.json")endend