diff --git a/app/models/contact.rb b/app/models/contact.rb index 6480d26274d43d2a6e9fcdcaa2f5b33ebc9599c8..46e40aa06ed557f2719c876b207ac80dc2610559 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -85,11 +85,26 @@ class Contact < ActiveRecord::Base user.share_with(person, user.auto_follow_back_aspect) if user.auto_follow_back && !receiving end + # object for local recipient + def object_to_receive + Contact.create_or_update_sharing_contact(person.owner, user.person) + end + # @return [Array<Person>] The recipient of the contact def subscribers [person] end + # creates or updates a contact with active sharing flag. Returns nil if already sharing. + def self.create_or_update_sharing_contact(recipient, sender) + contact = recipient.contacts.find_or_initialize_by(person_id: sender.id) + + return if contact.sharing + + contact.update(sharing: true) + contact + end + private def not_contact_with_closed_account diff --git a/app/models/user/connecting.rb b/app/models/user/connecting.rb index c163054e1fa3bf56e0820b34323479bdafd18ab4..7dddd0b7bbd48b4af384f3f8b7a792d782f39e3e 100644 --- a/app/models/user/connecting.rb +++ b/app/models/user/connecting.rb @@ -12,14 +12,15 @@ class User contact = contacts.find_or_initialize_by(person_id: person.id) return false unless contact.valid? - unless contact.receiving? - # TODO: dispatch - contact.receiving = true - end - + needs_dispatch = !contact.receiving? + contact.receiving = true contact.aspects << aspect contact.save + if needs_dispatch + Diaspora::Federation::Dispatcher.defer_dispatch(self, contact) + end + Notifications::StartedSharing.where(recipient_id: id, target: person.id, unread: true) .update_all(unread: false) diff --git a/lib/diaspora/federation/dispatcher.rb b/lib/diaspora/federation/dispatcher.rb index 4950cfcb984c23ea0f7b836b1a3a646a2984c3db..f0ffea4f7456ff02319b8b22c130eb919cab7d4e 100644 --- a/lib/diaspora/federation/dispatcher.rb +++ b/lib/diaspora/federation/dispatcher.rb @@ -39,7 +39,9 @@ module Diaspora end def deliver_to_local(people) - Workers::ReceiveLocal.perform_async(object.class.to_s, object.id, people.map(&:owner_id)) + obj = object.respond_to?(:object_to_receive) ? object.object_to_receive : object + return unless obj + Workers::ReceiveLocal.perform_async(obj.class.to_s, obj.id, people.map(&:owner_id)) end def deliver_to_remote(people) diff --git a/lib/diaspora/federation/receive.rb b/lib/diaspora/federation/receive.rb index 692026a5cdfd14b0e06ddf8e7a3608e622963334..aa5c4ee9531a39246ff5f9c60c0818543ee1769b 100644 --- a/lib/diaspora/federation/receive.rb +++ b/lib/diaspora/federation/receive.rb @@ -22,14 +22,7 @@ module Diaspora def self.contact(entity) recipient = Person.find_by(diaspora_handle: entity.recipient).owner - contact = recipient.contacts.find_or_initialize_by(person_id: author_of(entity).id) - - return if contact.sharing - - contact.tap do |contact| - contact.sharing = true - contact.save! - end + Contact.create_or_update_sharing_contact(recipient, author_of(entity)) end def self.conversation(entity) diff --git a/spec/models/user/connecting_spec.rb b/spec/models/user/connecting_spec.rb index 24afe624f44ac5254c9ffbf8095a653b2e86f47a..9647947b717c6cb31d35bf686b3eb9bcb6273dbe 100644 --- a/spec/models/user/connecting_spec.rb +++ b/spec/models/user/connecting_spec.rb @@ -109,7 +109,6 @@ describe User::Connecting, type: :model do end it "does set mutual on share-back request" do - skip # TODO eve.share_with(alice.person, eve.aspects.first) alice.share_with(eve.person, alice.aspects.first) @@ -127,25 +126,23 @@ describe User::Connecting, type: :model do context "dispatching" do it "dispatches a request on initial request" do - skip # TODO - contact = alice.contacts.new(person: eve.person) expect(alice.contacts).to receive(:find_or_initialize_by).and_return(contact) - # TODO: expect(contact).to receive(:dispatch_request) + allow(Diaspora::Federation::Dispatcher).to receive(:defer_dispatch) + expect(Diaspora::Federation::Dispatcher).to receive(:defer_dispatch).with(alice, contact) alice.share_with(eve.person, alice.aspects.first) end it "dispatches a request on a share-back" do - skip # TODO - eve.share_with(alice.person, eve.aspects.first) contact = alice.contact_for(eve.person) expect(alice.contacts).to receive(:find_or_initialize_by).and_return(contact) - # TODO: expect(contact).to receive(:dispatch_request) + allow(Diaspora::Federation::Dispatcher).to receive(:defer_dispatch) + expect(Diaspora::Federation::Dispatcher).to receive(:defer_dispatch).with(alice, contact) alice.share_with(eve.person, alice.aspects.first) end @@ -154,7 +151,8 @@ describe User::Connecting, type: :model do contact = alice.contacts.create(person: eve.person, receiving: true) allow(alice.contacts).to receive(:find_or_initialize_by).and_return(contact) - # TODO: expect(contact).not_to receive(:dispatch_request) + allow(Diaspora::Federation::Dispatcher).to receive(:defer_dispatch).with(alice, instance_of(Profile)) + expect(Diaspora::Federation::Dispatcher).not_to receive(:defer_dispatch).with(alice, instance_of(Contact)) alice.share_with(eve.person, aspect2) end