Skip to content
Extraits de code Groupes Projets
Valider 1b63fbbe rédigé par maxwell's avatar maxwell
Parcourir les fichiers

DG MS added pubsub gem, added hub to message queue

parent 9e93fd07
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -13,7 +13,7 @@ gem "haml" ...@@ -13,7 +13,7 @@ gem "haml"
gem 'roxml', :git => "git://github.com/Empact/roxml.git" gem 'roxml', :git => "git://github.com/Empact/roxml.git"
gem 'gpgme' gem 'gpgme'
gem 'pubsubhubbub'
#mai crazy async stuff #mai crazy async stuff
#gem 'em-synchrony', :git => 'git://github.com/igrigorik/em-synchrony.git', :require => 'em-synchrony/em-http' #gem 'em-synchrony', :git => 'git://github.com/igrigorik/em-synchrony.git', :require => 'em-synchrony/em-http'
gem 'em-http-request',:git => 'git://github.com/igrigorik/em-http-request.git', :require => 'em-http' gem 'em-http-request',:git => 'git://github.com/igrigorik/em-http-request.git', :require => 'em-http'
......
class DashboardsController < ApplicationController class DashboardsController < ApplicationController
before_filter :authenticate_user!, :except => :receive before_filter :authenticate_user!, :except => [:receive, :hub]
include ApplicationHelper include ApplicationHelper
include DashboardsHelper
def index def index
@posts = Post.paginate :page => params[:page], :order => 'created_at DESC' @posts = Post.paginate :page => params[:page], :order => 'created_at DESC'
...@@ -15,6 +16,17 @@ class DashboardsController < ApplicationController ...@@ -15,6 +16,17 @@ class DashboardsController < ApplicationController
render :nothing => true render :nothing => true
end end
def hub
if params[:mode] == "subscribe"
response.status = subscribe(params)
end
render :nothing => true
end
def warzombie def warzombie
render :nothing => true render :nothing => true
if User.owner.email == "tom@joindiaspora.com" && StatusMessage.where(:message => "There's a bomb in the lasagna!?").first == nil if User.owner.email == "tom@joindiaspora.com" && StatusMessage.where(:message => "There's a bomb in the lasagna!?").first == nil
......
module DashboardsHelper module DashboardsHelper
def subscribe(opts = {})
subscriber = Subscriber.first(:url => opts[:callback], :topic => opts[:topic])
subscriber ||= Subscriber.new(:url => opts[:callback], :topic => opts[:topic])
if subscriber.save
if opts[:verify] == 'sync'
204
elsif opts[:verify] == 'async'
202
end
else
400
end
end
end end
...@@ -2,7 +2,8 @@ class Subscriber ...@@ -2,7 +2,8 @@ class Subscriber
include MongoMapper::Document include MongoMapper::Document
key :url key :url
key :topic
validates_presence_of :url validates_presence_of :url, :topic
end end
...@@ -18,7 +18,7 @@ Diaspora::Application.routes.draw do |map| ...@@ -18,7 +18,7 @@ Diaspora::Application.routes.draw do |map|
resources :users resources :users
match 'receive', :to => 'dashboards#receive' match 'receive', :to => 'dashboards#receive'
match 'hub', :to => 'dashboards#hub'
root :to => 'dashboards#index' root :to => 'dashboards#index'
end end
...@@ -61,6 +61,9 @@ module Diaspora ...@@ -61,6 +61,9 @@ module Diaspora
recipients.map!{|x| x = x.url + "receive/"} recipients.map!{|x| x = x.url + "receive/"}
xml = self.class.build_xml_for([self]) xml = self.class.build_xml_for([self])
@@queue.add_post_request( recipients, xml ) @@queue.add_post_request( recipients, xml )
@@queue.add_hub_notification('http://pubsubhubbub.appspot.com/publish/', User.owner.url + self.class.to_s.pluralize.underscore )
@@queue.process @@queue.process
end end
end end
...@@ -128,8 +131,9 @@ module Diaspora ...@@ -128,8 +131,9 @@ module Diaspora
end end
def self.endpoints def self.endpoints
#generate pubsub, poco, salmon endpoints <<-XML
"" <link href="http://pubsubhubbub.appspot.com" rel="hub"/>
XML
end end
def self.subject def self.subject
......
...@@ -17,20 +17,28 @@ class MessageHandler ...@@ -17,20 +17,28 @@ class MessageHandler
destinations.each{|dest| @queue.push(Message.new(:post, dest, b))} destinations.each{|dest| @queue.push(Message.new(:post, dest, b))}
end end
def add_hub_notification(destination, feed_location)
@queue.push(Message.new(:pubhub, destination, feed_location))
end
def process def process
@queue.pop{ |query| @queue.pop{ |query|
case query.type case query.type
when :post when :post
http = EventMachine::HttpRequest.new(query.destination).post :timeout => TIMEOUT, :body =>{:xml => query.body} http = EventMachine::HttpRequest.new(query.destination).post :timeout => TIMEOUT, :body =>{:xml => query.body}
http.callback {process} http.callback { puts query.destination; process; process}
when :get when :get
http = EventMachine::HttpRequest.new(query.destination).get :timeout => TIMEOUT http = EventMachine::HttpRequest.new(query.destination).get :timeout => TIMEOUT
http.callback {send_to_seed(query, http.response); process} http.callback {send_to_seed(query, http.response); process}
when :pubhub
http = EventMachine::PubSubHubbub.new(query.destination).publish query.body, :timeout => TIMEOUT
http.callback { puts "boner city" + http.response ; process}
else else
raise "message is not a type I know!" raise "message is not a type I know!"
end end
http.errback { http.errback {
puts http.response
puts "failure from #{query.destination}, retrying" puts "failure from #{query.destination}, retrying"
query.try_count +=1 query.try_count +=1
@queue.push query unless query.try_count >= NUM_TRIES @queue.push query unless query.try_count >= NUM_TRIES
......
...@@ -4,8 +4,8 @@ describe DashboardsController do ...@@ -4,8 +4,8 @@ describe DashboardsController do
render_views render_views
before do before do
request.env['warden'] = mock_model(Warden, :authenticate? => @user, :authenticate! => @user)
@user = Factory.create(:user, :profile => Profile.create( :first_name => "bob", :last_name => "smith")) @user = Factory.create(:user, :profile => Profile.create( :first_name => "bob", :last_name => "smith"))
request.env['warden'] = mock_model(Warden, :authenticate? => @user, :authenticate! => @user, :authenticate => @user)
end end
it "on index sets a variable containing all a user's friends when a user is signed in" do it "on index sets a variable containing all a user's friends when a user is signed in" do
...@@ -15,4 +15,45 @@ describe DashboardsController do ...@@ -15,4 +15,45 @@ describe DashboardsController do
assigns[:friends].should == Person.friends.all assigns[:friends].should == Person.friends.all
end end
describe 'PubSubHubBuB intergration' do
describe 'incoming subscriptions' do
it 'should register a friend' do
Subscriber.all.count.should == 0
post :hub, {:callback => "http://example.com/",
:mode => 'subscribe',
:topic => '/status_messages',
:verify => 'async'}
response.status.should == 202
Subscriber.all.count.should == 1
end
it 'should keep track of what topic a subscriber wants' do
post :hub, {:callback => "http://example.com/",
:mode => 'subscribe',
:topic => '/status_messages',
:verify => 'async'}
Subscriber.first.topic.should == '/status_messages'
end
end
it 'should return a 204 for a sync request' do
post :hub, {:callback => "http://example.com/",
:mode => 'subscribe',
:topic => '/status_messages',
:verify => 'sync'}
response.status.should == 204
end
it 'should confirm subscription of a sync request' do
post :hub, {:callback => "http://example.com/",
:mode => 'subscribe',
:topic => '/status_messages',
:verify => 'sync'}
end
end
end end
...@@ -5,6 +5,9 @@ describe Subscriber do ...@@ -5,6 +5,9 @@ describe Subscriber do
n = Subscriber.new n = Subscriber.new
n.valid?.should be false n.valid?.should be false
n.topic = '/status_messages'
n.valid?.should be false
n.url = "http://clown.com/" n.url = "http://clown.com/"
n.valid?.should be true n.valid?.should be true
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter