From e2f78a8b8c22e1c8be2af92c4d578ad8bcadc40a Mon Sep 17 00:00:00 2001 From: Raphael Sofaer Date: Fri, 18 Mar 2011 14:52:27 -0700 Subject: [PATCH] Protect against pre-existing post_visibilities, log them so we can find out where they are coming from.' --- app/models/jobs/receive_local_batch.rb | 8 ++++++-- spec/models/jobs/receive_local_batch_spec.rb | 10 ++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/models/jobs/receive_local_batch.rb b/app/models/jobs/receive_local_batch.rb index 2179b638a..557ea9a20 100644 --- a/app/models/jobs/receive_local_batch.rb +++ b/app/models/jobs/receive_local_batch.rb @@ -17,8 +17,12 @@ module Job 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') aspects.each do |aspect| - PostVisibility.create(:aspect_id => aspect.id, :post_id => post.id) - post.socket_to_user(aspect.user_id, :aspect_ids => [aspect.id]) if post.respond_to? :socket_to_user + begin + PostVisibility.create(:aspect_id => aspect.id, :post_id => post.id) + rescue ActiveRecord::RecordNotUnique => e + Rails.logger.info(:event => :unexpected_pv, :aspect_id => aspect.id, :post_id => post.id) + #The post was already visible to that aspect + end end end def self.socket_to_users(post, recipient_user_ids) diff --git a/spec/models/jobs/receive_local_batch_spec.rb b/spec/models/jobs/receive_local_batch_spec.rb index 837038cb0..5be578416 100644 --- a/spec/models/jobs/receive_local_batch_spec.rb +++ b/spec/models/jobs/receive_local_batch_spec.rb @@ -25,9 +25,15 @@ describe Job::ReceiveLocalBatch do end describe '.create_visibilities' do it 'creates a visibility for each user' do - PostVisibility.exists?(:aspect_id => bob.aspects.first, :post_id => @post.id).should be_false + PostVisibility.exists?(:aspect_id => bob.aspects.first.id, :post_id => @post.id).should be_false Job::ReceiveLocalBatch.create_visibilities(@post, [bob.id]) - PostVisibility.exists?(:aspect_id => bob.aspects.first, :post_id => @post.id).should be_true + PostVisibility.exists?(:aspect_id => bob.aspects.first.id, :post_id => @post.id).should be_true + end + it 'does not raise if a visibility already exists' do + PostVisibility.create!(:aspect_id => bob.aspects.first.id, :post_id => @post.id) + lambda { + Job::ReceiveLocalBatch.create_visibilities(@post, [bob.id]) + }.should_not raise_error end end describe '.socket_to_users' do