Skip to content

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.rb
index 2e5c4616a0d..4e373f8c5e8 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -159,7 +159,7 @@ class TopicsController < ApplicationController
end
page = 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_count
raise Discourse::NotFound
end
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 790a20307e9..967857cbab1 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -2380,6 +2380,26 @@ RSpec.describe TopicsController do
expect(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" do
get "/t/#{topic.slug}"
expect(response).to redirect_to(topic.relative_url)