diff --git a/autotest/discover.rb b/autotest/discover.rb index a0d0bc50a..5e50a665a 100644 --- a/autotest/discover.rb +++ b/autotest/discover.rb @@ -4,3 +4,12 @@ Autotest.add_discovery { "rails" } Autotest.add_discovery { "rspec2" } +Autotest.add_hook :initialize do |at| + at.add_mapping(%r%^spec/(intergration|mailers|config)/.*rb$%) { |filename, _| + filename + } + + at.add_mapping(%r%^spec/misc_spec.rb$%) { |filename, _| + filename + } +end diff --git a/lib/postzord/receiver.rb b/lib/postzord/receiver.rb index 9b5c42375..e561e9bca 100644 --- a/lib/postzord/receiver.rb +++ b/lib/postzord/receiver.rb @@ -10,10 +10,8 @@ module Postzord def initialize(user, opts={}) @user = user @user_person = @user.person - @salmon_xml = opts[:salmon_xml] - @sender = opts[:person] || Webfinger.new(self.salmon.author_email).fetch @author = @sender @@ -30,54 +28,58 @@ module Postzord end def parse_and_receive(xml) + Rails.logger.info("event=receive status=start recipient=#{@user_person.diaspora_handle} payload_type=#{@object.class} sender=#{@sender.diaspora_handle}") @object ||= Diaspora::Parser.from_xml(xml) - if self.valid_object? + if self.validate_object receive_object end end def receive_object obj = @object.receive(@user, @author) - Notification.notify(@user, @object, @author) unless @object.is_a?(Retraction) + Notification.notify(@user, @object, @author) if @object.respond_to?(:notification_type) + Rails.logger.info("event=receive status=complete recipient=#{@user_person.diaspora_handle} sender=#{@sender.diaspora_handle} payload_type#{@object.class}") obj end - def salmon - @salmon ||= Salmon::SalmonSlap.parse(@salmon_xml, @user) - end - protected - def valid_object? - Rails.logger.info("event=receive status=start recipient=#{@user_person.diaspora_handle} payload_type=#{@object.class} sender=#{@sender.diaspora_handle}") + def salmon + @salmon ||= Salmon::SalmonSlap.parse(@salmon_xml, @user) + end - #special casey - if @object.is_a?(Request) - @object.sender_handle = @sender.diaspora_handle - end + def xml_author if @object.is_a?(Comment) - xml_author = (@user.owns?(@object.post))? @object.diaspora_handle : @object.post.person.diaspora_handle + #if A and B are friends, and A sends B a comment from C, we delegate the validation to the owner of the post being commented on + xml_author = @user.owns?(@object.post) ? @object.diaspora_handle : @object.post.person.diaspora_handle @author = Webfinger.new(@object.diaspora_handle).fetch else xml_author = @object.diaspora_handle end + xml_author + end + def validate_object #begin similar unless @object.is_a?(Request) || @user.contact_for(@sender) Rails.logger.info("event=receive status=abort reason='sender not connected to recipient' recipient=#{@user_person.diaspora_handle} sender=#{@sender.diaspora_handle} payload_type=#{@object.class}") return false end + #special casey + if @object.is_a?(Request) + @object.sender_handle = @sender.diaspora_handle + end + if (@author.diaspora_handle != xml_author) Rails.logger.info("event=receive status=abort reason='author in xml does not match retrieved person' payload_type=#{@object.class} recipient=#{@user_person.diaspora_handle} sender=#{@sender.diaspora_handle}") return false end if @author - Rails.logger.info("event=receive status=complete recipient=#{@user_person.diaspora_handle} sender=#{@sender.diaspora_handle} payload_type#{@object.class}") - @object.person = @author if @object.respond_to? :person= end + @object end end diff --git a/spec/intergration/receiving_spec.rb b/spec/intergration/receiving_spec.rb index 3a12660ee..709889606 100644 --- a/spec/intergration/receiving_spec.rb +++ b/spec/intergration/receiving_spec.rb @@ -15,17 +15,25 @@ describe 'a user receives a post' do let(:user3) { make_user } let(:aspect3) { user3.aspects.create(:name => 'heroes') } + def zord(user, person, xml) + zord = Postzord::Receiver.new(user, :person => person) + zord.parse_and_receive(xml) + end + before do connect_users(user, aspect, user2, aspect2) end + + it 'should stream only one message to the everyone aspect when a multi-aspected contacts posts' do contact = user.contact_for(user2.person) user.add_contact_to_aspect(contact, user.aspects.create(:name => "villains")) status = user2.post(:status_message, :message => "Users do things", :to => aspect2.id) - xml = status.to_diaspora_xml + #xml = status.to_diaspora_xml Diaspora::WebSocket.should_receive(:queue_to_user).exactly(:once) - Postzord::Receiver.new(user, :object => status, :person => user2.person) + zord = Postzord::Receiver.new(user, :object => status, :person => user2.person) + zord.receive_object end it 'should be able to parse and store a status message from xml' do @@ -36,8 +44,7 @@ describe 'a user receives a post' do status_message.destroy lambda { - zord = Postzord::Receiver.new(user, :person => user2.person) - zord.parse_and_receive(xml) + zord(user, user2.person, xml) }.should change(Post,:count).by(1) end @@ -56,7 +63,9 @@ describe 'a user receives a post' do status = user.post :status_message, :message => "store this!", :to => aspect.id status.message = 'foo' xml = status.to_diaspora_xml - user2.receive(xml, user.person) + + zord(user2, user.person, xml) + status.reload.message.should == 'store this!' end @@ -64,7 +73,10 @@ describe 'a user receives a post' do photo = user.post(:photo, :user_file => uploaded_photo, :caption => "Original", :to => aspect.id) photo.caption = 'foo' xml = photo.to_diaspora_xml - user2.reload.receive(xml, user.person) + user2.reload + + zord(user2, user.person, xml) + photo.reload.caption.should match(/foo/) end end @@ -110,7 +122,9 @@ describe 'a user receives a post' do it 'should not override userrefs on receive by another person' do user3.activate_contact(user2.person, aspect3) - user3.receive @status_message.to_diaspora_xml, user2.person + xml = @status_message.to_diaspora_xml + + zord(user3, user2.person, xml) @status_message.reload @status_message.user_refs.should == 2 @@ -126,8 +140,10 @@ describe 'a user receives a post' do connect_users(user, aspect, user3, aspect3) @post = user.post :status_message, :message => "hello", :to => aspect.id - user2.receive @post.to_diaspora_xml, user.person - user3.receive @post.to_diaspora_xml, user.person + xml = @post.to_diaspora_xml + + zord(user2, user.person, xml) + zord(user3, user.person, xml) @comment = user3.comment('tada',:on => @post) @comment.post_creator_signature = @comment.sign_with_key(user.encryption_key) @@ -141,7 +157,9 @@ describe 'a user receives a post' do user2.reload.raw_visible_posts.size.should == 1 post_in_db = user2.raw_visible_posts.first post_in_db.comments.should == [] - user2.receive(@xml, user.person) + + zord(user2, user.person, @xml) + post_in_db.reload post_in_db.comments.include?(@comment).should be true @@ -157,11 +175,12 @@ describe 'a user receives a post' do Person.should_receive(:by_account_identifier).and_return{ |handle| if handle == user.person.diaspora_handle; user.person.save user.person; else; remote_person.save; remote_person; end } - user2.reload.raw_visible_posts.size.should == 1 post_in_db = user2.raw_visible_posts.first post_in_db.comments.should == [] - user2.receive(@xml, user.person) + + zord(user2, user.person, @xml) + post_in_db.reload post_in_db.comments.include?(@comment).should be true @@ -174,7 +193,11 @@ describe 'a user receives a post' do let(:salmon){user.salmon( post )} it 'should receive a salmon for a post' do - user2.receive_salmon( salmon.xml_for(user2.person) ) + salmon_xml = salmon.xml_for(user2.person) + + zord = Postzord::Receiver.new(user2, :salmon_xml => salmon_xml) + zord.perform + user2.visible_post_ids.include?(post.id).should be true end end diff --git a/spec/lib/postzord/receiver_spec.rb b/spec/lib/postzord/receiver_spec.rb index ed47dbefa..a6a328e93 100644 --- a/spec/lib/postzord/receiver_spec.rb +++ b/spec/lib/postzord/receiver_spec.rb @@ -87,8 +87,16 @@ describe Postzord::Receiver do @salmon = @zord.instance_variable_get(:@salmon) end - it 'calls Notification.notify' do - Notification.should_receive(:notify) + it 'calls Notification.notify if object responds to notification_type' do + cm = Comment.new + cm.stub!(:receive) + Notification.should_receive(:notify).with(@user, cm, @person2) + zord = Postzord::Receiver.new(@user, :person => @person2, :object => cm) + zord.receive_object + end + + it 'does not call Notification.notify if object does not respond to notification_type' do + Notification.should_not_receive(:notify) @zord.receive_object end