module Diaspora module XMLParser def parse_owner_from_xml(xml) doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks } email = doc.xpath("//person/email").text.to_s Person.where(:email => email).first end def parse_body_contents_from_xml(xml) doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks } doc.xpath("/XML/posts/post") 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 object.person = parse_owner_from_xml post.to_s if object.respond_to? :person objects << object rescue puts "Not a real type: #{object.to_s}" end end objects end def store_objects_from_xml(xml) objects = parse_objects_from_xml(xml) objects.each do |p| if p.is_a? Retraction p.perform elsif p.is_a? Request User.owner.receive_friend_request(p) #This line checks if the sender was in the database, among other things? elsif p.respond_to?(:person) && !(p.person.nil?) && !(p.person.is_a? User) #WTF p.save end #p.save if p.respond_to?(:person) && !(p.person == nil) #WTF end end end module Webhooks def self.included(klass) klass.class_eval do include ROXML require 'message_handler' @@queue = MessageHandler.new def notify_people if self.person_id == User.owner.id push_to(people_with_permissions) end end def push_to(recipients) @@queue.add_hub_notification(APP_CONFIG[:pubsub_server], User.owner.url + self.class.to_s.pluralize.underscore + '.atom') unless recipients.empty? recipients.map!{|x| x = x.url + "receive/"} xml = self.class.build_xml_for([self]) @@queue.add_post_request( recipients, xml ) end @@queue.process end def push_to_url(url) hook_url = url + "receive/" xml = self.class.build_xml_for([self]) @@queue.add_post_request( [hook_url], xml ) @@queue.process end def prep_webhook "#{self.to_xml.to_s}" end def people_with_permissions Person.friends.all end def self.build_xml_for(posts) xml = "" xml += "\n " posts.each {|x| xml << x.prep_webhook} xml += "" xml += "" end end end end module XML OWNER = User.owner def self.generate(opts= {}) xml = Generate::headers(opts[:current_url]) xml << Generate::author xml << Generate::endpoints xml << Generate::subject xml << Generate::entries(opts[:objects]) xml << Generate::footer end module Generate def self.headers(current_url) <<-XML Diaspora #{current_url} Stream its a stream #{Time.now.xmlschema} XML end def self.author <<-XML #{OWNER.real_name} #{OWNER.url} XML end def self.endpoints <<-XML XML end def self.subject <<-XML http://activitystrea.ms/schema/1.0/person #{OWNER.url} #{OWNER.real_name} XML end def self.entries(objects) xml = "" if objects.respond_to? :each objects.each {|x| xml << self.entry(x)} else xml << self.entry(objects) end xml end def self.entry(object) eval "#{object.class}_build_entry(object)" end def self.StatusMessage_build_entry(status_message) <<-XML #{status_message.message} #{OWNER.url}status_messages/#{status_message.id} #{status_message.created_at.xmlschema} #{status_message.updated_at.xmlschema} XML end def self.footer <<-XML.strip XML end end end end