change message gateway to save received email as attachment to the todo

This commit is contained in:
Reinier Balt 2014-07-03 23:24:29 +02:00
parent 76de5bf2b3
commit 4d7c453ca7
4 changed files with 51 additions and 2 deletions

View file

@ -1,3 +1,5 @@
require 'tempfile'
class MessageGateway < ActionMailer::Base
def receive(email)
@ -10,8 +12,26 @@ class MessageGateway < ActionMailer::Base
todo_builder = TodoFromRichMessage.new(user, context.id, todo_params[:description], todo_params[:notes])
todo = todo_builder.construct
todo.save!
Rails.logger.info "Saved email as todo for user #{user.login} in context #{context.name}"
saved = todo.save!
if saved
Rails.logger.info "Saved email as todo for user #{user.login} in context #{context.name}"
attachment = todo.attachments.build
tmp = Tempfile.new(['attachment', '.eml'], cr_newline: false)
tmp.write email.raw_source.gsub(/\r\n?/, "\n") # replace \r with \n
Rails.logger.info "Saved received email to #{tmp.path}"
attachment.file = tmp
tmp.close
saved = attachment.save!
tmp.unlink
if saved
Rails.logger.info "Saved email as todo for user #{user.login} in context #{context.name}"
end
end
todo
end

View file

@ -0,0 +1,10 @@
#config/initilizers/paperclip.rb
require 'paperclip/media_type_spoof_detector'
module Paperclip
class MediaTypeSpoofDetector
def spoofed?
false
end
end
end

View file

@ -90,6 +90,7 @@ Rails.application.routes.draw do
get 'convert_to_project' # TODO: convert to PUT/POST
delete 'remove_predecessor' # TODO: convert to PUT/POST
post 'change_context'
get 'attachment'
end
collection do
get 'done'

View file

@ -83,4 +83,22 @@ class MessageGatewayTest < ActiveSupport::TestCase
assert_not_nil(invalid_context_todo)
assert_equal(@inbox, invalid_context_todo.context)
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