diff --git a/app/models/contact.rb b/app/models/contact.rb index e1f78d5fe..e7d747ee9 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -16,6 +16,7 @@ class Contact < ActiveRecord::Base has_many :posts, :through => :post_visibilities validate :not_contact_for_self + validates_uniqueness_of :person_id, :scope => :user_id def dispatch_request @@ -46,12 +47,8 @@ class Contact < ActiveRecord::Base :aspect_memberships => {:aspect_id => incoming_aspect_ids}).where(people[:id].not_eq(self.user.person.id)).select('DISTINCT people.*') end - def sharing? - self.persisted? && (self.mutual? || self.aspect_memberships.size == 0) - end - - def receiving? - self.aspect_memberships.size > 0 + def mutual? + self.sharing && self.receiving end private diff --git a/app/models/request.rb b/app/models/request.rb index 1308a8252..775d79361 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -62,11 +62,8 @@ class Request contact = user.contacts.find_or_initialize_by_person_id(self.sender.id) - if contact.receiving? - contact.update_attributes(:mutual => true) - else - contact.save - end + contact.sharing = true + contact.save self end diff --git a/app/views/aspects/_aspect_listings.haml b/app/views/aspects/_aspect_listings.haml index cda632f92..7e0d91bb7 100644 --- a/app/views/aspects/_aspect_listings.haml +++ b/app/views/aspects/_aspect_listings.haml @@ -11,3 +11,4 @@ %ul - for aspect in aspects = render 'aspects/aspect', :aspect => aspect, :contacts => aspect.contacts + diff --git a/db/migrate/20110405171412_contact_remove_pending_add_mutual.rb b/db/migrate/20110405171412_contact_remove_pending_add_sharing_and_receiving.rb similarity index 74% rename from db/migrate/20110405171412_contact_remove_pending_add_mutual.rb rename to db/migrate/20110405171412_contact_remove_pending_add_sharing_and_receiving.rb index 8c06a2124..a3cc57b59 100644 --- a/db/migrate/20110405171412_contact_remove_pending_add_mutual.rb +++ b/db/migrate/20110405171412_contact_remove_pending_add_sharing_and_receiving.rb @@ -1,10 +1,11 @@ -class ContactRemovePendingAddMutual < ActiveRecord::Migration +class ContactRemovePendingAddSharingAndReceiving < ActiveRecord::Migration def self.up - add_column :contacts, :mutual, :boolean, :default => false, :null => false + add_column :contacts, :sharing, :boolean, :default => false, :null => false + add_column :contacts, :receiving, :boolean, :default => false, :null => false execute( < 20110421120744) do t.datetime "created_at" t.datetime "updated_at" t.string "mongo_id" - t.boolean "mutual", :default => false, :null => false + t.boolean "sharing", :default => false, :null => false + t.boolean "receiving", :default => false, :null => false end add_index "contacts", ["mongo_id"], :name => "index_contacts_on_mongo_id" diff --git a/lib/diaspora/user/connecting.rb b/lib/diaspora/user/connecting.rb index 250291b24..0b65d8c68 100644 --- a/lib/diaspora/user/connecting.rb +++ b/lib/diaspora/user/connecting.rb @@ -9,11 +9,9 @@ module Diaspora contact = self.contacts.find_or_initialize_by_person_id(person.id) unless contact.receiving? contact.dispatch_request - - if contact.sharing? - contact.mutual = true - end + contact.receiving = true end + contact.aspects << aspect contact.save @@ -27,10 +25,10 @@ module Diaspora def remove_contact(contact, opts={:force => false}) posts = contact.posts.all - if !contact.mutual || opts[:force] + if !contact.mutual? || opts[:force] contact.destroy else - contact.update_attributes(:mutual => false) + contact.update_attributes(:sharing => false) end posts.each do |p| diff --git a/spec/helper_methods.rb b/spec/helper_methods.rb index 2f992628d..fe36f82eb 100644 --- a/spec/helper_methods.rb +++ b/spec/helper_methods.rb @@ -5,11 +5,13 @@ module HelperMethods def connect_users(user1, aspect1, user2, aspect2) user1.contacts.create!(:person => user2.person, :aspects => [aspect1], - :mutual => true) + :sharing => true, + :receiving => true) user2.contacts.create!(:person => user1.person, :aspects => [aspect2], - :mutual => true) + :sharing => true, + :receiving => true) end def stub_success(address = 'abc@example.com', opts = {}) diff --git a/spec/models/contact_spec.rb b/spec/models/contact_spec.rb index 796c1684c..1b776391d 100644 --- a/spec/models/contact_spec.rb +++ b/spec/models/contact_spec.rb @@ -13,7 +13,7 @@ describe Contact do end end - describe 'validations' do + context 'validations' do let(:contact){Contact.new} it 'requires a user' do @@ -44,6 +44,20 @@ describe Contact do contact.person = person contact.should_not be_valid end + + it 'validates that sharing and aspect membership count are consistant' do + pending + person = Factory(:person) + + contact2 = alice.contacts.create(:person=>person) + contact2.should be_valid + + contact2.aspect_memberships.create(:aspect => alice.aspects.first) + contact2.should_not be_sharing + contact2.should_not be_valid + + + end end describe '#contacts' do @@ -99,7 +113,6 @@ describe Contact do @contact.contacts.should == [] end end - end context 'requesting' do @@ -132,38 +145,4 @@ describe Contact do end end end - - context 'sharing/receiving status' do - before do - alice.share_with(eve.person, alice.aspects.first) - - @follower = eve.contact_for(alice.person) - @following = alice.contact_for(eve.person) - end - - describe '#sharing?' do - it 'returns true if contact has no aspect visibilities' do - @follower.should be_sharing - end - - it 'returns false if contact has aspect visibilities' do - @following.should_not be_sharing - end - - it 'returns false if contact is not persisted' do - Contact.new.should_not be_sharing - end - end - - describe '#receiving?' do - it 'returns false if contact has no aspect visibilities' do - @follower.should_not be_receiving - end - - it 'returns true if contact has aspect visibilities' do - @following.should be_receiving - end - end - end - end diff --git a/spec/models/request_spec.rb b/spec/models/request_spec.rb index 974d5a617..b0fd2a5d5 100644 --- a/spec/models/request_spec.rb +++ b/spec/models/request_spec.rb @@ -86,6 +86,12 @@ describe Request do }.from(false).to(true) end + + it 'sets sharing' do + Request.diaspora_initialize(:from => eve.person, :to => alice.person, + :into => eve.aspects.first).receive(alice, eve.person) + alice.contact_for(eve.person).should be_sharing + end end context 'xml' do diff --git a/spec/models/user/connecting_spec.rb b/spec/models/user/connecting_spec.rb index 4ff3d86a4..181982e88 100644 --- a/spec/models/user/connecting_spec.rb +++ b/spec/models/user/connecting_spec.rb @@ -28,9 +28,9 @@ describe Diaspora::UserModules::Connecting do end it 'removes a contacts mutual flag' do - bob.contacts.find_by_person_id(alice.person.id).mutual.should be_true + bob.contacts.find_by_person_id(alice.person.id).should be_mutual bob.remove_contact(bob.contact_for(alice.person)) - bob.contacts(true).find_by_person_id(alice.person.id).mutual.should be_false + bob.contacts(true).find_by_person_id(alice.person.id).should_not be_mutual end end @@ -83,8 +83,8 @@ describe Diaspora::UserModules::Connecting do it 'does set mutual on share-back request' do eve.share_with(alice.person, eve.aspects.first) - alice.share_with(eve.person, alice.aspects.first) + alice.contacts.find_by_person_id(eve.person.id).should be_mutual end @@ -116,16 +116,21 @@ describe Diaspora::UserModules::Connecting do alice.share_with(eve.person, alice.aspects.first) end - it 'does not dispatch a request on adding to aspect aspect' do + it 'does not dispatch a request if contact already marked as receiving' do a2 = alice.aspects.create(:name => "two") - contact = alice.contacts.create(:person => eve.person, :aspects => [eve.aspects.first]) + contact = alice.contacts.create(:person => eve.person, :receiving => true) alice.contacts.stub!(:find_or_initialize_by_person_id).and_return(contact) contact.should_not_receive(:dispatch_request) alice.share_with(eve.person, a2) end end + + it 'sets receiving' do + alice.share_with(eve.person, alice.aspects.first) + alice.contact_for(eve.person).should be_receiving + end it "should mark the corresponding notification as 'read'" do notification = Factory.create(:notification, :target => eve.person)