diff --git a/lib/postzord/receiver/local_batch.rb b/lib/postzord/receiver/local_batch.rb index e1423d02a..2cc694231 100644 --- a/lib/postzord/receiver/local_batch.rb +++ b/lib/postzord/receiver/local_batch.rb @@ -29,8 +29,12 @@ class Postzord::Receiver::LocalBatch < Postzord::Receiver def update_cache! @users.each do |user| - cache = RedisCache.new(user, "created_at") - cache.add(@object.created_at.to_i, @object.id) + # (NOTE) this can be optimized furter to not use n-query + contact = user.contact_for(object.author) + if contact && contact.aspect_memberships.size > 0 + cache = RedisCache.new(user, "created_at") + cache.add(@object.created_at.to_i, @object.id) + end end end diff --git a/lib/postzord/receiver/private.rb b/lib/postzord/receiver/private.rb index aa1a8dc76..4741b0ef5 100644 --- a/lib/postzord/receiver/private.rb +++ b/lib/postzord/receiver/private.rb @@ -49,8 +49,10 @@ class Postzord::Receiver::Private < Postzord::Receiver end def update_cache! - cache = RedisCache.new(@user, "created_at") - cache.add(@object.created_at.to_i, @object.id) + if @user.contact_for(@author).aspect_memberships.size > 0 + cache = RedisCache.new(@user, "created_at") + cache.add(@object.created_at.to_i, @object.id) + end end protected diff --git a/spec/lib/postzord/receiver/local_batch_spec.rb b/spec/lib/postzord/receiver/local_batch_spec.rb index 764c7c596..2663d2954 100644 --- a/spec/lib/postzord/receiver/local_batch_spec.rb +++ b/spec/lib/postzord/receiver/local_batch_spec.rb @@ -113,8 +113,12 @@ describe Postzord::Receiver::LocalBatch do end describe '#update_cache!' do - it 'adds to a redis cache for receiving_users' do - users = [alice, eve] + before do + + end + + it 'adds to a redis cache for users sharing with author' do + users = [bob] @zord = Postzord::Receiver::LocalBatch.new(@object, users.map{|u| u.id}) sort_order = "created_at" @@ -126,5 +130,24 @@ describe Postzord::Receiver::LocalBatch do @zord.update_cache! end + + it 'does not add to the redis cache of the users not contact with author' do + users = [bob, eve] + @zord = Postzord::Receiver::LocalBatch.new(@object, users.map{|u| u.id}) + + RedisCache.should_receive(:new).once.with(bob, anything()).and_return(stub.as_null_object) + + @zord.update_cache! + end + + it 'does not add to the redis cache of users not sharing with the author' do + alice.share_with(eve.person, alice.aspects.first) + users = [bob, eve] + @zord = Postzord::Receiver::LocalBatch.new(@object, users.map{|u| u.id}) + + RedisCache.should_receive(:new).once.with(bob, anything()).and_return(stub.as_null_object) + + @zord.update_cache! + end end end diff --git a/spec/lib/postzord/receiver/private_spec.rb b/spec/lib/postzord/receiver/private_spec.rb index cc8104662..188f6ad7e 100644 --- a/spec/lib/postzord/receiver/private_spec.rb +++ b/spec/lib/postzord/receiver/private_spec.rb @@ -10,15 +10,8 @@ require File.join(Rails.root, 'lib/postzord/receiver/private') describe Postzord::Receiver::Private do before do - @user = alice - @user2 = bob - @person2 = @user2.person - - aspect1 = @user.aspects.first - aspect2 = @user2.aspects.first - - @original_post = @user2.build_post(:status_message, :text => "hey", :aspect_ids => [aspect2.id]) - @salmon_xml = @user2.salmon(@original_post).xml_for(@user.person) + @alices_post = alice.build_post(:status_message, :text => "hey", :aspect_ids => [alice.aspects.first.id]) + @salmon_xml = alice.salmon(@alices_post).xml_for(bob.person) end describe '.initialize' do @@ -26,7 +19,7 @@ describe Postzord::Receiver::Private do Webfinger.should_not_receive(:new) Salmon::EncryptedSlap.should_not_receive(:from_xml) - zord = Postzord::Receiver::Private.new(@user, :person => @person2, :object => @original_post) + zord = Postzord::Receiver::Private.new(bob, :person => alice.person, :object => @alices_post) zord.instance_variable_get(:@user).should_not be_nil zord.instance_variable_get(:@sender).should_not be_nil zord.instance_variable_get(:@object).should_not be_nil @@ -37,10 +30,10 @@ describe Postzord::Receiver::Private do web_mock = mock() web_mock.should_receive(:fetch).and_return true salmon_mock.should_receive(:author_id).and_return(true) - Salmon::EncryptedSlap.should_receive(:from_xml).with(@salmon_xml, @user).and_return(salmon_mock) + Salmon::EncryptedSlap.should_receive(:from_xml).with(@salmon_xml, bob).and_return(salmon_mock) Webfinger.should_receive(:new).and_return(web_mock) - zord = Postzord::Receiver::Private.new(@user, :salmon_xml => @salmon_xml) + zord = Postzord::Receiver::Private.new(bob, :salmon_xml => @salmon_xml) zord.instance_variable_get(:@user).should_not be_nil zord.instance_variable_get(:@sender).should_not be_nil zord.instance_variable_get(:@salmon_xml).should_not be_nil @@ -49,7 +42,7 @@ describe Postzord::Receiver::Private do describe '#receive!' do before do - @zord = Postzord::Receiver::Private.new(@user, :salmon_xml => @salmon_xml) + @zord = Postzord::Receiver::Private.new(bob, :salmon_xml => @salmon_xml) @salmon = @zord.instance_variable_get(:@salmon) end @@ -73,14 +66,14 @@ describe Postzord::Receiver::Private do end it 'parses the salmon object' do - Diaspora::Parser.should_receive(:from_xml).with(@salmon.parsed_data).and_return(@original_post) + Diaspora::Parser.should_receive(:from_xml).with(@salmon.parsed_data).and_return(@alices_post) @zord.receive! end end describe 'receive_object' do before do - @zord = Postzord::Receiver::Private.new(@user, :person => @person2, :object => @original_post) + @zord = Postzord::Receiver::Private.new(bob, :person => alice.person, :object => @alices_post) @salmon = @zord.instance_variable_get(:@salmon) end @@ -88,8 +81,8 @@ describe Postzord::Receiver::Private do cm = Comment.new cm.stub(:receive).and_return(cm) - Notification.should_receive(:notify).with(@user, cm, @person2) - zord = Postzord::Receiver::Private.new(@user, :person => @person2, :object => cm) + Notification.should_receive(:notify).with(bob, cm, alice.person) + zord = Postzord::Receiver::Private.new(bob, :person => alice.person, :object => cm) zord.receive_object end @@ -105,15 +98,26 @@ describe Postzord::Receiver::Private do end describe '#update_cache!' do - it 'adds to redis cache for the given user' do - @original_post.save! + it 'adds to redis cache if the contact has aspect visibilities' do + @alices_post.save! - @zord = Postzord::Receiver::Private.new(@user, :person => @person2, :object => @original_post) + @zord = Postzord::Receiver::Private.new(bob, :person => alice.person, :object => @alices_post) sort_order = "created_at" - cache = RedisCache.new(@user, sort_order) - RedisCache.should_receive(:new).with(@user, sort_order).and_return(cache) - cache.should_receive(:add).with(@original_post.created_at.to_i, @original_post.id) + cache = RedisCache.new(bob, sort_order) + RedisCache.should_receive(:new).with(bob, sort_order).and_return(cache) + cache.should_receive(:add).with(@alices_post.created_at.to_i, @alices_post.id) + @zord.update_cache! + end + + it 'does not add to redis cache if the receiving user is not sharing with the sender' do + alice.share_with(eve.person, alice.aspects.first) + @alices_post.save! + + @zord = Postzord::Receiver::Private.new(eve, :person => alice.person, :object => @alices_post) + + sort_order = "created_at" + RedisCache.should_not_receive(:new) @zord.update_cache! end end