From a8085f15c4ccfb78fad1ab31ba41ecbddf104462 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Fri, 15 Feb 2013 12:58:14 +1100 Subject: [PATCH] grab highest post number from the raw data, so deletions at the end of a topic work --- app/serializers/topic_view_serializer.rb | 10 +++++++--- lib/topic_view.rb | 9 +++++++++ spec/components/topic_view_spec.rb | 4 +++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/serializers/topic_view_serializer.rb b/app/serializers/topic_view_serializer.rb index d5f64ee7358..003b15d7f54 100644 --- a/app/serializers/topic_view_serializer.rb +++ b/app/serializers/topic_view_serializer.rb @@ -5,7 +5,6 @@ class TopicViewSerializer < ApplicationSerializer [:id, :title, :posts_count, - :highest_post_number, :created_at, :views, :reply_count, @@ -41,7 +40,8 @@ class TopicViewSerializer < ApplicationSerializer :notification_level, :notifications_reason_id, :posts, - :at_bottom + :at_bottom, + :highest_post_number has_one :created_by, serializer: BasicUserSerializer, embed: :objects has_one :last_poster, serializer: BasicUserSerializer, embed: :objects @@ -185,7 +185,11 @@ class TopicViewSerializer < ApplicationSerializer # Whether we're at the bottom of a topic (last page) def at_bottom - posts.present? and (@highest_number_in_posts == object.topic.highest_post_number) + posts.present? and (@highest_number_in_posts == object.highest_post_number) + end + + def highest_post_number + object.highest_post_number end def posts diff --git a/lib/topic_view.rb b/lib/topic_view.rb index e5ad0f5fdcc..fb84a680a77 100644 --- a/lib/topic_view.rb +++ b/lib/topic_view.rb @@ -223,6 +223,15 @@ class TopicView @suggested_topics ||= TopicQuery.new(@user).list_suggested_for(topic) end + # This is pending a larger refactor, that allows custom orders + # for now we need to look for the highest_post_number in the stream + # the cache on topics is not correct if there are deleted posts at + # the end of the stream (for mods), nor is it correct for filtered + # streams + def highest_post_number + @highest_post_number ||= @posts.maximum(:post_number) + end + protected def read_posts_set diff --git a/spec/components/topic_view_spec.rb b/spec/components/topic_view_spec.rb index ef6814330ab..41bf551185d 100644 --- a/spec/components/topic_view_spec.rb +++ b/spec/components/topic_view_spec.rb @@ -106,10 +106,12 @@ describe TopicView do end it 'allows admins to see deleted posts' do + post_number = p3.post_number p3.destroy admin = Fabricate(:admin) topic_view = TopicView.new(topic.id, admin) topic_view.posts.count.should == 3 + topic_view.highest_post_number.should == post_number end it 'does not allow non admins to see deleted posts' do @@ -117,7 +119,7 @@ describe TopicView do topic_view.posts.count.should == 2 end - # Sam: disabled for now, we only need this for poss, if we do, roll it into topic + # Sam: disabled for now, we only need this for polls, if we do, roll it into topic # having to walk every post action is not really a good idea # # context '.voted_in_topic?' do