diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 858bccf2d..954e4b123 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -56,6 +56,7 @@ class UsersController < ApplicationController elsif u[:email] @user.unconfirmed_email = u[:email] if @user.save + @user.mail_confirm_email flash[:notice] = I18n.t 'users.update.unconfirmed_email_changed' else flash[:error] = I18n.t 'users.update.unconfirmed_email_not_changed' diff --git a/app/mailers/notifier.rb b/app/mailers/notifier.rb index c7b10bcf3..45fd635c2 100644 --- a/app/mailers/notifier.rb +++ b/app/mailers/notifier.rb @@ -115,6 +115,18 @@ class Notifier < ActionMailer::Base end end + def confirm_email(receiver_id) + @receiver = User.find_by_id(receiver_id) + + attachments.inline['logo_caps.png'] = ATTACHMENT + + I18n.with_locale(@receiver.language) do + mail(:to => "\"#{@receiver.name}\" <#{@receiver.unconfirmed_email}>", + :subject => I18n.t('notifier.confirm_email.subject', :unconfirmed_email => @receiver.unconfirmed_email), + :host => AppConfig[:pod_uri].host) + end + end + private def log_mail recipient_id, sender_id, type log_string = "event=mail mail_type=#{type} recipient_id=#{recipient_id} sender_id=#{sender_id}" diff --git a/app/models/jobs/mail_confirm_email.rb b/app/models/jobs/mail_confirm_email.rb new file mode 100644 index 000000000..6c8bc3248 --- /dev/null +++ b/app/models/jobs/mail_confirm_email.rb @@ -0,0 +1,8 @@ +module Job + class MailConfirmEmail < Base + @queue = :mail + def self.perform_delegate(user_id) + Notifier.confirm_email(user_id).deliver + end + end +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index d56ab6001..ee74711c1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -206,6 +206,12 @@ class User < ActiveRecord::Base end end + def mail_confirm_email + return false if unconfirmed_email.blank? + Resque.enqueue(Job::MailConfirmEmail, id) + true + end + ######### Posts and Such ############### def retract(post) if post.respond_to?(:relayable?) && post.relayable? diff --git a/app/views/notifier/confirm_email.html.haml b/app/views/notifier/confirm_email.html.haml new file mode 100644 index 000000000..88a880d68 --- /dev/null +++ b/app/views/notifier/confirm_email.html.haml @@ -0,0 +1,12 @@ +%p + = t('notifier.hello', :name => @receiver.profile.first_name) +%p + != t('notifier.confirm_email.click_link', :unconfirmed_email => @receiver.unconfirmed_email) + %br + = link_to root_url, root_url + + %br + %br + = t('notifier.love') + %br + = t('notifier.diaspora') \ No newline at end of file diff --git a/app/views/notifier/confirm_email.text.haml b/app/views/notifier/confirm_email.text.haml new file mode 100644 index 000000000..8a86f0afd --- /dev/null +++ b/app/views/notifier/confirm_email.text.haml @@ -0,0 +1,7 @@ +!= t('notifier.hello', :name => @receiver.profile.first_name) + +!= t('notifier.confirm_email.click_link', :unconfirmed_email => @receiver.unconfirmed_email) +!= root_url + +!= "#{t('notifier.love')} \n" +!= t('notifier.diaspora') diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index 012f4e579..48b29f141 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -401,6 +401,9 @@ en: subject: "%{name} has just liked your post" liked: "%{name} has just liked your post: " sign_in: "Sign in to view it" + confirm_email: + subject: "Please activate your new e-mail address %{unconfirmed_email}" + click_link: "To activate your new e-mail address %{unconfirmed_email}, please click this link:" people: zero: "no people" diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 8e070a0d7..824a28f75 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -109,6 +109,10 @@ describe UsersController do end describe 'email' do + before do + Resque.stub!(:enqueue) + end + it 'allow the user to change his (unconfirmed) email' do put(:update, :id => @user.id, :user => { :email => "my@newemail.com"}) @user.reload @@ -132,6 +136,11 @@ describe UsersController do @user.reload @user.unconfirmed_email.should eql(nil) end + + it 'sends out activation email on success' do + Resque.should_receive(:enqueue).with(Job::MailConfirmEmail, @user.id).once + put(:update, :id => @user.id, :user => { :email => "my@newemail.com"}) + end end describe 'email settings' do diff --git a/spec/mailers/notifier_spec.rb b/spec/mailers/notifier_spec.rb index 40c062067..dca03ed5b 100644 --- a/spec/mailers/notifier_spec.rb +++ b/spec/mailers/notifier_spec.rb @@ -207,5 +207,33 @@ describe Notifier do end + describe ".confirm_email" do + before do + user.update_attribute(:unconfirmed_email, "my@newemail.com") + end + + let!(:confirm_email) { Notifier.confirm_email(user.id) } + + it 'goes to the right person' do + confirm_email.to.should == [user.unconfirmed_email] + end + + it 'has the unconfirmed emil in the subject' do + confirm_email.subject.should include(user.unconfirmed_email) + end + + it 'has the unconfirmed emil in the body' do + confirm_email.body.encoded.should include(user.unconfirmed_email) + end + + it 'has the receivers name in the body' do + confirm_email.body.encoded.should include(user.person.profile.first_name) + end + + it 'has the activation link in the body' do + pending + end + + end end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 2b2747fc8..3bf33bd6e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -673,5 +673,18 @@ describe User do user.confirm_email_token.size.should eql(30) end end + + describe '#mail_confirm_email' do + it 'enqueues a mail job on user with unconfirmed email' do + user.update_attribute(:unconfirmed_email, "alice@newmail.com") + Resque.should_receive(:enqueue).with(Job::MailConfirmEmail, alice.id).once + alice.mail_confirm_email.should eql(true) + end + + it 'enqueues NO mail job on user without unconfirmed email' do + Resque.should_not_receive(:enqueue).with(Job::MailConfirmEmail, alice.id) + alice.mail_confirm_email.should eql(false) + end + end end end