From dbeb79321447c1d4c0cdd1034b9ff04d7ff3d005 Mon Sep 17 00:00:00 2001 From: Eric Allen Date: Sun, 29 Mar 2009 19:44:43 -0400 Subject: [PATCH] Support dispatching emailed-in todos based on To: field as well as From: field. Setting configured in site.yml. --- app/models/message_gateway.rb | 11 +++++++++-- config/site.yml.tmpl | 5 +++++ spec/models/message_gateway_spec.rb | 29 +++++++++++++++++++++++++++++ test/fixtures/sample_email.txt | 12 ++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 spec/models/message_gateway_spec.rb create mode 100644 test/fixtures/sample_email.txt diff --git a/app/models/message_gateway.rb b/app/models/message_gateway.rb index 501650e0..af67a782 100644 --- a/app/models/message_gateway.rb +++ b/app/models/message_gateway.rb @@ -3,9 +3,16 @@ class MessageGateway < ActionMailer::Base extend ActionView::Helpers::SanitizeHelper::ClassMethods def receive(email) - user = User.find(:first, :include => [:preference], :conditions => ["preferences.sms_email = ?", email.from[0].strip]) + address = '' + if SITE_CONFIG['email_dispatch'] == 'to' + address = email.to[0] + else + address = email.from[0] + end + + user = User.find(:first, :include => [:preference], :conditions => ["preferences.sms_email = ?", address.strip]) if user.nil? - user = User.find(:first, :include => [:preference], :conditions => ["preferences.sms_email = ?", email.from[0].strip[1,100]]) + user = User.find(:first, :include => [:preference], :conditions => ["preferences.sms_email = ?", address.strip[1,100]]) end return if user.nil? context = user.prefs.sms_context diff --git a/config/site.yml.tmpl b/config/site.yml.tmpl index b39ab11c..61745f6b 100644 --- a/config/site.yml.tmpl +++ b/config/site.yml.tmpl @@ -20,6 +20,11 @@ time_zone: "UTC" secure_cookies: false +# Uncomment if you want to dispatch todos that come from email based on the To: address +# rather than the From: address. +# email_dispatch: 'to' + + # Set this to the subdirectory you're hosting tracks in and uncomment if applicable # NOTE: you will also need to set up your web server to deal with the relative # URL. Mongrel, for example, has a --prefix option. diff --git a/spec/models/message_gateway_spec.rb b/spec/models/message_gateway_spec.rb new file mode 100644 index 00000000..2f0f5efc --- /dev/null +++ b/spec/models/message_gateway_spec.rb @@ -0,0 +1,29 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +describe MessageGateway do + before :each do + todo = mock_model(Todo, :description= => nil, :notes= => nil, :context_id= => nil, :save! => nil) + + @user = mock_model(User, + :prefs => mock_model(Preference, :sms_context => mock_model(Context)), + :todos => mock('Todo collection', :find => nil, :build => todo), + :contexts => mock('Context collection', :exists? => true, :find => nil)) + + User.stub!(:find).and_return(@user) + end + + def load_message(filename) + MessageGateway.receive(File.read(File.join(RAILS_ROOT, 'test', 'fixtures', filename))) + end + + + it "should dispatch on From: or To: according to site.yml" do + SITE_CONFIG['email_dispatch'] = 'from' + User.should_receive(:find).with(:first, :include => [:preference], :conditions => ["preferences.sms_email = ?", '5555555555@tmomail.net']) + load_message('sample_email.txt') + + SITE_CONFIG['email_dispatch'] = 'to' + User.should_receive(:find).with(:first, :include => [:preference], :conditions => ["preferences.sms_email = ?", 'gtd@tracks.com']) + load_message('sample_email.txt') + end +end diff --git a/test/fixtures/sample_email.txt b/test/fixtures/sample_email.txt new file mode 100644 index 00000000..8167d0b9 --- /dev/null +++ b/test/fixtures/sample_email.txt @@ -0,0 +1,12 @@ +Return-Path: <5555555555@tmomail.net> +Date: Tue, 3 Jun 2008 23:11:26 -0400 +From: 5555555555@tmomail.net +To: gtd@tracks.com +Message-ID: <6100602.65827251212549086388.JavaMail.imb@mgwatl02.cns.mms.com> +Subject: This is the subject +MIME-Version: 1.0 +Content-Type: text/plain;charset=utf-8 +Content-Transfer-Encoding: 7bit +Importance: Normal + +message_content \ No newline at end of file