Support dispatching emailed-in todos based on To: field as well as From: field. Setting configured in site.yml.

This commit is contained in:
Eric Allen 2009-03-29 19:44:43 -04:00
parent eb9fc5391c
commit dbeb793214
4 changed files with 55 additions and 2 deletions

View file

@ -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

View file

@ -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.

View file

@ -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

12
test/fixtures/sample_email.txt vendored Normal file
View file

@ -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