diff --git a/lib/diaspora/user/querying.rb b/lib/diaspora/user/querying.rb index 681c669fc..c1cb79daa 100644 --- a/lib/diaspora/user/querying.rb +++ b/lib/diaspora/user/querying.rb @@ -13,11 +13,12 @@ module Diaspora end def raw_visible_posts(opts = {}) + opts = opts.dup opts[:type] ||= ['StatusMessage', 'Photo'] opts[:limit] ||= 20 opts[:order] ||= 'updated_at DESC' opts[:hidden] ||= false - opts[:order] = '`posts`.' + opts[:order] + order_with_table = '`posts`.' + opts[:order] opts[:limit] = opts[:limit].to_i * opts[:page].to_i if opts[:page] posts_from_others = Post.joins(:contacts).where( :post_visibilities => {:hidden => opts[:hidden]}, :contacts => {:user_id => self.id}) @@ -29,10 +30,10 @@ module Diaspora posts_from_self = posts_from_self.joins(:aspect_visibilities).where(:aspect_visibilities => {:aspect_id => opts[:by_members_of]}) end - post_ids = Post.connection.execute(posts_from_others.select('posts.id').limit(opts[:limit]).order(opts[:order]).to_sql).map{|r| r.first} - post_ids += Post.connection.execute(posts_from_self.select('posts.id').limit(opts[:limit]).order(opts[:order]).to_sql).map{|r| r.first} + post_ids = Post.connection.execute(posts_from_others.select('posts.id').limit(opts[:limit]).order(order_with_table).to_sql).map{|r| r.first} + post_ids += Post.connection.execute(posts_from_self.select('posts.id').limit(opts[:limit]).order(order_with_table).to_sql).map{|r| r.first} - Post.where(:id => post_ids, :pending => false, :type => opts[:type]).select('DISTINCT `posts`.*').limit(opts[:limit]) + Post.where(:id => post_ids, :pending => false, :type => opts[:type]).select('DISTINCT `posts`.*').limit(opts[:limit]).order(opts[:order]) end def visible_photos diff --git a/spec/models/user/querying_spec.rb b/spec/models/user/querying_spec.rb index 70ed20e6a..a9b17968b 100644 --- a/spec/models/user/querying_spec.rb +++ b/spec/models/user/querying_spec.rb @@ -127,9 +127,32 @@ describe User do end it "returns an empty array when passed an aspect the user doesn't own" do - other_user = Factory(:user_with_aspect) - connect_users(eve, eve.aspects.first, other_user, other_user.aspects.first) - alice.people_in_aspects([other_user.aspects.first]).should == [] + alice.people_in_aspects([eve.aspects.first]).should == [] + end + end + context 'with many posts' do + before do + bob.move_contact(eve.person, bob.aspects.first, bob.aspects.create(:name => 'new aspect')) + (1..18).each do |n| + [alice, bob, eve].each do |u| + post = u.post :status_message, :text => "#{u.username} - #{n}", :to => u.aspects.first.id + post.created_at = post.created_at - n*10*u.id + post.updated_at = post.updated_at - n*10*u.id + post.save + end + end + end + it 'works' do #This is in one spec to save time + bob.raw_visible_posts.should == bob.raw_visible_posts(:by_members_of => bob.aspects.map{|a| a.id}) + bob.raw_visible_posts.sort_by{|p| p.updated_at}.map{|p| p.id}.should == bob.raw_visible_posts.map{|p| p.id}.reverse + + opts = {:limit => 40} + bob.raw_visible_posts(opts).should == bob.raw_visible_posts(opts.merge(:by_members_of => bob.aspects.map{|a| a.id})) + bob.raw_visible_posts(opts).sort_by{|p| p.updated_at}.map{|p| p.id}.should == bob.raw_visible_posts(opts).map{|p| p.id}.reverse + + opts = {:page => 2} + bob.raw_visible_posts(opts).map{|p| p.id}.should == bob.raw_visible_posts(opts.merge(:by_members_of => bob.aspects.map{|a| a.id})).map{|p| p.id} + bob.raw_visible_posts(opts).sort_by{|p| p.updated_at}.map{|p| p.id}.should == bob.raw_visible_posts(opts).map{|p| p.id}.reverse end end end