From 609439f9485b71924885211f009f41a4bf74bc88 Mon Sep 17 00:00:00 2001 From: Raphael Sofaer Date: Mon, 23 May 2011 18:33:06 -0700 Subject: [PATCH] Destroy notifications on contact destroy --- app/models/contact.rb | 8 ++++++++ lib/diaspora/user/connecting.rb | 4 ++-- spec/models/user/connecting_spec.rb | 15 +++++++++++---- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/app/models/contact.rb b/app/models/contact.rb index 0a59eb215..33ae11fba 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -27,6 +27,14 @@ class Contact < ActiveRecord::Base where(:receiving => true) } + before_destroy :destroy_notifications + def destroy_notifications + Notification.where(:target_type => "Person", + :target_id => person_id, + :recipient_id => user_id, + :type => "Notifications::StartedSharing").delete_all + end + def dispatch_request request = self.generate_request Postzord::Dispatch.new(self.user, request).post diff --git a/lib/diaspora/user/connecting.rb b/lib/diaspora/user/connecting.rb index 9ddc3531d..3c083f821 100644 --- a/lib/diaspora/user/connecting.rb +++ b/lib/diaspora/user/connecting.rb @@ -18,7 +18,7 @@ module Diaspora if notification = Notification.where(:target_id => person.id).first notification.update_attributes(:unread=>false) end - + contact end @@ -44,7 +44,7 @@ module Diaspora retraction = Retraction.for(self) retraction.subscribers = [person]#HAX Postzord::Dispatch.new(self, retraction).post - + AspectMembership.where(:contact_id => bad_contact.id).delete_all remove_contact(bad_contact) end diff --git a/spec/models/user/connecting_spec.rb b/spec/models/user/connecting_spec.rb index 7a1c94883..855c3db72 100644 --- a/spec/models/user/connecting_spec.rb +++ b/spec/models/user/connecting_spec.rb @@ -39,6 +39,13 @@ describe Diaspora::UserModules::Connecting do bob.should_receive(:remove_contact).with(bob.contact_for(alice.person)) bob.disconnected_by(alice.person) end + + it 'removes notitications' do + alice.share_with(eve.person, alice.aspects.first) + Notifications::StartedSharing.where(:recipient_id => eve.id).first.should_not be_nil + eve.disconnected_by(alice.person) + Notifications::StartedSharing.where(:recipient_id => eve.id).first.should be_nil + end end describe '#disconnect' do @@ -58,7 +65,7 @@ describe Diaspora::UserModules::Connecting do end it 'should remove the contact from all aspects they are in' do - contact = alice.contact_for(bob.person) + contact = alice.contact_for(bob.person) new_aspect = alice.aspects.create(:name => 'new') alice.add_contact_to_aspect(contact, new_aspect) @@ -75,7 +82,7 @@ describe Diaspora::UserModules::Connecting do alice.share_with(eve.person, alice.aspects.first) }.should change(alice.contacts, :count).by(1) end - + it 'does not set mutual on intial share request' do alice.share_with(eve.person, alice.aspects.first) alice.contacts.find_by_person_id(eve.person.id).should_not be_mutual @@ -87,7 +94,7 @@ describe Diaspora::UserModules::Connecting do alice.contacts.find_by_person_id(eve.person.id).should be_mutual end - + it 'adds a contact to an aspect' do contact = alice.contacts.create(:person => eve.person) alice.contacts.stub!(:find_or_initialize_by_person_id).and_return(contact) @@ -126,7 +133,7 @@ describe Diaspora::UserModules::Connecting do 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