diff --git a/Procfile b/Procfile index 9f7bd77c1..d457b0b8b 100644 --- a/Procfile +++ b/Procfile @@ -1,5 +1,5 @@ web: bundle exec unicorn -c config/unicorn.rb -p $PORT redis: redis-server catchall_worker: env QUEUE=* bundle exec rake resque:work -slow_worker: env QUEUES=socket_webfinger,photos,http_service,receive_local,mail,receive,receive_salmon,http,delete_account bundle exec rake resque:work -priority_worker: env QUEUES=socket_webfinger,photos,http_service,mail,delete_account bundle exec rake resque:work +slow_worker: env QUEUES=socket_webfinger,photos,http_service,dispatch,receive_local,mail,receive,receive_salmon,http,delete_account,* bundle exec rake resque:work +priority_worker: env QUEUES=socket_webfinger,photos,http_service,dispatch,mail,delete_account bundle exec rake resque:work diff --git a/app/models/jobs/deferred_dispatch.rb b/app/models/jobs/deferred_dispatch.rb new file mode 100644 index 000000000..205287ced --- /dev/null +++ b/app/models/jobs/deferred_dispatch.rb @@ -0,0 +1,20 @@ +# 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 Jobs + class DeferredDispatch < Base + @queue = :dispatch + + def self.perform(user_id, object_class_name, object_id, opts) + user = User.find(user_id) + object = object_class_name.constantize.find(object_id) + + if opts[:additional_subscribers].present? + opts[:additional_subscribers] = Person.where(:id => opts[:additional_subscribers]) + end + + Postzord::Dispatcher.build(user, object, opts).post + end + end +end diff --git a/app/models/status_message.rb b/app/models/status_message.rb index b4e013faf..0e47afe1a 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -142,7 +142,11 @@ class StatusMessage < Post XML end - def after_dispatch sender + def after_dispatch(sender) + self.update_and_dispatch_attached_photos(sender) + end + + def update_and_dispatch_attached_photos(sender) unless self.photos.empty? self.photos.update_all(:pending => false, :public => self.public) for photo in self.photos diff --git a/app/models/user.rb b/app/models/user.rb index f6fd62367..feaad6c0b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -228,7 +228,7 @@ class User < ActiveRecord::Base end ######## Posting ######## - def build_post(class_name, opts = {}) + def build_post(class_name, opts={}) opts[:author] = self.person opts[:diaspora_handle] = opts[:author].diaspora_handle @@ -236,15 +236,15 @@ class User < ActiveRecord::Base model_class.diaspora_initialize(opts) end - def dispatch_post(post, opts = {}) - mailman = Postzord::Dispatcher.build(self, post, opts) - mailman.post + def dispatch_post(post, opts={}) + Postzord::Dispatcher.defer_build_and_post(self, post, opts) end - def update_post(post, post_hash = {}) + def update_post(post, post_hash={}) if self.owns? post + puts 'ownin' post.update_attributes(post_hash) - Postzord::Dispatcher.build(self, post).post + self.dispatch_post(post) end end diff --git a/lib/postzord/dispatcher.rb b/lib/postzord/dispatcher.rb index b80562f22..778ef75b6 100644 --- a/lib/postzord/dispatcher.rb +++ b/lib/postzord/dispatcher.rb @@ -35,6 +35,12 @@ class Postzord::Dispatcher end end + def self.defer_build_and_post(user, object, opts={}) + opts[:additional_subscribers] ||= [] + opts[:additional_subscribers] = [*opts[:additional_subscribers]].map(&:id) + Resque.enqueue(Jobs::DeferredDispatch, user.id, object.class.to_s, object.id, opts) + end + # @param object [Object] # @return [Boolean] def self.object_should_be_processed_as_public?(object) diff --git a/spec/controllers/status_messages_controller_spec.rb b/spec/controllers/status_messages_controller_spec.rb index e7e9ef7de..6280484d6 100644 --- a/spec/controllers/status_messages_controller_spec.rb +++ b/spec/controllers/status_messages_controller_spec.rb @@ -125,19 +125,24 @@ describe StatusMessagesController do @hash = status_message_hash @hash[:photos] = [@photo1.id.to_s, @photo2.id.to_s] end + it "will post a photo without text" do @hash.delete :text post :create, @hash response.should be_redirect end + it "attaches all referenced photos" do post :create, @hash assigns[:status_message].photos.map(&:id).should =~ [@photo1, @photo2].map(&:id) end + it "sets the pending bit of referenced photos" do - post :create, @hash - @photo1.reload.pending.should be_false - @photo2.reload.pending.should be_false + fantasy_resque do + post :create, @hash + @photo1.reload.pending.should be_false + @photo2.reload.pending.should be_false + end end end end diff --git a/spec/lib/postzord/dispatcher_spec.rb b/spec/lib/postzord/dispatcher_spec.rb index a73d69db9..0570e2d30 100644 --- a/spec/lib/postzord/dispatcher_spec.rb +++ b/spec/lib/postzord/dispatcher_spec.rb @@ -283,10 +283,10 @@ describe Postzord::Dispatcher do it 'does not push to hub for non-public posts' do @sm = Factory(:status_message) - mailman = Postzord::Dispatcher.build(alice, @sm) + mailman = Postzord::Dispatcher.build(alice, @sm, :url => "http://joindiaspora.com/p/123") mailman.should_not_receive(:deliver_to_hub) - mailman.post(:url => "http://joindiaspora.com/p/123") + mailman.post end it 'only pushes to specified services' do @@ -294,20 +294,20 @@ describe Postzord::Dispatcher do alice.services << @s1 @s2 = Factory(:service, :user_id => alice.id) alice.services << @s2 - mailman = Postzord::Dispatcher.build(alice, Factory(:status_message)) + mailman = Postzord::Dispatcher.build(alice, Factory(:status_message), :url => "http://joindiaspora.com/p/123", :services => [@s1]) Resque.stub!(:enqueue).with(Jobs::PublishToHub, anything) Resque.stub!(:enqueue).with(Jobs::HttpMulti, anything, anything, anything) Resque.should_receive(:enqueue).with(Jobs::PostToService, @s1.id, anything, anything) - mailman.post(:url => "http://joindiaspora.com/p/123", :services => [@s1]) + mailman.post end it 'does not push to services if none are specified' do - mailman = Postzord::Dispatcher.build(alice, Factory(:status_message)) + mailman = Postzord::Dispatcher.build(alice, Factory(:status_message), :url => "http://joindiaspora.com/p/123") Resque.stub!(:enqueue).with(Jobs::PublishToHub, anything) Resque.should_not_receive(:enqueue).with(Jobs::PostToService, anything, anything, anything) - mailman.post(:url => "http://joindiaspora.com/p/123") + mailman.post end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 4eeeb49c1..96af835c6 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -580,11 +580,9 @@ describe User do end describe '#update_post' do - it 'sends a notification to aspects' do - m = mock() - m.should_receive(:post) - Postzord::Dispatcher.should_receive(:build).and_return(m) + it 'should dispatch post' do photo = alice.build_post(:photo, :user_file => uploaded_photo, :text => "hello", :to => alice.aspects.first.id) + alice.should_receive(:dispatch_post).with(photo) alice.update_post(photo, :text => 'hellp') end end