diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/models/incoming_message.rb | 17 | ||||
-rw-r--r-- | app/models/info_request.rb | 13 | ||||
-rw-r--r-- | app/models/request_mailer.rb | 81 |
3 files changed, 72 insertions, 39 deletions
diff --git a/app/models/incoming_message.rb b/app/models/incoming_message.rb new file mode 100644 index 000000000..f637cd005 --- /dev/null +++ b/app/models/incoming_message.rb @@ -0,0 +1,17 @@ +# models/incoming_message.rb: +# A message from really anybody to be logged with a request. e.g. +# A response from the public body. +# +# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. +# Email: francis@mysociety.org; WWW: http://www.mysociety.org/ +# +# $Id: incoming_message.rb,v 1.1 2007-10-29 18:11:34 francis Exp $ + +class IncomingMessage < ActiveRecord::Base + belongs_to :info_request + validates_presence_of :info_request + + validates_presence_of :raw_data +end + + diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 763e751be..4752a396f 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -4,7 +4,7 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: info_request.rb,v 1.7 2007-10-26 18:00:26 francis Exp $ +# $Id: info_request.rb,v 1.8 2007-10-29 18:11:34 francis Exp $ require 'digest/sha1' @@ -12,12 +12,13 @@ class InfoRequest < ActiveRecord::Base validates_presence_of :title belongs_to :user -# validates_presence_of :user_id + validates_presence_of :user_id belongs_to :public_body validates_presence_of :public_body_id has_many :outgoing_messages + has_many :incoming_messages # Email which public body should use to respond to request. This is in # the format PREFIXrequest-ID-HASH@DOMAIN. Here ID is the id of the @@ -48,9 +49,13 @@ class InfoRequest < ActiveRecord::Base end # A new incoming email to this request - def receive(email) - raise "TBD" + def receive(email, raw_email) + incoming_message = IncomingMessage.new + incoming_message.raw_data = raw_email + incoming_message.info_request = self + incoming_message.save end + end diff --git a/app/models/request_mailer.rb b/app/models/request_mailer.rb index 0825e4b03..66e3bd434 100644 --- a/app/models/request_mailer.rb +++ b/app/models/request_mailer.rb @@ -4,47 +4,58 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: request_mailer.rb,v 1.3 2007-10-26 18:00:26 francis Exp $ +# $Id: request_mailer.rb,v 1.4 2007-10-29 18:11:34 francis Exp $ class RequestMailer < ActionMailer::Base - def initial_request(info_request, outgoing_message) - @from = info_request.incoming_email - if MySociety::Config.getbool("STAGING_SITE", 1) - @recipients = @from - else - @recipients = info_request.public_body.request_email - end - @subject = 'Freedom of Information Request - ' + info_request.title - @body = {:info_request => info_request, :outgoing_message => outgoing_message} - end - - def receive(email) - # Find which info requests the email is for - info_requests = [] - for address in (email.to || []) + (email.cc || []) - info_request = InfoRequest.find_by_incoming_email(address) - info_requests.push(info_request) if info_request + def initial_request(info_request, outgoing_message) + @from = info_request.incoming_email + if MySociety::Config.getbool("STAGING_SITE", 1) + @recipients = @from + else + @recipients = info_request.public_body.request_email + end + @subject = 'Freedom of Information Request - ' + info_request.title + @body = {:info_request => info_request, :outgoing_message => outgoing_message} end - # Deal with each on - for info_request in info_requests - info_request.receive(email) + # Copy of function from action_mailer/base.rb, which passes the + # raw_email to the member function, as we want to record it. + # script/mailin calls this function. + def self.receive(raw_email) + logger.info "Received mail:\n #{raw_email}" unless logger.nil? + mail = TMail::Mail.parse(raw_email) + mail.base64_decode + new.receive(mail, raw_email) end - # email.cc -# page = Page.find_by_address(email.to.first) -# page.emails.create( -# :subject => email.subject, :body => email.body -# ) -# -# if email.has_attachments? -# for attachment in email.attachments -# page.attachments.create({ -# :file => attachment, :description => email.subject -# }) -# end -# end - end + + def receive(email, raw_email) + # Find which info requests the email is for + info_requests = [] + for address in (email.to || []) + (email.cc || []) + info_request = InfoRequest.find_by_incoming_email(address) + info_requests.push(info_request) if info_request + end + + # Deal with each on + for info_request in info_requests + info_request.receive(email, raw_email) + end + + # email.cc + # page = Page.find_by_address(email.to.first) + # page.emails.create( + # :subject => email.subject, :body => email.body + # ) + # + # if email.has_attachments? + # for attachment in email.attachments + # page.attachments.create({ + # :file => attachment, :description => email.subject + # }) + # end + # end + end end |