80 lines
2.4 KiB
Ruby
80 lines
2.4 KiB
Ruby
module Diaspora
|
|
module Parser
|
|
def parse_owner_from_xml(xml)
|
|
doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
|
|
email = doc.xpath("//person/email").text.to_s
|
|
Person.first(:email => email)
|
|
end
|
|
|
|
def parse_body_contents_from_xml(xml)
|
|
doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
|
|
doc.xpath("/XML/posts/post")
|
|
end
|
|
|
|
def parse_owner_id_from_xml(doc)
|
|
id = doc.xpath("//person_id").text.to_s
|
|
Person.first(:id => id)
|
|
end
|
|
|
|
def get_or_create_person_object_from_xml(doc)
|
|
person_xml = doc.xpath("//request/person").to_s
|
|
person_id = doc.xpath("//request/person/_id").text.to_s
|
|
person = Person.first(:_id => person_id)
|
|
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
|
|
|
|
objects << object
|
|
rescue NameError => e
|
|
if e.message.include? 'wrong constant name'
|
|
Rails.logger.info "Not a real type: #{object.to_s}"
|
|
else
|
|
raise e
|
|
end
|
|
end
|
|
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}")
|
|
if p.is_a? Retraction
|
|
Rails.logger.debug "Got a retraction for #{p.post_id}"
|
|
p.perform
|
|
|
|
elsif p.is_a? Request
|
|
puts user.pending_requests.count
|
|
user.receive_friend_request(p)
|
|
puts user.pending_requests.count
|
|
|
|
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
|
|
end
|
|
end
|
|
end
|
|
end
|