From d2e3be7262ac9da9888689058f08dfdf279c864d Mon Sep 17 00:00:00 2001
From: maxwell <maxwell@joindiaspora.com>
Date: Fri, 22 Oct 2010 00:13:18 -0700
Subject: [PATCH] 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 56f0f35554..8ce7995b42 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 => "<h1>#{@receiver.person.profile.first_name}This is HTML</h1>" }
-    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 daa54c0ebf..54cee3ddde 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 0000000000..1c1505f74b
--- /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 0000000000..e94ea51339
--- /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 0000000000..efc89596fc
--- /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 570ce35445..c7809b39d2 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 c66fa4f869..8d0b76aee2 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 97316890da..394733cea0 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 67235f447f..36a586647a 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
-- 
GitLab