diff --git a/app/models/jobs/post_to_service.rb b/app/models/jobs/post_to_service.rb new file mode 100644 index 000000000..20b695fd2 --- /dev/null +++ b/app/models/jobs/post_to_service.rb @@ -0,0 +1,16 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. +# +module Jobs + class PostToService + extend ResqueJobLogging + @queue = :http + + def self.perform(service_id, post_id, url) + service = Service.find_by_id(service_id) + post = Post.find_by_id(post_id) + service.post(post, url) + end + end +end diff --git a/app/views/aspects/show.html.haml b/app/views/aspects/show.html.haml index 99f7567ab..99ff874f9 100644 --- a/app/views/aspects/show.html.haml +++ b/app/views/aspects/show.html.haml @@ -23,6 +23,6 @@ = render 'aspects/no_contacts_message', :aspect => @aspect, :contact_count => @aspect_contacts_count, :options => false = render 'aspects/no_posts_message', :post_count => @post_count, :contact_count=> @aspect_contacts_count - %ul{:class => 'stream', :id => 'main_stream'} + #main_stream.stream = render 'shared/stream', :posts => @post_hashes =will_paginate @posts diff --git a/lib/pubsubhubbub.rb b/lib/pubsubhubbub.rb index bb0bcf2a3..2610027cf 100644 --- a/lib/pubsubhubbub.rb +++ b/lib/pubsubhubbub.rb @@ -4,16 +4,14 @@ class PubSubHubbub - H = {"User-Agent" => "PubSubHubbub Ruby", "Content-Type" => "application/x-www-form-urlencoded"} def initialize(hub, options={}) - @headers = H.merge(options[:head]) if options[:head] - @hub = hub.kind_of?(URI) ? hub : URI::parse(hub) - end + @headers = H.merge(options[:head]) if options[:head] + @hub = hub + end - def publish(feed) - RestClient.post @hub, :headers => H, 'hub.url' => feed, 'hub.mode' => 'publish' - end + def publish(feed) + RestClient.post(@hub, :headers => @headers, 'hub.url' => feed, 'hub.mode' => 'publish') + end end - diff --git a/lib/tasks/fixtures.rake b/lib/tasks/fixtures.rake index e10882cf5..4d41da041 100644 --- a/lib/tasks/fixtures.rake +++ b/lib/tasks/fixtures.rake @@ -9,4 +9,75 @@ namespace :fixtures do UserFixer.regenerate_user_fixtures puts "Fixture regeneration complete." end + task :for_mysql_export do + puts "Populating DB for export." + require File.join(Rails.root,"config/environment") + require File.join(Rails.root,"spec/helper_methods") + require File.join(Rails.root,"spec/factories") + require File.join(Rails.root,"spec/support/user_methods") + require File.join(Rails.root,"spec/support/fake_resque") + + include HelperMethods + + Jobs::HttpPost.class_eval do + def self.perform(*args) + end + end + fantasy_resque do + models = [] + Factory(:person) + Factory(:person) + Factory(:person) + user1 = Factory(:user_with_aspect) + user2 = Factory(:user_with_aspect) + connect_users(user1, user1.aspects.first, user2, user2.aspects.first) + user2.activate_contact(Factory(:person), user2.aspects.first) + user2.reload + + user3 = Factory(:user_with_aspect) + user4 = Factory(:user_with_aspect) + user3.send_contact_request_to(user4.person, user3.aspects.first) + user3.reload; user4.reload + user3.send_contact_request_to(Factory(:person), user3.aspects.first) + + batch_invitee = Invitation.create_invitee(:email => "random@example.com", :name => "Curious George", :invites => 3) + invitee = user1.invite_user("random2@example.net", user1.aspects.first.id, "Hello!") + user1.reload + + u1post = user1.post(:status_message, :message => "User2 can see this", :to => [user1.aspects.first.id]) + user1.reload + u3post = user3.post(:status_message, :message => "User3 can see this", :to => [user3.aspects.first.id]) + user3.reload + user3.comment("Hey me!", :on => u3post) + user3.reload + user2.comment("Hey you!", :on => u1post) + user2.reload + + user2.post(:photo, :user_file => uploaded_photo, :to => [user2.aspects.first.id]) + user2.reload + user3.post(:photo, :user_file => uploaded_photo, :to => [user3.aspects.first.id]) + user3.reload + + remote_user = Factory(:user_with_aspect) + user4.activate_contact(remote_user.person, user4.aspects.first) + user4.reload + remote_message = remote_user.build_post(:photo, :user_file => uploaded_photo, :to => remote_user.aspects.first.id) + remote_photo = remote_user.build_post(:status_message, :message => "from another server!", :to => remote_user.aspects.first.id) + request = remote_user.send_contact_request_to(user2.person, remote_user.aspects.first) + remote_user.reload + remote_user.delete + Contact.where(:user_id => remote_user.id).each{|c| c.delete} + Aspect.where(:user_id => remote_user.id).each{|c| c.delete} + remote_person = remote_user.person + remote_person.owner_id = nil + remote_person.save + + user4.receive(remote_message.to_diaspora_xml, remote_person) + user3.reload + user4.receive(remote_photo.to_diaspora_xml, remote_person) + user3.reload + end + puts "Done generating fixtures, you can now export" + + end end diff --git a/spec/lib/pubsubhubbub_spec.rb b/spec/lib/pubsubhubbub_spec.rb new file mode 100644 index 000000000..1a90c29c4 --- /dev/null +++ b/spec/lib/pubsubhubbub_spec.rb @@ -0,0 +1,31 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +require 'spec_helper' + +describe PubSubHubbub do + before :all do + RestClient.unstub!(:post) + end + + after :all do + RestClient.stub!(:post).and_return(FakeHttpRequest.new(:success)) + end + + describe '#initialize' do + end + + describe '#publish' do + it 'posts the feed to the given hub' do + hub = "http://hubzord.com/" + feed = 'http://rss.com/dom.atom' + body = {'hub.url' => feed, 'hub.mode' => 'publish'} + + stub_request(:post, "http://hubzord.com/"). + with(:body => "hub.url=http%3A%2F%2Frss.com%2Fdom.atom&headers=&hub.mode=publish", + :headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'65', 'Content-Type'=>'application/x-www-form-urlencoded'}).to_return(:status => [202, 'you are awesome']) + PubSubHubbub.new(hub).publish(feed).code.should == 202 + end + end +end diff --git a/spec/models/jobs/post_to_service_spec.rb b/spec/models/jobs/post_to_service_spec.rb new file mode 100644 index 000000000..59cb062c6 --- /dev/null +++ b/spec/models/jobs/post_to_service_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe Jobs::PostToService do + it 'calls service#post with the given service' do + user = make_user + aspect = user.aspects.create(:name => "yeah") + post = user.post(:status_message, :message => 'foo', :to => aspect.id) + User.stub!(:find_by_id).with(user.id.to_s).and_return(user) + m = mock() + url = "foobar" + m.should_receive(:post).with(anything, url) + Service.stub!(:find_by_id).and_return(m) + Jobs::PostToService.perform("123", post.id.to_s, url) + end +end + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2d9901dae..cf8572166 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -54,61 +54,8 @@ class FakeRedis true end end -module Resque - def enqueue(klass, *args) - if $process_queue - klass.send(:perform, *args) - else - true - end - end -end ImageUploader.enable_processing = false -class User - def send_contact_request_to(desired_contact, aspect) - fantasy_resque do - contact = Contact.new(:person => desired_contact, - :user => self, - :pending => true) - contact.aspects << aspect - - if contact.save! - contact.dispatch_request - else - nil - end - end - end - - def post(class_name, opts = {}) - fantasy_resque do - p = build_post(class_name, opts) - if p.save! - raise 'MongoMapper failed to catch a failed save' unless p.id - - self.aspects.reload - - add_to_streams(p, opts[:to]) - dispatch_post(p, :to => opts[:to]) - end - p - end - end - - def comment(text, options = {}) - fantasy_resque do - c = build_comment(text, options) - if c.save! - raise 'MongoMapper failed to catch a failed save' unless c.id - dispatch_comment(c) - end - c - end - end -end - - class FakeHttpRequest def initialize(callback_wanted) diff --git a/spec/support/fake_resque.rb b/spec/support/fake_resque.rb new file mode 100644 index 000000000..165d9ea10 --- /dev/null +++ b/spec/support/fake_resque.rb @@ -0,0 +1,9 @@ +module Resque + def enqueue(klass, *args) + if $process_queue + klass.send(:perform, *args) + else + true + end + end +end diff --git a/spec/support/user_methods.rb b/spec/support/user_methods.rb new file mode 100644 index 000000000..d0288d7fa --- /dev/null +++ b/spec/support/user_methods.rb @@ -0,0 +1,42 @@ +class User + def send_contact_request_to(desired_contact, aspect) + fantasy_resque do + contact = Contact.new(:person => desired_contact, + :user => self, + :pending => true) + contact.aspects << aspect + + if contact.save! + contact.dispatch_request + else + nil + end + end + end + + def post(class_name, opts = {}) + fantasy_resque do + p = build_post(class_name, opts) + if p.save! + raise 'MongoMapper failed to catch a failed save' unless p.id + + self.aspects.reload + + add_to_streams(p, opts[:to]) + dispatch_post(p, :to => opts[:to]) + end + p + end + end + + def comment(text, options = {}) + fantasy_resque do + c = build_comment(text, options) + if c.save! + raise 'MongoMapper failed to catch a failed save' unless c.id + dispatch_comment(c) + end + c + end + end +end