diff options
-rw-r--r-- | app/controllers/api_controller.rb | 36 | ||||
-rw-r--r-- | app/models/info_request.rb | 4 | ||||
-rw-r--r-- | app/models/request_mailer.rb | 11 |
3 files changed, 21 insertions, 30 deletions
diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb index 5661819b9..c1156845d 100644 --- a/app/controllers/api_controller.rb +++ b/app/controllers/api_controller.rb @@ -136,40 +136,20 @@ class ApiController < ApplicationController ) else # In the 'response' direction, i.e. what we (Alaveteli) regard as incoming - - raw_email = RawEmail.new - incoming_message = IncomingMessage.new( - :info_request => request, - :raw_email => raw_email, - :sent_at => sent_at - ) - raw_email.incoming_message = incoming_message - raw_email.save! - raw_email.data = "From external\nFrom: <none@example.org>\nTo: <none@example.org>\nDate: #{sent_at.rfc2822}\nSubject: Response\n\n" + body - - request.incoming_messages << incoming_message - request.save! - - attachments.each_with_index do |attachment, i| + attachment_hashes = [] + (attachments || []).each_with_index do |attachment, i| filename = File.basename(attachment.original_filename) body = attachment.read content_type = AlaveteliFileTypes.filename_and_content_to_mimetype(filename, body) || 'application/octet-stream' - - a = FoiAttachment.new( - :incoming_message_id => incoming_message.id, - :filename => filename, - :content_type => content_type + attachment_hashes.push( + :content_type => content_type, + :body => body, + :filename => filename ) - a.body = body - a.save! end - request.log_event("response", - :api => true, - :email => nil, - :incoming_message_id => incoming_message.id, - :smtp_message_id => nil - ) + mail = RequestMailer.create_external_response(request, body, sent_at, attachment_hashes) + request.receive(mail, mail.encoded, true) end head :no_content diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 6cefc3dab..336d51cee 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -269,7 +269,7 @@ public return self.magic_email("request-") end def incoming_name_and_email - return TMail::Address.address_from_name_and_email(self.user.name, self.incoming_email).to_s + return TMail::Address.address_from_name_and_email(self.user_name, self.incoming_email).to_s end # Subject lines for emails about the request @@ -490,7 +490,7 @@ public self.save! end self.info_request_events.each { |event| event.xapian_mark_needs_index } # for the "waiting_classification" index - RequestMailer.deliver_new_response(self, incoming_message) + RequestMailer.deliver_new_response(self, incoming_message) if !is_external? end diff --git a/app/models/request_mailer.rb b/app/models/request_mailer.rb index 8764b827b..f7cc088c1 100644 --- a/app/models/request_mailer.rb +++ b/app/models/request_mailer.rb @@ -28,6 +28,17 @@ class RequestMailer < ApplicationMailer :filename => attachment_name end end + + # Used when a response is uploaded using the API + def external_response(info_request, body, sent_at, attachments) + @from = blackhole_email + @recipients = info_request.incoming_name_and_email + @body = { :body => body } + + attachments.each do |attachment_hash| + attachment attachment_hash + end + end # Incoming message arrived for a request, but new responses have been stopped. def stopped_responses(info_request, email, raw_email_data) |