add #object_to_receive to Diaspora::Federated::Base

This commit is contained in:
Benjamin Neff 2016-06-20 03:04:17 +02:00
parent 1cf11db634
commit bb0d7e46dc
5 changed files with 34 additions and 11 deletions

View file

@ -3,6 +3,8 @@
# the COPYRIGHT file.
class Contact < ActiveRecord::Base
include Diaspora::Federated::Base
belongs_to :user
validates :user, presence: true
@ -85,7 +87,7 @@ class Contact < ActiveRecord::Base
user.share_with(person, user.auto_follow_back_aspect) if user.auto_follow_back && !receiving
end
# object for local recipient
# object for local recipients
def object_to_receive
Contact.create_or_update_sharing_contact(person.owner, user.person)
end

View file

@ -7,6 +7,11 @@
module Diaspora
module Federated
module Base
# object for local recipients
def object_to_receive
self
end
# @abstract
# @note this must return [Array<Person>]
# @return [Array<Person>]

View file

@ -42,9 +42,9 @@ module Diaspora
end
def deliver_to_local(people)
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))
object_to_receive = object.object_to_receive
return unless object_to_receive
Workers::ReceiveLocal.perform_async(object_to_receive.class.to_s, object_to_receive.id, people.map(&:owner_id))
end
def deliver_to_remote(_people)

View file

@ -5,15 +5,21 @@
require "spec_helper"
describe Diaspora::Federated::Base do
class Foo
include Diaspora::Federated::Base
end
let(:foo) { Foo.new }
describe "#object_to_receive" do
it "returns self" do
expect(foo.object_to_receive).to eq(foo)
end
end
describe "#subscribers" do
it "throws an error if the including module does not redefine it" do
class Foo
include Diaspora::Federated::Base
end
f = Foo.new
expect { f.subscribers }.to raise_error(/override subscribers/)
expect { foo.subscribers }.to raise_error(/override subscribers/)
end
end
end

View file

@ -210,6 +210,16 @@ describe Contact, type: :model do
end
end
describe "#object_to_receive" do
it "returns the contact for the recipient" do
user = FactoryGirl.create(:user)
contact = alice.contacts.create(person: user.person)
receive = contact.object_to_receive
expect(receive.user).to eq(user)
expect(receive.person).to eq(alice.person)
end
end
describe "#subscribers" do
it "returns an array with recipient of the contact" do
contact = alice.contacts.create(person: eve.person)