FIX: Use filtered posts when determining the next page

FIX: Use filtered posts when determining the next page

This bug was causing some unusual behavior when the last post is filtered (e.g. from an ignored user). In some situations this would cause suggested topics to be omitted from the payload.

The next_page specs have been updated to remove most of the stubs

diff --git a/lib/topic_view.rb b/lib/topic_view.rb
index 0f26832202..b95254d14a 100644
--- a/lib/topic_view.rb
+++ b/lib/topic_view.rb
@@ -152,7 +152,7 @@ class TopicView
 
   def next_page
     @next_page ||= begin
-      if last_post && (@topic.highest_post_number > last_post.post_number)
+      if last_post && highest_post_number && (highest_post_number > last_post.post_number)
         @page + 1
       end
     end
diff --git a/spec/components/topic_view_spec.rb b/spec/components/topic_view_spec.rb
index ce1f0e844a..2d6948c6b8 100644
--- a/spec/components/topic_view_spec.rb
+++ b/spec/components/topic_view_spec.rb
@@ -48,6 +48,11 @@ describe TopicView do
         expect(tv.filtered_post_ids).to eq([post.id, post2.id])
       end
 
+      it "returns nil for next_page" do
+        tv = TopicView.new(topic.id, evil_trout)
+        expect(tv.next_page).to eq(nil)
+      end
+
       describe "when an ignored user made the original post" do
         let!(:post) { Fabricate(:post, topic: topic, user: user) }
 
@@ -245,22 +250,16 @@ describe TopicView do
     end
 
     describe "#next_page" do
-      let(:p2) { stub(post_number: 2) }
-      let(:topic) do
-        topic = create_topic
-        topic.stubs(:highest_post_number).returns(5)
-        topic
-      end
+      let!(:post) { Fabricate(:post, topic: topic, user: user) }
+      let!(:post2) { Fabricate(:post, topic: topic, user: user) }
+      let!(:post3) { Fabricate(:post, topic: topic, user: user) }
 
       before do
-        TopicView.any_instance.expects(:find_topic).with(1234).returns(topic)
-        TopicView.any_instance.stubs(:filter_posts)
-        TopicView.any_instance.stubs(:last_post).returns(p2)
         TopicView.stubs(:chunk_size).returns(2)
       end
 
       it "should return the next page" do
-        expect(TopicView.new(1234, user).next_page).to eql(2)
+        expect(TopicView.new(topic.id, user).next_page).to eql(2)
       end
     end

GitHub sha: be1510b0

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there: