delete old postzord dispatcher
This commit is contained in:
parent
bb66b973a6
commit
f95e0faa37
8 changed files with 0 additions and 505 deletions
|
|
@ -14,7 +14,6 @@ require 'diaspora'
|
|||
require 'direction_detector'
|
||||
require 'email_inviter'
|
||||
require 'evil_query'
|
||||
require 'postzord'
|
||||
require 'publisher'
|
||||
require 'pubsubhubbub'
|
||||
require 'stream'
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
# Copyright (c) 2010-2011, Diaspora Inc. This file is
|
||||
# licensed under the Affero General Public License version 3 or later. See
|
||||
# the COPYRIGHT file.
|
||||
|
||||
module Postzord
|
||||
require 'postzord/dispatcher'
|
||||
end
|
||||
|
|
@ -1,142 +0,0 @@
|
|||
# Copyright (c) 2010-2011, Diaspora Inc. This file is
|
||||
# licensed under the Affero General Public License version 3 or later. See
|
||||
# the COPYRIGHT file.
|
||||
|
||||
|
||||
class Postzord::Dispatcher
|
||||
include Diaspora::Logging
|
||||
|
||||
require 'postzord/dispatcher/private'
|
||||
require 'postzord/dispatcher/public'
|
||||
|
||||
attr_reader :sender, :object, :xml, :subscribers, :opts
|
||||
|
||||
# @param user [User] User dispatching the object in question
|
||||
# @param object [Object] The object to be sent to other Diaspora installations
|
||||
# @opt additional_subscribers [Array<Person>] Additional subscribers
|
||||
def initialize(user, object, opts={})
|
||||
@sender = user
|
||||
@object = object
|
||||
@xml = @object.to_diaspora_xml
|
||||
@opts = opts
|
||||
|
||||
additional_subscribers = opts[:additional_subscribers] || []
|
||||
@subscribers = subscribers_from_object | [*additional_subscribers]
|
||||
end
|
||||
|
||||
# @return [Postzord::Dispatcher] Public or private dispatcher depending on the object's intended audience
|
||||
def self.build(user, object, opts={})
|
||||
unless object.respond_to? :to_diaspora_xml
|
||||
raise 'This object does not respond_to? to_diaspora xml. Try including Diaspora::Federated::Base into your object'
|
||||
end
|
||||
|
||||
if self.object_should_be_processed_as_public?(object)
|
||||
Postzord::Dispatcher::Public.new(user, object, opts)
|
||||
else
|
||||
Postzord::Dispatcher::Private.new(user, object, opts)
|
||||
end
|
||||
end
|
||||
|
||||
def self.defer_build_and_post(user, object, opts={})
|
||||
opts[:additional_subscribers] ||= []
|
||||
if opts[:additional_subscribers].present?
|
||||
opts[:additional_subscribers] = [*opts[:additional_subscribers]].map(&:id)
|
||||
end
|
||||
|
||||
if opts[:to].present?
|
||||
opts[:to] = [*opts[:to]].map {|e| e.respond_to?(:id) ? e.id : e }
|
||||
end
|
||||
|
||||
Workers::DeferredDispatch.perform_async(user.id, object.class.to_s, object.id, opts)
|
||||
end
|
||||
|
||||
# @param object [Object]
|
||||
# @return [Boolean]
|
||||
def self.object_should_be_processed_as_public?(object)
|
||||
if object.respond_to?(:public?) && object.public?
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
# @return [Object]
|
||||
def post
|
||||
self.deliver_to_services(@opts[:url], @opts[:services] || [])
|
||||
self.post_to_subscribers if @subscribers.present?
|
||||
@object
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def post_to_subscribers
|
||||
remote_people, local_people = @subscribers.partition{ |person| person.owner_id.nil? }
|
||||
|
||||
unless @object.respond_to?(:relayable?) && @sender.owns?(@object.parent)
|
||||
self.deliver_to_local(local_people)
|
||||
end
|
||||
|
||||
self.deliver_to_remote(remote_people)
|
||||
end
|
||||
|
||||
# @return [Array<Person>] Recipients of the object, minus any additional subscribers
|
||||
def subscribers_from_object
|
||||
@object.subscribers
|
||||
end
|
||||
|
||||
# @param remote_people [Array<Person>] Recipients of the post on other pods
|
||||
def deliver_to_remote(remote_people)
|
||||
return if remote_people.blank?
|
||||
queue_remote_delivery_job(remote_people)
|
||||
end
|
||||
|
||||
# Enqueues a job
|
||||
# @param remote_people [Array<Person>] Recipients of the post on other pods
|
||||
# @return [void]
|
||||
def queue_remote_delivery_job(remote_people)
|
||||
end
|
||||
|
||||
# @param people [Array<Person>] Recipients of the post
|
||||
def deliver_to_local(people)
|
||||
return if people.blank? || @object.is_a?(Profile)
|
||||
if @object.respond_to?(:persisted?) && !@object.is_a?(Conversation)
|
||||
batch_deliver_to_local(people)
|
||||
else
|
||||
people.each do |person|
|
||||
logger.info "event=push route=local sender=#{@sender.diaspora_handle} recipient=#{person.diaspora_handle} " \
|
||||
"payload_type=#{@object.class}"
|
||||
# TODO: Workers::Receive.perform_async(person.owner_id, @xml, @sender.person_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# @param people [Array<Person>] Recipients of the post
|
||||
def batch_deliver_to_local(people)
|
||||
ids = people.map{ |p| p.owner_id }
|
||||
#Workers::ReceiveLocalBatch.perform_async(@object.class.to_s, @object.id, ids)
|
||||
logger.info "event=push route=local sender=#{@sender.diaspora_handle} recipients=#{ids.join(',')} " \
|
||||
"payload_type=#{@object.class}"
|
||||
end
|
||||
|
||||
def deliver_to_hub
|
||||
logger.debug "event=post_to_service type=pubsub sender_handle=#{@sender.diaspora_handle}"
|
||||
Workers::PublishToHub.perform_async(@sender.atom_url)
|
||||
end
|
||||
|
||||
# @param url [String]
|
||||
# @param services [Array<Service>]
|
||||
def deliver_to_services(url, services)
|
||||
if @object.respond_to?(:public) && @object.public
|
||||
deliver_to_hub
|
||||
end
|
||||
services.each do |service|
|
||||
if @object.instance_of?(StatusMessage)
|
||||
Workers::PostToService.perform_async(service.id, @object.id, url)
|
||||
end
|
||||
if @object.instance_of?(SignedRetraction)
|
||||
Workers::DeletePostFromService.perform_async(service.id, @object.target.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
# Copyright (c) 2010-2011, Diaspora Inc. This file is
|
||||
# licensed under the Affero General Public License version 3 or later. See
|
||||
# the COPYRIGHT file.
|
||||
|
||||
class Postzord::Dispatcher::Private < Postzord::Dispatcher
|
||||
|
||||
# @param user [User]
|
||||
# @param activity [String]
|
||||
# @return [Salmon::EncryptedSlap]
|
||||
def self.salmon(user, activity)
|
||||
Salmon::EncryptedSlap.create_by_user_and_activity(user, activity)
|
||||
end
|
||||
|
||||
# @param person [Person]
|
||||
# @return [String]
|
||||
def self.receive_url_for(person)
|
||||
person.receive_url
|
||||
end
|
||||
end
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
# Copyright (c) 2010-2011, Diaspora Inc. This file is
|
||||
# licensed under the Affero General Public License version 3 or later. See
|
||||
# the COPYRIGHT file.
|
||||
|
||||
class Postzord::Dispatcher::Public < Postzord::Dispatcher
|
||||
|
||||
# @param user [User]
|
||||
# @param activity [String]
|
||||
# @return [Salmon::EncryptedSlap]
|
||||
def self.salmon(user, activity)
|
||||
Salmon::Slap.create_by_user_and_activity(user, activity)
|
||||
end
|
||||
|
||||
# @param person [Person]
|
||||
# @return [String]
|
||||
def self.receive_url_for(person)
|
||||
person.url + 'receive/public'
|
||||
end
|
||||
end
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
# Copyright (c) 2010-2011, Diaspora Inc. This file is
|
||||
# licensed under the Affero General Public License version 3 or later. See
|
||||
# the COPYRIGHT file.
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe Postzord::Dispatcher::Private do
|
||||
|
||||
describe '#salmon' do
|
||||
end
|
||||
|
||||
describe '#receive_url_for' do
|
||||
end
|
||||
|
||||
describe '#queue_remote_delivery_job' do
|
||||
end
|
||||
end
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
# Copyright (c) 2011, Diaspora Inc. This file is
|
||||
# licensed under the Affero General Public License version 3 or later. See
|
||||
# the COPYRIGHT file.
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe Postzord::Dispatcher::Public do
|
||||
|
||||
end
|
||||
|
|
@ -1,291 +0,0 @@
|
|||
# Copyright (c) 2011, Diaspora Inc. This file is
|
||||
# licensed under the Affero General Public License version 3 or later. See
|
||||
# the COPYRIGHT file.
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe Postzord::Dispatcher do
|
||||
before do
|
||||
skip # TODO delete later
|
||||
@sm = FactoryGirl.create(:status_message, :public => true, :author => alice.person)
|
||||
@subscribers = []
|
||||
5.times{@subscribers << FactoryGirl.create(:person)}
|
||||
allow(@sm).to receive(:subscribers).and_return(@subscribers)
|
||||
@xml = @sm.to_diaspora_xml
|
||||
end
|
||||
|
||||
describe '.initialize' do
|
||||
it 'sets @sender, @object, @xml' do
|
||||
zord = Postzord::Dispatcher.build(alice, @sm)
|
||||
expect(zord.sender).to eq(alice)
|
||||
expect(zord.object).to eq(@sm)
|
||||
expect(zord.xml).to eq(@sm.to_diaspora_xml)
|
||||
end
|
||||
|
||||
context 'setting @subscribers' do
|
||||
it 'sets @subscribers from object' do
|
||||
expect(@sm).to receive(:subscribers).and_return(@subscribers)
|
||||
zord = Postzord::Dispatcher.build(alice, @sm)
|
||||
expect(zord.subscribers).to eq(@subscribers)
|
||||
end
|
||||
|
||||
it 'accepts additional subscribers from opts' do
|
||||
new_person = FactoryGirl.create(:person)
|
||||
|
||||
expect(@sm).to receive(:subscribers).and_return(@subscribers)
|
||||
zord = Postzord::Dispatcher.build(alice, @sm, :additional_subscribers => new_person)
|
||||
expect(zord.subscribers).to eq(@subscribers | [new_person])
|
||||
end
|
||||
end
|
||||
|
||||
it 'raises and gives you a helpful message if the object can not federate' do
|
||||
expect {
|
||||
Postzord::Dispatcher.build(alice, [])
|
||||
}.to raise_error /Diaspora::Federated::Base/
|
||||
end
|
||||
end
|
||||
|
||||
context 'instance methods' do
|
||||
before do
|
||||
@subscribers << bob.person
|
||||
@remote_people, @local_people = @subscribers.partition{ |person| person.owner_id.nil? }
|
||||
|
||||
@zord = Postzord::Dispatcher.build(alice, @sm)
|
||||
end
|
||||
|
||||
describe '#post' do
|
||||
it 'calls Array#partition on subscribers' do
|
||||
@zord.instance_variable_set(:@subscribers, @subscribers)
|
||||
expect(@subscribers).to receive(:partition).and_return([@remote_people, @local_people])
|
||||
@zord.post
|
||||
end
|
||||
|
||||
it 'calls #deliver_to_local with local people' do
|
||||
expect(@zord).to receive(:deliver_to_local).with(@local_people)
|
||||
@zord.post
|
||||
end
|
||||
|
||||
it 'calls #deliver_to_remote with remote people' do
|
||||
expect(@zord).to receive(:deliver_to_remote).with(@remote_people)
|
||||
@zord.post
|
||||
end
|
||||
end
|
||||
|
||||
context "comments" do
|
||||
before do
|
||||
@local_luke, @local_leia, @remote_raphael = set_up_friends
|
||||
end
|
||||
|
||||
context "local luke's post is commented on by" do
|
||||
before do
|
||||
@post = @local_luke.post(:status_message, :text => "hello", :to => @local_luke.aspects.first)
|
||||
end
|
||||
context "local leia" do
|
||||
before do
|
||||
@comment = @local_leia.build_comment :text => "yo", :post => @post
|
||||
@comment.save
|
||||
end
|
||||
context "local leia's mailman" do
|
||||
before do
|
||||
@mailman = Postzord::Dispatcher.build(@local_leia, @comment)
|
||||
end
|
||||
|
||||
it 'calls deliver_to_local with local_luke' do
|
||||
expect(@mailman).to receive(:deliver_to_local).with([@local_luke.person])
|
||||
@mailman.post
|
||||
end
|
||||
|
||||
it 'calls deliver_to_remote with nobody' do
|
||||
expect(@mailman).to receive(:deliver_to_remote).with([])
|
||||
@mailman.post
|
||||
end
|
||||
|
||||
it 'does not call notify_users' do
|
||||
expect(@mailman).not_to receive(:notify_users)
|
||||
@mailman.post
|
||||
end
|
||||
end
|
||||
context "local luke's mailman" do
|
||||
before do
|
||||
@mailman = Postzord::Dispatcher.build(@local_luke, @comment)
|
||||
end
|
||||
|
||||
it 'does not call deliver_to_local' do
|
||||
expect(@mailman).not_to receive(:deliver_to_local)
|
||||
@mailman.post
|
||||
end
|
||||
|
||||
it 'calls deliver_to_remote with remote raphael' do
|
||||
expect(@mailman).to receive(:deliver_to_remote).with([@remote_raphael])
|
||||
@mailman.post
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "remote raphael" do
|
||||
before do
|
||||
@comment = FactoryGirl.create(:comment, :author => @remote_raphael, :post => @post)
|
||||
@comment.save
|
||||
@mailman = Postzord::Dispatcher.build(@local_luke, @comment)
|
||||
end
|
||||
|
||||
it 'does not call deliver_to_local' do
|
||||
expect(@mailman).not_to receive(:deliver_to_local)
|
||||
@mailman.post
|
||||
end
|
||||
|
||||
it 'calls deliver_to_remote with remote_raphael' do
|
||||
expect(@mailman).to receive(:deliver_to_remote).with([@remote_raphael])
|
||||
@mailman.post
|
||||
end
|
||||
end
|
||||
|
||||
context "local luke" do
|
||||
before do
|
||||
@comment = @local_luke.build_comment :text => "yo", :post => @post
|
||||
@comment.save
|
||||
@mailman = Postzord::Dispatcher.build(@local_luke, @comment)
|
||||
end
|
||||
|
||||
it 'does not call deliver_to_local' do
|
||||
expect(@mailman).not_to receive(:deliver_to_local)
|
||||
@mailman.post
|
||||
end
|
||||
|
||||
it 'calls deliver_to_remote with remote_raphael' do
|
||||
expect(@mailman).to receive(:deliver_to_remote).with([@remote_raphael])
|
||||
@mailman.post
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "remote raphael's post is commented on by local luke" do
|
||||
before do
|
||||
@post = FactoryGirl.create(:status_message, :author => @remote_raphael)
|
||||
@comment = @local_luke.build_comment :text => "yo", :post => @post
|
||||
@comment.save
|
||||
@mailman = Postzord::Dispatcher.build(@local_luke, @comment)
|
||||
end
|
||||
|
||||
it 'calls deliver_to_remote with remote_raphael' do
|
||||
expect(@mailman).to receive(:deliver_to_remote).with([@remote_raphael])
|
||||
@mailman.post
|
||||
end
|
||||
|
||||
it 'calls deliver_to_local with nobody' do
|
||||
expect(@mailman).to receive(:deliver_to_local).with([])
|
||||
@mailman.post
|
||||
end
|
||||
|
||||
it 'does not call notify_users' do
|
||||
expect(@mailman).not_to receive(:notify_users)
|
||||
@mailman.post
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#deliver_to_local' do
|
||||
before do
|
||||
@mailman = Postzord::Dispatcher.build(alice, @sm)
|
||||
end
|
||||
|
||||
it 'queues a batch receive' do
|
||||
local_people = []
|
||||
local_people << alice.person
|
||||
expect(Workers::ReceiveLocalBatch).to receive(:perform_async).with(@sm.class.to_s, @sm.id, [alice.id]).once
|
||||
@mailman.send(:deliver_to_local, local_people)
|
||||
end
|
||||
|
||||
it 'returns if people are empty' do
|
||||
expect(Workers::ReceiveLocalBatch).not_to receive(:perform_async)
|
||||
@mailman.send(:deliver_to_local, [])
|
||||
end
|
||||
|
||||
it 'returns if the object is a profile' do
|
||||
@mailman.instance_variable_set(:@object, Profile.new)
|
||||
expect(Workers::ReceiveLocalBatch).not_to receive(:perform_async)
|
||||
@mailman.send(:deliver_to_local, [1])
|
||||
end
|
||||
end
|
||||
|
||||
describe '#object_should_be_processed_as_public?' do
|
||||
it 'returns true with a comment on a public post' do
|
||||
f = FactoryGirl.create(:comment, :post => FactoryGirl.build(:status_message, :public => true))
|
||||
expect(Postzord::Dispatcher.object_should_be_processed_as_public?(f)).to be true
|
||||
end
|
||||
|
||||
it 'returns false with a comment on a private post' do
|
||||
f = FactoryGirl.create(:comment, :post => FactoryGirl.build(:status_message, :public => false))
|
||||
expect(Postzord::Dispatcher.object_should_be_processed_as_public?(f)).to be false
|
||||
end
|
||||
|
||||
it 'returns true with a like on a comment on a public post' do
|
||||
f = FactoryGirl.create(:like, :target => FactoryGirl.build(:comment, :post => FactoryGirl.build(:status_message, :public => true)))
|
||||
expect(Postzord::Dispatcher.object_should_be_processed_as_public?(f)).to be true
|
||||
end
|
||||
|
||||
it 'returns false with a like on a comment on a private post' do
|
||||
f = FactoryGirl.create(:like, :target => FactoryGirl.build(:comment, :post => FactoryGirl.build(:status_message, :public => false)))
|
||||
expect(Postzord::Dispatcher.object_should_be_processed_as_public?(f)).to be false
|
||||
end
|
||||
|
||||
it 'returns false for a relayable_retraction' do
|
||||
f = RelayableRetraction.new
|
||||
f.target = FactoryGirl.create(:status_message, :public => true)
|
||||
expect(Postzord::Dispatcher.object_should_be_processed_as_public?(f)).to be false
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
describe '#deliver_to_services' do
|
||||
before do
|
||||
alice.aspects.create(:name => "whatever")
|
||||
@service = Services::Facebook.new(:access_token => "yeah")
|
||||
alice.services << @service
|
||||
end
|
||||
|
||||
it 'queues a job to notify the hub' do
|
||||
allow(Workers::PostToService).to receive(:perform_async).with(anything, anything, anything)
|
||||
expect(Workers::PublishToHub).to receive(:perform_async).with(alice.atom_url)
|
||||
@zord.send(:deliver_to_services, nil, [])
|
||||
end
|
||||
|
||||
it 'does not push to hub for non-public posts' do
|
||||
@sm = FactoryGirl.create(:status_message)
|
||||
mailman = Postzord::Dispatcher.build(alice, @sm, :url => "http://joindiaspora.com/p/123")
|
||||
|
||||
expect(mailman).not_to receive(:deliver_to_hub)
|
||||
mailman.post
|
||||
end
|
||||
|
||||
it 'only pushes to specified services' do
|
||||
@s1 = FactoryGirl.create(:service, :user_id => alice.id)
|
||||
alice.services << @s1
|
||||
@s2 = FactoryGirl.create(:service, :user_id => alice.id)
|
||||
alice.services << @s2
|
||||
mailman = Postzord::Dispatcher.build(alice, FactoryGirl.create(:status_message), :url => "http://joindiaspora.com/p/123", :services => [@s1])
|
||||
|
||||
allow(Workers::PublishToHub).to receive(:perform_async).with(anything)
|
||||
expect(Workers::PostToService).to receive(:perform_async).with(@s1.id, anything, anything)
|
||||
mailman.post
|
||||
end
|
||||
|
||||
it 'does not push to services if none are specified' do
|
||||
mailman = Postzord::Dispatcher.build(alice, FactoryGirl.create(:status_message), :url => "http://joindiaspora.com/p/123")
|
||||
|
||||
allow(Workers::PublishToHub).to receive(:perform_async).with(anything)
|
||||
expect(Workers::PostToService).not_to receive(:perform_async).with(anything, anything, anything)
|
||||
mailman.post
|
||||
end
|
||||
|
||||
it 'queues a job to delete if given retraction' do
|
||||
retraction = SignedRetraction.build(alice, FactoryGirl.create(:status_message))
|
||||
mailman = Postzord::Dispatcher.build(alice, retraction, :url => "http://joindiaspora.com/p/123", :services => [@service])
|
||||
|
||||
expect(Workers::DeletePostFromService).to receive(:perform_async).with(anything, anything)
|
||||
mailman.post
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in a new issue