Fix stream on pages other than 1, rename raw_visible_posts to visible_posts

This commit is contained in:
Raphael Sofaer 2011-04-13 14:35:20 -07:00
parent 9383388bb6
commit efe79eb351
19 changed files with 88 additions and 69 deletions

View file

@ -30,9 +30,9 @@ class ApisController < ApplicationController #We should start with this versione
def home_timeline
set_defaults
timeline = current_user.raw_visible_posts.includes(:comments, :photos, :likes, :dislikes).paginate(
:page => params[:page], :per_page => params[:per_page], :order => "#{params[:order]} DESC")
timeline = current_user.visible_posts(:max_time => params[:max_time],
:per_page => params[:per_page],
:order => "#{params[:order]} DESC").includes(:comments, :photos, :likes, :dislikes)
respond_with timeline do |format|
format.json{ render :json => timeline.to_json(:format => :twitter) }
end

View file

@ -30,10 +30,10 @@ class AspectsController < ApplicationController
@selected_contacts = @aspects.map { |aspect| aspect.contacts }.flatten.uniq unless params[:only_posts]
@aspect_ids = @aspects.map { |a| a.id }
posts = current_user.raw_visible_posts(:by_members_of => @aspect_ids,
posts = current_user.visible_posts(:by_members_of => @aspect_ids,
:type => 'StatusMessage',
:order => session[:sort_order] + ' DESC',
:page => params[:page]
:max_time => params[:max_time].to_i
).includes(:comments, :mentions, :likes, :dislikes)
@posts = PostsFake.new(posts)
@ -160,6 +160,10 @@ class AspectsController < ApplicationController
@aspect.save
end
def ensure_page
params[:max_time] ||= Time.now + 1
end
protected
def save_sort_order

View file

@ -3,6 +3,9 @@
# the COPYRIGHT file.
module AspectsHelper
def next_page_path
aspects_path(:max_time => @posts.last.send(session[:sort_order].to_sym).to_i, :a_ids => params[:a_ids])
end
def link_for_aspect(aspect, opts={})
opts[:params] ||= {}
params ||= {}

View file

@ -77,7 +77,7 @@ class Post < ActiveRecord::Base
local_post = Post.where(:guid => self.guid).first
if local_post && local_post.author_id == self.author_id
known_post = user.raw_visible_posts.where(:guid => self.guid).first
known_post = user.visible_posts.where(:guid => self.guid).first
if known_post
if known_post.mutable?
known_post.update_attributes(self.attributes)

View file

@ -13,6 +13,6 @@
- if posts.length > 0
= render 'shared/stream', :posts => posts
#pagination
=link_to(t('more'), aspects_path(:page => next_page, :a_ids => params[:a_ids]), :class => 'paginate')
=link_to(t('more'), next_page_path, :class => 'paginate')
- else
= render 'aspects/no_posts_message', :post_count => posts.length

View file

@ -12,8 +12,9 @@
= link_to t('.post_a_message'), '#publisher_page', :id => 'publisher_button'
#main_stream.stream
= render 'shared/stream', :posts => @posts
-if @posts.length > 0
#pagination
%a.more-link.paginate{:href => aspects_path(:a_ids => params[:a_ids], :page => params[:page] + 1)}
%a.more-link.paginate{:href => next_page_path}
%h2= t("more")
- content_for :subpages do
= render 'shared/publisher', :aspect_ids => @aspect_ids

View file

@ -21,7 +21,7 @@
.content
.from
= person_link(post.author, :class => 'author')
%time.timeago{:datetime => post.created_at}
%time.timeago{:datetime => post.created_at, :integer => post.created_at.to_i}
= render 'status_messages/status_message', :post => post, :photos => post.photos
.info

View file

@ -5,7 +5,7 @@ module PhotoMover
FileUtils::mkdir_p temp_dir
Dir.chdir 'tmp/exports'
photos = user.raw_visible_posts.where(:author_id => user.person.id, :type => 'Photo')
photos = user.visible_posts.where(:author_id => user.person.id, :type => 'Photo')
photos_dir = "#{user.id}/photos"
FileUtils::mkdir_p photos_dir

View file

@ -64,7 +64,7 @@ module Diaspora
}
xml.posts {
user.raw_visible_posts.find_all_by_author_id(user_person_id).each do |post|
user.visible_posts.find_all_by_author_id(user_person_id).each do |post|
#post.comments.each do |comment|
# post_doc << comment.to_xml
#end

View file

@ -12,15 +12,16 @@ module Diaspora
post ||= Post.where(:id => id, :public => true).where(opts).first
end
def raw_visible_posts(opts = {})
def visible_posts(opts = {})
opts = opts.dup
opts[:type] ||= ['StatusMessage', 'Photo']
opts[:limit] ||= 15
opts[:order] ||= 'updated_at DESC'
order_field = opts[:order].split.first.to_sym
opts[:hidden] ||= false
order_with_table = 'posts.' + opts[:order]
opts[:page] ||= 1
opts[:offset] = opts[:page] == 1 ? 0 : opts[:limit] * (opts[:page] - 1)
opts[:max_time] = Time.at(opts[:max_time]) if opts[:max_time].instance_of?(Fixnum)
opts[:max_time] ||= Time.now + 1
select_clause ='DISTINCT posts.id, posts.updated_at AS updated_at, posts.created_at AS created_at'
posts_from_others = Post.joins(:contacts).where( :post_visibilities => {:hidden => opts[:hidden]}, :contacts => {:user_id => self.id})
@ -32,17 +33,17 @@ module Diaspora
posts_from_self = posts_from_self.joins(:aspect_visibilities).where(:aspect_visibilities => {:aspect_id => opts[:by_members_of]})
end
posts_from_others = posts_from_others.select(select_clause).limit(opts[:limit]*opts[:page]).order(order_with_table)
posts_from_self = posts_from_self.select(select_clause).limit(opts[:limit]*opts[:page]).order(order_with_table)
posts_from_others = posts_from_others.select(select_clause).limit(opts[:limit]).order(order_with_table).where(Post.arel_table[order_field].lt(opts[:max_time]))
posts_from_self = posts_from_self.select(select_clause).limit(opts[:limit]).order(order_with_table).where(Post.arel_table[order_field].lt(opts[:max_time]))
all_posts = "(#{posts_from_others.to_sql}) UNION ALL (#{posts_from_self.to_sql}) ORDER BY #{opts[:order]} LIMIT #{opts[:limit]} OFFSET #{opts[:offset]}"
all_posts = "(#{posts_from_others.to_sql}) UNION ALL (#{posts_from_self.to_sql}) ORDER BY #{opts[:order]} LIMIT #{opts[:limit]}"
post_ids = Post.connection.execute(all_posts).map{|r| r.first}
Post.where(:id => post_ids, :pending => false, :type => opts[:type]).select('DISTINCT posts.*').limit(opts[:limit]).order(order_with_table)
end
def visible_photos
raw_visible_posts(:type => 'Photo')
def visible_photos(opts = {})
visible_posts(opts.merge(:type => 'Photo'))
end
def contact_for(person)

View file

@ -1,6 +1,6 @@
class PostsFake
attr_reader :people_hash, :post_fakes
delegate :length, :each, :to_ary, :to => :post_fakes
delegate :length, :each, :to_ary, :last, :to => :post_fakes
def initialize(posts)
author_ids = []

View file

@ -7,8 +7,11 @@ var InfiniteScroll = {
itemSelector : ".stream_element",
// selector for all items you'll retrieve
pathParse : function( pathStr, nextPage ){
console.log(pathStr);
console.log(nextPage);
var newPath = pathStr.replace("?", "?only_posts=true&");
return newPath.replace( "page=2", "page=" + nextPage);
var last_time = $('#main_stream .stream_element').last().find('time.timeago').attr('integer');
return newPath.replace( /max_time=\d+/, 'max_time=' + last_time);
},
bufferPx: 500,
debug: false,

View file

@ -8,9 +8,9 @@ Diaspora.widgets.add("directionDetector", function() {
this.start = function() {
Diaspora.widgets.directionDetector.updateBinds();
InfiniteScroll.postScrollCallback = function() {
InfiniteScroll.postScroll(function() {
Diaspora.widgets.directionDetector.updateBinds();
}
});
};
this.isRTL = function(str) {

View file

@ -66,7 +66,7 @@ describe 'a user receives a post' do
bob.dispatch_post(sm, :to => bob.aspects.first)
end
alice.raw_visible_posts.count.should == 1
alice.visible_posts.count.should == 1
end
context 'mentions' do
@ -158,14 +158,14 @@ describe 'a user receives a post' do
end
it "adds a received post to the the contact" do
@user1.raw_visible_posts.include?(@status_message).should be_true
@user1.visible_posts.include?(@status_message).should be_true
@contact.posts.include?(@status_message).should be_true
end
it 'removes posts upon disconnecting' do
@user1.disconnect(@contact)
@user1.reload
@user1.raw_visible_posts.should_not include @status_message
@user1.visible_posts.should_not include @status_message
end
context 'dependant delete' do
@ -243,7 +243,7 @@ describe 'a user receives a post' do
end
it 'should correctly attach the user already on the pod' do
@user2.reload.raw_visible_posts.size.should == 1
@user2.reload.visible_posts.size.should == 1
post_in_db = StatusMessage.find(@post.id)
post_in_db.comments.should == []
receive_with_zord(@user2, @user1.person, @xml)
@ -270,7 +270,7 @@ describe 'a user receives a post' do
end
}
@user2.reload.raw_visible_posts.size.should == 1
@user2.reload.visible_posts.size.should == 1
post_in_db = StatusMessage.find(@post.id)
post_in_db.comments.should == []
@ -341,7 +341,7 @@ describe 'a user receives a post' do
zord = Postzord::Receiver.new(@user2, :salmon_xml => salmon_xml)
zord.perform
@user2.raw_visible_posts.include?(post).should be_true
@user2.visible_posts.include?(post).should be_true
end
end

View file

@ -46,7 +46,7 @@ describe 'making sure the spec runner works' do
it 'allows posting after running' do
message = @user1.post(:status_message, :text => "Connection!", :to => @aspect1.id)
@user2.reload.raw_visible_posts.should include message
@user2.reload.visible_posts.should include message
end
end

View file

@ -30,7 +30,7 @@ describe "attack vectors" do
zord = Postzord::Receiver.new(user, :salmon_xml => salmon_xml)
zord.perform
user.raw_visible_posts.include?(post_from_non_contact).should be_false
user.visible_posts.include?(post_from_non_contact).should be_false
Post.count.should == post_count
end
@ -49,7 +49,7 @@ describe "attack vectors" do
zord = Postzord::Receiver.new(user, :salmon_xml => salmon_xml)
zord.perform
user3.reload.raw_visible_posts.should_not include(StatusMessage.find(original_message.id))
user3.reload.visible_posts.should_not include(StatusMessage.find(original_message.id))
end
context 'malicious contact attack vector' do
@ -89,10 +89,10 @@ describe "attack vectors" do
zord = Postzord::Receiver.new(user, :salmon_xml => salmon_xml)
zord.perform
}.should_not change{user.reload.raw_visible_posts.count}
}.should_not change{user.reload.visible_posts.count}
original_message.reload.text.should == "store this!"
user.raw_visible_posts.first.text.should == "store this!"
user.visible_posts.first.text.should == "store this!"
end
end
it 'should not overwrite another persons profile profile' do
@ -119,7 +119,7 @@ describe "attack vectors" do
zord = Postzord::Receiver.new(user, :salmon_xml => salmon_xml)
zord.perform
user.raw_visible_posts.count.should == 1
user.visible_posts.count.should == 1
StatusMessage.count.should == 1
ret = Retraction.new
@ -132,7 +132,7 @@ describe "attack vectors" do
zord.perform
StatusMessage.count.should == 1
user.raw_visible_posts.count.should == 1
user.visible_posts.count.should == 1
end
it "disregards retractions for non-existent posts that are from someone other than the post's author" do
@ -163,7 +163,7 @@ describe "attack vectors" do
zord.perform
user.raw_visible_posts.count.should == 1
user.visible_posts.count.should == 1
ret = Retraction.new
ret.post_guid = original_message.guid
@ -177,7 +177,7 @@ describe "attack vectors" do
zord.perform
}.should_not change(StatusMessage, :count)
user.reload.raw_visible_posts.count.should == 1
user.reload.visible_posts.count.should == 1
end
it 'it should not allow you to send retractions for other people' do

View file

@ -275,9 +275,9 @@ describe Diaspora::UserModules::Connecting do
end
it "deletes the disconnected user's posts from visible_posts" do
bob.reload.raw_visible_posts.include?(@message).should be_true
bob.reload.visible_posts.include?(@message).should be_true
bob.disconnect bob.contact_for(alice.person)
bob.reload.raw_visible_posts.include?(@message).should be_false
bob.reload.visible_posts.include?(@message).should be_false
end
end

View file

@ -28,8 +28,8 @@ describe User do
it 'saves post into visible post ids' do
proc {
user.add_to_streams(@post, @aspects)
}.should change{user.raw_visible_posts(:by_members_of => @aspects).length}.by(1)
user.raw_visible_posts(:by_members_of => @aspects).should include @post
}.should change{user.visible_posts(:by_members_of => @aspects).length}.by(1)
user.visible_posts(:by_members_of => @aspects).should include @post
end
it 'saves post into each aspect in aspect_ids' do

View file

@ -11,7 +11,7 @@ describe User do
@eves_aspect = eve.aspects.first
end
describe "#raw_visible_posts" do
describe "#visible_posts" do
it "returns all the posts the user can see" do
connect_users(eve, @eves_aspect, alice, @alices_aspect)
self_post = alice.post(:status_message, :text => "hi", :to => @alices_aspect.id)
@ -19,7 +19,7 @@ describe User do
dogs = eve.aspects.create(:name => "dogs")
invisible_post = eve.post(:status_message, :text => "foobar", :to => dogs.id)
stream = alice.raw_visible_posts
stream = alice.visible_posts
stream.should include(self_post)
stream.should include(visible_post)
stream.should_not include(invisible_post)
@ -31,28 +31,35 @@ describe User do
(1..25).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 + time_interval
post.updated_at = post.updated_at + time_interval
post.created_at = post.created_at - time_interval
post.updated_at = post.updated_at - time_interval
post.save
time_interval += 1000
end
end
end
it 'works' do #This is in one spec to save time
bob.raw_visible_posts.length.should == 15 #it returns 15 by default
bob.raw_visible_posts.should == bob.raw_visible_posts(:by_members_of => bob.aspects.map{|a| a.id}) # it is the same when joining through aspects
bob.raw_visible_posts.sort_by{|p| p.updated_at}.map{|p| p.id}.should == bob.raw_visible_posts.map{|p| p.id}.reverse #it is sorted updated_at desc by default
bob.visible_posts.length.should == 15 #it returns 15 by default
bob.visible_posts.should == bob.visible_posts(:by_members_of => bob.aspects.map{|a| a.id}) # it is the same when joining through aspects
bob.visible_posts.sort_by{|p| p.updated_at}.map{|p| p.id}.should == bob.visible_posts.map{|p| p.id}.reverse #it is sorted updated_at desc by default
opts = {:limit => 40}
bob.raw_visible_posts(opts).length.should == 40 #it takes a limit
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
bob.visible_posts(opts).length.should == 40 #it takes a limit
bob.visible_posts(opts).should == bob.visible_posts(opts.merge(:by_members_of => bob.aspects.map{|a| a.id}))
bob.visible_posts(opts).sort_by{|p| p.updated_at}.map{|p| p.id}.should == bob.visible_posts(opts).map{|p| p.id}.reverse
opts = {:page => 2}
bob.raw_visible_posts(opts).length.should == 15
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
bob.raw_visible_posts(opts).map{|p|p.id}.should == bob.raw_visible_posts(:limit => 40)[15...30].map{|p|p.id} #pagination should return the right posts
last_time_of_last_page = bob.visible_posts.last.updated_at
opts = {:max_time => last_time_of_last_page}
bob.visible_posts(opts).length.should == 15
bob.visible_posts(opts).map{|p| p.id}.should == bob.visible_posts(opts.merge(:by_members_of => bob.aspects.map{|a| a.id})).map{|p| p.id}
bob.visible_posts(opts).sort_by{|p| p.updated_at}.map{|p| p.id}.should == bob.visible_posts(opts).map{|p| p.id}.reverse
bob.visible_posts(opts).map{|p|p.id}.should == bob.visible_posts(:limit => 40)[15...30].map{|p|p.id} #pagination should return the right posts
opts = {:max_time => last_time_of_last_page.to_i}
bob.visible_posts(opts).length.should == 15
bob.visible_posts(opts).map{|p| p.id}.should == bob.visible_posts(opts.merge(:by_members_of => bob.aspects.map{|a| a.id})).map{|p| p.id}
bob.visible_posts(opts).sort_by{|p| p.updated_at}.map{|p| p.id}.should == bob.visible_posts(opts).map{|p| p.id}.reverse
bob.visible_posts(opts).map{|p|p.id}.should == bob.visible_posts(:limit => 40)[15...30].map{|p|p.id} #pagination should return the right posts
end
end
end
@ -72,7 +79,7 @@ describe User do
describe "#visible_posts" do
it "queries by person id" do
query = eve.raw_visible_posts.where(:author_id => eve.person.id)
query = eve.visible_posts.where(:author_id => eve.person.id)
query.include?(@status_message1).should == true
query.include?(@status_message2).should == true
query.include?(@status_message3).should == false
@ -81,7 +88,7 @@ describe User do
end
it "selects public posts" do
query = eve.raw_visible_posts.where(:public => true)
query = eve.visible_posts.where(:public => true)
query.include?(@status_message1).should == false
query.include?(@status_message2).should == true
query.include?(@status_message3).should == true
@ -90,7 +97,7 @@ describe User do
end
it "selects non public posts" do
query = eve.raw_visible_posts.where(:public => false)
query = eve.visible_posts.where(:public => false)
query.include?(@status_message1).should == true
query.include?(@status_message2).should == false
query.include?(@status_message3).should == false
@ -99,13 +106,13 @@ describe User do
end
it "selects by message contents" do
query = eve.raw_visible_posts.where(:text=> "hi")
query = eve.visible_posts.where(:text=> "hi")
query.should == [@status_message1]
end
it "does not return pending posts" do
@pending_status_message.pending.should be_true
eve.raw_visible_posts.should_not include @pending_status_message
eve.visible_posts.should_not include @pending_status_message
end
it '#find_visible_post_by_id' do