diff --git a/app/models/jobs/mail_request_acceptance.rb b/app/models/jobs/mail_request_acceptance.rb new file mode 100644 index 000000000..6e00b4373 --- /dev/null +++ b/app/models/jobs/mail_request_acceptance.rb @@ -0,0 +1,9 @@ +module Jobs + class MailRequestAcceptance + @queue = :mail + def self.perform(recipient_id, sender_id, aspect_id) + Notifier.request_accepted(recipient_id, sender_id, aspect_id).deliver + end + end +end + diff --git a/app/models/jobs/mail_request_received.rb b/app/models/jobs/mail_request_received.rb new file mode 100644 index 000000000..b57e16fd6 --- /dev/null +++ b/app/models/jobs/mail_request_received.rb @@ -0,0 +1,9 @@ +module Jobs + class MailRequestReceived + @queue = :mail + def self.perform(recipient_id, sender_id) + Notifier.new_request(recipient_id, sender_id).deliver + end + end +end + diff --git a/app/models/request.rb b/app/models/request.rb index 3fce59fa9..8fd756316 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -6,7 +6,6 @@ class Request require File.join(Rails.root, 'lib/diaspora/webhooks') include MongoMapper::Document - include Magent::Async include Diaspora::Webhooks include ROXML @@ -24,8 +23,6 @@ class Request validate :no_pending_request, :if => :sent validate :not_friending_yourself - #before_validation :clean_link - scope :from, lambda { |person| target = (person.is_a?(User) ? person.person : person) where(:from_id => target.id) @@ -51,26 +48,10 @@ class Request ) end - - def self.send_request_accepted(user, person, aspect) - self.async.send_request_accepted!(user.id, person.id, aspect.id).commit! - end - - def self.send_request_accepted!(user_id, person_id, aspect_id) - Notifier.request_accepted(user_id, person_id, aspect_id).deliver - end - - def self.send_new_request(user, person) - self.async.send_new_request!(user.id, person.id).commit! - end - - def self.send_new_request!(user_id, person_id) - Notifier.new_request(user_id, person_id).deliver - end - def sender_handle from.diaspora_handle end + def sender_handle= sender_handle self.from = Person.first(:diaspora_handle => sender_handle) end @@ -78,6 +59,7 @@ class Request def recipient_handle to.diaspora_handle end + def recipient_handle= recipient_handle self.to = Person.first(:diaspora_handle => recipient_handle) end diff --git a/chef/cookbooks/common/recipes/daemontools.rb b/chef/cookbooks/common/recipes/daemontools.rb index 6d723964a..11c742b8d 100644 --- a/chef/cookbooks/common/recipes/daemontools.rb +++ b/chef/cookbooks/common/recipes/daemontools.rb @@ -42,13 +42,6 @@ execute "executable" do command "chmod -R 755 /service/websocket" end -execute "magent run" do - command "mkdir -p /service/magent && echo '#!/bin/sh' > /service/magent/run && echo 'cd /usr/local/app/diaspora && RAILS_ENV=production exec /usr/local/bin/bundle exec /usr/local/bin/magent start --log-path=/usr/local/app/diaspora/log/' >> /service/magent/run" -end -execute "executable" do - command "chmod -R 755 /service/magent" -end - execute "redis run" do command "mkdir -p /service/redis && echo '#!/bin/sh' > /service/redis/run && echo 'cd /usr/sbin/ && exec /usr/sbin/redis-server /usr/local/etc/redis.conf' >> /service/redis/run" end diff --git a/lib/diaspora/user/connecting.rb b/lib/diaspora/user/connecting.rb index fe9e8f99c..8f990bca4 100644 --- a/lib/diaspora/user/connecting.rb +++ b/lib/diaspora/user/connecting.rb @@ -56,22 +56,21 @@ module Diaspora end def receive_contact_request(contact_request) - Rails.logger.info("receiving contact request #{contact_request.to_json}") #response from a contact request you sent if original_request = original_request(contact_request) receive_request_acceptance(contact_request, original_request) - #this is a new contact request + #this is a new contact request elsif !request_from_me?(contact_request) if contact_request.save! self.pending_requests << contact_request self.save! - Rails.logger.info("#{self.name} has received a contact request") - Request.send_new_request(self, contact_request.from) + Rails.logger.info("event=contact_request status=received_new_request from=#{contact_request.from.diaspora_handle} to=#{self.diaspora_handle}") + Resque.enqueue(Jobs::MailRequestReceived, self.id, contact_request.from.id) end else - Rails.logger.info "#{self.name} is trying to receive a contact request from himself." + Rails.logger.info "event=contact_request status=abort from=#{contact_request.from.diaspora_handle} to=#{self.diaspora_handle} reason=self-love" return nil end contact_request @@ -80,13 +79,13 @@ module Diaspora def receive_request_acceptance(received_request, sent_request) destination_aspect = self.aspect_by_id(sent_request.into_id) activate_contact(received_request.from, destination_aspect) - Rails.logger.info("#{self.name}'s contact request has been accepted") + Rails.logger.info("event=contact_request status=received_acceptance from=#{received_request.from.diaspora_handle} to=#{self.diaspora_handle}") received_request.destroy pending_requests.delete(sent_request) sent_request.destroy self.save - Request.send_request_accepted(self, received_request.from, destination_aspect) + Resque.enqueue(Jobs::MailRequestAcceptance, self.id, received_request.from.id, destination_aspect.id) end def disconnect(bad_contact) diff --git a/lib/tasks/resque.rake b/lib/tasks/resque.rake index 5da255015..9e4b75083 100644 --- a/lib/tasks/resque.rake +++ b/lib/tasks/resque.rake @@ -1,25 +1,25 @@ require 'resque/tasks' task "resque:setup" => :environment do Dir[File.join(Rails.root, 'app', 'uploaders', '*.rb')].each { |file| - class_name = File.basename(file)[0..-4].camelize - begin - klass = Module.const_get(class_name) - klass.is_a?(Class) - rescue NameError - require file - end + safe_require(file) } Dir[File.join(Rails.root, 'app', 'models', '*.rb')].each { |file| - class_name = File.basename(file)[0..-4].camelize - begin - klass = Module.const_get(class_name) - klass.is_a?(Class) - rescue NameError - require file - end + safe_require(file) + } + Dir[File.join(Rails.root, 'app', 'mailers', '*.rb')].each { |file| + safe_require(file) } require File.join(Rails.root, 'app', 'controllers', 'application_controller.rb') require File.join(Rails.root, 'app', 'controllers', 'sockets_controller.rb') Rails.logger.info("event=resque_setup rails_env=#{Rails.env}") end +def safe_require(file) + class_name = File.basename(file)[0..-4].camelize + begin + klass = Module.const_get(class_name) + klass.is_a?(Class) + rescue NameError + require file + end +end diff --git a/script/server b/script/server index 6868db628..46e88cf3f 100755 --- a/script/server +++ b/script/server @@ -82,5 +82,6 @@ fi mkdir -p -v log/thin/ bundle exec ruby ./script/websocket_server.rb& -bundle exec magent start --log-path=log/ & +redis-server& +QUEUE=* bundle exec rake resque:work& bundle exec thin start $args diff --git a/spec/models/request_spec.rb b/spec/models/request_spec.rb index 2762bf2ee..468fbdb0c 100644 --- a/spec/models/request_spec.rb +++ b/spec/models/request_spec.rb @@ -165,48 +165,4 @@ describe Request do end end end - - context 'mailers' do - context 'sugar around contacts' do - before do - Request.should_receive(:async).and_return(Request) - @mock_request = mock() - @mock_request.should_receive(:commit!) - end - - describe '.send_request_accepted' do - it 'should make a call to push to the queue' do - Request.should_receive(:send_request_accepted!).with(user.id, person.id, aspect.id).and_return(@mock_request) - Request.send_request_accepted(user, person, aspect) - end - end - - describe '.send_new_request' do - it 'should make a call to push to the queue' do - Request.should_receive(:send_new_request!).with(user.id, person.id).and_return(@mock_request) - Request.send_new_request(user, person) - end - end - end - - context 'actual calls to mailer' do - before do - @mock_mail = mock() - @mock_mail.should_receive(:deliver) - end - describe '.send_request_accepted!' do - it 'should deliver the message' do - Notifier.should_receive(:request_accepted).and_return(@mock_mail) - Request.send_request_accepted!(user.id, person.id, aspect.id) - end - end - - describe '.send_new_request' do - it 'should deliver the message' do - Notifier.should_receive(:new_request).and_return(@mock_mail) - Request.send_new_request!(user.id, person.id) - end - end - end - end end diff --git a/spec/models/user/connecting_spec.rb b/spec/models/user/connecting_spec.rb index 2584f4fdd..d9a6b2e07 100644 --- a/spec/models/user/connecting_spec.rb +++ b/spec/models/user/connecting_spec.rb @@ -30,14 +30,22 @@ describe Diaspora::UserModules::Connecting do context 'contact requesting' do describe '#receive_contact_request' do + before do + @r = Request.instantiate(:to => user.person, :from => person) + end + it 'adds a request to pending if it was not sent by user' do - r = Request.instantiate(:to => user.person, :from => person) - user.receive_contact_request(r) - user.reload.pending_requests.should include r + user.receive_contact_request(@r) + user.reload.pending_requests.should include @r + end + + it 'enqueues a mail job' do + Resque.should_receive(:enqueue).with(Jobs::MailRequestReceived, user.id, person.id) + user.receive_contact_request(@r) end end - describe '#receive_request_accepted' do + describe '#receive_request_acceptance' do before do @original_request = user.send_contact_request_to(user2.person, aspect) @acceptance = @original_request.reverse_for(user2) @@ -56,6 +64,10 @@ describe Diaspora::UserModules::Connecting do user.pending_requests.include?(@acceptance).should be_false Request.find(@acceptance.id).should be_nil end + it 'enqueues a mail job' do + Resque.should_receive(:enqueue).with(Jobs::MailRequestAcceptance, user.id, user2.person.id, aspect.id).once + user.receive_request(@acceptance, user2.person) + end end context 'received a contact request' do @@ -106,13 +118,6 @@ describe Diaspora::UserModules::Connecting do }.should raise_error(MongoMapper::DocumentNotValid) end - it 'should send an email on acceptance if a contact request' do - Request.should_receive(:send_request_accepted) - request = user.send_contact_request_to(user2.person, aspect) - user.receive_request(request.reverse_for(user2), user2.person) - end - - describe 'multiple users accepting/rejecting the same person' do before do @@ -151,11 +156,6 @@ describe Diaspora::UserModules::Connecting do }.should_not change(Person, :count) user2.contact_for(user.person).should be_nil end - - it 'sends an email to the receiving user' do - Request.should_receive(:send_new_request).and_return(true) - user.receive @req_xml, person_one - end end context 'Two users receiving requests from one person' do