mirror of
https://github.com/TracksApp/tracks.git
synced 2025-12-23 02:30:12 +01:00
enhance MessageGateway to save email as attachment
This commit is contained in:
parent
f2c6c2d3af
commit
5cabeca155
2 changed files with 57 additions and 13 deletions
|
|
@ -10,13 +10,41 @@ class MessageGateway < ActionMailer::Base
|
||||||
|
|
||||||
todo_builder = TodoFromRichMessage.new(user, context.id, todo_params[:description], todo_params[:notes])
|
todo_builder = TodoFromRichMessage.new(user, context.id, todo_params[:description], todo_params[:notes])
|
||||||
todo = todo_builder.construct
|
todo = todo_builder.construct
|
||||||
todo.save!
|
|
||||||
Rails.logger.info "Saved email as todo for user #{user.login} in context #{context.name}"
|
if todo.save!
|
||||||
|
Rails.logger.info "Saved email as todo for user #{user.login} in context #{context.name}"
|
||||||
|
|
||||||
|
if attach_email_to_todo(todo, email)
|
||||||
|
Rails.logger.info "Saved email as attachment to todo for user #{user.login} in context #{context.name}"
|
||||||
|
end
|
||||||
|
end
|
||||||
todo
|
todo
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def attach_email_to_todo(todo, email)
|
||||||
|
attachment = todo.attachments.build
|
||||||
|
|
||||||
|
# create temp file
|
||||||
|
tmp = Tempfile.new(['attachment', '.eml'], {universal_newline: true})
|
||||||
|
tmp.write email.raw_source.gsub(/\r/, "")
|
||||||
|
|
||||||
|
# add temp file to attachment. paperclip will copy the file to the right location
|
||||||
|
Rails.logger.info "Saved received email to #{tmp.path}"
|
||||||
|
attachment.file = tmp
|
||||||
|
tmp.close
|
||||||
|
saved = attachment.save!
|
||||||
|
|
||||||
|
# enable write permissions on group, since MessageGateway could be run under different
|
||||||
|
# user than Tracks (i.e. apache versus mail)
|
||||||
|
dir = File.open(File.dirname(attachment.file.path))
|
||||||
|
dir.chmod(0770)
|
||||||
|
|
||||||
|
# delete temp file
|
||||||
|
tmp.unlink
|
||||||
|
end
|
||||||
|
|
||||||
def get_todo_params(email)
|
def get_todo_params(email)
|
||||||
params = {}
|
params = {}
|
||||||
|
|
||||||
|
|
@ -111,5 +139,4 @@ class MessageGateway < ActionMailer::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -13,18 +13,18 @@ class MessageGatewayTest < ActiveSupport::TestCase
|
||||||
|
|
||||||
def test_sms_with_no_subject
|
def test_sms_with_no_subject
|
||||||
todo_count = Todo.count
|
todo_count = Todo.count
|
||||||
|
|
||||||
load_message('sample_sms.txt')
|
load_message('sample_sms.txt')
|
||||||
# assert some stuff about it being created
|
# assert some stuff about it being created
|
||||||
assert_equal(todo_count+1, Todo.count)
|
assert_equal(todo_count+1, Todo.count)
|
||||||
|
|
||||||
message_todo = Todo.where(:description => "message_content").first
|
message_todo = Todo.where(:description => "message_content").first
|
||||||
assert_not_nil(message_todo)
|
assert_not_nil(message_todo)
|
||||||
|
|
||||||
assert_equal(@inbox, message_todo.context)
|
assert_equal(@inbox, message_todo.context)
|
||||||
assert_equal(@user, message_todo.user)
|
assert_equal(@user, message_todo.user)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_mms_with_subject
|
def test_mms_with_subject
|
||||||
todo_count = Todo.count
|
todo_count = Todo.count
|
||||||
|
|
||||||
|
|
@ -40,7 +40,7 @@ class MessageGatewayTest < ActiveSupport::TestCase
|
||||||
assert_equal(@user, message_todo.user)
|
assert_equal(@user, message_todo.user)
|
||||||
assert_equal("This is the message body", message_todo.notes)
|
assert_equal("This is the message body", message_todo.notes)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_email_with_winmail_dat
|
def test_email_with_winmail_dat
|
||||||
todo_count = Todo.count
|
todo_count = Todo.count
|
||||||
|
|
||||||
|
|
@ -58,7 +58,7 @@ class MessageGatewayTest < ActiveSupport::TestCase
|
||||||
# assert some stuff about it being created
|
# assert some stuff about it being created
|
||||||
assert_equal(todo_count+1, Todo.count)
|
assert_equal(todo_count+1, Todo.count)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_no_user
|
def test_no_user
|
||||||
todo_count = Todo.count
|
todo_count = Todo.count
|
||||||
badmessage = File.read(File.join(Rails.root, 'test', 'fixtures', 'sample_sms.txt'))
|
badmessage = File.read(File.join(Rails.root, 'test', 'fixtures', 'sample_sms.txt'))
|
||||||
|
|
@ -66,21 +66,38 @@ class MessageGatewayTest < ActiveSupport::TestCase
|
||||||
MessageGateway.receive(badmessage)
|
MessageGateway.receive(badmessage)
|
||||||
assert_equal(todo_count, Todo.count)
|
assert_equal(todo_count, Todo.count)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_direct_to_context
|
def test_direct_to_context
|
||||||
message = File.read(File.join(Rails.root, 'test', 'fixtures', 'sample_sms.txt'))
|
message = File.read(File.join(Rails.root, 'test', 'fixtures', 'sample_sms.txt'))
|
||||||
|
|
||||||
valid_context_msg = message.gsub('message_content', 'this is a task @ anothercontext')
|
valid_context_msg = message.gsub('message_content', 'this is a task @ anothercontext')
|
||||||
invalid_context_msg = message.gsub('message_content', 'this is also a task @ notacontext')
|
invalid_context_msg = message.gsub('message_content', 'this is also a task @ notacontext')
|
||||||
|
|
||||||
MessageGateway.receive(valid_context_msg)
|
MessageGateway.receive(valid_context_msg)
|
||||||
valid_context_todo = Todo.where(:description => "this is a task").first
|
valid_context_todo = Todo.where(:description => "this is a task").first
|
||||||
assert_not_nil(valid_context_todo)
|
assert_not_nil(valid_context_todo)
|
||||||
assert_equal(contexts(:anothercontext), valid_context_todo.context)
|
assert_equal(contexts(:anothercontext), valid_context_todo.context)
|
||||||
|
|
||||||
MessageGateway.receive(invalid_context_msg)
|
MessageGateway.receive(invalid_context_msg)
|
||||||
invalid_context_todo = Todo.where(:description => 'this is also a task').first
|
invalid_context_todo = Todo.where(:description => 'this is also a task').first
|
||||||
assert_not_nil(invalid_context_todo)
|
assert_not_nil(invalid_context_todo)
|
||||||
assert_equal(@inbox, invalid_context_todo.context)
|
assert_equal(@inbox, invalid_context_todo.context)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_receiving_email_adds_attachment
|
||||||
|
attachment_count = Attachment.count
|
||||||
|
|
||||||
|
load_message('sample_mms.txt')
|
||||||
|
|
||||||
|
message_todo = Todo.where(:description => "This is the subject").first
|
||||||
|
assert_not_nil(message_todo)
|
||||||
|
|
||||||
|
assert_equal attachment_count+1, Attachment.count
|
||||||
|
assert_equal 1,message_todo.attachments.count
|
||||||
|
|
||||||
|
orig = File.read(File.join(Rails.root, 'test', 'fixtures', 'sample_mms.txt'))
|
||||||
|
attachment = File.read(message_todo.attachments.first.file.path)
|
||||||
|
|
||||||
|
assert_equal orig, attachment
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue