From 8816bd7f91c5ad8b95836d856a690c17778b4263 Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Mon, 28 Mar 2011 18:44:10 -0700 Subject: [PATCH] 8 failures left. wip. --- app/controllers/apis_controller.rb | 5 +---- app/helpers/application_helper.rb | 4 ++-- app/models/contact.rb | 5 +++++ app/models/post.rb | 6 ++---- lib/collect_user_photos.rb | 2 +- lib/diaspora/user/querying.rb | 5 ++--- spec/integration/receiving_spec.rb | 10 ++++++++-- 7 files changed, 21 insertions(+), 16 deletions(-) diff --git a/app/controllers/apis_controller.rb b/app/controllers/apis_controller.rb index 4060ebe20..1a24696c5 100644 --- a/app/controllers/apis_controller.rb +++ b/app/controllers/apis_controller.rb @@ -30,10 +30,7 @@ class ApisController < ApplicationController #We should start with this versione def home_timeline set_defaults - - aspect_ids = current_user.aspects.map{|a| a.id} - timeline = StatusMessage.joins(:aspects).where(:pending => false, - :aspects => {:id => aspect_ids}).includes(:comments, :photos, :likes, :dislikes).select('DISTINCT `posts`.*').paginate( + timeline = current_user.raw_visible_posts.includes(:comments, :photos, :likes, :dislikes).paginate( :page => params[:page], :per_page => params[:per_page], :order => "#{params[:order]} DESC") respond_with timeline do |format| diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index c1b018221..944831697 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -21,13 +21,13 @@ module ApplicationHelper def aspects_with_post aspects, post aspects.select do |aspect| - PostVisibility.exists?(:aspect_id => aspect.id, :post_id => post.id) + AspectVisibility.exists?(:aspect_id => aspect.id, :post_id => post.id) end end def aspects_without_post aspects, post aspects.reject do |aspect| - PostVisibility.exists?(:aspect_id => aspect.id, :post_id => post.id) + AspectVisibility.exists?(:aspect_id => aspect.id, :post_id => post.id) end end diff --git a/app/models/contact.rb b/app/models/contact.rb index aa6d50f0f..bfbdccc7b 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -32,6 +32,11 @@ class Contact < ActiveRecord::Base :aspect => aspects.first) end + def receive_post(post) + PostVisibility.create!(:post_id => post.id, :contact_id => self.id) + post.socket_to_user(self.user, :aspect_ids => self.aspect_ids) if post.respond_to? :socket_to_user + end + def contacts people = Person.arel_table incoming_aspects = Aspect.joins(:contacts).where( diff --git a/app/models/post.rb b/app/models/post.rb index b26f7b283..4d32311f8 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -84,16 +84,14 @@ class Post < ActiveRecord::Base Rails.logger.info("event=receive payload_type=#{self.class} update=true status=abort sender=#{self.diaspora_handle} reason=immutable existing_post=#{known_post.id}") end else - contact = user.contact_for(person) - PostVisibility.create(:post_id => self.id, :contact_id => contact.id) + user.contact_for(person).receive_post(local_post) user.notify_if_mentioned(local_post) Rails.logger.info("event=receive payload_type=#{self.class} update=true status=complete sender=#{self.diaspora_handle} existing_post=#{local_post.id}") return local_post end elsif !local_post self.save - contact = user.contact_for(person) - PostVisibility.create(:post_id => self.id, :contact_id => contact.id) + user.contact_for(person).receive_post(self) user.notify_if_mentioned(self) Rails.logger.info("event=receive payload_type=#{self.class} update=false status=complete sender=#{self.diaspora_handle}") return self diff --git a/lib/collect_user_photos.rb b/lib/collect_user_photos.rb index 31c855441..358ce4e0c 100644 --- a/lib/collect_user_photos.rb +++ b/lib/collect_user_photos.rb @@ -5,7 +5,7 @@ module PhotoMover FileUtils::mkdir_p temp_dir Dir.chdir 'tmp/exports' - photos = user.raw_visible_posts(:author_id => user.person.id, :type => 'Photo') + photos = user.raw_visible_posts.where(:author_id => user.person.id, :type => 'Photo') photos_dir = "#{user.id}/photos" FileUtils::mkdir_p photos_dir diff --git a/lib/diaspora/user/querying.rb b/lib/diaspora/user/querying.rb index 35d2c2d79..cd3b6e930 100644 --- a/lib/diaspora/user/querying.rb +++ b/lib/diaspora/user/querying.rb @@ -20,9 +20,7 @@ module Diaspora end def visible_photos - p = Photo.arel_table - Photo.joins(:aspects).where(p[:status_message_id].not_eq(nil).or(p[:pending].eq(false)) - ).where(:aspects => {:user_id => self.id}).select('DISTINCT `posts`.*').order("posts.updated_at DESC") + raw_visible_posts.where(:type => 'Photo') end def contact_for(person) @@ -70,6 +68,7 @@ module Diaspora end def posts_from(person) + return self.person.posts.where(:pending => false) if person == self.person con = Contact.arel_table p = Post.arel_table post_ids = [] diff --git a/spec/integration/receiving_spec.rb b/spec/integration/receiving_spec.rb index cc0b9cca7..2865cef6d 100644 --- a/spec/integration/receiving_spec.rb +++ b/spec/integration/receiving_spec.rb @@ -154,11 +154,12 @@ describe 'a user receives a post' do @status_message = @user2.post :status_message, :text => "hi", :to => @aspect2.id @user1.reload @aspect.reload + @contact = @user1.contact_for(@user2.person) end - it "adds a received post to the aspect and visible_posts array" do + it "adds a received post to the the contact" do @user1.raw_visible_posts.include?(@status_message).should be_true - @aspect.posts.include?(@status_message).should be_true + @contact.posts.include?(@status_message).should be_true end it 'removes posts upon disconnecting' do @@ -174,6 +175,11 @@ describe 'a user receives a post' do @post = Factory.create(:status_message, :author => @person) @post.post_visibilities.should be_empty receive_with_zord(@user1, @person, @post.to_diaspora_xml) + @contact = @user1.contact_for(@person) + @contact.post_visibilities.reset + @contact.posts(true).should include(@post) + @post.post_visibilities.reset + end it 'deletes a post if the noone links to it' do