diff --git a/app/models/user.rb b/app/models/user.rb index 1593ee29c..f5b9b7ffa 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -54,7 +54,7 @@ class User many :visible_people, :in => :visible_person_ids, :class_name => 'Person' # One of these needs to go many :pending_requests, :in => :pending_request_ids, :class_name => 'Request' many :raw_visible_posts, :in => :visible_post_ids, :class_name => 'Post' - many :aspects, :class_name => 'Aspect', :dependent => :destroy + many :aspects, :class_name => 'Aspect' after_create :seed_aspects @@ -118,6 +118,22 @@ class User false end + def add_person_to_aspect(person_id, aspect_id) + raise "Can not add person to an aspect you do not own" unless aspect = self.aspects.find_by_id(aspect_id) + raise "Can not add person you are not friends with" unless person = self.find_friend_by_id(person_id) + raise 'Can not add person who is already in the aspect' if aspect.person_ids.include?(person_id) + aspect.people << person + aspect.save + end + + def delete_person_from_aspect(person_id, aspect_id) + raise "Can not delete a person from an aspect you do not own" unless aspect = self.aspects.find_by_id(aspect_id) + aspect.person_ids.delete(person_id) + id_array = aspect.posts.find_all_by_person_id(person_id).collect{|x| x.id} + aspect.post_ids = aspect.post_ids - id_array + aspect.save + end + ######## Posting ######## def post(class_name, options = {}) if class_name == :photo diff --git a/lib/diaspora/user/querying.rb b/lib/diaspora/user/querying.rb index 7e6413749..eae34862b 100644 --- a/lib/diaspora/user/querying.rb +++ b/lib/diaspora/user/querying.rb @@ -34,6 +34,11 @@ module Diaspora aspects.detect{|x| x.id == id } end + def find_friend_by_id(id) + id = id.to_id + friends.detect{|x| x.id == id } + end + def aspects_with_post( id ) self.aspects.find_all_by_post_ids( id.to_id ) end diff --git a/spec/models/aspect_spec.rb b/spec/models/aspect_spec.rb index e91d4a258..abb995ebd 100644 --- a/spec/models/aspect_spec.rb +++ b/spec/models/aspect_spec.rb @@ -129,59 +129,129 @@ describe Aspect do end end - describe "aspect editing" do + context "aspect editing" do + let(:aspect) {@user.aspect(:name => 'losers')} + let(:aspect2) {@user2.aspect(:name => 'failures')} + let(:aspect1) {@user.aspect(:name => 'cats')} + let(:not_friend) { Factory(:person, :diaspora_handle => "not@person.com")} + let(:user3) {Factory(:user)} + let(:aspect3) {user3.aspect(:name => "lala")} + before do - @aspect = @user.aspect(:name => 'losers') - @aspect2 = @user2.aspect(:name => 'failures') - friend_users(@user, @aspect, @user2, @aspect2) - @aspect.reload - @aspect3 = @user.aspect(:name => 'cats') + friend_users(@user, aspect, @user2, aspect2) + aspect.reload @user.reload end it 'should be able to move a friend from one of users existing aspects to another' do - @user.move_friend(:friend_id => @user2.person.id, :from => @aspect.id, :to => @aspect3.id) - @aspect.reload - @aspect3.reload + @user.move_friend(:friend_id => @user2.person.id, :from => aspect.id, :to => aspect1.id) + aspect.reload + aspect1.reload - @aspect.person_ids.include?(@user2.person.id).should be false - @aspect3.people.include?(@user2.person).should be true + aspect.person_ids.include?(@user2.person.id).should be false + aspect1.people.include?(@user2.person).should be true end it "should not move a person who is not a friend" do - @user.move_friend(:friend_id => @friend.id, :from => @aspect.id, :to => @aspect3.id) - @aspect.reload - @aspect3.reload - @aspect.people.include?(@friend).should be false - @aspect3.people.include?(@friend).should be false + @user.move_friend(:friend_id => @friend.id, :from => aspect.id, :to => aspect1.id) + aspect.reload + aspect1.reload + aspect.people.include?(@friend).should be false + aspect1.people.include?(@friend).should be false end it "should not move a person to a aspect that's not his" do - @user.move_friend(:friend_id => @user2.person.id, :from => @aspect.id, :to => @aspect2.id) - @aspect.reload - @aspect2.reload - @aspect.people.include?(@user2.person).should be true - @aspect2.people.include?(@user2.person).should be false + @user.move_friend(:friend_id => @user2.person.id, :from => aspect.id, :to => aspect2.id) + aspect.reload + aspect2.reload + aspect.people.include?(@user2.person).should be true + aspect2.people.include?(@user2.person).should be false end - it 'should move all the by that user to the new aspect' do - message = @user2.post(:status_message, :message => "Hey Dude", :to => @aspect2.id) + it 'should move all posts by that user to the new aspect' do + message = @user2.post(:status_message, :message => "Hey Dude", :to => aspect2.id) @user.receive message.to_diaspora_xml, @user2.person - @aspect.reload + aspect.reload - @aspect.posts.count.should == 1 - @aspect3.posts.count.should == 0 + aspect.posts.count.should == 1 + aspect1.posts.count.should == 0 @user.reload - @user.move_friend(:friend_id => @user2.person.id, :from => @aspect.id, :to => @aspect3.id) - @aspect.reload - @aspect3.reload - - @aspect3.posts.count.should == 1 - @aspect.posts.count.should == 0 + @user.move_friend(:friend_id => @user2.person.id, :from => aspect.id, :to => aspect1.id) + aspect.reload + aspect1.reload + aspect1.posts.count.should == 1 + aspect.posts.count.should == 0 end + describe "#add_person_to_aspect" do + it 'adds the user to the aspect' do + aspect1.people.should_not include @user2.person + @user.add_person_to_aspect(@user2.person.id, aspect1.id) + aspect1.reload + aspect1.people.should include @user2.person + end + + it 'raises if its an aspect that the user does not own'do + proc{@user.add_person_to_aspect(@user2.person.id, aspect2.id) }.should raise_error /Can not add person to an aspect you do not own/ + end + + it 'does not allow to have duplicate people in an aspect' do + proc{@user.add_person_to_aspect(not_friend.id, aspect1.id) }.should raise_error /Can not add person you are not friends with/ + end + + it 'does not allow you to add a person if they are already in the aspect' do + proc{@user.add_person_to_aspect(@user2.person.id, aspect.id) }.should raise_error /Can not add person who is already in the aspect/ + end + end + + describe '#delete_person_from_aspect' do + it 'deletes a user from the aspect' do + @user.add_person_to_aspect(@user2.person.id, aspect1.id) + @user.reload + @user.aspects.find_by_id(aspect1.id).people.include?(@user2.person).should be true + @user.delete_person_from_aspect(@user2.person.id, aspect1.id) + @user.reload + @user.aspects.find_by_id(aspect1.id).people.include?(@user2.person).should be false + end + + it 'should check to make sure you have the aspect ' do + proc{@user.delete_person_from_aspect(@user2.person.id, aspect2.id) }.should raise_error /Can not delete a person from an aspect you do not own/ + end + + context 'removing posts' do + before do + friend_users(@user, aspect, user3, aspect3) + + message = @user2.post(:status_message, :message => "Hey Dude", :to => aspect2.id) + @user.receive message.to_diaspora_xml, @user2.person + aspect.reload + aspect.posts.count.should == 1 + end + + it 'should remove the users posts from that aspect' do + @user.reload + @user.delete_person_from_aspect(@user2.person.id, aspect.id) + aspect.reload + aspect.posts.count.should == 0 + end + + it 'should not delete other peoples posts' do + message2 = user3.post(:status_message, :message => "other post", :to => aspect3.id) + + @user.receive message2.to_diaspora_xml, user3.person + + aspect.reload + aspect.posts.count.should == 2 + + @user.reload + @user.delete_person_from_aspect(@user2.person.id, aspect.id) + aspect.reload + aspect.posts.should == [message2] + end + end + end end end diff --git a/spec/models/user/visible_posts_spec.rb b/spec/models/user/visible_posts_spec.rb index 6f4345251..4c06643c1 100644 --- a/spec/models/user/visible_posts_spec.rb +++ b/spec/models/user/visible_posts_spec.rb @@ -23,6 +23,7 @@ describe User do before do friend_users(user, first_aspect, user2, user2.aspects.first) + friend_users(user, second_aspect, user3, user3.aspects.first) end describe "#visible_posts" do @@ -48,7 +49,6 @@ describe User do end it "queries by aspect" do - friend_users(user, second_aspect, user3, user3.aspects.first) friend_users(user, second_aspect, user4, user4.aspects.first) user.receive status_message4.to_diaspora_xml, user2.person @@ -67,6 +67,19 @@ describe User do user.find_visible_post_by_id(status_message1.id).should == nil end end + + describe '#find_friend_by_id' do + it 'should find both friends' do + user.reload + user.find_friend_by_id(user2.person.id).should == user2.person + user.find_friend_by_id(user3.person.id).should == user3.person + end + + it 'should not find a non-friend' do + user3.find_friend_by_id(user4.person.id).should be nil + end + + end end context 'albums' do