From f3c34692d8afb4b8cc2e6dca152e7a1cdba830e3 Mon Sep 17 00:00:00 2001 From: danielvincent Date: Tue, 5 Oct 2010 13:39:56 -0700 Subject: [PATCH] pubsub support --- app/controllers/publics_controller.rb | 6 ++++++ app/models/person.rb | 2 +- app/models/user.rb | 12 +++++++++--- config/routes.rb | 2 ++ lib/message_handler.rb | 7 +++++++ spec/lib/message_handler_spec.rb | 20 ++++++++++++++++++++ 6 files changed, 45 insertions(+), 4 deletions(-) diff --git a/app/controllers/publics_controller.rb b/app/controllers/publics_controller.rb index e3c90fc16..311fdf73f 100644 --- a/app/controllers/publics_controller.rb +++ b/app/controllers/publics_controller.rb @@ -28,6 +28,12 @@ class PublicsController < ApplicationController end end + def hub + if params['hub.mode'] == 'subscribe' || params['hub.mode'] == 'unsubscribe' + render :text => params['hub.challenge'], :status => 202, :layout => false + end + end + def receive render :nothing => true return unless params[:xml] diff --git a/app/models/person.rb b/app/models/person.rb index 28503e921..75bd2b39b 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -50,7 +50,7 @@ class Person end def public_url - "#{self.url}users/#{self.owner.username}/public" + "#{self.url}users/#{self.owner.username}/public.atom" end diff --git a/app/models/user.rb b/app/models/user.rb index 2f63f4b5f..91c82f101 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -167,6 +167,8 @@ class User target_people = target_people | aspect.people } + push_to_hub(post) if post.respond_to?(:public) && post.public + push_to_people(post, target_people) end @@ -179,10 +181,14 @@ class User end def push_to_person( person, xml ) - Rails.logger.debug("Adding xml for #{self} to message queue to #{url}") - QUEUE.add_post_request( person.receive_url, xml ) - QUEUE.process + Rails.logger.debug("Adding xml for #{self} to message queue to #{self.url}") + QUEUE.add_post_request( person.receive_url, xml ) + QUEUE.process + end + def push_to_hub(post) + Rails.logger.debug("Pushing update to pubsub server") + QUEUE.add_hub_notification(APP_CONFIG[:pubsub_server], self.public_url) end def salmon( post ) diff --git a/config/routes.rb b/config/routes.rb index 56ec559c9..16f6a9c98 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -43,6 +43,8 @@ Diaspora::Application.routes.draw do match 'webfinger', :to => 'publics#webfinger' match 'hcard/users/:id', :to => 'publics#hcard' + match 'hub', :to => 'publics#hub' + match '.well-known/host-meta',:to => 'publics#host_meta' match 'receive/users/:id', :to => 'publics#receive' match 'log', :to => "dev_utilities#log" diff --git a/lib/message_handler.rb b/lib/message_handler.rb index e6ce75ffc..c6d1f013c 100644 --- a/lib/message_handler.rb +++ b/lib/message_handler.rb @@ -20,6 +20,10 @@ class MessageHandler [*destinations].each{|dest| @queue.push(Message.new(:post, dest, :body => b))} end + def add_hub_notification(hub_url, feed_url) + @queue.push(Message.new(:hub_publish, hub_url, :body => feed_url)) + end + def process @queue.pop{ |query| case query.type @@ -29,6 +33,9 @@ class MessageHandler when :get http = EventMachine::HttpRequest.new(query.destination).get :timeout => TIMEOUT http.callback {process} + when :hub_publish + http = EventMachine::PubSubHubBub.new(query.destination).get :timeout => TIMEOUT + http.callback {process} else raise "message is not a type I know!" end diff --git a/spec/lib/message_handler_spec.rb b/spec/lib/message_handler_spec.rb index cb0592e9a..ddc822334 100644 --- a/spec/lib/message_handler_spec.rb +++ b/spec/lib/message_handler_spec.rb @@ -94,6 +94,26 @@ describe MessageHandler do end end + describe "Hub publish" do + it 'should correctly queue up a pubsubhubbub publish request' do + destination = "http://identi.ca/hub/" + feed_location = "http://google.com/" + + EventMachine.run { + @handler.add_hub_notification(destination, feed_location) + q = @handler.instance_variable_get(:@queue) + + message = "" + q.pop{|m| message = m} + + message.destination.should == destination + message.body.should == feed_location + + EventMachine.stop + } + end + end + describe "Mixed Queries" do it 'should process both POST and GET requests in the same queue' do