diff options
-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 | ||||
-rw-r--r-- | db/migrate/013_create_incoming_messages.rb | 14 | ||||
-rw-r--r-- | db/schema.rb | 9 | ||||
-rw-r--r-- | test/fixtures/incoming_messages.yml | 13 | ||||
-rw-r--r-- | test/unit/incoming_message_test.rb | 10 |
7 files changed, 117 insertions, 40 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 diff --git a/db/migrate/013_create_incoming_messages.rb b/db/migrate/013_create_incoming_messages.rb new file mode 100644 index 000000000..c747fcb8c --- /dev/null +++ b/db/migrate/013_create_incoming_messages.rb @@ -0,0 +1,14 @@ +class CreateIncomingMessages < ActiveRecord::Migration + def self.up + create_table :incoming_messages do |t| + t.column :info_request_id, :integer + t.column :raw_data, :text + t.column :created_at, :datetime + t.column :updated_at, :datetime + end + end + + def self.down + drop_table :incoming_messages + end +end diff --git a/db/schema.rb b/db/schema.rb index 2a8a7978b..8350f3943 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -2,7 +2,14 @@ # migrations feature of ActiveRecord to incrementally modify your database, and # then regenerate this schema definition. -ActiveRecord::Schema.define(:version => 12) do +ActiveRecord::Schema.define(:version => 13) do + + create_table "incoming_messages", :force => true do |t| + t.column "info_request_id", :integer + t.column "raw_data", :text + t.column "created_at", :datetime + t.column "updated_at", :datetime + end create_table "info_requests", :force => true do |t| t.column "title", :text diff --git a/test/fixtures/incoming_messages.yml b/test/fixtures/incoming_messages.yml new file mode 100644 index 000000000..fe1186b0b --- /dev/null +++ b/test/fixtures/incoming_messages.yml @@ -0,0 +1,13 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +one: + id: 1 + info_request_id: 1 + raw_data: MyText + created_at: 2007-10-29 17:22:21 + udpated_at: 2007-10-29 17:22:21 +two: + id: 2 + info_request_id: 1 + raw_data: MyText + created_at: 2007-10-29 17:22:21 + udpated_at: 2007-10-29 17:22:21 diff --git a/test/unit/incoming_message_test.rb b/test/unit/incoming_message_test.rb new file mode 100644 index 000000000..3a320cf93 --- /dev/null +++ b/test/unit/incoming_message_test.rb @@ -0,0 +1,10 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class IncomingMessageTest < Test::Unit::TestCase + fixtures :incoming_messages + + # Replace this with your real tests. + def test_truth + assert true + end +end |