From da9f33aa72e79705e2fc467f0bc11ac75c667363 Mon Sep 17 00:00:00 2001 From: danielvincent Date: Wed, 16 Jun 2010 17:35:39 -0700 Subject: [PATCH 01/19] fixed post stream to ascend by time --- app/controllers/dashboard_controller.rb | 9 +-------- app/models/post.rb | 18 +++++++++++++++--- app/views/dashboard/index.html.haml | 2 ++ config/environments/test.rb | 2 +- config/mongoid.yml | 10 +++++----- spec/spec_helper.rb | 1 + 6 files changed, 25 insertions(+), 17 deletions(-) diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index e534252b6..c9d082015 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -2,13 +2,6 @@ class DashboardController < ApplicationController before_filter :authenticate_user! def index - @posts = Post.all - - @bookmarks = Bookmark.all - @status_messages = StatusMessage.all - @blogs = Blog.all - #@status_messages = @posts.select{ |x| x._type == "StatusMessage"} - #@blogs = @posts.select{ |x| x._type == "Blog"} - #@bookmarks = @posts.select{ |x| x._type == "Bookmarks"} + @posts = Post.recent_ordered_posts end end diff --git a/app/models/post.rb b/app/models/post.rb index 34af94757..e4f4eb175 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -14,10 +14,24 @@ class Post field :source field :snippet - before_create :set_defaults #after_update :notify_friends + + + @@models = ["StatusMessage", "Bookmark", "Blog"] + + def self.recent_ordered_posts + # Need to explicitly name each inherited model for dev environment + query = if Rails.env == "development" + Post.criteria.all(:_type => @@models) + else + Post.criteria.all + end + query.order_by( [:created_at, :desc] ) + end + + protected def set_defaults @@ -33,7 +47,5 @@ class Post #xml = self.to_xml_to_s #friends.each{|friend| ping friend :with => xml } #end - end - diff --git a/app/views/dashboard/index.html.haml b/app/views/dashboard/index.html.haml index 5a63f6b9f..1c60dc8e2 100644 --- a/app/views/dashboard/index.html.haml +++ b/app/views/dashboard/index.html.haml @@ -1,7 +1,9 @@ - title "Dashboard" + %ul#stream - for post in @posts %li = render "shared/post", :post =>post + /= post.inspect diff --git a/config/environments/test.rb b/config/environments/test.rb index fab2538d4..40d222032 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -5,7 +5,7 @@ Diaspora::Application.configure do # test suite. You never need to work with it otherwise. Remember that # your test database is "scratch space" for the test suite and is wiped # and recreated between test runs. Don't rely on the data there! - config.cache_classes = false + config.cache_classes = true # Log error messages when you accidentally call methods on nil. config.whiny_nils = true diff --git a/config/mongoid.yml b/config/mongoid.yml index 2a97fd67c..e0df0977c 100644 --- a/config/mongoid.yml +++ b/config/mongoid.yml @@ -19,8 +19,8 @@ test: # set these environment variables on your prod server production: <<: *defaults - host: <%= ENV['MONGOID_HOST'] %> - port: <%= ENV['MONGOID_PORT'] %> - username: <%= ENV['MONGOID_USERNAME'] %> - password: <%= ENV['MONGOID_PASSWORD'] %> - database: <%= ENV['MONGOID_DATABASE'] %> + #host: <%= ENV['MONGOID_HOST'] %> + #port: <%= ENV['MONGOID_PORT'] %> + #username: <%= ENV['MONGOID_USERNAME'] %> + #password: <%= ENV['MONGOID_PASSWORD'] %> + #database: <%= ENV['MONGOID_DATABASE'] %> diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index dbfcae118..bc3f0762a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -24,6 +24,7 @@ Rspec.configure do |config| config.before(:each) do Mongoid.master.collections.select { |c| c.name != 'system.indexes' }.each(&:drop) + end From 43207aeb417839f79d2a77a6bfaa94602010859b Mon Sep 17 00:00:00 2001 From: danielvincent Date: Wed, 16 Jun 2010 23:12:23 -0700 Subject: [PATCH 02/19] pending: resolving post action in queue --- Gemfile | 5 +- app/controllers/application_controller.rb | 6 ++ app/models/post.rb | 3 + app/models/status_message.rb | 2 +- config/routes.rb | 3 +- lib/common.rb | 67 +++++++++++++++++++---- lib/message_handler.rb | 64 ++++++++++++++++++++++ lib/net/curl.rb | 8 ++- 8 files changed, 141 insertions(+), 17 deletions(-) create mode 100644 lib/message_handler.rb diff --git a/Gemfile b/Gemfile index a37827cf3..acc67da92 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,10 @@ source 'http://rubygems.org' gem 'rails', '3.0.0.beta4' - +gem 'mongrel' +gem 'thin' +gem 'em-http-request' +gem 'addressable' gem "mongoid", :git => "http://github.com/durran/mongoid.git" gem "bson_ext", "1.0.1" gem "haml" diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f2569b3a7..958d7a9e2 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,4 +1,10 @@ class ApplicationController < ActionController::Base protect_from_forgery layout 'application' + + def receive + puts response.inspect + puts "holy boner batman" + end + end diff --git a/app/models/post.rb b/app/models/post.rb index e4f4eb175..6375cbe84 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,4 +1,5 @@ class Post + require 'lib/common' # XML accessors must always preceed mongo field tags @@ -6,6 +7,8 @@ class Post include Mongoid::Timestamps include ROXML + include Diaspora::Hookey + xml_accessor :owner xml_accessor :snippet xml_accessor :source diff --git a/app/models/status_message.rb b/app/models/status_message.rb index 4aec011e6..b9eb2c4fc 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -18,7 +18,7 @@ class StatusMessage < Post end def self.retrieve_from_friend(friend) - StatusMessages.from_xml Curl.curl(friend.url+"status_messages.xml") + StatusMessages.from_xml Curl.get(friend.url+"status_messages.xml") end def ==(other) diff --git a/config/routes.rb b/config/routes.rb index 81cd16e47..f1466709a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,7 +13,8 @@ Diaspora::Application.routes.draw do |map| match 'login', :to => 'devise/sessions#new', :as => "new_user_session" match 'logout', :to => 'devise/sessions#destroy', :as => "destroy_user_session" match 'signup', :to => 'devise/registrations#new', :as => "new_user_registration" - + + match 'receive', :to => 'application#receive' resources :users resources :status_messages diff --git a/lib/common.rb b/lib/common.rb index a89072fc5..312d7f773 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -1,18 +1,61 @@ -module CommonField +module Diaspora + module CommonFields + def self.included(klass) + klass.class_eval do + include Mongoid::Document + include ROXML + include Mongoid::Timestamps - def self.included(klass) - klass.class_eval do - include Mongoid::Document - include ROXML - include Mongoid::Timestamps + xml_accessor :owner + xml_accessor :snippet + xml_accessor :source - xml_accessor :owner - xml_accessor :snippet - xml_accessor :source + field :owner + field :source + field :snippet + end + end + end - field :owner - field :source - field :snippet + module Hookey + +class Curl + def self.post(s) + `curl -X POST -d #{s}`;; + end + + def self.get(s) + `curl -X GET #{s}` + end +end + + + def self.included(klass) + + klass.class_eval do + require 'lib/message_handler' + before_save :notify_friends + + def notify_friends + m = MessageHandler.new + + xml = prep_webhook + #friends_with_permissions.each{ |friend| puts friend; Curl.post( "\"" + xml + "\" " + friend) } + m.add_post_request( friends_with_permissions, xml ) + m.process + end + + def prep_webhook + self.to_xml.to_s.chomp + end + + def friends_with_permissions + #Friend.only(:url).map{|x| x = x.url + "/receive/"} + googles = [] + 5.times{ googles <<"http://google.com/"} #"http://localhost:4567/receive/"} #"http://google.com/"} + googles + end + end end end end diff --git a/lib/message_handler.rb b/lib/message_handler.rb new file mode 100644 index 000000000..8ef7594fa --- /dev/null +++ b/lib/message_handler.rb @@ -0,0 +1,64 @@ + require 'em-http' + require 'eventmachine' + require 'addressable/uri' + + class MessageHandler + + + NUM_TRIES = 3 + TIMEOUT = 5 #seconds + + def initialize + @queue = EM::Queue.new + end + + def add_get_request(destinations) + destinations.each{ |dest| @queue.push(Message.new(:get, dest))} + end + + + def add_post_request(destinations, body) + destinations.each{|dest| @queue.push(Message.new(:post, dest, body))} + end + + def process + @queue.pop{ |query| + case query.type + when :post + http = EventMachine::HttpRequest.new(query.destination).post :timeout => TIMEOUT, :body =>query.body + http.callback { process} + when :get + http = EventMachine::HttpRequest.new(query.destination).get :timeout => TIMEOUT + http.callback {send_to_seed(query, http.response); process} + else + raise "message is not a type I know!" + end + + http.errback { + query.try_count +=1 + @queue.push query unless query.try_count >= NUM_TRIES + process + } + } unless @queue.size == 0 + end + + + def send_to_seed(message, http_response) + #DO SOMETHING! + end + + def size + @queue.size + end + + class Message + attr_accessor :type, :destination, :body, :try_count + def initialize(type, dest, body= nil) + @type = type + @destination = dest + @body = body + @try_count = 0 + end + end +end + diff --git a/lib/net/curl.rb b/lib/net/curl.rb index 410255146..4220a27f7 100644 --- a/lib/net/curl.rb +++ b/lib/net/curl.rb @@ -1,6 +1,10 @@ class Curl - def self.curl(s) - `curl #{s}` + def self.post(s) + `curl -X POST -d #{s}`;; + end + + def self.get(s) + `curl -X GET #{s}` end end From 67535c1113d326b536335fa053d3e9d1b2845b37 Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Thu, 17 Jun 2010 09:20:59 -0700 Subject: [PATCH 03/19] tried to git rid of error in em-http-request --- Gemfile | 2 +- app/models/post.rb | 7 +-- lib/common.rb | 21 +++------ lib/message_handler.rb | 100 ++++++++++++++++++++--------------------- 4 files changed, 59 insertions(+), 71 deletions(-) diff --git a/Gemfile b/Gemfile index acc67da92..ddc3e0013 100644 --- a/Gemfile +++ b/Gemfile @@ -3,7 +3,7 @@ source 'http://rubygems.org' gem 'rails', '3.0.0.beta4' gem 'mongrel' gem 'thin' -gem 'em-http-request' +gem 'em-http-request', :git => 'git@github.com:maxwell/em-http-request.git' gem 'addressable' gem "mongoid", :git => "http://github.com/durran/mongoid.git" gem "bson_ext", "1.0.1" diff --git a/app/models/post.rb b/app/models/post.rb index 6375cbe84..879147fcb 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,12 +1,14 @@ class Post require 'lib/common' - + require 'lib/message_handler' + + # XML accessors must always preceed mongo field tags include Mongoid::Document include Mongoid::Timestamps include ROXML - +@@queue = MessageHandler.new include Diaspora::Hookey xml_accessor :owner @@ -21,7 +23,6 @@ class Post #after_update :notify_friends - @@models = ["StatusMessage", "Bookmark", "Blog"] def self.recent_ordered_posts diff --git a/lib/common.rb b/lib/common.rb index 312d7f773..b31081d18 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -19,30 +19,19 @@ module Diaspora module Hookey -class Curl - def self.post(s) - `curl -X POST -d #{s}`;; - end - - def self.get(s) - `curl -X GET #{s}` - end -end - - def self.included(klass) klass.class_eval do - require 'lib/message_handler' + #include EventQueue::MessageHandler before_save :notify_friends def notify_friends - m = MessageHandler.new - + + @@queue = MessageHandler.new xml = prep_webhook #friends_with_permissions.each{ |friend| puts friend; Curl.post( "\"" + xml + "\" " + friend) } - m.add_post_request( friends_with_permissions, xml ) - m.process + @@queue.add_post_request( friends_with_permissions, xml ) + @@queue.process end def prep_webhook diff --git a/lib/message_handler.rb b/lib/message_handler.rb index 8ef7594fa..ff58f3583 100644 --- a/lib/message_handler.rb +++ b/lib/message_handler.rb @@ -4,61 +4,59 @@ class MessageHandler - - NUM_TRIES = 3 - TIMEOUT = 5 #seconds - - def initialize - @queue = EM::Queue.new - end + NUM_TRIES = 3 + TIMEOUT = 5 #seconds - def add_get_request(destinations) - destinations.each{ |dest| @queue.push(Message.new(:get, dest))} - end + def initialize + @queue = EM::Queue.new + end + + def add_get_request(destinations) + destinations.each{ |dest| @queue.push(Message.new(:get, dest))} + end - def add_post_request(destinations, body) - destinations.each{|dest| @queue.push(Message.new(:post, dest, body))} - end + def add_post_request(destinations, body) + destinations.each{|dest| @queue.push(Message.new(:post, dest, body))} + end - def process - @queue.pop{ |query| - case query.type - when :post - http = EventMachine::HttpRequest.new(query.destination).post :timeout => TIMEOUT, :body =>query.body - http.callback { process} - when :get - http = EventMachine::HttpRequest.new(query.destination).get :timeout => TIMEOUT - http.callback {send_to_seed(query, http.response); process} - else - raise "message is not a type I know!" + def process + @queue.pop{ |query| + case query.type + when :post + http = EventMachine::HttpRequest.new(query.destination).post :timeout => TIMEOUT, :body => query.body + http.callback { process} + when :get + http = EventMachine::HttpRequest.new(query.destination).get :timeout => TIMEOUT + http.callback {send_to_seed(query, http.response); process} + else + raise "message is not a type I know!" + end + + http.errback { + query.try_count +=1 + @queue.push query unless query.try_count >= NUM_TRIES + process + } + } unless @queue.size == 0 + end + + + def send_to_seed(message, http_response) + #DO SOMETHING! + end + + def size + @queue.size + end + + class Message + attr_accessor :type, :destination, :body, :try_count + def initialize(type, dest, body= nil) + @type = type + @destination = dest + @body = body + @try_count = 0 end - - http.errback { - query.try_count +=1 - @queue.push query unless query.try_count >= NUM_TRIES - process - } - } unless @queue.size == 0 - end - - - def send_to_seed(message, http_response) - #DO SOMETHING! - end - - def size - @queue.size - end - - class Message - attr_accessor :type, :destination, :body, :try_count - def initialize(type, dest, body= nil) - @type = type - @destination = dest - @body = body - @try_count = 0 end end -end - From d91cec088115f19a9a29f3fd555de30aeb6f8866 Mon Sep 17 00:00:00 2001 From: danielvincent Date: Thu, 17 Jun 2010 10:29:30 -0700 Subject: [PATCH 04/19] queue works. needs cleanup. --- Gemfile | 5 ++++- app/models/post.rb | 32 ++++++++++++++++++++++++-------- lib/common.rb | 8 ++++---- lib/message_handler.rb | 15 ++++++++++----- 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/Gemfile b/Gemfile index ddc3e0013..cfa740a53 100644 --- a/Gemfile +++ b/Gemfile @@ -3,7 +3,7 @@ source 'http://rubygems.org' gem 'rails', '3.0.0.beta4' gem 'mongrel' gem 'thin' -gem 'em-http-request', :git => 'git@github.com:maxwell/em-http-request.git' +gem 'em-http-request' gem 'addressable' gem "mongoid", :git => "http://github.com/durran/mongoid.git" gem "bson_ext", "1.0.1" @@ -12,6 +12,9 @@ gem "devise", :git => "git://github.com/plataformatec/devise.git" gem 'roxml', :git => "git://github.com/Empact/roxml.git" + +gem "dm-core" + group :test do gem 'rspec', '>= 2.0.0.beta.10' gem 'rspec-rails', ">= 2.0.0.beta.8" diff --git a/app/models/post.rb b/app/models/post.rb index 879147fcb..b0d431f8c 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,5 +1,4 @@ class Post - require 'lib/common' require 'lib/message_handler' @@ -8,8 +7,6 @@ class Post include Mongoid::Document include Mongoid::Timestamps include ROXML -@@queue = MessageHandler.new - include Diaspora::Hookey xml_accessor :owner xml_accessor :snippet @@ -21,7 +18,31 @@ class Post before_create :set_defaults #after_update :notify_friends + after_save :notify_friends + + @@queue = MessageHandler.new + + def notify_friends + puts "hello" + xml = prep_webhook + #friends_with_permissions.each{ |friend| puts friend; Curl.post( "\"" + xml + "\" " + friend) } + @@queue.add_post_request( friends_with_permissions, xml ) + @@queue.process + end + + def prep_webhook + self.to_xml.to_s.chomp + end + + def friends_with_permissions + #friends = Friend.only(:url).map{|x| x = x.url + "/receive/"} + #3.times {friends = friends + friends} + #friends + googles = [] + 100.times{ googles <<"http://google.com/"} #"http://localhost:4567/receive/"} #"http://google.com/"} + googles + end @@models = ["StatusMessage", "Bookmark", "Blog"] @@ -46,10 +67,5 @@ class Post end - #def notify_friends - #friends = Permissions.get_list_for(self) - #xml = self.to_xml_to_s - #friends.each{|friend| ping friend :with => xml } - #end end diff --git a/lib/common.rb b/lib/common.rb index b31081d18..db57b2958 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -22,12 +22,12 @@ module Diaspora def self.included(klass) klass.class_eval do - #include EventQueue::MessageHandler + include EventQueue::MessageHandler before_save :notify_friends def notify_friends + puts "hello" - @@queue = MessageHandler.new xml = prep_webhook #friends_with_permissions.each{ |friend| puts friend; Curl.post( "\"" + xml + "\" " + friend) } @@queue.add_post_request( friends_with_permissions, xml ) @@ -40,8 +40,8 @@ module Diaspora def friends_with_permissions #Friend.only(:url).map{|x| x = x.url + "/receive/"} - googles = [] - 5.times{ googles <<"http://google.com/"} #"http://localhost:4567/receive/"} #"http://google.com/"} + #googles = [] + #5.times{ googles <<"http://google.com/"} #"http://localhost:4567/receive/"} #"http://google.com/"} googles end end diff --git a/lib/message_handler.rb b/lib/message_handler.rb index ff58f3583..dba4083a9 100644 --- a/lib/message_handler.rb +++ b/lib/message_handler.rb @@ -1,7 +1,12 @@ - require 'em-http' - require 'eventmachine' - require 'addressable/uri' - +require 'active_support' +require 'active_support/core_ext' +require 'roxml' +require 'dm-core' +require 'eventmachine' +require 'em-http' + + + class MessageHandler NUM_TRIES = 3 @@ -25,7 +30,7 @@ case query.type when :post http = EventMachine::HttpRequest.new(query.destination).post :timeout => TIMEOUT, :body => query.body - http.callback { process} + http.callback {puts "YAR"; process} when :get http = EventMachine::HttpRequest.new(query.destination).get :timeout => TIMEOUT http.callback {send_to_seed(query, http.response); process} From 4cecb522b6e9102e0547046a4ef25e057b3f7c81 Mon Sep 17 00:00:00 2001 From: danielvincent Date: Thu, 17 Jun 2010 10:36:54 -0700 Subject: [PATCH 05/19] DG MS; queue still working. half-way finished cleaning up code. --- app/models/post.rb | 30 +----------- lib/common.rb | 11 ++--- lib/message_handler.rb | 106 ++++++++++++++++++++--------------------- 3 files changed, 57 insertions(+), 90 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index b0d431f8c..a72520074 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,4 +1,5 @@ class Post + require 'lib/common' require 'lib/message_handler' @@ -7,6 +8,7 @@ class Post include Mongoid::Document include Mongoid::Timestamps include ROXML + include Diaspora::Hookey xml_accessor :owner xml_accessor :snippet @@ -17,32 +19,6 @@ class Post field :snippet before_create :set_defaults - #after_update :notify_friends - after_save :notify_friends - - @@queue = MessageHandler.new - - def notify_friends - puts "hello" - - xml = prep_webhook - #friends_with_permissions.each{ |friend| puts friend; Curl.post( "\"" + xml + "\" " + friend) } - @@queue.add_post_request( friends_with_permissions, xml ) - @@queue.process - end - - def prep_webhook - self.to_xml.to_s.chomp - end - - def friends_with_permissions - #friends = Friend.only(:url).map{|x| x = x.url + "/receive/"} - #3.times {friends = friends + friends} - #friends - googles = [] - 100.times{ googles <<"http://google.com/"} #"http://localhost:4567/receive/"} #"http://google.com/"} - googles - end @@models = ["StatusMessage", "Bookmark", "Blog"] @@ -65,7 +41,5 @@ class Post self.source ||= user_email self.snippet ||= user_email end - - end diff --git a/lib/common.rb b/lib/common.rb index db57b2958..942f476fb 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -22,14 +22,12 @@ module Diaspora def self.included(klass) klass.class_eval do - include EventQueue::MessageHandler before_save :notify_friends + + @@queue = MessageHandler.new def notify_friends - puts "hello" - xml = prep_webhook - #friends_with_permissions.each{ |friend| puts friend; Curl.post( "\"" + xml + "\" " + friend) } @@queue.add_post_request( friends_with_permissions, xml ) @@queue.process end @@ -39,10 +37,7 @@ module Diaspora end def friends_with_permissions - #Friend.only(:url).map{|x| x = x.url + "/receive/"} - #googles = [] - #5.times{ googles <<"http://google.com/"} #"http://localhost:4567/receive/"} #"http://google.com/"} - googles + Friend.only(:url).map{|x| x = x.url + "/receive/"} end end end diff --git a/lib/message_handler.rb b/lib/message_handler.rb index dba4083a9..b5b49d5a0 100644 --- a/lib/message_handler.rb +++ b/lib/message_handler.rb @@ -5,63 +5,61 @@ require 'dm-core' require 'eventmachine' require 'em-http' +class MessageHandler + + NUM_TRIES = 3 + TIMEOUT = 5 #seconds + + def initialize + @queue = EM::Queue.new + end + + def add_get_request(destinations) + destinations.each{ |dest| @queue.push(Message.new(:get, dest))} + end - class MessageHandler + def add_post_request(destinations, body) + destinations.each{|dest| @queue.push(Message.new(:post, dest, body))} + end - NUM_TRIES = 3 - TIMEOUT = 5 #seconds - - def initialize - @queue = EM::Queue.new - end - - def add_get_request(destinations) - destinations.each{ |dest| @queue.push(Message.new(:get, dest))} - end - - - def add_post_request(destinations, body) - destinations.each{|dest| @queue.push(Message.new(:post, dest, body))} - end - - def process - @queue.pop{ |query| - case query.type - when :post - http = EventMachine::HttpRequest.new(query.destination).post :timeout => TIMEOUT, :body => query.body - http.callback {puts "YAR"; process} - when :get - http = EventMachine::HttpRequest.new(query.destination).get :timeout => TIMEOUT - http.callback {send_to_seed(query, http.response); process} - else - raise "message is not a type I know!" - end - - http.errback { - query.try_count +=1 - @queue.push query unless query.try_count >= NUM_TRIES - process - } - } unless @queue.size == 0 - end - - - def send_to_seed(message, http_response) - #DO SOMETHING! - end - - def size - @queue.size - end - - class Message - attr_accessor :type, :destination, :body, :try_count - def initialize(type, dest, body= nil) - @type = type - @destination = dest - @body = body - @try_count = 0 + def process + @queue.pop{ |query| + case query.type + when :post + http = EventMachine::HttpRequest.new(query.destination).post :timeout => TIMEOUT, :body => query.body + http.callback {puts "YAR"; process} + when :get + http = EventMachine::HttpRequest.new(query.destination).get :timeout => TIMEOUT + http.callback {send_to_seed(query, http.response); process} + else + raise "message is not a type I know!" end + + http.errback { + query.try_count +=1 + @queue.push query unless query.try_count >= NUM_TRIES + process + } + } unless @queue.size == 0 + end + + + def send_to_seed(message, http_response) + #DO SOMETHING! + end + + def size + @queue.size + end + + class Message + attr_accessor :type, :destination, :body, :try_count + def initialize(type, dest, body= nil) + @type = type + @destination = dest + @body = body + @try_count = 0 end end +end From 3371b87fffe8a56fe78cc3408009081788849d08 Mon Sep 17 00:00:00 2001 From: danielvincent Date: Thu, 17 Jun 2010 11:32:13 -0700 Subject: [PATCH 06/19] DG MS; queue works. just need to move some tests over. --- Gemfile | 7 +++---- app/controllers/application_controller.rb | 5 +++-- lib/message_handler.rb | 5 +---- lib/net/curl.rb | 1 + 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/Gemfile b/Gemfile index cfa740a53..00dd77f64 100644 --- a/Gemfile +++ b/Gemfile @@ -5,18 +5,17 @@ gem 'mongrel' gem 'thin' gem 'em-http-request' gem 'addressable' -gem "mongoid", :git => "http://github.com/durran/mongoid.git" +gem "mongoid", :git => "git://github.com/durran/mongoid.git" gem "bson_ext", "1.0.1" gem "haml" gem "devise", :git => "git://github.com/plataformatec/devise.git" gem 'roxml', :git => "git://github.com/Empact/roxml.git" - -gem "dm-core" +gem 'dm-core' group :test do - gem 'rspec', '>= 2.0.0.beta.10' + gem 'rspec', '>= 2.0.0.beta.12' gem 'rspec-rails', ">= 2.0.0.beta.8" gem "mocha" gem 'webrat' diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 958d7a9e2..e65e00d7c 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,10 +1,11 @@ class ApplicationController < ActionController::Base - protect_from_forgery + protect_from_forgery :except => :receive layout 'application' def receive - puts response.inspect + puts params.inspect puts "holy boner batman" + render :nothing => true end end diff --git a/lib/message_handler.rb b/lib/message_handler.rb index b5b49d5a0..27480f400 100644 --- a/lib/message_handler.rb +++ b/lib/message_handler.rb @@ -1,7 +1,4 @@ -require 'active_support' -require 'active_support/core_ext' -require 'roxml' -require 'dm-core' +require 'addressable/uri' require 'eventmachine' require 'em-http' diff --git a/lib/net/curl.rb b/lib/net/curl.rb index 4220a27f7..6ccc409b1 100644 --- a/lib/net/curl.rb +++ b/lib/net/curl.rb @@ -1,6 +1,7 @@ class Curl def self.post(s) `curl -X POST -d #{s}`;; + true end def self.get(s) From 628af0372fc31341b63662bdb49123fcaf18aead Mon Sep 17 00:00:00 2001 From: danielvincent Date: Thu, 17 Jun 2010 14:29:41 -0700 Subject: [PATCH 07/19] DG MS; completed event-queue intergration. --- app/controllers/dashboard_controller.rb | 2 +- app/models/post.rb | 3 +- lib/message_handler.rb | 2 +- lib/net/curl.rb | 1 - spec/factories.rb | 1 + spec/lib/message_handler_spec.rb | 152 ++++++++++++++++++++++++ spec/models/post_spec.rb | 25 +++- spec/models/status_message_spec.rb | 4 +- 8 files changed, 178 insertions(+), 12 deletions(-) create mode 100644 spec/lib/message_handler_spec.rb diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index c9d082015..62911ac1c 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -2,6 +2,6 @@ class DashboardController < ApplicationController before_filter :authenticate_user! def index - @posts = Post.recent_ordered_posts + @posts = Post.stream end end diff --git a/app/models/post.rb b/app/models/post.rb index a72520074..926545c1c 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,6 +1,5 @@ class Post require 'lib/common' - require 'lib/message_handler' # XML accessors must always preceed mongo field tags @@ -22,7 +21,7 @@ class Post @@models = ["StatusMessage", "Bookmark", "Blog"] - def self.recent_ordered_posts + def self.stream # Need to explicitly name each inherited model for dev environment query = if Rails.env == "development" Post.criteria.all(:_type => @@models) diff --git a/lib/message_handler.rb b/lib/message_handler.rb index 27480f400..428d88942 100644 --- a/lib/message_handler.rb +++ b/lib/message_handler.rb @@ -25,7 +25,7 @@ class MessageHandler case query.type when :post http = EventMachine::HttpRequest.new(query.destination).post :timeout => TIMEOUT, :body => query.body - http.callback {puts "YAR"; process} + http.callback { process} when :get http = EventMachine::HttpRequest.new(query.destination).get :timeout => TIMEOUT http.callback {send_to_seed(query, http.response); process} diff --git a/lib/net/curl.rb b/lib/net/curl.rb index 6ccc409b1..4220a27f7 100644 --- a/lib/net/curl.rb +++ b/lib/net/curl.rb @@ -1,7 +1,6 @@ class Curl def self.post(s) `curl -X POST -d #{s}`;; - true end def self.get(s) diff --git a/spec/factories.rb b/spec/factories.rb index a63f32c31..5997c11e5 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -1,6 +1,7 @@ #For Guidance #http://github.com/thoughtbot/factory_girl #http://railscasts.com/episodes/158-factories-not-fixtures + Factory.define :friend do |f| f.username 'max' f.url 'http://max.com/' diff --git a/spec/lib/message_handler_spec.rb b/spec/lib/message_handler_spec.rb new file mode 100644 index 000000000..3ec0a6e2c --- /dev/null +++ b/spec/lib/message_handler_spec.rb @@ -0,0 +1,152 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +describe MessageHandler do + before do + @handler = MessageHandler.new + @message_body = "I want to pump you up" + @message_urls = ["http://www.google.com/", "http://yahoo.com/", "http://foo.com/"] + end + + describe 'GET messages' do + describe 'creating a GET query' do + it 'should be able to add a GET query to the queue with required destinations' do + EventMachine.run{ + @handler.add_get_request(@message_urls) + @handler.size.should == @message_urls.size + EventMachine.stop + } + end + + end + + describe 'processing a GET query' do + it 'should remove sucessful http requests from the queue' do + request = FakeHttpRequest.new(:success) + request.should_receive(:get).and_return(request) + EventMachine::HttpRequest.stub!(:new).and_return(request) + + EventMachine.run { + @handler.add_get_request("http://www.google.com/") + @handler.size.should == 1 + @handler.process + @handler.size.should == 0 + EventMachine.stop + } + end + + + it 'should only retry a bad request three times ' do + request = FakeHttpRequest.new(:failure) + request.should_receive(:get).exactly(MessageHandler::NUM_TRIES).times.and_return(request) + EventMachine::HttpRequest.stub!(:new).and_return(request) + + EventMachine.run { + @handler.add_get_request("http://asdfsdajfsdfbasdj.com/") + @handler.size.should == 1 + @handler.process + @handler.size.should == 0 + + EventMachine.stop + } + end + end + end + + describe 'POST messages' do + + + it 'should be able to add a post message to the queue' do + EventMachine.run { + @handler.size.should ==0 + @handler.add_post_request(@message_urls.first, @message_body) + @handler.size.should == 1 + + EventMachine.stop + } + end + + it 'should be able to insert many posts into the queue' do + EventMachine.run { + @handler.size.should == 0 + @handler.add_post_request(@message_urls, @message_body) + @handler.size.should == @message_urls.size + EventMachine.stop + } + end + + it 'should post a single message to a given URL' do + request = FakeHttpRequest.new(:success) + request.should_receive(:post).and_return(request) + EventMachine::HttpRequest.stub!(:new).and_return(request) + EventMachine.run{ + + @handler.add_post_request(@message_urls.first, @message_body) + @handler.size.should == 1 + @handler.process + @handler.size.should == 0 + + EventMachine.stop + + } + end + end + + describe "Mixed Queries" do + + it 'should process both POST and GET requests in the same queue' do + request = FakeHttpRequest.new(:success) + request.should_receive(:get).exactly(3).times.and_return(request) + request.should_receive(:post).exactly(3).times.and_return(request) + EventMachine::HttpRequest.stub!(:new).and_return(request) + + EventMachine.run{ + @handler.add_post_request(@message_urls,@message_body) + @handler.size.should == 3 + @handler.add_get_request(@message_urls) + @handler.size.should == 6 + @handler.process + timer = EventMachine::Timer.new(1) do + @handler.size.should == 0 + EventMachine.stop + end + } + end + + it 'should be able to have seperate POST and GET have different callbacks' do + request = FakeHttpRequest.new(:success) + request.should_receive(:get).exactly(1).times.and_return(request) + request.should_receive(:post).exactly(1).times.and_return(request) + @handler.should_receive(:send_to_seed).once + + EventMachine::HttpRequest.stub!(:new).and_return(request) + + EventMachine.run{ + @handler.add_post_request(@message_urls.first,@message_body) + @handler.add_get_request(@message_urls.first) + @handler.process + + EventMachine.stop + } + + end + end +end + +class FakeHttpRequest + def initialize(callback_wanted) + @callback = callback_wanted + end + def response + "NOTE YOU ARE IN FAKE HTTP" + end + + def post; end + def get; end + def callback(&b) + b.call if @callback == :success + end + def errback(&b) + b.call if @callback == :failure + end +end + diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 313956825..79457ab03 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -3,13 +3,15 @@ require File.dirname(__FILE__) + '/../spec_helper' describe Post do before do Factory.create(:user, :email => "bob@aol.com") - @post = Factory.create(:post, :owner => nil, :source => nil, :snippet => nil) end describe 'requirements' do end describe 'defaults' do + before do + @post = Factory.create(:post, :owner => nil, :source => nil, :snippet => nil) + end it "should add an owner if none is present" do @post.owner.should == "bob@aol.com" @@ -23,10 +25,21 @@ describe Post do @post.snippet.should == "bob@aol.com" end end + + it "should list child types in reverse chronological order" do + Factory.create(:status_message, :message => "puppies", :created_at => Time.now+1) + Factory.create(:bookmark, :title => "Reddit", :link => "http://reddit.com", :created_at => Time.now+2) + Factory.create(:status_message, :message => "kittens", :created_at => Time.now+3) + Factory.create(:blog, :title => "Bears", :body => "Bear's body", :created_at => Time.now+4) + Factory.create(:bookmark, :title => "Google", :link => "http://google.com", :created_at => Time.now+5) + + stream = Post.stream + stream.count.should == 5 + stream[0].class.should == Bookmark + stream[1].class.should == Blog + stream[2].class.should == StatusMessage + stream[3].class.should == Bookmark + stream[4].class.should == StatusMessage + end end -#question! -#STI ? do i need to call mongoid doc on child? -# validations inherit? -# type param. -# inheriting snippet builder method diff --git a/spec/models/status_message_spec.rb b/spec/models/status_message_spec.rb index 899a43484..67e9f4dbb 100644 --- a/spec/models/status_message_spec.rb +++ b/spec/models/status_message_spec.rb @@ -51,7 +51,7 @@ describe StatusMessage do describe "retrieving" do before do @remote = Factory.create(:friend, :url => "fakeurl")#http://localhost:1254/") - Curl.stub!(:curl).and_return(@@remote_xml) + Curl.stub!(:get).and_return(@@remote_xml) end it "should marshal xml and serialize it without error" do StatusMessages.from_xml(@@remote_xml).to_xml.to_s.should == @@remote_xml @@ -59,6 +59,8 @@ describe StatusMessage do it "marshal retrieved xml" do remote_msgs = StatusMessage.retrieve_from_friend(@remote) local_msgs = StatusMessages.from_xml(@@remote_xml) + + remote_msgs.statusmessages.each{ |m| local_msgs.statusmessages.include?(m).should be_true} local_msgs.statusmessages.each{ |m| remote_msgs.statusmessages.include?(m).should be_true} end From ba6d136bc751810ee214f8898536589cf6174ccb Mon Sep 17 00:00:00 2001 From: danielvincent Date: Thu, 17 Jun 2010 15:22:02 -0700 Subject: [PATCH 08/19] DG MS; added specs for common module --- app/models/post.rb | 2 +- lib/common.rb | 25 +++---------------------- spec/lib/common_spec.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 23 deletions(-) create mode 100644 spec/lib/common_spec.rb diff --git a/app/models/post.rb b/app/models/post.rb index 926545c1c..be9ff1591 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -7,7 +7,7 @@ class Post include Mongoid::Document include Mongoid::Timestamps include ROXML - include Diaspora::Hookey + include Diaspora::Webhooks xml_accessor :owner xml_accessor :snippet diff --git a/lib/common.rb b/lib/common.rb index 942f476fb..218d34ca5 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -1,26 +1,7 @@ module Diaspora - module CommonFields + + module Webhooks def self.included(klass) - klass.class_eval do - include Mongoid::Document - include ROXML - include Mongoid::Timestamps - - xml_accessor :owner - xml_accessor :snippet - xml_accessor :source - - field :owner - field :source - field :snippet - end - end - end - - module Hookey - - def self.included(klass) - klass.class_eval do before_save :notify_friends @@ -33,7 +14,7 @@ module Diaspora end def prep_webhook - self.to_xml.to_s.chomp + self.to_xml.to_s end def friends_with_permissions diff --git a/spec/lib/common_spec.rb b/spec/lib/common_spec.rb new file mode 100644 index 000000000..0e9a3ebf9 --- /dev/null +++ b/spec/lib/common_spec.rb @@ -0,0 +1,40 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +include Diaspora + +describe Diaspora do + + describe Webhooks do + before do + @post = Factory.build(:post) + end + + it "should add the following methods to Post on inclusion" do + @post.respond_to?(:notify_friends).should be true + @post.respond_to?(:prep_webhook).should be true + @post.respond_to?(:friends_with_permissions).should be true + end + + it "should convert an object to a proper webhook" do + @post.prep_webhook.should == @post.to_xml.to_s + end + + it "should retrieve all valid friend endpoints" do + Factory.create(:friend, :url => "http://www.bob.com") + Factory.create(:friend, :url => "http://www.alice.com") + Factory.create(:friend, :url => "http://www.jane.com") + + @post.friends_with_permissions.should include("http://www.bob.com/receive/") + @post.friends_with_permissions.should include("http://www.alice.com/receive/") + @post.friends_with_permissions.should include("http://www.jane.com/receive/") + end + + it "should send all prepped webhooks to be processed" do + MessageHandler.any_instance.stubs(:add_post_request).returns(true) + MessageHandler.any_instance.stubs(:process).returns(true) + @post.notify_friends.should be true + end + + end + +end From 02137b9fb973e96671fd57d16050995eabe84eea Mon Sep 17 00:00:00 2001 From: danielvincent Date: Thu, 17 Jun 2010 18:29:56 -0700 Subject: [PATCH 09/19] DG MS; receive hook working. --- app/controllers/application_controller.rb | 5 ----- app/controllers/dashboard_controller.rb | 14 +++++++++++++- app/helpers/application_helper.rb | 11 +++++++++++ app/helpers/dashboard_helper.rb | 5 +++++ app/models/status_message.rb | 2 ++ config/routes.rb | 2 +- lib/common.rb | 12 +++++++++++- spec/helpers/parser_spec.rb | 21 +++++++++++++++++++++ 8 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 spec/helpers/parser_spec.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e65e00d7c..42dd80cd4 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,10 +2,5 @@ class ApplicationController < ActionController::Base protect_from_forgery :except => :receive layout 'application' - def receive - puts params.inspect - puts "holy boner batman" - render :nothing => true - end end diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index 62911ac1c..91eff3cac 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -1,7 +1,19 @@ class DashboardController < ApplicationController - before_filter :authenticate_user! + + before_filter :authenticate_user!, :except => :receive + include ApplicationHelper def index @posts = Post.stream end + + + def receive + store_posts_from_xml (params[:xml]) + + + + puts "holy boner batman" + render :nothing => true + end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f58313c9b..4580f9cd3 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -6,4 +6,15 @@ module ApplicationHelper def object_fields(object) object.attributes.keys end + + def store_posts_from_xml(xml) + doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks } + doc.xpath("//post").each do |post| #this is the post wrapper + post.children.each do|type| #now the text of post itself is the type + #type object to xml is the the thing we want to from_xml + object = type.name.camelize.constantize.from_xml type.to_s + object.save + end + end + end end diff --git a/app/helpers/dashboard_helper.rb b/app/helpers/dashboard_helper.rb index a94ddfc2e..d06bca135 100644 --- a/app/helpers/dashboard_helper.rb +++ b/app/helpers/dashboard_helper.rb @@ -1,2 +1,7 @@ module DashboardHelper + + + + + end diff --git a/app/models/status_message.rb b/app/models/status_message.rb index b9eb2c4fc..0b58ee72b 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -2,6 +2,8 @@ class StatusMessage < Post include StatusMessagesHelper require 'lib/net/curl' + xml_name :status_message + xml_accessor :message field :message diff --git a/config/routes.rb b/config/routes.rb index f1466709a..efdc9426f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -14,7 +14,7 @@ Diaspora::Application.routes.draw do |map| match 'logout', :to => 'devise/sessions#destroy', :as => "destroy_user_session" match 'signup', :to => 'devise/registrations#new', :as => "new_user_registration" - match 'receive', :to => 'application#receive' + match 'receive', :to => 'dashboard#receive' resources :users resources :status_messages diff --git a/lib/common.rb b/lib/common.rb index 218d34ca5..7b9538e88 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -16,10 +16,20 @@ module Diaspora def prep_webhook self.to_xml.to_s end - + + def prep_many + "#{self.to_xml.to_s}" + end + def friends_with_permissions Friend.only(:url).map{|x| x = x.url + "/receive/"} end + + def self.build_xml_for(posts) + xml = "" + posts.each {|x| xml << x.prep_many} + xml = xml + "" + end end end end diff --git a/spec/helpers/parser_spec.rb b/spec/helpers/parser_spec.rb new file mode 100644 index 000000000..04c1846b4 --- /dev/null +++ b/spec/helpers/parser_spec.rb @@ -0,0 +1,21 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +include ApplicationHelper + +describe DashboardHelper do + before do + Factory.create(:user) + end + + it "should store objects sent from xml" do + status_messages = [] + 10.times { status_messages << Factory.build(:status_message)} + + xml = Post.build_xml_for(status_messages) + + store_posts_from_xml(xml) + StatusMessage.count.should == 10 + end + + +end From 39118293eb115a7ae13e9aad92c20fe9892d8590 Mon Sep 17 00:00:00 2001 From: danielvincent Date: Thu, 17 Jun 2010 18:45:50 -0700 Subject: [PATCH 10/19] DG MS; fixed minor webhook bug when generating xml collection. --- app/controllers/dashboard_controller.rb | 4 ---- app/models/post.rb | 4 ++++ lib/common.rb | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index 91eff3cac..7196b7fb5 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -10,10 +10,6 @@ class DashboardController < ApplicationController def receive store_posts_from_xml (params[:xml]) - - - - puts "holy boner batman" render :nothing => true end end diff --git a/app/models/post.rb b/app/models/post.rb index be9ff1591..95985d170 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -31,6 +31,10 @@ class Post query.order_by( [:created_at, :desc] ) end + def each + yield self + end + protected diff --git a/lib/common.rb b/lib/common.rb index 7b9538e88..4a64c570c 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -8,7 +8,7 @@ module Diaspora @@queue = MessageHandler.new def notify_friends - xml = prep_webhook + xml = Post.build_xml_for(self) @@queue.add_post_request( friends_with_permissions, xml ) @@queue.process end @@ -18,7 +18,7 @@ module Diaspora end def prep_many - "#{self.to_xml.to_s}" + "#{self.prep_webhook}" end def friends_with_permissions From 1ce01be534b82c03cce2bd398c4b5f8322bc399f Mon Sep 17 00:00:00 2001 From: danielvincent Date: Thu, 17 Jun 2010 20:06:44 -0700 Subject: [PATCH 11/19] DG MS; fixed infinate loop --- lib/common.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/common.rb b/lib/common.rb index 4a64c570c..38d8c416f 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -8,9 +8,11 @@ module Diaspora @@queue = MessageHandler.new def notify_friends - xml = Post.build_xml_for(self) - @@queue.add_post_request( friends_with_permissions, xml ) - @@queue.process + if self.owner == User.first.email + xml = Post.build_xml_for(self) + @@queue.add_post_request( friends_with_permissions, xml ) + @@queue.process + end end def prep_webhook From cbdb3ecbc756d87b8cc6318b14bf1b5a1ca689ed Mon Sep 17 00:00:00 2001 From: danielvincent Date: Thu, 17 Jun 2010 20:32:44 -0700 Subject: [PATCH 12/19] quick fix with notify_friends. sets xml variable in post request. --- lib/common.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/common.rb b/lib/common.rb index 38d8c416f..5e4040959 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -9,9 +9,9 @@ module Diaspora def notify_friends if self.owner == User.first.email - xml = Post.build_xml_for(self) - @@queue.add_post_request( friends_with_permissions, xml ) - @@queue.process + xml = Post.build_xml_for(self) + @@queue.add_post_request( friends_with_permissions, "xml=#{xml}" ) + @@queue.process end end From e7835955ebbe17f179f9af76c4ddb474a783d6a8 Mon Sep 17 00:00:00 2001 From: maxwell Date: Thu, 17 Jun 2010 20:33:59 -0700 Subject: [PATCH 13/19] fixed a bug --- lib/message_handler.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/message_handler.rb b/lib/message_handler.rb index 428d88942..de4368f09 100644 --- a/lib/message_handler.rb +++ b/lib/message_handler.rb @@ -24,7 +24,7 @@ class MessageHandler @queue.pop{ |query| case query.type when :post - http = EventMachine::HttpRequest.new(query.destination).post :timeout => TIMEOUT, :body => query.body + http = EventMachine::HttpRequest.new(query.destination).post :timeout => TIMEOUT, :body =>{:xml => query.body} http.callback { process} when :get http = EventMachine::HttpRequest.new(query.destination).get :timeout => TIMEOUT From 81016729c426025c921ad59e03ac53aed7371963 Mon Sep 17 00:00:00 2001 From: maxwell Date: Thu, 17 Jun 2010 20:35:16 -0700 Subject: [PATCH 14/19] fixing dans commit --- lib/common.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/common.rb b/lib/common.rb index 5e4040959..779b8034c 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -10,7 +10,7 @@ module Diaspora def notify_friends if self.owner == User.first.email xml = Post.build_xml_for(self) - @@queue.add_post_request( friends_with_permissions, "xml=#{xml}" ) + @@queue.add_post_request( friends_with_permissions, xml ) @@queue.process end end From 043924b5f514a33b596fe2119077cd49ffcc282d Mon Sep 17 00:00:00 2001 From: maxwell Date: Thu, 17 Jun 2010 20:58:20 -0700 Subject: [PATCH 15/19] made everyting so the lastest is the first thing you see --- app/controllers/blogs_controller.rb | 2 +- app/controllers/bookmarks_controller.rb | 2 +- app/controllers/friends_controller.rb | 2 +- app/controllers/status_messages_controller.rb | 2 +- app/controllers/users_controller.rb | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb index e4bf80484..f39cecdbf 100644 --- a/app/controllers/blogs_controller.rb +++ b/app/controllers/blogs_controller.rb @@ -3,7 +3,7 @@ class BlogsController < ApplicationController def index - @blogs = Blog.all + @blogs = Blog.criteria.all.order_by( [:created_at, :desc] ) end def show diff --git a/app/controllers/bookmarks_controller.rb b/app/controllers/bookmarks_controller.rb index 8cfd3f066..6defe2f07 100644 --- a/app/controllers/bookmarks_controller.rb +++ b/app/controllers/bookmarks_controller.rb @@ -2,7 +2,7 @@ class BookmarksController < ApplicationController before_filter :authenticate_user! def index - @bookmarks = Bookmark.all + @bookmarks = Bookmark.criteria.all.order_by( [:created_at, :desc] ) end def edit diff --git a/app/controllers/friends_controller.rb b/app/controllers/friends_controller.rb index 000185f6e..55778861f 100644 --- a/app/controllers/friends_controller.rb +++ b/app/controllers/friends_controller.rb @@ -2,7 +2,7 @@ class FriendsController < ApplicationController before_filter :authenticate_user! def index - @friends = Friend.all + @friends = Friend.criteria.all.order_by( [:created_at, :desc] ) end def show diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb index beae57255..22d4b023b 100644 --- a/app/controllers/status_messages_controller.rb +++ b/app/controllers/status_messages_controller.rb @@ -3,7 +3,7 @@ class StatusMessagesController < ApplicationController include StatusMessagesHelper def index - @status_messages = StatusMessage.all + @status_messages = StatusMessage.criteria.all.order_by( [:created_at, :desc] ) @friends = Friend.all respond_to do |format| diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index f029dc33d..da904fab0 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -3,7 +3,7 @@ class UsersController < ApplicationController before_filter :authenticate_user! def index - @users = User.all + @users = User.criteria.all.order_by( [:created_at, :desc] ) end end From d4bf2f539dbf3e7b2159a29a758f39f1713939c9 Mon Sep 17 00:00:00 2001 From: maxwell Date: Fri, 18 Jun 2010 10:48:37 -0700 Subject: [PATCH 16/19] DG MS; removed signup link and route --- app/controllers/status_messages_controller.rb | 2 +- app/views/devise/shared/_links.haml | 6 ++-- config/routes.rb | 2 +- lib/common.rb | 2 +- spec/lib/common_spec.rb | 30 +++++++++++++++++-- 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb index 22d4b023b..5b53d898e 100644 --- a/app/controllers/status_messages_controller.rb +++ b/app/controllers/status_messages_controller.rb @@ -1,5 +1,5 @@ class StatusMessagesController < ApplicationController - #before_filter :authenticate_user! + before_filter :authenticate_user! include StatusMessagesHelper def index diff --git a/app/views/devise/shared/_links.haml b/app/views/devise/shared/_links.haml index a7547353f..5da9aab33 100644 --- a/app/views/devise/shared/_links.haml +++ b/app/views/devise/shared/_links.haml @@ -1,9 +1,9 @@ - if controller_name != 'sessions' = link_to "Sign in", new_session_path(resource_name) %br/ -- if devise_mapping.registerable? && controller_name != 'registrations' - = link_to "Sign up", new_registration_path(resource_name) - %br/ +/- if devise_mapping.registerable? && controller_name != 'registrations' +/= link_to "Sign up", new_registration_path(resource_name) +/%br/ - if devise_mapping.recoverable? && controller_name != 'passwords' = link_to "Forgot your password?", new_password_path(resource_name) %br/ diff --git a/config/routes.rb b/config/routes.rb index efdc9426f..7d6f2a0a0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,7 +12,7 @@ Diaspora::Application.routes.draw do |map| devise_for :users, :path_names => {:sign_up => "signup", :sign_in => "login", :sign_out => "logout"} match 'login', :to => 'devise/sessions#new', :as => "new_user_session" match 'logout', :to => 'devise/sessions#destroy', :as => "destroy_user_session" - match 'signup', :to => 'devise/registrations#new', :as => "new_user_registration" + #match 'signup', :to => 'devise/registrations#new', :as => "new_user_registration" match 'receive', :to => 'dashboard#receive' diff --git a/lib/common.rb b/lib/common.rb index 779b8034c..2c20d85cc 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -3,7 +3,7 @@ module Diaspora module Webhooks def self.included(klass) klass.class_eval do - before_save :notify_friends + after_save :notify_friends @@queue = MessageHandler.new diff --git a/spec/lib/common_spec.rb b/spec/lib/common_spec.rb index 0e9a3ebf9..90cf185b2 100644 --- a/spec/lib/common_spec.rb +++ b/spec/lib/common_spec.rb @@ -1,11 +1,13 @@ require File.dirname(__FILE__) + '/../spec_helper' + include Diaspora describe Diaspora do describe Webhooks do before do + @user = Factory.create(:user) @post = Factory.build(:post) end @@ -30,11 +32,35 @@ describe Diaspora do end it "should send all prepped webhooks to be processed" do - MessageHandler.any_instance.stubs(:add_post_request).returns(true) - MessageHandler.any_instance.stubs(:process).returns(true) + MessageHandler.any_instance.stubs(:add_post_request).returns true + MessageHandler.any_instance.stubs(:process).returns true @post.notify_friends.should be true end + + it "should check that it only sends a user's posts to their friends" do + Factory.create(:friend, :url => "http://www.bob.com") + Factory.create(:friend, :url => "http://www.alice.com") + Factory.create(:status_message) + Factory.create(:bookmark) + # this is a messagequeue thing; out of scope for webhooks action + end + + it "should ensure no duplicate url posts" do + pending + # this is a messagequeue thing; out of scope for webhooks action + + end + + it "should build an xml object containing multiple Post types" do + Factory.create(:status_message) + Factory.create(:bookmark) + + stream = Post.stream + xml = Post.build_xml_for(stream) + xml.should include "" + xml.should include "" + end end end From c430aaff18b493a554639d4adbc046f0984c64a2 Mon Sep 17 00:00:00 2001 From: maxwell Date: Fri, 18 Jun 2010 14:35:51 -0700 Subject: [PATCH 17/19] DG MS; added database_cleaner for spec runner. touched up common specs for webhooks; depirated old status message to xml --- Gemfile | 2 +- app/controllers/status_messages_controller.rb | 3 +- app/helpers/status_messages_helper.rb | 10 ----- config/environments/test.rb | 14 ++++++ lib/common.rb | 19 +++----- spec/lib/common_spec.rb | 30 ++++++------- spec/models/bookmark_spec.rb | 18 ++++++++ spec/models/status_message_spec.rb | 44 +------------------ spec/spec_helper.rb | 18 ++++++-- 9 files changed, 69 insertions(+), 89 deletions(-) diff --git a/Gemfile b/Gemfile index 00dd77f64..274a0b8f5 100644 --- a/Gemfile +++ b/Gemfile @@ -11,7 +11,6 @@ gem "haml" gem "devise", :git => "git://github.com/plataformatec/devise.git" gem 'roxml', :git => "git://github.com/Empact/roxml.git" - gem 'dm-core' group :test do @@ -22,6 +21,7 @@ group :test do gem 'redgreen' gem 'autotest' gem 'factory_girl_rails' + gem 'database_cleaner' end group :development do diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb index 5b53d898e..0d9e1b9d8 100644 --- a/app/controllers/status_messages_controller.rb +++ b/app/controllers/status_messages_controller.rb @@ -1,6 +1,5 @@ class StatusMessagesController < ApplicationController before_filter :authenticate_user! - include StatusMessagesHelper def index @status_messages = StatusMessage.criteria.all.order_by( [:created_at, :desc] ) @@ -8,7 +7,7 @@ class StatusMessagesController < ApplicationController respond_to do |format| format.html - format.xml {render :xml => StatusMessages.new(@status_messages).to_xml } + format.xml {render :xml => Post.build_xml_for(@status_messages)} format.json { render :json => @status_messages } end diff --git a/app/helpers/status_messages_helper.rb b/app/helpers/status_messages_helper.rb index 5d1bcdc93..a2b81e2e3 100644 --- a/app/helpers/status_messages_helper.rb +++ b/app/helpers/status_messages_helper.rb @@ -9,14 +9,4 @@ module StatusMessagesHelper end end - class StatusMessages - include ROXML - - def initialize(messages=[]) - @statusmessages = messages - end - - xml_accessor :statusmessages, :as => [StatusMessage] - attr_accessor :statusmessages - end end diff --git a/config/environments/test.rb b/config/environments/test.rb index 40d222032..1a9134407 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -29,4 +29,18 @@ Diaspora::Application.configure do # This is necessary if your schema can't be completely dumped by the schema dumper, # like if you have constraints or database-specific column types # config.active_record.schema_format = :sql + # + # + # + + + +begin + require 'database_cleaner' + DatabaseCleaner.strategy = :truncation + DatabaseCleaner.orm = "mongoid" +rescue LoadError => ignore_if_database_cleaner_not_present + puts "Error on cleaner" +end + end diff --git a/lib/common.rb b/lib/common.rb index 2c20d85cc..14714245c 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -4,23 +4,18 @@ module Diaspora def self.included(klass) klass.class_eval do after_save :notify_friends - @@queue = MessageHandler.new def notify_friends - if self.owner == User.first.email - xml = Post.build_xml_for(self) - @@queue.add_post_request( friends_with_permissions, xml ) - @@queue.process + if self.owner == User.first.email + xml = Post.build_xml_for(self) + @@queue.add_post_request( friends_with_permissions, xml ) + @@queue.process end end - - def prep_webhook - self.to_xml.to_s - end - def prep_many - "#{self.prep_webhook}" + def prep_webhook + "#{self.to_xml.to_s}" end def friends_with_permissions @@ -29,7 +24,7 @@ module Diaspora def self.build_xml_for(posts) xml = "" - posts.each {|x| xml << x.prep_many} + posts.each {|x| xml << x.prep_webhook} xml = xml + "" end end diff --git a/spec/lib/common_spec.rb b/spec/lib/common_spec.rb index 90cf185b2..369143186 100644 --- a/spec/lib/common_spec.rb +++ b/spec/lib/common_spec.rb @@ -8,7 +8,7 @@ describe Diaspora do describe Webhooks do before do @user = Factory.create(:user) - @post = Factory.build(:post) + @post = Factory.create(:post) end it "should add the following methods to Post on inclusion" do @@ -18,7 +18,7 @@ describe Diaspora do end it "should convert an object to a proper webhook" do - @post.prep_webhook.should == @post.to_xml.to_s + @post.prep_webhook.should == "#{@post.to_xml.to_s}" end it "should retrieve all valid friend endpoints" do @@ -31,25 +31,21 @@ describe Diaspora do @post.friends_with_permissions.should include("http://www.jane.com/receive/") end - it "should send all prepped webhooks to be processed" do - MessageHandler.any_instance.stubs(:add_post_request).returns true - MessageHandler.any_instance.stubs(:process).returns true - @post.notify_friends.should be true + it "should send an owners post to their friends" do + Post.stub(:build_xml_for).and_return(true) + Post.should_receive(:build_xml_for).and_return true + @post.save end - it "should check that it only sends a user's posts to their friends" do - Factory.create(:friend, :url => "http://www.bob.com") - Factory.create(:friend, :url => "http://www.alice.com") - Factory.create(:status_message) - Factory.create(:bookmark) - - # this is a messagequeue thing; out of scope for webhooks action + it "should check that it does not send a friends post to an owners friends" do + Post.stub(:build_xml_for).and_return(true) + Post.should_not_receive(:build_xml_for) + Factory.create(:post, :owner => "nottheowner@post.com") end - it "should ensure no duplicate url posts" do - pending - # this is a messagequeue thing; out of scope for webhooks action - + it "should ensure one url is created for every friend" do + 5.times {Factory.create(:friend)} + @post.friends_with_permissions.size.should == 5 end it "should build an xml object containing multiple Post types" do diff --git a/spec/models/bookmark_spec.rb b/spec/models/bookmark_spec.rb index 99cbbd2a5..4cb8b5fc6 100644 --- a/spec/models/bookmark_spec.rb +++ b/spec/models/bookmark_spec.rb @@ -13,4 +13,22 @@ describe Bookmark do n = Factory.create(:bookmark) n.owner.should == "bob@aol.com" end + + describe "XML" do + it 'should serialize to XML' do + Factory.create(:user) + message = Factory.create(:bookmark, :title => "Reddit", :link => "http://reddit.com") + message.to_xml.to_s.should include "Reddit" + message.to_xml.to_s.should include "http://reddit.com" + end + + it 'should marshal serialized XML to object' do + xml = "Reddit</message><link>http://reddit.com</link><owner>bob@aol.com</owner></bookmark>" + parsed = Bookmark.from_xml(xml) + parsed.title.should == "Reddit" + parsed.link.should == "http://reddit.com" + parsed.owner.should == "bob@aol.com" + parsed.valid?.should be_true + end + end end diff --git a/spec/models/status_message_spec.rb b/spec/models/status_message_spec.rb index 67e9f4dbb..7bfa0af00 100644 --- a/spec/models/status_message_spec.rb +++ b/spec/models/status_message_spec.rb @@ -1,5 +1,4 @@ require File.dirname(__FILE__) + '/../spec_helper' -include StatusMessagesHelper describe StatusMessage do before do @@ -40,53 +39,12 @@ describe StatusMessage do end it 'should marshal serialized XML to object' do - xml = "<statusmessage>\n <message>I hate WALRUSES!</message><owner>Bob@rob.ert</owner></statusmessage>" + xml = "<statusmessage><message>I hate WALRUSES!</message><owner>Bob@rob.ert</owner></statusmessage>" parsed = StatusMessage.from_xml(xml) parsed.message.should == "I hate WALRUSES!" parsed.owner.should == "Bob@rob.ert" parsed.valid?.should be_true end end - - describe "retrieving" do - before do - @remote = Factory.create(:friend, :url => "fakeurl")#http://localhost:1254/") - Curl.stub!(:get).and_return(@@remote_xml) - end - it "should marshal xml and serialize it without error" do - StatusMessages.from_xml(@@remote_xml).to_xml.to_s.should == @@remote_xml - end - it "marshal retrieved xml" do - remote_msgs = StatusMessage.retrieve_from_friend(@remote) - local_msgs = StatusMessages.from_xml(@@remote_xml) - - - remote_msgs.statusmessages.each{ |m| local_msgs.statusmessages.include?(m).should be_true} - local_msgs.statusmessages.each{ |m| remote_msgs.statusmessages.include?(m).should be_true} - end - end end -@@remote_xml = -"<statusmessages> - <statusmessage> - <message>jimmy's 22 whales</message> - <owner>tester@yahoo.com</owner> - </statusmessage> - <statusmessage> - <message>jimmy's 23 whales</message> - <owner>tester@yahoo.com</owner> - </statusmessage> - <statusmessage> - <message>jimmy's 24 whales</message> - <owner>tester@yahoo.com</owner> - </statusmessage> - <statusmessage> - <message>jimmy's 25 whales</message> - <owner>tester@yahoo.com</owner> - </statusmessage> - <statusmessage> - <message>jimmy's 26 whales</message> - <owner>tester@yahoo.com</owner> - </statusmessage> -</statusmessages>" diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bc3f0762a..060511f9a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,6 +3,7 @@ ENV["RAILS_ENV"] ||= 'test' require File.dirname(__FILE__) + "/../config/environment" unless defined?(Rails) require 'rspec/rails' +require 'database_cleaner' #require File.dirname(__FILE__) + "/factories" include Devise::TestHelpers @@ -20,12 +21,21 @@ Rspec.configure do |config| # config.mock_with :rr config.mock_with :rspec - config.fixture_path = "#{::Rails.root}/spec/fixtures" - config.before(:each) do - Mongoid.master.collections.select { |c| c.name != 'system.indexes' }.each(&:drop) + DatabaseCleaner.strategy = :truncation + DatabaseCleaner.orm = "mongoid" + config.before(:suite) do + DatabaseCleaner.strategy = :transaction + DatabaseCleaner.clean_with(:truncation) + end - end + config.before(:each) do + DatabaseCleaner.start + end + + config.after(:each) do + DatabaseCleaner.clean + end # If you're not using ActiveRecord, or you'd prefer not to run each of your From 34df8fa946932e443ef45d1083c006ce4e7af0d5 Mon Sep 17 00:00:00 2001 From: maxwell <maxwell@joindiaspora.com> Date: Fri, 18 Jun 2010 16:47:34 -0700 Subject: [PATCH 18/19] DG MS; resolved unknown types going to receive handler. only post objects are inputted --- app/helpers/application_helper.rb | 15 +++++- app/models/bookmark.rb | 13 +++++ app/models/friend.rb | 14 +++++- spec/controllers/dashboard_controller_spec.rb | 1 + spec/helpers/parser_spec.rb | 19 +++++++ spec/models/bookmark_spec.rb | 50 +++++++++++++++++-- spec/models/friend_spec.rb | 48 +++++++++++++++++- 7 files changed, 153 insertions(+), 7 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 4580f9cd3..397d48999 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -9,12 +9,23 @@ module ApplicationHelper def store_posts_from_xml(xml) doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks } + + #i need to check some sort of metadata field + doc.xpath("//post").each do |post| #this is the post wrapper post.children.each do|type| #now the text of post itself is the type #type object to xml is the the thing we want to from_xml - object = type.name.camelize.constantize.from_xml type.to_s - object.save + check_and_save_post(type) end end end + + def check_and_save_post(type) + begin + object = type.name.camelize.constantize.from_xml type.to_s + object.save if object.is_a? Post + rescue + puts "Not of type post" + end + end end diff --git a/app/models/bookmark.rb b/app/models/bookmark.rb index c6241931f..61f4697f7 100644 --- a/app/models/bookmark.rb +++ b/app/models/bookmark.rb @@ -9,4 +9,17 @@ class Bookmark < Post validates_presence_of :link + validates_format_of :link, :with => + /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix + + before_validation :clean_link + + protected + + def clean_link + if self.link + self.link = 'http://' + self.link unless self.link.match('http://' || 'https://') + self.link = self.link + '/' if self.link[-1,1] != '/' + end + end end diff --git a/app/models/friend.rb b/app/models/friend.rb index 061a2094b..69b60fd36 100644 --- a/app/models/friend.rb +++ b/app/models/friend.rb @@ -9,5 +9,17 @@ class Friend field :url validates_presence_of :username, :url - + validates_format_of :url, :with => + /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix + + before_validation :clean_url + + protected + + def clean_url + if self.url + self.url = 'http://' + self.url unless self.url.match('http://' || 'https://') + self.url = self.url + '/' if self.url[-1,1] != '/' + end + end end diff --git a/spec/controllers/dashboard_controller_spec.rb b/spec/controllers/dashboard_controller_spec.rb index 0b20b21ab..b6ad08b93 100644 --- a/spec/controllers/dashboard_controller_spec.rb +++ b/spec/controllers/dashboard_controller_spec.rb @@ -8,4 +8,5 @@ describe DashboardController do get :index response.should render_template(:index) end + end diff --git a/spec/helpers/parser_spec.rb b/spec/helpers/parser_spec.rb index 04c1846b4..978a5c83e 100644 --- a/spec/helpers/parser_spec.rb +++ b/spec/helpers/parser_spec.rb @@ -17,5 +17,24 @@ describe DashboardHelper do StatusMessage.count.should == 10 end + it 'should discard posts where it does not know the type' do + xml = "<posts> + <post><status_message>\n <message>Here is another message</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post> + <post><not_a_real_type></not_a_real_type></post> + <post><status_message>\n <message>HEY DUDE</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post> + </posts>" + store_posts_from_xml(xml) + Post.count.should == 2 + end + + it 'should discard types which are not of type post' do + xml = "<posts> + <post><status_message>\n <message>Here is another message</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post> + <post><friend></friend></post> + <post><status_message>\n <message>HEY DUDE</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post> + </posts>" + store_posts_from_xml(xml) + Post.count.should == 2 + end end diff --git a/spec/models/bookmark_spec.rb b/spec/models/bookmark_spec.rb index 4cb8b5fc6..f46b05dea 100644 --- a/spec/models/bookmark_spec.rb +++ b/spec/models/bookmark_spec.rb @@ -14,19 +14,63 @@ describe Bookmark do n.owner.should == "bob@aol.com" end + it 'should validate its link' do + bookmark = Factory.build(:bookmark) + + #links changed valid + bookmark.link = "google.com" + bookmark.valid?.should == true + bookmark.link.should == "http://google.com/" + + bookmark.link = "www.google.com" + bookmark.valid?.should == true + bookmark.link.should == "http://www.google.com/" + + bookmark.link = "google.com/" + bookmark.valid?.should == true + bookmark.link.should == "http://google.com/" + + bookmark.link = "www.google.com/" + bookmark.valid?.should == true + bookmark.link.should == "http://www.google.com/" + + bookmark.link = "http://google.com" + bookmark.valid?.should == true + bookmark.link.should == "http://google.com/" + + bookmark.link = "http://www.google.com" + bookmark.valid?.should == true + + #invalid links + bookmark.link = "zsdvzxdg" + bookmark.valid?.should == false + bookmark.link = "sdfasfa.c" + bookmark.valid?.should == false + bookmark.link = "http://.com/" + bookmark.valid?.should == false + bookmark.link = "http://www..com/" + bookmark.valid?.should == false + bookmark.link = "http:/www.asodij.com/" + bookmark.valid?.should == false + bookmark.link = "https:/www.asodij.com/" + bookmark.valid?.should == false + bookmark.link = "http:///www.asodij.com/" + bookmark.valid?.should == false + end + describe "XML" do it 'should serialize to XML' do Factory.create(:user) message = Factory.create(:bookmark, :title => "Reddit", :link => "http://reddit.com") message.to_xml.to_s.should include "<title>Reddit" - message.to_xml.to_s.should include "http://reddit.com" + message.to_xml.to_s.should include "http://reddit.com/" end it 'should marshal serialized XML to object' do - xml = "Reddit</message><link>http://reddit.com</link><owner>bob@aol.com</owner></bookmark>" + xml = "<bookmark><title>Reddit</message><link>http://reddit.com/</link><owner>bob@aol.com</owner></bookmark>" parsed = Bookmark.from_xml(xml) parsed.title.should == "Reddit" - parsed.link.should == "http://reddit.com" + parsed.link.should == "http://reddit.com/" parsed.owner.should == "bob@aol.com" parsed.valid?.should be_true end diff --git a/spec/models/friend_spec.rb b/spec/models/friend_spec.rb index 7106e16ee..4ccf0ee7c 100644 --- a/spec/models/friend_spec.rb +++ b/spec/models/friend_spec.rb @@ -1,13 +1,59 @@ require File.dirname(__FILE__) + '/../spec_helper' describe Friend do + it 'should have a diaspora username and diaspora url' do - n = Factory.build(:friend, :url => nil) + n = Factory.build(:friend, :url => "") n.valid?.should be false n.url = "http://max.com/" n.valid?.should be true end + + it 'should validate its url' do + friend = Factory.build(:friend) + + #urls changed valid + friend.url = "google.com" + friend.valid?.should == true + friend.url.should == "http://google.com/" + + friend.url = "www.google.com" + friend.valid?.should == true + friend.url.should == "http://www.google.com/" + + friend.url = "google.com/" + friend.valid?.should == true + friend.url.should == "http://google.com/" + + friend.url = "www.google.com/" + friend.valid?.should == true + friend.url.should == "http://www.google.com/" + + friend.url = "http://google.com" + friend.valid?.should == true + friend.url.should == "http://google.com/" + + friend.url = "http://www.google.com" + friend.valid?.should == true + + #invalid urls + friend.url = "zsdvzxdg" + friend.valid?.should == false + friend.url = "sdfasfa.c" + friend.valid?.should == false + friend.url = "http://.com/" + friend.valid?.should == false + friend.url = "http://www..com/" + friend.valid?.should == false + friend.url = "http:/www.asodij.com/" + friend.valid?.should == false + friend.url = "https:/www.asodij.com/" + friend.valid?.should == false + friend.url = "http:///www.asodij.com/" + friend.valid?.should == false + end + describe "XML" do before do @f = Factory.build(:friend) From 5286b6be69ecc30b9d2d2868cef6c14fe425d6c8 Mon Sep 17 00:00:00 2001 From: maxwell <maxwell@joindiaspora.com> Date: Fri, 18 Jun 2010 18:03:13 -0700 Subject: [PATCH 19/19] DG MS; require XML root node; added header to XML (not yet required) --- app/helpers/application_helper.rb | 2 +- lib/common.rb | 20 ++++- spec/helpers/parser_spec.rb | 11 +-- spec/lib/common_spec.rb | 121 +++++++++++++++++++----------- 4 files changed, 101 insertions(+), 53 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 397d48999..efb413b32 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -12,7 +12,7 @@ module ApplicationHelper #i need to check some sort of metadata field - doc.xpath("//post").each do |post| #this is the post wrapper + doc.xpath("/XML/posts/post").each do |post| #this is the post wrapper post.children.each do|type| #now the text of post itself is the type #type object to xml is the the thing we want to from_xml check_and_save_post(type) diff --git a/lib/common.rb b/lib/common.rb index 14714245c..8e34b4591 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -19,14 +19,28 @@ module Diaspora end def friends_with_permissions - Friend.only(:url).map{|x| x = x.url + "/receive/"} + Friend.only(:url).map{|x| x = x.url + "receive/"} end def self.build_xml_for(posts) - xml = "<posts>" + xml = "<XML>" + xml += Post.generate_header + xml += "<posts>" posts.each {|x| xml << x.prep_webhook} - xml = xml + "</posts>" + xml += "</posts>" + xml += "</XML>" end + + + def self.generate_header + "<head> + <sender> + <email>#{User.first.email}</email> + <url>#{User.first.email}</url> + </sender> + </head>" + end + end end end diff --git a/spec/helpers/parser_spec.rb b/spec/helpers/parser_spec.rb index 978a5c83e..542a1aa0a 100644 --- a/spec/helpers/parser_spec.rb +++ b/spec/helpers/parser_spec.rb @@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../spec_helper' include ApplicationHelper -describe DashboardHelper do +describe ApplicationHelper do before do Factory.create(:user) end @@ -18,23 +18,24 @@ describe DashboardHelper do end it 'should discard posts where it does not know the type' do - xml = "<posts> + xml = "<XML><posts> <post><status_message>\n <message>Here is another message</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post> <post><not_a_real_type></not_a_real_type></post> <post><status_message>\n <message>HEY DUDE</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post> - </posts>" + </posts></XML>" store_posts_from_xml(xml) Post.count.should == 2 end it 'should discard types which are not of type post' do - xml = "<posts> + xml = "<XML><posts> <post><status_message>\n <message>Here is another message</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post> <post><friend></friend></post> <post><status_message>\n <message>HEY DUDE</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post> - </posts>" + </posts></XML>" store_posts_from_xml(xml) Post.count.should == 2 end end + diff --git a/spec/lib/common_spec.rb b/spec/lib/common_spec.rb index 369143186..ec9433119 100644 --- a/spec/lib/common_spec.rb +++ b/spec/lib/common_spec.rb @@ -7,56 +7,89 @@ describe Diaspora do describe Webhooks do before do - @user = Factory.create(:user) - @post = Factory.create(:post) + Factory.create(:user, :email => "bob@aol.com") end - it "should add the following methods to Post on inclusion" do - @post.respond_to?(:notify_friends).should be true - @post.respond_to?(:prep_webhook).should be true - @post.respond_to?(:friends_with_permissions).should be true + describe "header" do + before do + Factory.create(:status_message) + Factory.create(:bookmark) + stream = Post.stream + @xml = Post.build_xml_for(stream) + end + + it "should generate" do + @xml.should include "<head>" + @xml.should include "</head>" + end + + it "should provide a sender" do + @xml.should include "<sender>" + @xml.should include "</sender>" + end + + it "should provide the owner's email" do + @xml.should include "<email>bob@aol.com</email>" + end + + it "should provide the owner's url" do + pending "user does not have url field" + end end - it "should convert an object to a proper webhook" do - @post.prep_webhook.should == "<post>#{@post.to_xml.to_s}</post>" + describe "body" do + before do + @post = Factory.create(:post) + end + + it "should add the following methods to Post on inclusion" do + @post.respond_to?(:notify_friends).should be true + @post.respond_to?(:prep_webhook).should be true + @post.respond_to?(:friends_with_permissions).should be true + end + + it "should convert an object to a proper webhook" do + @post.prep_webhook.should == "<post>#{@post.to_xml.to_s}</post>" + end + + it "should retrieve all valid friend endpoints" do + Factory.create(:friend, :url => "http://www.bob.com/") + Factory.create(:friend, :url => "http://www.alice.com/") + Factory.create(:friend, :url => "http://www.jane.com/") + + @post.friends_with_permissions.should include("http://www.bob.com/receive/") + @post.friends_with_permissions.should include("http://www.alice.com/receive/") + @post.friends_with_permissions.should include("http://www.jane.com/receive/") + end + + it "should send an owners post to their friends" do + Post.stub(:build_xml_for).and_return(true) + Post.should_receive(:build_xml_for).and_return true + @post.save + end + + it "should check that it does not send a friends post to an owners friends" do + Post.stub(:build_xml_for).and_return(true) + Post.should_not_receive(:build_xml_for) + Factory.create(:post, :owner => "nottheowner@post.com") + end + + it "should ensure one url is created for every friend" do + 5.times {Factory.create(:friend)} + @post.friends_with_permissions.size.should == 5 + end + + it "should build an xml object containing multiple Post types" do + Factory.create(:status_message) + Factory.create(:bookmark) + + stream = Post.stream + xml = Post.build_xml_for(stream) + xml.should include "<status_message>" + xml.should include "<bookmark>" + end end - it "should retrieve all valid friend endpoints" do - Factory.create(:friend, :url => "http://www.bob.com") - Factory.create(:friend, :url => "http://www.alice.com") - Factory.create(:friend, :url => "http://www.jane.com") - - @post.friends_with_permissions.should include("http://www.bob.com/receive/") - @post.friends_with_permissions.should include("http://www.alice.com/receive/") - @post.friends_with_permissions.should include("http://www.jane.com/receive/") - end - - it "should send an owners post to their friends" do - Post.stub(:build_xml_for).and_return(true) - Post.should_receive(:build_xml_for).and_return true - @post.save - end - - it "should check that it does not send a friends post to an owners friends" do - Post.stub(:build_xml_for).and_return(true) - Post.should_not_receive(:build_xml_for) - Factory.create(:post, :owner => "nottheowner@post.com") - end - - it "should ensure one url is created for every friend" do - 5.times {Factory.create(:friend)} - @post.friends_with_permissions.size.should == 5 - end - - it "should build an xml object containing multiple Post types" do - Factory.create(:status_message) - Factory.create(:bookmark) - - stream = Post.stream - xml = Post.build_xml_for(stream) - xml.should include "<status_message>" - xml.should include "<bookmark>" - end end end