RS, DG; Cleaned up the switch in user.receive
This commit is contained in:
parent
bee71c5a23
commit
0907d7a9fd
5 changed files with 39 additions and 45 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]}"
|
||||
@user.receive params[:xml]
|
||||
@user.receive params[:xml] if params[:xml]
|
||||
render :nothing => true
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -120,7 +120,7 @@ class User
|
|||
|
||||
###### Receiving #######
|
||||
def receive xml
|
||||
object = Diaspora::Parser.parse_from_xml(xml)
|
||||
object = Diaspora::Parser.from_xml(xml)
|
||||
Rails.logger.debug("Receiving object:\n#{object.inspect}")
|
||||
|
||||
if object.is_a? Retraction
|
||||
|
|
@ -128,7 +128,7 @@ class User
|
|||
object.perform
|
||||
|
||||
elsif object.is_a? Request
|
||||
person = get_or_create_person_object_from_xml( xml )
|
||||
person = Diaspora::Parser.get_or_create_person_object_from_xml( xml )
|
||||
person.serialized_key ||= object.exported_key
|
||||
object.person = person
|
||||
object.person.save
|
||||
|
|
@ -136,10 +136,10 @@ class User
|
|||
receive_friend_request(object)
|
||||
|
||||
elsif object.is_a? Profile
|
||||
person = Diaspora::Parser.parse_owner_id_from_xml xml
|
||||
person = Diaspora::Parser.owner_id_from_xml xml
|
||||
person.profile = object
|
||||
person.save
|
||||
elsif object.respond_to?(:person) && !(object.person.nil?) && !(object.person.is_a? User)
|
||||
else
|
||||
Rails.logger.debug("Saving object with success: #{object.save}")
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,17 +1,12 @@
|
|||
module Diaspora
|
||||
module Parser
|
||||
def parse_body_contents_from_xml(xml)
|
||||
doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
|
||||
doc.xpath("/XML/post")
|
||||
end
|
||||
|
||||
def parse_owner_id_from_xml(xml)
|
||||
def self.owner_id_from_xml(xml)
|
||||
doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
|
||||
id = doc.xpath("//person_id").text.to_s
|
||||
Person.first(:id => id)
|
||||
end
|
||||
|
||||
def get_or_create_person_object_from_xml(xml)
|
||||
def self.get_or_create_person_object_from_xml(xml)
|
||||
doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
|
||||
person_xml = doc.xpath("//request/person").to_s
|
||||
person_id = doc.xpath("//request/person/_id").text.to_s
|
||||
|
|
@ -19,9 +14,10 @@ module Diaspora
|
|||
person ? person : Person.from_xml( person_xml)
|
||||
end
|
||||
|
||||
def parse_from_xml(xml)
|
||||
def self.from_xml(xml)
|
||||
|
||||
return unless body = parse_body_contents_from_xml(xml).children.first
|
||||
doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
|
||||
return unless body = doc.xpath("/XML/post").children.first
|
||||
|
||||
begin
|
||||
body.name.camelize.constantize.from_xml body.to_s
|
||||
|
|
|
|||
|
|
@ -10,29 +10,35 @@ describe Diaspora::Parser do
|
|||
@user = Factory.create(:user, :email => "bob@aol.com")
|
||||
@person = Factory.create(:person_with_private_key, :email => "bill@gates.com")
|
||||
end
|
||||
describe 'with encryption' do
|
||||
before do
|
||||
unstub_mocha_stubs
|
||||
end
|
||||
after do
|
||||
stub_signature_verification
|
||||
end
|
||||
it "should not store posts from me" do
|
||||
10.times {
|
||||
message = Factory.build(:status_message, :person => @user)
|
||||
xml = message.to_diaspora_xml
|
||||
@user.receive xml
|
||||
}
|
||||
StatusMessage.count.should == 0
|
||||
end
|
||||
|
||||
it "should reject xml with no sender" do
|
||||
xml = "<XML>
|
||||
<head>
|
||||
</head>
|
||||
<post><status_message>\n <message>Here is another message</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></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>
|
||||
</XML>"
|
||||
@user.receive xml
|
||||
Post.count.should == 0
|
||||
|
||||
it "should not store posts from me" do
|
||||
10.times {
|
||||
message = Factory.build(:status_message, :person => @user)
|
||||
xml = message.to_diaspora_xml
|
||||
@user.receive xml
|
||||
}
|
||||
StatusMessage.count.should == 0
|
||||
end
|
||||
|
||||
it "should reject xml with no sender" do
|
||||
xml = "<XML>
|
||||
<head>
|
||||
</head>
|
||||
<post><status_message>\n <message>Here is another message</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></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>
|
||||
</XML>"
|
||||
@user.receive xml
|
||||
Post.count.should == 0
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
it 'should discard types which are not of type post' do
|
||||
xml = "<XML>
|
||||
<post><person></person></post>
|
||||
|
|
@ -47,12 +53,6 @@ describe Diaspora::Parser do
|
|||
before do
|
||||
@xml = Factory.build(:status_message).to_diaspora_xml
|
||||
end
|
||||
|
||||
it 'should be able to parse the body\'s contents' do
|
||||
body = parse_body_contents_from_xml(@xml).to_s
|
||||
body.should include "<post>"
|
||||
body.should include "</post>"
|
||||
end
|
||||
|
||||
it 'should be able to correctly handle comments' do
|
||||
person = Factory.create(:person, :email => "test@testing.com")
|
||||
|
|
@ -60,7 +60,7 @@ describe Diaspora::Parser do
|
|||
comment = Factory.build(:comment, :post => post, :person => person, :text => "Freedom!")
|
||||
xml = comment.to_diaspora_xml
|
||||
|
||||
comment = parse_from_xml(xml)
|
||||
comment = Diaspora::Parser.from_xml(xml)
|
||||
comment.text.should == "Freedom!"
|
||||
comment.person.should == person
|
||||
comment.post.should == post
|
||||
|
|
|
|||
|
|
@ -1,7 +1,5 @@
|
|||
require File.dirname(__FILE__) + '/../spec_helper'
|
||||
|
||||
include Diaspora::Parser
|
||||
|
||||
describe User do
|
||||
before do
|
||||
@user = Factory.create(:user)
|
||||
|
|
|
|||
Loading…
Reference in a new issue