pubsub support

This commit is contained in:
danielvincent 2010-10-05 13:39:56 -07:00
parent aed9125e93
commit f3c34692d8
6 changed files with 45 additions and 4 deletions

View file

@ -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]

View file

@ -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

View file

@ -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 )

View file

@ -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"

View file

@ -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

View file

@ -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