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
|
def receive
|
||||||
@user = Person.first(:id => params[:id]).owner
|
@user = Person.first(:id => params[:id]).owner
|
||||||
Rails.logger.debug "PublicsController has received: #{params[:xml]}"
|
Rails.logger.debug "PublicsController has received: #{params[:xml]}"
|
||||||
store_objects_from_xml params[:xml], @user
|
store_from_xml params[:xml], @user
|
||||||
render :nothing => true
|
render :nothing => true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,57 +23,51 @@ module Diaspora
|
||||||
person ? person : Person.from_xml( person_xml)
|
person ? person : Person.from_xml( person_xml)
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_objects_from_xml(xml)
|
def parse_from_xml(xml)
|
||||||
objects = []
|
|
||||||
body = parse_body_contents_from_xml(xml)
|
return unless body = parse_body_contents_from_xml(xml).children.first
|
||||||
body.children.each do |post|
|
|
||||||
begin
|
begin
|
||||||
object = post.name.camelize.constantize.from_xml post.to_s
|
object = body.name.camelize.constantize.from_xml body.to_s
|
||||||
if object.is_a? Retraction
|
if object.is_a? Retraction
|
||||||
elsif object.is_a? Profile
|
elsif object.is_a? Profile
|
||||||
person = parse_owner_id_from_xml post
|
person = parse_owner_id_from_xml body
|
||||||
person.profile = object
|
person.profile = object
|
||||||
person.save
|
person.save
|
||||||
elsif object.is_a? Request
|
elsif object.is_a? Request
|
||||||
person = get_or_create_person_object_from_xml(post)
|
person = get_or_create_person_object_from_xml(body)
|
||||||
person.serialized_key ||= object.exported_key
|
person.serialized_key ||= object.exported_key
|
||||||
object.person = person
|
object.person = person
|
||||||
object.person.save
|
object.person.save
|
||||||
object.save
|
object.save
|
||||||
elsif object.respond_to? :person
|
elsif object.respond_to? :person
|
||||||
object.person = parse_owner_from_xml post.to_s
|
object.person = parse_owner_from_xml body.to_s
|
||||||
end
|
end
|
||||||
|
object
|
||||||
objects << object
|
|
||||||
rescue NameError => e
|
rescue NameError => e
|
||||||
if e.message.include? 'wrong constant name'
|
if e.message.include? 'wrong constant name'
|
||||||
Rails.logger.info "Not a real type: #{object.to_s}"
|
Rails.logger.info "Not a real type: #{object.to_s}"
|
||||||
raise e
|
end
|
||||||
else
|
|
||||||
raise e
|
raise e
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
objects
|
|
||||||
end
|
|
||||||
|
|
||||||
def store_objects_from_xml(xml, user)
|
def store_from_xml(xml, user)
|
||||||
objects = parse_objects_from_xml(xml)
|
object = parse_from_xml(xml)
|
||||||
objects.each do |p|
|
Rails.logger.debug("Receiving object:\n#{object.inspect}")
|
||||||
Rails.logger.debug("Receiving object:\n#{p.inspect}")
|
|
||||||
|
|
||||||
if p.is_a? Retraction
|
if object.is_a? Retraction
|
||||||
Rails.logger.debug "Got a retraction for #{p.post_id}"
|
Rails.logger.debug "Got a retraction for #{object.post_id}"
|
||||||
p.perform
|
object.perform
|
||||||
|
|
||||||
elsif p.is_a? Request
|
elsif object.is_a? Request
|
||||||
user.receive_friend_request(p)
|
user.receive_friend_request(object)
|
||||||
|
|
||||||
elsif p.is_a? Profile
|
elsif object.is_a? Profile
|
||||||
p.save
|
object.save
|
||||||
elsif p.respond_to?(:person) && !(p.person.nil?) && !(p.person.is_a? User)
|
|
||||||
Rails.logger.debug("Saving object with success: #{p.save}")
|
elsif object.respond_to?(:person) && !(object.person.nil?) && !(object.person.is_a? User)
|
||||||
end
|
Rails.logger.debug("Saving object with success: #{object.save}")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ describe Diaspora::Parser do
|
||||||
10.times {
|
10.times {
|
||||||
message = Factory.build(:status_message, :person => @user)
|
message = Factory.build(:status_message, :person => @user)
|
||||||
xml = message.to_diaspora_xml
|
xml = message.to_diaspora_xml
|
||||||
store_objects_from_xml(xml, @user)
|
store_from_xml(xml, @user)
|
||||||
}
|
}
|
||||||
StatusMessage.count.should == 0
|
StatusMessage.count.should == 0
|
||||||
end
|
end
|
||||||
|
|
@ -28,23 +28,17 @@ describe Diaspora::Parser do
|
||||||
<post><person></person></post>
|
<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>
|
<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>"
|
</XML>"
|
||||||
store_objects_from_xml(xml, @user)
|
store_from_xml(xml, @user)
|
||||||
Post.count.should == 0
|
Post.count.should == 0
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should discard types which are not of type post' do
|
it 'should discard types which are not of type post' do
|
||||||
xml = "<XML>
|
xml = "<XML>
|
||||||
<head>
|
|
||||||
<sender>
|
|
||||||
<email>#{Person.first.email}</email>
|
|
||||||
</sender>
|
|
||||||
</head>
|
|
||||||
<posts>
|
|
||||||
<post><person></person></post>
|
<post><person></person></post>
|
||||||
</posts></XML>"
|
</XML>"
|
||||||
|
|
||||||
store_objects_from_xml(xml, @user)
|
store_from_xml(xml, @user)
|
||||||
Post.count.should == 0
|
Post.count.should == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -60,20 +54,13 @@ describe Diaspora::Parser do
|
||||||
body.should include "</post>"
|
body.should include "</post>"
|
||||||
end
|
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
|
it 'should be able to correctly handle comments' do
|
||||||
person = Factory.create(:person, :email => "test@testing.com")
|
person = Factory.create(:person, :email => "test@testing.com")
|
||||||
post = Factory.create(:status_message, :person => @user.person)
|
post = Factory.create(:status_message, :person => @user.person)
|
||||||
comment = Factory.build(:comment, :post => post, :person => person, :text => "Freedom!")
|
comment = Factory.build(:comment, :post => post, :person => person, :text => "Freedom!")
|
||||||
xml = comment.to_diaspora_xml
|
xml = comment.to_diaspora_xml
|
||||||
|
|
||||||
objects = parse_objects_from_xml(xml)
|
comment = parse_from_xml(xml)
|
||||||
comment = objects.first
|
|
||||||
comment.text.should == "Freedom!"
|
comment.text.should == "Freedom!"
|
||||||
comment.person.should == person
|
comment.person.should == person
|
||||||
comment.post.should == post
|
comment.post.should == post
|
||||||
|
|
@ -86,7 +73,7 @@ describe Diaspora::Parser do
|
||||||
request = retraction.to_diaspora_xml
|
request = retraction.to_diaspora_xml
|
||||||
|
|
||||||
StatusMessage.count.should == 1
|
StatusMessage.count.should == 1
|
||||||
store_objects_from_xml( request, @user )
|
store_from_xml( request, @user )
|
||||||
StatusMessage.count.should == 0
|
StatusMessage.count.should == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -98,7 +85,7 @@ describe Diaspora::Parser do
|
||||||
|
|
||||||
@person.destroy
|
@person.destroy
|
||||||
Person.all.count.should be 1
|
Person.all.count.should be 1
|
||||||
store_objects_from_xml(xml, @user)
|
store_from_xml(xml, @user)
|
||||||
Person.all.count.should be 2
|
Person.all.count.should be 2
|
||||||
|
|
||||||
Person.first(:_id => original_person_id).serialized_key.include?("PUBLIC").should be true
|
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
|
Person.all.count.should be 3
|
||||||
store_objects_from_xml(xml, @user)
|
store_from_xml(xml, @user)
|
||||||
Person.all.count.should be 3
|
Person.all.count.should be 3
|
||||||
|
|
||||||
@user2.reload
|
@user2.reload
|
||||||
|
|
@ -144,7 +131,7 @@ describe Diaspora::Parser do
|
||||||
|
|
||||||
@person.destroy
|
@person.destroy
|
||||||
request_remote.destroy
|
request_remote.destroy
|
||||||
store_objects_from_xml(xml, @user)
|
store_from_xml(xml, @user)
|
||||||
new_person = Person.first(:url => @person.url)
|
new_person = Person.first(:url => @person.url)
|
||||||
new_person.nil?.should be false
|
new_person.nil?.should be false
|
||||||
|
|
||||||
|
|
@ -158,7 +145,7 @@ describe Diaspora::Parser do
|
||||||
request = retraction.to_diaspora_xml
|
request = retraction.to_diaspora_xml
|
||||||
|
|
||||||
Person.count.should == 2
|
Person.count.should == 2
|
||||||
store_objects_from_xml( request , @user)
|
store_from_xml( request , @user)
|
||||||
Person.count.should == 1
|
Person.count.should == 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -184,7 +171,7 @@ describe Diaspora::Parser do
|
||||||
old_profile.first_name.should == 'bob'
|
old_profile.first_name.should == 'bob'
|
||||||
|
|
||||||
#Marshal profile
|
#Marshal profile
|
||||||
store_objects_from_xml xml, @user
|
store_from_xml xml, @user
|
||||||
|
|
||||||
#Check that marshaled profile is the same as old profile
|
#Check that marshaled profile is the same as old profile
|
||||||
person = Person.first(:id => person.id)
|
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
|
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.pending_requests.size.should be 1
|
||||||
@user2.accept_friend_request @request_three.id
|
@user2.accept_friend_request @request_three.id
|
||||||
@user2.friends.include?(@user.person).should be true
|
@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
|
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.pending_requests.size.should be 1
|
||||||
@user2.ignore_friend_request @request_three.id
|
@user2.ignore_friend_request @request_three.id
|
||||||
@user2.friends.include?(@user.person).should be false
|
@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
|
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.pending_requests.size.should be 1
|
||||||
@user.accept_friend_request @request.id
|
@user.accept_friend_request @request.id
|
||||||
@user.friends.include?(@person_one).should be true
|
@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.pending_requests.size.should be 1
|
||||||
@user2.accept_friend_request @request_two.id
|
@user2.accept_friend_request @request_two.id
|
||||||
@user2.friends.include?(@person_one).should be true
|
@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
|
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.pending_requests.size.should be 1
|
||||||
@user.accept_friend_request @request.id
|
@user.accept_friend_request @request.id
|
||||||
@user.friends.include?(@person_one).should be true
|
@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.pending_requests.size.should be 1
|
||||||
@user2.ignore_friend_request @request_two.id
|
@user2.ignore_friend_request @request_two.id
|
||||||
@user2.friends.include?(@person_one).should be false
|
@user2.friends.include?(@person_one).should be false
|
||||||
|
|
@ -135,12 +135,12 @@ describe User do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should not keep the person around if the users ignores them' do
|
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.pending_requests.size.should be 1
|
||||||
@user.ignore_friend_request @user.pending_requests.first.id
|
@user.ignore_friend_request @user.pending_requests.first.id
|
||||||
@user.friends.include?(@person_one).should be false
|
@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.pending_requests.size.should be 1
|
||||||
@user2.ignore_friend_request @user2.pending_requests.first.id#@request_two.id
|
@user2.ignore_friend_request @user2.pending_requests.first.id#@request_two.id
|
||||||
@user2.friends.include?(@person_one).should be false
|
@user2.friends.include?(@person_one).should be false
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,7 @@ describe 'user encryption' do
|
||||||
xml = request.to_diaspora_xml
|
xml = request.to_diaspora_xml
|
||||||
person.destroy
|
person.destroy
|
||||||
personcount = Person.all.count
|
personcount = Person.all.count
|
||||||
store_objects_from_xml(xml, @user)
|
store_from_xml(xml, @user)
|
||||||
Person.all.count.should == personcount + 1
|
Person.all.count.should == personcount + 1
|
||||||
new_person = Person.first(:url => "http://test.url/")
|
new_person = Person.first(:url => "http://test.url/")
|
||||||
new_person.id.should == id
|
new_person.id.should == id
|
||||||
|
|
@ -113,7 +113,7 @@ describe 'user encryption' do
|
||||||
xml = message.to_diaspora_xml
|
xml = message.to_diaspora_xml
|
||||||
message.destroy
|
message.destroy
|
||||||
Post.count.should be 0
|
Post.count.should be 0
|
||||||
store_objects_from_xml(xml, @user)
|
store_from_xml(xml, @user)
|
||||||
Post.count.should be 0
|
Post.count.should be 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue