diff --git a/app/mailers/notifier.rb b/app/mailers/notifier.rb index 40f61a2bd..e95b34b15 100644 --- a/app/mailers/notifier.rb +++ b/app/mailers/notifier.rb @@ -1,37 +1,26 @@ class Notifier < ActionMailer::Base - include Magent::Async 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) @receiver = User.find_by_id(recipient_id) @sender = Person.find_by_id(sender_id) - puts "#{@receiver}" - puts "#{@sender}" + attachments.inline['diaspora_white_on_grey.png'] = ATTACHMENT - mail(:to => "#{@receiver.real_name} <#{@reciever.email}>", - :subject => "new Diaspora* friend request from #{@sender.real_name}", :host => APP_CONFIG[:terse_pod_url]) + mail(:to => "#{@receiver.real_name} <#{@receiver.email}>", + :subject => "new Diaspora* friend request from #{@sender.real_name}", :host => APP_CONFIG[:terse_pod_url]) end def request_accepted(recipient_id, sender_id, aspect_id) @receiver = User.find_by_id(recipient_id) @sender = Person.find_by_id(sender_id) @aspect = Aspect.find_by_id(aspect_id) - puts "fooooo" attachments.inline['diaspora_white_on_grey.png'] = ATTACHMENT mail(:to => "#{@receiver.real_name} <#{@receiver.email}>", - :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! + :subject => "#{@sender.real_name} has accepted your friend request on Diaspora*", :host => APP_CONFIG[:terse_pod_url]) end end diff --git a/app/models/request.rb b/app/models/request.rb index 7f3ab6eff..5480043d6 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -6,6 +6,7 @@ class Request require File.join(Rails.root, 'lib/diaspora/webhooks') include MongoMapper::Document + include Magent::Async include Diaspora::Webhooks include ROXML @@ -41,6 +42,24 @@ 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 + + protected def clean_link if self.destination_url diff --git a/lib/diaspora/user/friending.rb b/lib/diaspora/user/friending.rb index 8561462d3..a37a64e1b 100644 --- a/lib/diaspora/user/friending.rb +++ b/lib/diaspora/user/friending.rb @@ -65,9 +65,10 @@ module Diaspora destination_aspect = self.aspect_by_id(original_request.aspect_id) activate_friend(friend_request.person, destination_aspect) Rails.logger.info("#{self.real_name}'s friend request has been accepted") + friend_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 elsif !request_from_me?(friend_request) @@ -75,7 +76,7 @@ module Diaspora self.save Rails.logger.info("#{self.real_name} has received a friend request") friend_request.save - Notifier.send_new_request!(self, friend_request.person) + Request.send_new_request(self, friend_request.person) else raise "#{self.real_name} is trying to receive a friend request from himself." end diff --git a/spec/mailers/notifier_spec.rb b/spec/mailers/notifier_spec.rb index c50dd6932..c0b3a4aea 100644 --- a/spec/mailers/notifier_spec.rb +++ b/spec/mailers/notifier_spec.rb @@ -6,8 +6,8 @@ describe Notifier do let!(:user) {make_user} let!(:aspect) {user.aspects.create(:name => "science")} let!(:person) {Factory.create :person} - let!(:request_mail) {Notifier.new_request(user, person)} - let!(:request_accepted_mail) {Notifier.request_accepted(user, person, aspect)} + let!(:request_mail) {Notifier.new_request(user.id, person.id)} + let!(:request_accepted_mail) {Notifier.request_accepted(user.id, person.id, aspect.id)} describe "#new_request" do diff --git a/spec/models/request_spec.rb b/spec/models/request_spec.rb index 6e7a0af22..096baf595 100644 --- a/spec/models/request_spec.rb +++ b/spec/models/request_spec.rb @@ -69,7 +69,50 @@ describe Request do it 'does not serialize the id' do @xml.should_not include @request.id.to_s 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 \ No newline at end of file diff --git a/spec/models/user/friending_spec.rb b/spec/models/user/friending_spec.rb index a1850cab6..b8d8d33d7 100644 --- a/spec/models/user/friending_spec.rb +++ b/spec/models/user/friending_spec.rb @@ -88,7 +88,7 @@ describe Diaspora::UserModules::Friending do end 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) user.receive_friend_request(request.reverse_for(user2)) end @@ -134,7 +134,7 @@ describe Diaspora::UserModules::Friending do end 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 end end