From e4eb7a9a793e1127edfe51b75b86c3202bb14a35 Mon Sep 17 00:00:00 2001 From: ilya Date: Thu, 21 Oct 2010 19:35:35 -0700 Subject: [PATCH 1/3] started the new invitation email --- app/mailers/notifier.rb | 14 ++++++ app/views/notifier/new_request.html.haml | 61 ++++++++++++++++++++++++ lib/diaspora/user/friending.rb | 1 + spec/mailers/notifier_spec.rb | 19 ++++++++ spec/models/user/user_friending_spec.rb | 14 +++++- 5 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 app/mailers/notifier.rb create mode 100644 app/views/notifier/new_request.html.haml create mode 100644 spec/mailers/notifier_spec.rb diff --git a/app/mailers/notifier.rb b/app/mailers/notifier.rb new file mode 100644 index 000000000..56f0f3555 --- /dev/null +++ b/app/mailers/notifier.rb @@ -0,0 +1,14 @@ +class Notifier < ActionMailer::Base + default :from => "no-reply@joindiaspora.com" + + def new_request(recipient, sender) + @receiver = recipient + @sender = sender + mail(:to => recipient.email) do |format| + format.text { render :text => "This is text!" } + format.html { render :text => "

#{@receiver.person.profile.first_name}This is HTML

" } + end + end + + +end diff --git a/app/views/notifier/new_request.html.haml b/app/views/notifier/new_request.html.haml new file mode 100644 index 000000000..daa54c0eb --- /dev/null +++ b/app/views/notifier/new_request.html.haml @@ -0,0 +1,61 @@ +!!! +%html + %head + %meta{"http-equiv"=>"Content-Type", :content=>"text/html; charset=utf-8"}/ + :css + body{ + width:600px; + font-family:'Arial','Helvetica',sans-serif; + font-size:14px; + color:#333; + } + #container{ + margin-bottom:25px + min-height:400px; + padding-left:15px; + } + header{ + background-color:#333; + padding: 15px; + margin-bottom: 25px; + } + p{ + padding:5px; + } + p.small{ + font-size:smaller; + color:#999; + font-style:italic; + } + a{ + color:#107FC9; + font-weight:bold; + } + a:hover{ + color: #22AAE0; + } + a:active{ + color: #005D9C; + } + .large_text{ + font-size:21px; + font-family:"Helvetica Neue",Arial,Helvetica,sans-serif; + } + %body + %header + = image_tag '/images/diaspora_white.png' + #container + %p + Hello #{@receiver.first_name}! + %p + #{(@resource.inviters.count == 1)? ( @resource.inviters.first.real_name + " (#{@resource.inviters.first.diaspora_handle})" + " has") : (@resource.inviters.map{|inv| inv.real_name + " (#{inv.diaspora_handle})"}.join(",") + " have")} invited you to join Diaspora at #{root_url}, you can accept it through the link below. + - @resource.inviters.each do |inv| + - if @resource.invite_messages[inv.id.to_s] + = "#{inv.real_name}:" + = @resource.invite_messages[inv.id.to_s] + %p + %p= link_to 'Accept invitation', accept_invitation_url(@resource, :invitation_token => @resource.invitation_token), :class => "large_text" + %p.small + If you don't want to accept the invitation, please ignore this email. + %br/ + Your account won't be created until you access the link above and sign up. diff --git a/lib/diaspora/user/friending.rb b/lib/diaspora/user/friending.rb index 6946910f2..570ce3544 100644 --- a/lib/diaspora/user/friending.rb +++ b/lib/diaspora/user/friending.rb @@ -74,6 +74,7 @@ module Diaspora else self.pending_requests << friend_request self.save + Notifier.new_request(self, friend_request.person) Rails.logger.info("#{self.real_name} has received a friend request") friend_request.save end diff --git a/spec/mailers/notifier_spec.rb b/spec/mailers/notifier_spec.rb new file mode 100644 index 000000000..97316890d --- /dev/null +++ b/spec/mailers/notifier_spec.rb @@ -0,0 +1,19 @@ + +require 'spec_helper' + +describe Notifier do + + let(:user) {Factory.create :user} + let(:person) {Factory.create :person} + let(:request_mail) {Notifier.new_request(user, person)} + + describe "#new_request" do + it 'goes to the right person' do + request_mail.to.should == [user.email] + end + + it 'has the receivers name in the body' do + request_mail.body.encoded.includes?(user.first_name).should be true + end + end +end diff --git a/spec/models/user/user_friending_spec.rb b/spec/models/user/user_friending_spec.rb index 2fde487b3..83c1f456a 100644 --- a/spec/models/user/user_friending_spec.rb +++ b/spec/models/user/user_friending_spec.rb @@ -93,6 +93,16 @@ describe User do }.should_not change(Person, :count) user2.friends.include?(user.person).should be false end + + it 'sends an email to the receiving user' do + Notifier.should_receive(:new_request) + user.receive @req_xml, person_one + end + + + it 'should send a an email saying your friend request was confirmed' do + pending + end end context 'Two users receiving requests from one person' do before do @@ -107,7 +117,7 @@ describe User do user2.accept_friend_request @request_two.id, aspect2.id user2.friends.include?(person_one).should be true end - + it 'should keep the person around if one of the users rejects him' do user.accept_friend_request @request.id, aspect.id user.friends.include?(person_one).should be true @@ -124,6 +134,8 @@ describe User do user2.friends.include?(person_one).should be false end end + + end describe 'a user accepting rejecting multiple people' do From d2e3be7262ac9da9888689058f08dfdf279c864d Mon Sep 17 00:00:00 2001 From: maxwell Date: Fri, 22 Oct 2010 00:13:18 -0700 Subject: [PATCH 2/3] should send the email when a new request is received. emails in dev should be intercepted to email@joindiaspora.com for now --- app/mailers/notifier.rb | 8 ++------ app/views/notifier/new_request.html.haml | 21 +++++++++------------ app/views/notifier/new_request.text.haml | 9 +++++++++ config/initializers/setup_mail.rb | 1 + lib/development_mail_interceptor.rb | 7 +++++++ lib/diaspora/user/friending.rb | 2 +- spec/factories.rb | 9 +++++---- spec/mailers/notifier_spec.rb | 13 +++++++++---- spec/models/user/attack_vectors_spec.rb | 7 ++++--- 9 files changed, 47 insertions(+), 30 deletions(-) create mode 100644 app/views/notifier/new_request.text.haml create mode 100644 config/initializers/setup_mail.rb create mode 100644 lib/development_mail_interceptor.rb diff --git a/app/mailers/notifier.rb b/app/mailers/notifier.rb index 56f0f3555..8ce7995b4 100644 --- a/app/mailers/notifier.rb +++ b/app/mailers/notifier.rb @@ -4,11 +4,7 @@ class Notifier < ActionMailer::Base def new_request(recipient, sender) @receiver = recipient @sender = sender - mail(:to => recipient.email) do |format| - format.text { render :text => "This is text!" } - format.html { render :text => "

#{@receiver.person.profile.first_name}This is HTML

" } - end + mail(:to => "#{recipient.real_name} <#{recipient.email}>", + :subject => "new friend request from #{@sender.inspect}") end - - end diff --git a/app/views/notifier/new_request.html.haml b/app/views/notifier/new_request.html.haml index daa54c0eb..54cee3ddd 100644 --- a/app/views/notifier/new_request.html.haml +++ b/app/views/notifier/new_request.html.haml @@ -46,16 +46,13 @@ = image_tag '/images/diaspora_white.png' #container %p - Hello #{@receiver.first_name}! + Hello #{@receiver.profile.first_name}! %p - #{(@resource.inviters.count == 1)? ( @resource.inviters.first.real_name + " (#{@resource.inviters.first.diaspora_handle})" + " has") : (@resource.inviters.map{|inv| inv.real_name + " (#{inv.diaspora_handle})"}.join(",") + " have")} invited you to join Diaspora at #{root_url}, you can accept it through the link below. - - @resource.inviters.each do |inv| - - if @resource.invite_messages[inv.id.to_s] - = "#{inv.real_name}:" - = @resource.invite_messages[inv.id.to_s] - %p - %p= link_to 'Accept invitation', accept_invitation_url(@resource, :invitation_token => @resource.invitation_token), :class => "large_text" - %p.small - If you don't want to accept the invitation, please ignore this email. - %br/ - Your account won't be created until you access the link above and sign up. + = "#{@sender.real_name} (#{@sender.diaspora_handle})" + just sent you a friend request on Diaspora* + You should really think about checking it out. + + = link_to "sign in here", new_user_session_path + + love, + the diaspora email robot diff --git a/app/views/notifier/new_request.text.haml b/app/views/notifier/new_request.text.haml new file mode 100644 index 000000000..1c1505f74 --- /dev/null +++ b/app/views/notifier/new_request.text.haml @@ -0,0 +1,9 @@ += "hey #{@receiver.profile.first_name}," += "#{@sender.real_name} (#{@sender.diaspora_handle})" +just sent you a friend request on Diaspora* +You should really think about checking it out. + += "sign in here: #{new_user_session_path}" + +love, +the diaspora email robot diff --git a/config/initializers/setup_mail.rb b/config/initializers/setup_mail.rb new file mode 100644 index 000000000..e94ea5133 --- /dev/null +++ b/config/initializers/setup_mail.rb @@ -0,0 +1 @@ +ActionMailer::Base.register_interceptor(DevelopmentMailInterceptor) if Rails.env.development? diff --git a/lib/development_mail_interceptor.rb b/lib/development_mail_interceptor.rb new file mode 100644 index 000000000..efc89596f --- /dev/null +++ b/lib/development_mail_interceptor.rb @@ -0,0 +1,7 @@ +class DevelopmentMailInterceptor + def self.delivering_email(message) + message.subject = "[#{message.to}] #{message.subject}" + message.to = "email@joindiaspora.com" + end +end + diff --git a/lib/diaspora/user/friending.rb b/lib/diaspora/user/friending.rb index 570ce3544..c7809b39d 100644 --- a/lib/diaspora/user/friending.rb +++ b/lib/diaspora/user/friending.rb @@ -74,7 +74,7 @@ module Diaspora else self.pending_requests << friend_request self.save - Notifier.new_request(self, friend_request.person) + Notifier.new_request(self, friend_request.person).deliver Rails.logger.info("#{self.real_name} has received a friend request") friend_request.save end diff --git a/spec/factories.rb b/spec/factories.rb index c66fa4f86..8d0b76aee 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -8,14 +8,15 @@ #This inclsion, because gpg-agent(not needed) is never run and hence never sets any env. variables on a MAC Factory.define :profile do |p| - p.first_name "Robert" - p.last_name "Grimm" + p.sequence(:first_name){|n| "Robert#{n}"} + p.sequence(:last_name){|n| "Grimm#{n}"} end + Factory.define :person do |p| p.sequence(:diaspora_handle) {|n| "bob-person-#{n}@aol.com"} p.sequence(:url) {|n| "http://google-#{n}.com/"} - p.profile Factory.create(:profile) + p.profile Factory.create(:profile, :first_name => "eugene", :last_name => "weinstien") p.serialized_public_key OpenSSL::PKey::RSA.generate(1024).public_key.export end @@ -32,7 +33,7 @@ Factory.define :user do |u| u.password_confirmation "bluepin7" u.serialized_private_key OpenSSL::PKey::RSA.generate(1024).export u.after_build do |user| - user.person = Factory.build(:person, :owner_id => user._id, + user.person = Factory.build(:person, :profile => Factory.create(:profile), :owner_id => user._id, :serialized_public_key => user.encryption_key.public_key.export, :diaspora_handle => "#{user.username}@#{APP_CONFIG[:pod_url].gsub(/(https?:|www\.)\/\//, '').chop!}") end diff --git a/spec/mailers/notifier_spec.rb b/spec/mailers/notifier_spec.rb index 97316890d..394733cea 100644 --- a/spec/mailers/notifier_spec.rb +++ b/spec/mailers/notifier_spec.rb @@ -3,9 +3,9 @@ require 'spec_helper' describe Notifier do - let(:user) {Factory.create :user} - let(:person) {Factory.create :person} - let(:request_mail) {Notifier.new_request(user, person)} + let!(:user) {Factory.create :user} + let!(:person) {Factory.create :person} + let!(:request_mail) {Notifier.new_request(user, person)} describe "#new_request" do it 'goes to the right person' do @@ -13,7 +13,12 @@ describe Notifier do end it 'has the receivers name in the body' do - request_mail.body.encoded.includes?(user.first_name).should be true + request_mail.body.encoded.include?(user.person.profile.first_name).should be true + end + + + it 'has the name of person sending the request' do + request_mail.body.encoded.include?(person.real_name).should be true end end end diff --git a/spec/models/user/attack_vectors_spec.rb b/spec/models/user/attack_vectors_spec.rb index 67235f447..36a586647 100644 --- a/spec/models/user/attack_vectors_spec.rb +++ b/spec/models/user/attack_vectors_spec.rb @@ -4,7 +4,7 @@ require 'spec_helper' -describe User do +describe "attack vectors" do let(:user) { Factory(:user) } let(:aspect) { user.aspect(:name => 'heroes') } @@ -73,10 +73,11 @@ describe User do profile.first_name = "Not BOB" user2.reload - user2.profile.first_name.should == "Robert" + + first_name = user2.profile.first_name proc{user.receive_salmon(user3.salmon(profile).xml_for(user.person))}.should raise_error /Malicious Post/ user2.reload - user2.profile.first_name.should == "Robert" + user2.profile.first_name.should == first_name end it 'should not overwrite another persons profile through comment' do From 93ba0730097f38522565c8232df2d6ec0eb4fcc9 Mon Sep 17 00:00:00 2001 From: maxwell Date: Fri, 22 Oct 2010 00:40:01 -0700 Subject: [PATCH 3/3] small bugfixes for the mailer --- app/mailers/notifier.rb | 2 +- app/views/notifier/new_request.html.haml | 7 ++++--- app/views/notifier/new_request.text.haml | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/mailers/notifier.rb b/app/mailers/notifier.rb index 8ce7995b4..40a94a1f3 100644 --- a/app/mailers/notifier.rb +++ b/app/mailers/notifier.rb @@ -5,6 +5,6 @@ class Notifier < ActionMailer::Base @receiver = recipient @sender = sender mail(:to => "#{recipient.real_name} <#{recipient.email}>", - :subject => "new friend request from #{@sender.inspect}") + :subject => "new Diaspora* friend request from #{@sender.real_name}") end end diff --git a/app/views/notifier/new_request.html.haml b/app/views/notifier/new_request.html.haml index 54cee3ddd..71932dc44 100644 --- a/app/views/notifier/new_request.html.haml +++ b/app/views/notifier/new_request.html.haml @@ -51,8 +51,9 @@ = "#{@sender.real_name} (#{@sender.diaspora_handle})" just sent you a friend request on Diaspora* You should really think about checking it out. - - = link_to "sign in here", new_user_session_path - + %br + = link_to "sign in here", new_user_session_url + %br love, + %br the diaspora email robot diff --git a/app/views/notifier/new_request.text.haml b/app/views/notifier/new_request.text.haml index 1c1505f74..8ab5bc570 100644 --- a/app/views/notifier/new_request.text.haml +++ b/app/views/notifier/new_request.text.haml @@ -3,7 +3,7 @@ just sent you a friend request on Diaspora* You should really think about checking it out. -= "sign in here: #{new_user_session_path}" += "sign in here: #{new_user_session_url}" love, the diaspora email robot