A platform for community discussion. Free, open, simple.
FIX: topic pagination overflow with deleted posts (#35081)
Renato Atilio9fba48486d8e
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rbindex 2e5c4616a0d..4e373f8c5e8 100644--- a/app/controllers/topics_controller.rb+++ b/app/controllers/topics_controller.rb@@ -159,7 +159,7 @@ class TopicsController < ApplicationControllerendpage = params[:page]- if (page < 0) || ((page - 1) * @topic_view.chunk_size >= @topic_view.topic.highest_post_number)+ if page < 0 || page > 1 && (page - 1) * @topic_view.chunk_size >= @topic_view.topic.posts_countraise Discourse::NotFoundenddiff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 790a20307e9..967857cbab1 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -2380,6 +2380,26 @@ RSpec.describe TopicsController doexpect(response.status).to eq(404)end+ it "handles pagination correctly with deleted posts" do+ topic_with_posts = Fabricate(:topic)++ 24.times do |i|+ Fabricate(:post, topic: topic_with_posts, deleted_at: i.even? ? DateTime.now : nil)+ end++ Topic.reset_highest(topic_with_posts.id)+ topic_with_posts.reload++ expect(topic_with_posts.posts_count).to eq(12)+ expect(topic_with_posts.highest_post_number).to eq(24)++ 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.status).to eq(404)+ end+it "can find a topic given a slug in the id param" doget "/t/#{topic.slug}"expect(response).to redirect_to(topic.relative_url)