create new contact for local receive

This commit is contained in:
Benjamin Neff 2016-05-20 02:12:36 +02:00
parent 970e8bb3ec
commit f9f91a0e9e
5 changed files with 31 additions and 22 deletions

View file

@ -85,11 +85,26 @@ class Contact < ActiveRecord::Base
user.share_with(person, user.auto_follow_back_aspect) if user.auto_follow_back && !receiving user.share_with(person, user.auto_follow_back_aspect) if user.auto_follow_back && !receiving
end 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 # @return [Array<Person>] The recipient of the contact
def subscribers def subscribers
[person] [person]
end 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 private
def not_contact_with_closed_account def not_contact_with_closed_account

View file

@ -12,14 +12,15 @@ class User
contact = contacts.find_or_initialize_by(person_id: person.id) contact = contacts.find_or_initialize_by(person_id: person.id)
return false unless contact.valid? return false unless contact.valid?
unless contact.receiving? needs_dispatch = !contact.receiving?
# TODO: dispatch contact.receiving = true
contact.receiving = true
end
contact.aspects << aspect contact.aspects << aspect
contact.save contact.save
if needs_dispatch
Diaspora::Federation::Dispatcher.defer_dispatch(self, contact)
end
Notifications::StartedSharing.where(recipient_id: id, target: person.id, unread: true) Notifications::StartedSharing.where(recipient_id: id, target: person.id, unread: true)
.update_all(unread: false) .update_all(unread: false)

View file

@ -39,7 +39,9 @@ module Diaspora
end end
def deliver_to_local(people) 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 end
def deliver_to_remote(people) def deliver_to_remote(people)

View file

@ -22,14 +22,7 @@ module Diaspora
def self.contact(entity) def self.contact(entity)
recipient = Person.find_by(diaspora_handle: entity.recipient).owner recipient = Person.find_by(diaspora_handle: entity.recipient).owner
contact = recipient.contacts.find_or_initialize_by(person_id: author_of(entity).id) Contact.create_or_update_sharing_contact(recipient, author_of(entity))
return if contact.sharing
contact.tap do |contact|
contact.sharing = true
contact.save!
end
end end
def self.conversation(entity) def self.conversation(entity)

View file

@ -109,7 +109,6 @@ describe User::Connecting, type: :model do
end end
it "does set mutual on share-back request" do it "does set mutual on share-back request" do
skip # TODO
eve.share_with(alice.person, eve.aspects.first) eve.share_with(alice.person, eve.aspects.first)
alice.share_with(eve.person, alice.aspects.first) alice.share_with(eve.person, alice.aspects.first)
@ -127,25 +126,23 @@ describe User::Connecting, type: :model do
context "dispatching" do context "dispatching" do
it "dispatches a request on initial request" do it "dispatches a request on initial request" do
skip # TODO
contact = alice.contacts.new(person: eve.person) contact = alice.contacts.new(person: eve.person)
expect(alice.contacts).to receive(:find_or_initialize_by).and_return(contact) 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) alice.share_with(eve.person, alice.aspects.first)
end end
it "dispatches a request on a share-back" do it "dispatches a request on a share-back" do
skip # TODO
eve.share_with(alice.person, eve.aspects.first) eve.share_with(alice.person, eve.aspects.first)
contact = alice.contact_for(eve.person) contact = alice.contact_for(eve.person)
expect(alice.contacts).to receive(:find_or_initialize_by).and_return(contact) 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) alice.share_with(eve.person, alice.aspects.first)
end end
@ -154,7 +151,8 @@ describe User::Connecting, type: :model do
contact = alice.contacts.create(person: eve.person, receiving: true) contact = alice.contacts.create(person: eve.person, receiving: true)
allow(alice.contacts).to receive(:find_or_initialize_by).and_return(contact) 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) alice.share_with(eve.person, aspect2)
end end