From 5228e512626d003de9df4555c63ae49962274e55 Mon Sep 17 00:00:00 2001 From: Raphael Date: Wed, 11 Aug 2010 14:40:48 -0700 Subject: [PATCH] RS, DG; store_from_xml now takes one objects. All talk of arrays and collections has been eliminated. --- app/controllers/publics_controller.rb | 2 +- lib/diaspora/parser.rb | 82 +++++++++++++-------------- spec/lib/diaspora_parser_spec.rb | 35 ++++-------- spec/models/user_spec.rb | 16 +++--- spec/user_encryption_spec.rb | 4 +- 5 files changed, 60 insertions(+), 79 deletions(-) diff --git a/app/controllers/publics_controller.rb b/app/controllers/publics_controller.rb index a41d73a23..eac2540ac 100644 --- a/app/controllers/publics_controller.rb +++ b/app/controllers/publics_controller.rb @@ -25,7 +25,7 @@ class PublicsController < ApplicationController def receive @user = Person.first(:id => params[:id]).owner Rails.logger.debug "PublicsController has received: #{params[:xml]}" - store_objects_from_xml params[:xml], @user + store_from_xml params[:xml], @user render :nothing => true end diff --git a/lib/diaspora/parser.rb b/lib/diaspora/parser.rb index 5783e3e5d..471a4fbed 100644 --- a/lib/diaspora/parser.rb +++ b/lib/diaspora/parser.rb @@ -23,57 +23,51 @@ module Diaspora person ? person : Person.from_xml( person_xml) end - def parse_objects_from_xml(xml) - objects = [] - body = parse_body_contents_from_xml(xml) - body.children.each do |post| - begin - object = post.name.camelize.constantize.from_xml post.to_s - if object.is_a? Retraction - elsif object.is_a? Profile - person = parse_owner_id_from_xml post - person.profile = object - person.save - elsif object.is_a? Request - person = get_or_create_person_object_from_xml(post) - person.serialized_key ||= object.exported_key - object.person = person - object.person.save - object.save - elsif object.respond_to? :person - object.person = parse_owner_from_xml post.to_s - end + def parse_from_xml(xml) - objects << object - rescue NameError => e - if e.message.include? 'wrong constant name' - Rails.logger.info "Not a real type: #{object.to_s}" - raise e - else - raise e - end + return unless body = parse_body_contents_from_xml(xml).children.first + + begin + object = body.name.camelize.constantize.from_xml body.to_s + if object.is_a? Retraction + elsif object.is_a? Profile + person = parse_owner_id_from_xml body + person.profile = object + person.save + elsif object.is_a? Request + person = get_or_create_person_object_from_xml(body) + person.serialized_key ||= object.exported_key + object.person = person + object.person.save + object.save + elsif object.respond_to? :person + object.person = parse_owner_from_xml body.to_s end + object + rescue NameError => e + if e.message.include? 'wrong constant name' + Rails.logger.info "Not a real type: #{object.to_s}" + end + raise e end - objects end - def store_objects_from_xml(xml, user) - objects = parse_objects_from_xml(xml) - objects.each do |p| - Rails.logger.debug("Receiving object:\n#{p.inspect}") + def store_from_xml(xml, user) + object = parse_from_xml(xml) + Rails.logger.debug("Receiving object:\n#{object.inspect}") - if p.is_a? Retraction - Rails.logger.debug "Got a retraction for #{p.post_id}" - p.perform - - elsif p.is_a? Request - user.receive_friend_request(p) + if object.is_a? Retraction + Rails.logger.debug "Got a retraction for #{object.post_id}" + object.perform + + elsif object.is_a? Request + user.receive_friend_request(object) - elsif p.is_a? Profile - p.save - elsif p.respond_to?(:person) && !(p.person.nil?) && !(p.person.is_a? User) - Rails.logger.debug("Saving object with success: #{p.save}") - end + elsif object.is_a? Profile + object.save + + elsif object.respond_to?(:person) && !(object.person.nil?) && !(object.person.is_a? User) + Rails.logger.debug("Saving object with success: #{object.save}") end end end diff --git a/spec/lib/diaspora_parser_spec.rb b/spec/lib/diaspora_parser_spec.rb index 6bbf68b5f..c4d56fc11 100644 --- a/spec/lib/diaspora_parser_spec.rb +++ b/spec/lib/diaspora_parser_spec.rb @@ -15,7 +15,7 @@ describe Diaspora::Parser do 10.times { message = Factory.build(:status_message, :person => @user) xml = message.to_diaspora_xml - store_objects_from_xml(xml, @user) + store_from_xml(xml, @user) } StatusMessage.count.should == 0 end @@ -28,23 +28,17 @@ describe Diaspora::Parser do \n HEY DUDE\n a@a.com\n a@a.com\n a@a.com\n " - store_objects_from_xml(xml, @user) + store_from_xml(xml, @user) Post.count.should == 0 end it 'should discard types which are not of type post' do xml = " - - - #{Person.first.email} - - - - " + " - store_objects_from_xml(xml, @user) + store_from_xml(xml, @user) Post.count.should == 0 end @@ -59,12 +53,6 @@ describe Diaspora::Parser do body.should include "" body.should include "" end - - it 'should be able to extract all posts to an array' do - posts = parse_objects_from_xml(@xml) - posts.is_a?(Array).should be true - posts.count.should == 1 - end it 'should be able to correctly handle comments' do person = Factory.create(:person, :email => "test@testing.com") @@ -72,8 +60,7 @@ describe Diaspora::Parser do comment = Factory.build(:comment, :post => post, :person => person, :text => "Freedom!") xml = comment.to_diaspora_xml - objects = parse_objects_from_xml(xml) - comment = objects.first + comment = parse_from_xml(xml) comment.text.should == "Freedom!" comment.person.should == person comment.post.should == post @@ -86,7 +73,7 @@ describe Diaspora::Parser do request = retraction.to_diaspora_xml StatusMessage.count.should == 1 - store_objects_from_xml( request, @user ) + store_from_xml( request, @user ) StatusMessage.count.should == 0 end @@ -98,7 +85,7 @@ describe Diaspora::Parser do @person.destroy Person.all.count.should be 1 - store_objects_from_xml(xml, @user) + store_from_xml(xml, @user) Person.all.count.should be 2 Person.first(:_id => original_person_id).serialized_key.include?("PUBLIC").should be true @@ -115,7 +102,7 @@ describe Diaspora::Parser do Person.all.count.should be 3 - store_objects_from_xml(xml, @user) + store_from_xml(xml, @user) Person.all.count.should be 3 @user2.reload @@ -144,7 +131,7 @@ describe Diaspora::Parser do @person.destroy request_remote.destroy - store_objects_from_xml(xml, @user) + store_from_xml(xml, @user) new_person = Person.first(:url => @person.url) new_person.nil?.should be false @@ -158,7 +145,7 @@ describe Diaspora::Parser do request = retraction.to_diaspora_xml Person.count.should == 2 - store_objects_from_xml( request , @user) + store_from_xml( request , @user) Person.count.should == 1 end @@ -184,7 +171,7 @@ describe Diaspora::Parser do old_profile.first_name.should == 'bob' #Marshal profile - store_objects_from_xml xml, @user + store_from_xml xml, @user #Check that marshaled profile is the same as old profile person = Person.first(:id => person.id) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index ddb20f095..b1ae2df6a 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -90,7 +90,7 @@ describe User do it 'should befriend the user other user on the same pod' do - store_objects_from_xml @req_three_xml, @user2 + store_from_xml @req_three_xml, @user2 @user2.pending_requests.size.should be 1 @user2.accept_friend_request @request_three.id @user2.friends.include?(@user.person).should be true @@ -99,7 +99,7 @@ describe User do it 'should not delete the ignored user on the same pod' do - store_objects_from_xml @req_three_xml, @user2 + store_from_xml @req_three_xml, @user2 @user2.pending_requests.size.should be 1 @user2.ignore_friend_request @request_three.id @user2.friends.include?(@user.person).should be false @@ -108,12 +108,12 @@ describe User do it 'should both users should befriend the same person' do - store_objects_from_xml @req_xml, @user + store_from_xml @req_xml, @user @user.pending_requests.size.should be 1 @user.accept_friend_request @request.id @user.friends.include?(@person_one).should be true - store_objects_from_xml @req_two_xml, @user2 + store_from_xml @req_two_xml, @user2 @user2.pending_requests.size.should be 1 @user2.accept_friend_request @request_two.id @user2.friends.include?(@person_one).should be true @@ -122,12 +122,12 @@ describe User do it 'should keep the person around if one of the users rejects him' do - store_objects_from_xml @req_xml, @user + store_from_xml @req_xml, @user @user.pending_requests.size.should be 1 @user.accept_friend_request @request.id @user.friends.include?(@person_one).should be true - store_objects_from_xml @req_two_xml, @user2 + store_from_xml @req_two_xml, @user2 @user2.pending_requests.size.should be 1 @user2.ignore_friend_request @request_two.id @user2.friends.include?(@person_one).should be false @@ -135,12 +135,12 @@ describe User do end it 'should not keep the person around if the users ignores them' do - store_objects_from_xml @req_xml, @user + store_from_xml @req_xml, @user @user.pending_requests.size.should be 1 @user.ignore_friend_request @user.pending_requests.first.id @user.friends.include?(@person_one).should be false - store_objects_from_xml @req_two_xml, @user2 + store_from_xml @req_two_xml, @user2 @user2.pending_requests.size.should be 1 @user2.ignore_friend_request @user2.pending_requests.first.id#@request_two.id @user2.friends.include?(@person_one).should be false diff --git a/spec/user_encryption_spec.rb b/spec/user_encryption_spec.rb index ac6c96fcb..39bfbd2cd 100644 --- a/spec/user_encryption_spec.rb +++ b/spec/user_encryption_spec.rb @@ -54,7 +54,7 @@ describe 'user encryption' do xml = request.to_diaspora_xml person.destroy personcount = Person.all.count - store_objects_from_xml(xml, @user) + store_from_xml(xml, @user) Person.all.count.should == personcount + 1 new_person = Person.first(:url => "http://test.url/") new_person.id.should == id @@ -113,7 +113,7 @@ describe 'user encryption' do xml = message.to_diaspora_xml message.destroy Post.count.should be 0 - store_objects_from_xml(xml, @user) + store_from_xml(xml, @user) Post.count.should be 0 end