diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5b15d3ae2..42abc665c 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -30,7 +30,7 @@ class ApplicationController < ActionController::Base @aspects = current_user.aspects @aspects_dropdown_array = current_user.aspects.collect{|x| [x.to_s, x.id]} - @friends = current_user.friends.map{|c| c.person} + @friends = current_user.person_objects end end diff --git a/app/controllers/aspects_controller.rb b/app/controllers/aspects_controller.rb index 1d6038b42..47e04e62d 100644 --- a/app/controllers/aspects_controller.rb +++ b/app/controllers/aspects_controller.rb @@ -47,7 +47,7 @@ class AspectsController < ApplicationController unless @aspect render :file => "#{Rails.root}/public/404.html", :layout => false, :status => 404 else - @friends = @aspect.people.map{|c| c.person} + @friends = @aspect.person_objects @posts = current_user.visible_posts( :by_members_of => @aspect ).paginate :per_page => 15, :order => 'created_at DESC' respond_with @aspect end diff --git a/app/models/aspect.rb b/app/models/aspect.rb index 782e9ea7c..56210f0db 100644 --- a/app/models/aspect.rb +++ b/app/models/aspect.rb @@ -30,6 +30,11 @@ class Aspect posts.detect{|x| x.person.id == id } end + def person_objects + person_ids = people.map{|x| x.person_id} + Person.all(:id.in => person_ids) + end + def as_json(opts = {}) { :aspect => { diff --git a/app/models/contact.rb b/app/models/contact.rb index b11b591cb..9ae223579 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -15,4 +15,5 @@ class Contact key :aspect_ids, Array, :typecast => 'ObjectId' many :aspects, :in => :aspect_ids, :class_name => 'Aspect' validates_presence_of :aspects + end diff --git a/app/models/user.rb b/app/models/user.rb index f71d7e2ca..82d103034 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -236,7 +236,7 @@ class User push_to_hub(post) if post.respond_to?(:public) && post.public - push_to_people(post, target_contacts.map{|c|c.person}) + push_to_people(post, self.person_objects(target_contacts)) end def push_to_people(post, people) @@ -289,7 +289,8 @@ class User if owns? comment.post comment.post_creator_signature = comment.sign_with_key(encryption_key) comment.save - push_to_people comment, contacts_in_aspects(aspects_with_post(comment.post.id)).map{|c|c.person} + aspects = aspects_with_post(comment.post_id) + push_to_people(comment, people_in_aspects(aspects)) elsif owns? comment comment.save push_to_people comment, [comment.post.person] @@ -303,7 +304,7 @@ class User post.unsocket_from_uid(self.id, :aspect_ids => aspect_ids) if post.respond_to? :unsocket_from_uid retraction = Retraction.for(post) - push_to_people retraction, contacts_in_aspects(aspects_with_post(post.id)).map{|c| c.person} + push_to_people retraction, people_in_aspects(aspects_with_post(post.id)) retraction end diff --git a/lib/diaspora/user/querying.rb b/lib/diaspora/user/querying.rb index f0097a172..157470d03 100644 --- a/lib/diaspora/user/querying.rb +++ b/lib/diaspora/user/querying.rb @@ -33,7 +33,18 @@ module Diaspora end def friends_not_in_aspect( aspect ) - Contact.all(:user_id => self.id, :aspect_ids.ne => aspect._id).map{|c| c.person} + person_ids = Contact.all(:user_id => self.id, :aspect_ids.ne => aspect._id).collect{|x| x.person_id } + Person.all(:id.in => person_ids) + end + + def person_objects(contacts = self.friends) + person_ids = contacts.collect{|x| x.person_id} + Person.all(:id.in => person_ids) + end + + def people_in_aspects(aspects) + person_ids = contacts_in_aspects(aspects).collect{|x| x.person_id} + Person.all(:id.in => person_ids) end def aspect_by_id( id ) @@ -45,6 +56,7 @@ module Diaspora self.aspects.find_all_by_post_ids( id.to_id ) end + def aspects_with_person person contact_for(person).aspects end diff --git a/spec/controllers/aspects_controller_spec.rb b/spec/controllers/aspects_controller_spec.rb index e1695eb3f..b762a49ba 100644 --- a/spec/controllers/aspects_controller_spec.rb +++ b/spec/controllers/aspects_controller_spec.rb @@ -22,7 +22,7 @@ describe AspectsController do it "assigns @friends to all the user's friends" do Factory.create :person get :index - assigns[:friends].should == @user.friends.map{|c| c.person} + assigns[:friends].should == @user.person_objects end end diff --git a/spec/models/user/visible_posts_spec.rb b/spec/models/user/querying_spec.rb similarity index 79% rename from spec/models/user/visible_posts_spec.rb rename to spec/models/user/querying_spec.rb index 77490681a..ad18eb89d 100644 --- a/spec/models/user/visible_posts_spec.rb +++ b/spec/models/user/querying_spec.rb @@ -61,19 +61,47 @@ describe User do let!(:user) {Factory :user} let!(:first_aspect) {user.aspect(:name => 'bruisers')} let!(:second_aspect) {user.aspect(:name => 'losers')} + let!(:user4) { Factory.create(:user_with_aspect)} + + before do + friend_users(user, first_aspect, user4, user4.aspects.first) + friend_users(user, second_aspect, user2, user2.aspects.first) + end describe '#friends_not_in_aspect' do it 'finds the people who are not in the given aspect' do - user4 = Factory.create(:user_with_aspect) - friend_users(user, first_aspect, user4, user4.aspects.first) - friend_users(user, second_aspect, user2, user2.aspects.first) - people = user.friends_not_in_aspect(first_aspect) people.should == [user2.person] end end + describe '#person_objects' do + it 'returns "person" objects for all of my friends' do + people = user.person_objects + people.size.should == 2 + [user4.person, user2.person].each{ |p| people.should include p } + end + + it 'should return people objects given a collection of contacts' do + target_contacts = [user.contact_for(user2.person)] + people = user.person_objects(target_contacts) + people.should == [user2.person] + end + + end + + describe '#people_in_aspects' do + it 'should return people objects for a users friend in each aspect' do + people = user.people_in_aspects([first_aspect]) + people.should == [user4.person] + people = user.people_in_aspects([second_aspect]) + people.should == [user2.person] + end + end end + + + describe '#albums_by_aspect' do let!(:first_aspect) {user2.aspect(:name => 'bruisers')} let!(:second_aspect) {user2.aspect(:name => 'losers')}