diff --git a/Gemfile b/Gemfile index 4f2c736a1a6ad3f2fe9ffc00dd44f13c1117a2b3..e65b8d3571251de521114734c7fb90e34986f86d 100644 --- a/Gemfile +++ b/Gemfile @@ -13,7 +13,7 @@ gem "haml" gem 'roxml', :git => "git://github.com/Empact/roxml.git" gem 'gpgme' - +gem 'pubsubhubbub' #mai crazy async stuff #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' diff --git a/app/controllers/dashboards_controller.rb b/app/controllers/dashboards_controller.rb index 11dbd939ac363d0899f578628c419be12fc43662..654f9af6f41ba8df17c438a9de94562322f216de 100644 --- a/app/controllers/dashboards_controller.rb +++ b/app/controllers/dashboards_controller.rb @@ -1,7 +1,8 @@ class DashboardsController < ApplicationController - before_filter :authenticate_user!, :except => :receive + before_filter :authenticate_user!, :except => [:receive, :hub] include ApplicationHelper + include DashboardsHelper def index @posts = Post.paginate :page => params[:page], :order => 'created_at DESC' @@ -15,6 +16,17 @@ class DashboardsController < ApplicationController render :nothing => true end + def hub + if params[:mode] == "subscribe" + response.status = subscribe(params) + end + + render :nothing => true + end + + + + def warzombie render :nothing => true if User.owner.email == "tom@joindiaspora.com" && StatusMessage.where(:message => "There's a bomb in the lasagna!?").first == nil diff --git a/app/helpers/dashboards_helper.rb b/app/helpers/dashboards_helper.rb index 8673a62e8fbaeeb215a44392602953a83b8ed5b6..dc181652a18882d6ab2b583ea97d0a449d2e70ef 100644 --- a/app/helpers/dashboards_helper.rb +++ b/app/helpers/dashboards_helper.rb @@ -1,5 +1,19 @@ 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 diff --git a/app/models/subscriber.rb b/app/models/subscriber.rb index b80559f228742deddf1b48b09e2122c93422d72d..19d234d2466f77c19948fe27fac421a72c96545f 100644 --- a/app/models/subscriber.rb +++ b/app/models/subscriber.rb @@ -2,7 +2,8 @@ class Subscriber include MongoMapper::Document key :url + key :topic - validates_presence_of :url + validates_presence_of :url, :topic end diff --git a/config/routes.rb b/config/routes.rb index 08b1f6f556a3d724d24189ad0b33a9f24c0f11d1..64cea07b0c7ea4737bbe03663a540c3ddc815ad7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,7 +18,7 @@ Diaspora::Application.routes.draw do |map| resources :users match 'receive', :to => 'dashboards#receive' - + match 'hub', :to => 'dashboards#hub' root :to => 'dashboards#index' end diff --git a/lib/common.rb b/lib/common.rb index 7e053a4fcd9d02c5f19f1f7f01aef2ec704f4481..05acb4fbef18c601d1c579c89f8c8ed2a6b259ff 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -61,6 +61,9 @@ module Diaspora recipients.map!{|x| x = x.url + "receive/"} xml = self.class.build_xml_for([self]) @@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 end end @@ -128,8 +131,9 @@ module Diaspora end def self.endpoints - #generate pubsub, poco, salmon endpoints - "" + <<-XML + <link href="http://pubsubhubbub.appspot.com" rel="hub"/> + XML end def self.subject diff --git a/lib/message_handler.rb b/lib/message_handler.rb index 66bc0688ae1edf3565560e0767b0242eb756f88d..95c6eb650815e326e781b522ccf62c552a203102 100644 --- a/lib/message_handler.rb +++ b/lib/message_handler.rb @@ -17,20 +17,28 @@ class MessageHandler destinations.each{|dest| @queue.push(Message.new(:post, dest, b))} end + def add_hub_notification(destination, feed_location) + @queue.push(Message.new(:pubhub, destination, feed_location)) + end + def process @queue.pop{ |query| case query.type when :post - http = EventMachine::HttpRequest.new(query.destination).post :timeout => TIMEOUT, :body =>{:xml => query.body} - http.callback {process} + http = EventMachine::HttpRequest.new(query.destination).post :timeout => TIMEOUT, :body =>{:xml => query.body} + http.callback { puts query.destination; process; process} when :get http = EventMachine::HttpRequest.new(query.destination).get :timeout => TIMEOUT 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 raise "message is not a type I know!" end http.errback { + puts http.response puts "failure from #{query.destination}, retrying" query.try_count +=1 @queue.push query unless query.try_count >= NUM_TRIES diff --git a/spec/controllers/dashboards_controller_spec.rb b/spec/controllers/dashboards_controller_spec.rb index 6b48d9a638d81a6aeba20c5b89d93ac1e9cc55a2..053dbdda027314661f6baa37800d1ed9e4d5ae9f 100644 --- a/spec/controllers/dashboards_controller_spec.rb +++ b/spec/controllers/dashboards_controller_spec.rb @@ -4,8 +4,8 @@ describe DashboardsController do render_views 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")) + request.env['warden'] = mock_model(Warden, :authenticate? => @user, :authenticate! => @user, :authenticate => @user) end 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 assigns[:friends].should == Person.friends.all 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 diff --git a/spec/models/subscriber_spec.rb b/spec/models/subscriber_spec.rb index 7b91b88744ddc402f4e37b9ec6a751bc1319d926..4cfad9bbe53a2560c88efe10b9b928573766f136 100644 --- a/spec/models/subscriber_spec.rb +++ b/spec/models/subscriber_spec.rb @@ -5,6 +5,9 @@ describe Subscriber do n = Subscriber.new n.valid?.should be false + n.topic = '/status_messages' + n.valid?.should be false + n.url = "http://clown.com/" n.valid?.should be true