RS, DG; store_from_xml now takes one objects. All talk of arrays and collections has been eliminated.
This commit is contained in:
parent
11758beaf3
commit
5228e51262
5 changed files with 60 additions and 79 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
|||
<post><person></person></post>
|
||||
<post><status_message>\n <message>HEY DUDE</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
|
||||
</XML>"
|
||||
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 = "<XML>
|
||||
<head>
|
||||
<sender>
|
||||
<email>#{Person.first.email}</email>
|
||||
</sender>
|
||||
</head>
|
||||
<posts>
|
||||
<post><person></person></post>
|
||||
</posts></XML>"
|
||||
</XML>"
|
||||
|
||||
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 "<post>"
|
||||
body.should include "</post>"
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue