diff --git a/app/models/jobs/notify_local_users.rb b/app/models/jobs/notify_local_users.rb new file mode 100644 index 000000000..994055341 --- /dev/null +++ b/app/models/jobs/notify_local_users.rb @@ -0,0 +1,20 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +module Jobs + class NotifyLocalUsers + extend ResqueJobLogging + @queue = :receive_local + + require File.join(Rails.root, 'app/models/notification') + + def self.perform(user_id, object_klass, object_id, person_id) + user = User.find_by_id(user_id) + object = object_klass.constantize.find_by_id(object_id) + person = Person.find_by_id(person_id) + + Notification.notify(user, object, person) + end + end +end diff --git a/lib/postzord/dispatch.rb b/lib/postzord/dispatch.rb index 0a8d0ed13..451fdde07 100644 --- a/lib/postzord/dispatch.rb +++ b/lib/postzord/dispatch.rb @@ -22,7 +22,7 @@ class Postzord::Dispatch if @object.is_a?(Comment) user_ids = [*local_people].map{|x| x.owner_id } local_users = User.all(:id.in => user_ids, :fields => ['person_id, username, language, email']) - self.socket_to_users(local_users) + self.socket_and_notify_users(local_users) else self.deliver_to_local(local_people) end @@ -65,11 +65,13 @@ class Postzord::Dispatch end end - def socket_to_users(users) - if @object.respond_to?(:socket_to_uid) - users.each do |user| + def socket_and_notify_users(users) + socket = @object.respond_to?(:socket_to_uid) + users.each do |user| + if socket @object.socket_to_uid(user) end + Resque.enqueue(Jobs::NotifyLocalUsers, user.id, @object.class.to_s, @object.id, @sender_person.id) end end end diff --git a/spec/lib/postzord/dispatch_spec.rb b/spec/lib/postzord/dispatch_spec.rb index df4801a85..dbf8e81ab 100644 --- a/spec/lib/postzord/dispatch_spec.rb +++ b/spec/lib/postzord/dispatch_spec.rb @@ -57,7 +57,7 @@ describe Postzord::Dispatch do describe '#post' do before do - @zord.stub!(:socket_to_users) + @zord.stub!(:socket_and_notify_users) end it 'calls Array#partition on subscribers' do @subscribers.should_receive(:partition).and_return([@remote_people, @local_people]) @@ -74,26 +74,37 @@ describe Postzord::Dispatch do @zord.post end - it 'calls socket_to_users with the local users if the object is a comment' do - comment = @local_user.comment "yo", :on => Factory(:status_message) - comment.should_receive(:subscribers).and_return([@local_user]) - mailman = Postzord::Dispatch.new(@user, comment) - mailman.should_receive(:socket_to_users) - mailman.post - end + context 'passed a comment' do + it 'calls socket_to_users with the local users' do + comment = @local_user.comment "yo", :on => Factory(:status_message) + comment.should_receive(:subscribers).and_return([@local_user]) + mailman = Postzord::Dispatch.new(@user, comment) + mailman.should_receive(:socket_and_notify_users) + mailman.post + end - it 'does not call deliver_to_local if the object is a comment' do - comment = @local_user.comment "yo", :on => Factory(:status_message) - comment.should_receive(:subscribers).and_return([@local_user]) - mailman = Postzord::Dispatch.new(@user, comment) - mailman.should_receive(:socket_to_users) - mailman.should_not_receive(:deliver_to_local) - mailman.post + it 'does not call deliver_to_local' do + comment = @local_user.comment "yo", :on => Factory(:status_message) + comment.should_receive(:subscribers).and_return([@local_user]) + mailman = Postzord::Dispatch.new(@user, comment) + mailman.should_receive(:socket_and_notify_users) + mailman.should_not_receive(:deliver_to_local) + + mailman.post + end + + it 'calls notify local users' do + comment = @local_user.comment "yo", :on => Factory(:status_message) + comment.should_receive(:subscribers).and_return([@local_user]) + mailman = Postzord::Dispatch.new(@user, comment) + mailman.stub!(:deliver_to_local) + mailman.stub!(:socket_and_notify_users) + mailman.post + end end end describe '#deliver_to_remote' do - before do @remote_people = [] @remote_people << @user.person @@ -150,10 +161,10 @@ describe Postzord::Dispatch do end end - describe '#socket_to_users' do + describe '#socket_and_notify_users' do it 'should call object#socket_to_uid for each local user' do @zord.instance_variable_get(:@object).should_receive(:socket_to_uid) - @zord.send(:socket_to_users, [@local_user]) + @zord.send(:socket_and_notify_users, [@local_user]) end it 'only tries to socket when the object responds to #socket_to_uid' do @@ -161,8 +172,14 @@ describe Postzord::Dispatch do f.stub!(:subscribers) users = [@user] z = Postzord::Dispatch.new(@user, f) - users.should_not_receive(:each) # checking if each is called due to respond_to, actually trying to - z.send(:socket_to_users, users) + z.instance_variable_get(:@object).should_receive(:socket_to_uid).once + z.send(:socket_and_notify_users, users) + end + + it 'queues a Jobs::NotifyLocalUsers jobs' do + @zord.instance_variable_get(:@object).should_receive(:socket_to_uid).and_return(false) + Resque.should_receive(:enqueue).with(Jobs::NotifyLocalUsers, @local_user.id, @sm.class.to_s, @sm.id, anything) + @zord.send(:socket_and_notify_users, [@local_user]) end end end diff --git a/spec/models/jobs/notify_local_users_spec.rb b/spec/models/jobs/notify_local_users_spec.rb new file mode 100644 index 000000000..c93bf8017 --- /dev/null +++ b/spec/models/jobs/notify_local_users_spec.rb @@ -0,0 +1,18 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +require 'spec_helper' + +describe Jobs::NotifyLocalUsers do + describe '#perfom' do + it 'should call Notification.notify on the object' do + user = make_user + person = Factory :person + object = Factory :status_message + + Notification.should_receive(:notify).with(instance_of(User), instance_of(StatusMessage), instance_of(Person)) + Jobs::NotifyLocalUsers.perform(user.id, object.class.to_s, object.id, person.id) + end + end +end