mailer queue now works

This commit is contained in:
maxwell 2010-11-05 11:40:27 -07:00
parent 42c24e2b04
commit cc92f61583
6 changed files with 78 additions and 26 deletions

View file

@ -1,37 +1,26 @@
class Notifier < ActionMailer::Base class Notifier < ActionMailer::Base
include Magent::Async
default :from => "no-reply@joindiaspora.com" default :from => "no-reply@joindiaspora.com"
ATTACHMENT = File.read("#{Rails.root}/public/images/diaspora_white_on_grey.png")
ATTACHMENT = File.read("#{Rails.root}/public/images/diaspora_white_on_grey.png")
def new_request(recipient_id, sender_id) def new_request(recipient_id, sender_id)
@receiver = User.find_by_id(recipient_id) @receiver = User.find_by_id(recipient_id)
@sender = Person.find_by_id(sender_id) @sender = Person.find_by_id(sender_id)
puts "#{@receiver}"
puts "#{@sender}"
attachments.inline['diaspora_white_on_grey.png'] = ATTACHMENT attachments.inline['diaspora_white_on_grey.png'] = ATTACHMENT
mail(:to => "#{@receiver.real_name} <#{@reciever.email}>", mail(:to => "#{@receiver.real_name} <#{@receiver.email}>",
:subject => "new Diaspora* friend request from #{@sender.real_name}", :host => APP_CONFIG[:terse_pod_url]) :subject => "new Diaspora* friend request from #{@sender.real_name}", :host => APP_CONFIG[:terse_pod_url])
end end
def request_accepted(recipient_id, sender_id, aspect_id) def request_accepted(recipient_id, sender_id, aspect_id)
@receiver = User.find_by_id(recipient_id) @receiver = User.find_by_id(recipient_id)
@sender = Person.find_by_id(sender_id) @sender = Person.find_by_id(sender_id)
@aspect = Aspect.find_by_id(aspect_id) @aspect = Aspect.find_by_id(aspect_id)
puts "fooooo"
attachments.inline['diaspora_white_on_grey.png'] = ATTACHMENT attachments.inline['diaspora_white_on_grey.png'] = ATTACHMENT
mail(:to => "#{@receiver.real_name} <#{@receiver.email}>", mail(:to => "#{@receiver.real_name} <#{@receiver.email}>",
:subject => "#{@sender.real_name} has accepted your friend request on Diaspora*", :host => APP_CONFIG[:terse_pod_url]) :subject => "#{@sender.real_name} has accepted your friend request on Diaspora*", :host => APP_CONFIG[:terse_pod_url])
end
def self.send_request_accepted!(user, person, aspect)
Notifier.async.request_accepted(user.id, person.id, aspect.id ).deliver.commit!
end
def self.send_new_request!(user, person)
Notifier.async.new_request(user.id, person.id).deliver.commit!
end end
end end

View file

@ -6,6 +6,7 @@ class Request
require File.join(Rails.root, 'lib/diaspora/webhooks') require File.join(Rails.root, 'lib/diaspora/webhooks')
include MongoMapper::Document include MongoMapper::Document
include Magent::Async
include Diaspora::Webhooks include Diaspora::Webhooks
include ROXML include ROXML
@ -41,6 +42,24 @@ class Request
) )
end 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
protected protected
def clean_link def clean_link
if self.destination_url if self.destination_url

View file

@ -65,9 +65,10 @@ module Diaspora
destination_aspect = self.aspect_by_id(original_request.aspect_id) destination_aspect = self.aspect_by_id(original_request.aspect_id)
activate_friend(friend_request.person, destination_aspect) activate_friend(friend_request.person, destination_aspect)
Rails.logger.info("#{self.real_name}'s friend request has been accepted") Rails.logger.info("#{self.real_name}'s friend request has been accepted")
friend_request.destroy friend_request.destroy
original_request.destroy original_request.destroy
Notifier.send_request_accepted!(self, friend_request.person, destination_aspect) Request.send_request_accepted(self, friend_request.person, destination_aspect)
#this is a new friend request #this is a new friend request
elsif !request_from_me?(friend_request) elsif !request_from_me?(friend_request)
@ -75,7 +76,7 @@ module Diaspora
self.save self.save
Rails.logger.info("#{self.real_name} has received a friend request") Rails.logger.info("#{self.real_name} has received a friend request")
friend_request.save friend_request.save
Notifier.send_new_request!(self, friend_request.person) Request.send_new_request(self, friend_request.person)
else else
raise "#{self.real_name} is trying to receive a friend request from himself." raise "#{self.real_name} is trying to receive a friend request from himself."
end end

View file

@ -6,8 +6,8 @@ describe Notifier do
let!(:user) {make_user} let!(:user) {make_user}
let!(:aspect) {user.aspects.create(:name => "science")} let!(:aspect) {user.aspects.create(:name => "science")}
let!(:person) {Factory.create :person} let!(:person) {Factory.create :person}
let!(:request_mail) {Notifier.new_request(user, person)} let!(:request_mail) {Notifier.new_request(user.id, person.id)}
let!(:request_accepted_mail) {Notifier.request_accepted(user, person, aspect)} let!(:request_accepted_mail) {Notifier.request_accepted(user.id, person.id, aspect.id)}
describe "#new_request" do describe "#new_request" do

View file

@ -69,7 +69,50 @@ describe Request do
it 'does not serialize the id' do it 'does not serialize the id' do
@xml.should_not include @request.id.to_s @xml.should_not include @request.id.to_s
end end
end end
end context 'mailers' do
context 'suger around friends' 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

View file

@ -88,7 +88,7 @@ describe Diaspora::UserModules::Friending do
end end
it 'should send an email on acceptance if a friend request' do it 'should send an email on acceptance if a friend request' do
Notifier.should_receive(:send_request_accepted!) Request.should_receive(:send_request_accepted)
request = user.send_friend_request_to(user2.person, aspect) request = user.send_friend_request_to(user2.person, aspect)
user.receive_friend_request(request.reverse_for(user2)) user.receive_friend_request(request.reverse_for(user2))
end end
@ -134,7 +134,7 @@ describe Diaspora::UserModules::Friending do
end end
it 'sends an email to the receiving user' do it 'sends an email to the receiving user' do
Notifier.should_receive(:send_new_request!).and_return(true) Request.should_receive(:send_new_request).and_return(true)
user.receive @req_xml, person_one user.receive @req_xml, person_one
end end
end end