From f9f91a0e9e6457508891c9282dd1dcbf2f85b5a6 Mon Sep 17 00:00:00 2001 From: Benjamin Neff <benjamin@coding4coffee.ch> Date: Fri, 20 May 2016 02:12:36 +0200 Subject: [PATCH] create new contact for local receive --- app/models/contact.rb | 15 +++++++++++++++ app/models/user/connecting.rb | 11 ++++++----- lib/diaspora/federation/dispatcher.rb | 4 +++- lib/diaspora/federation/receive.rb | 9 +-------- spec/models/user/connecting_spec.rb | 14 ++++++-------- 5 files changed, 31 insertions(+), 22 deletions(-) diff --git a/app/models/contact.rb b/app/models/contact.rb index 6480d26274..46e40aa06e 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 c163054e1f..7dddd0b7bb 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 4950cfcb98..f0ffea4f74 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 692026a5cd..aa5c4ee953 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 24afe624f4..9647947b71 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 -- GitLab