Skip to content
Extraits de code Groupes Projets
Valider 71cae6b8 rédigé par danielvincent's avatar danielvincent
Parcourir les fichiers

Move request mail into resque

parent 8e8398fd
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
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
module Jobs
class MailRequestReceived
@queue = :mail
def self.perform(recipient_id, sender_id)
Notifier.new_request(recipient_id, sender_id).deliver
end
end
end
......@@ -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
......
......@@ -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)
......
......@@ -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
......@@ -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
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter