From 97d8b34599706250d6d28c975674c0f8a868d4f3 Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Tue, 29 Mar 2011 11:34:18 -0700 Subject: [PATCH] Fix all specs, add foreign key constraints on post_visibility --- app/controllers/aspects_controller.rb | 5 ++--- app/controllers/photos_controller.rb | 6 +++--- app/models/post.rb | 1 + app/models/post_visibility.rb | 5 ----- ...328202414_post_visibilities_on_contacts.rb | 2 ++ db/schema.rb | 3 +++ lib/diaspora/user/connecting.rb | 3 +-- lib/diaspora/user/querying.rb | 21 +++++++++++++------ spec/integration/receiving_spec.rb | 3 ++- 9 files changed, 29 insertions(+), 20 deletions(-) diff --git a/app/controllers/aspects_controller.rb b/app/controllers/aspects_controller.rb index c6318ea3c..d2d5df483 100644 --- a/app/controllers/aspects_controller.rb +++ b/app/controllers/aspects_controller.rb @@ -26,9 +26,8 @@ class AspectsController < ApplicationController @selected_contacts = @aspects.map { |aspect| aspect.contacts }.flatten.uniq @aspect_ids = @aspects.map { |a| a.id } - @posts = StatusMessage.joins(:aspects).where(:pending => false, - :aspects => {:id => @aspect_ids}).includes(:comments, :photos, :likes, :dislikes).select('DISTINCT `posts`.*').paginate( - :page => params[:page], :per_page => 15, :order => session[:sort_order] + ' DESC') + @posts = current_user.raw_visible_posts(:by_members_of => @aspect_ids, :type => 'StatusMessage').includes( + :comments, :likes, :dislikes).paginate(:page => params[:page], :per_page => 15, :order => session[:sort_order] + ' DESC') @fakes = PostsFake.new(@posts) @contact_count = current_user.contacts.count diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index d843be48c..f6161e8a3 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -141,10 +141,10 @@ class PhotosController < ApplicationController end def show - @photo = current_user.visible_photos.where(:id => params[:id]).includes(:author, :status_message => :photos).first - @photo ||= Photo.where(:public => true, :id => params[:id]).includes(:author, :status_message => :photos).first + @photo = current_user.visible_photos.where(:id => params[:id]).first + @photo ||= Photo.where(:public => true, :id => params[:id]).first if @photo - @parent = @photo.status_message + @parent = StatusMessage.where(:id => @photo.status_message_id).includes(:photos).first if @photo.status_message_id #if photo is not an attachment, fetch comments for self if @parent diff --git a/app/models/post.rb b/app/models/post.rb index 4d32311f8..0040843ee 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -23,6 +23,7 @@ class Post < ActiveRecord::Base has_many :post_visibilities has_many :contacts, :through => :post_visibilities has_many :mentions, :dependent => :destroy + belongs_to :author, :class_name => 'Person' cattr_reader :per_page diff --git a/app/models/post_visibility.rb b/app/models/post_visibility.rb index 2ab7d9bdd..69122f046 100644 --- a/app/models/post_visibility.rb +++ b/app/models/post_visibility.rb @@ -3,11 +3,6 @@ # the COPYRIGHT file. class PostVisibility < ActiveRecord::Base - belongs_to :contact - validates_presence_of :contact - belongs_to :post - validates_presence_of :post - end diff --git a/db/migrate/20110328202414_post_visibilities_on_contacts.rb b/db/migrate/20110328202414_post_visibilities_on_contacts.rb index 2f7879f91..2d7182501 100644 --- a/db/migrate/20110328202414_post_visibilities_on_contacts.rb +++ b/db/migrate/20110328202414_post_visibilities_on_contacts.rb @@ -50,6 +50,8 @@ SQL remove_index :post_visibilities, [:aspect_id, :post_id] remove_column :post_visibilities, :aspect_id + add_foreign_key :post_visibilities, :contacts, :dependent => :delete + add_foreign_key :post_visibilities, :posts, :dependent => :delete end def self.down diff --git a/db/schema.rb b/db/schema.rb index 125e9f6c3..adf7fd928 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -389,6 +389,9 @@ ActiveRecord::Schema.define(:version => 20110328202414) do add_foreign_key "notification_actors", "notifications", :name => "notification_actors_notification_id_fk", :dependent => :delete + add_foreign_key "post_visibilities", "contacts", :name => "post_visibilities_contact_id_fk", :dependent => :delete + add_foreign_key "post_visibilities", "posts", :name => "post_visibilities_post_id_fk", :dependent => :delete + add_foreign_key "posts", "people", :name => "posts_author_id_fk", :column => "author_id", :dependent => :delete add_foreign_key "profiles", "people", :name => "profiles_person_id_fk", :dependent => :delete diff --git a/lib/diaspora/user/connecting.rb b/lib/diaspora/user/connecting.rb index 7a32767d0..e765d2bcd 100644 --- a/lib/diaspora/user/connecting.rb +++ b/lib/diaspora/user/connecting.rb @@ -85,13 +85,12 @@ module Diaspora def remove_contact(contact) bad_person_id = contact.person_id posts = contact.posts.all - contact.post_visibilities.delete_all + contact.destroy posts.each do |post| if post.user_refs < 1 post.destroy end end - contact.destroy end def disconnected_by(person) diff --git a/lib/diaspora/user/querying.rb b/lib/diaspora/user/querying.rb index cd3b6e930..31d71afe9 100644 --- a/lib/diaspora/user/querying.rb +++ b/lib/diaspora/user/querying.rb @@ -10,17 +10,26 @@ module Diaspora self.raw_visible_posts.where(:id => id).includes({:author => :profile}, {:comments => {:author => :profile}}, :photos).first end - def raw_visible_posts - post_ids = [] - post_ids = Post.joins(:contacts).where(:contacts => {:user_id => self.id}).map{|p| p.id} + def raw_visible_posts(opts = {}) + opts[:type] ||= ['StatusMessage', 'Photo'] - post_ids += Post.joins(:aspect_visibilities => :aspect).where(:aspects => {:user_id => self.id}).select('posts.id').map{|p| p.id} + posts_from_others = Post.joins(:contacts).where(:contacts => {:user_id => self.id}) + posts_from_self = self.person.posts.joins(:aspect_visibilities => :aspect).where(:aspects => {:user_id => self.id}) - Post.where(:id => post_ids, :pending => false).select('DISTINCT `posts`.*') + if opts[:by_members_of] + posts_from_others = posts_from_others.joins(:contacts => :aspect_memberships).where( + :aspect_memberships => {:aspect_id => opts[:by_members_of]}) + posts_from_self = posts_from_self.where(:aspects => {:id => opts[:by_members_of]}) + end + + post_ids = posts_from_others.select('posts.id').map{|p| p.id} + post_ids += posts_from_self.select('posts.id').map{|p| p.id} + + Post.where(:id => post_ids, :pending => false, :type => opts[:type]).select('DISTINCT `posts`.*') end def visible_photos - raw_visible_posts.where(:type => 'Photo') + raw_visible_posts(:type => 'Photo') end def contact_for(person) diff --git a/spec/integration/receiving_spec.rb b/spec/integration/receiving_spec.rb index 2865cef6d..849512032 100644 --- a/spec/integration/receiving_spec.rb +++ b/spec/integration/receiving_spec.rb @@ -179,7 +179,6 @@ describe 'a user receives a post' do @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 @@ -197,9 +196,11 @@ describe 'a user receives a post' do it 'should keep track of user references for one person ' do @status_message.reload @status_message.user_refs.should == 3 + @status_message.contacts(true).should include(@contact) @user1.disconnect(@contact) @status_message.reload + @status_message.contacts(true).should_not include(@contact) @status_message.post_visibilities.reset @status_message.user_refs.should == 2 end