From d2cdc20e2bf02de3fde22a1481734939a7aed137 Mon Sep 17 00:00:00 2001 From: Raphael Sofaer Date: Thu, 17 Mar 2011 12:55:29 -0700 Subject: [PATCH] Finish receive local batch job --- app/models/jobs/receive_local_batch.rb | 16 +++++++++++- spec/models/jobs/receive_local_batch_spec.rb | 27 +++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/app/models/jobs/receive_local_batch.rb b/app/models/jobs/receive_local_batch.rb index 525472109..bcf0af2f1 100644 --- a/app/models/jobs/receive_local_batch.rb +++ b/app/models/jobs/receive_local_batch.rb @@ -8,7 +8,11 @@ module Job require File.join(Rails.root, 'lib/postzord/receiver') @queue = :receive - def self.perform_delegate(author_id, post_id, recipient_user_ids) + def self.perform_delegate(post_id, recipient_user_ids) + post = Post.find(post_id) + create_visibilities(post, recipient_user_ids) + socket_to_users(post, recipient_user_ids) + notify_mentioned_users(post) end def self.create_visibilities(post, recipient_user_ids) aspects = Aspect.where(:user_id => recipient_user_ids).joins(:contacts).where(:contacts => {:person_id => post.author_id}).select('aspects.id, aspects.user_id') @@ -17,5 +21,15 @@ module Job post.socket_to_user(aspect.user_id, :aspect_ids => [aspect.id]) if post.respond_to? :socket_to_user end end + def self.socket_to_users(post, recipient_user_ids) + recipient_user_ids.each do |id| + SocketsController.new.outgoing(id, post) + end + end + def self.notify_mentioned_users(post) + post.mentions.each do |mention| + mention.notify_recipient + end + end end end diff --git a/spec/models/jobs/receive_local_batch_spec.rb b/spec/models/jobs/receive_local_batch_spec.rb index d3ff0207e..f569d2df7 100644 --- a/spec/models/jobs/receive_local_batch_spec.rb +++ b/spec/models/jobs/receive_local_batch_spec.rb @@ -11,10 +11,16 @@ describe Job::ReceiveLocalBatch do end describe '.perform_delegate' do it 'calls .create_visibilities' do + Job::ReceiveLocalBatch.should_receive(:create_visibilities).with(@post, [bob.id]) + Job::ReceiveLocalBatch.perform_delegate(@post.id, [bob.id]) end it 'sockets to users' do + Job::ReceiveLocalBatch.should_receive(:socket_to_users).with(@post, [bob.id]) + Job::ReceiveLocalBatch.perform_delegate(@post.id, [bob.id]) end it 'notifies mentioned users' do + Job::ReceiveLocalBatch.should_receive(:notify_mentioned_users).with(@post) + Job::ReceiveLocalBatch.perform_delegate(@post.id, [bob.id]) end end describe '.create_visibilities' do @@ -25,6 +31,25 @@ describe Job::ReceiveLocalBatch do end end describe '.socket_to_users' do - + before do + @controller = mock() + SocketsController.stub(:new).and_return(@controller) + end + it 'sockets to each user' do + @controller.should_receive(:outgoing).with(bob.id, @post) + Job::ReceiveLocalBatch.socket_to_users(@post, [bob.id]) + end + end + describe '.notify_mentioned_users' do + it 'calls notify person for a mentioned person' do + @post = alice.build_post(:status_message, :text => "Hey @{Bob; #{bob.diaspora_handle}}") + @post.save! + Notification.should_receive(:notify).with(bob, anything, alice.person) + Job::ReceiveLocalBatch.notify_mentioned_users(@post) + end + it 'does not call notify person for a non-mentioned person' do + Notification.should_not_receive(:notify) + Job::ReceiveLocalBatch.notify_mentioned_users(@post) + end end end