diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/info_request.rb | 15 | ||||
-rw-r--r-- | app/models/post_redirect.rb | 2 | ||||
-rw-r--r-- | app/models/raw_email.rb | 36 | ||||
-rw-r--r-- | app/models/track_mailer.rb | 6 |
4 files changed, 44 insertions, 15 deletions
diff --git a/app/models/info_request.rb b/app/models/info_request.rb index dcef9e5b5..e6a520fd2 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -112,7 +112,7 @@ class InfoRequest < ActiveRecord::Base include InfoRequestCustomStates @@custom_states_loaded = true end - rescue NameError + rescue MissingSourceFile end # only check on create, so existing models with mixed case are allowed @@ -347,14 +347,7 @@ public # XXX this *should* also check outgoing message joined to is an initial # request (rather than follow up) def InfoRequest.find_by_existing_request(title, public_body_id, body) - # XXX can add other databases here which have regexp_replace - if ActiveRecord::Base.connection.adapter_name == "PostgreSQL" - # Exclude spaces from the body comparison using regexp_replace - return InfoRequest.find(:first, :conditions => [ "title = ? and public_body_id = ? and regexp_replace(outgoing_messages.body, '[[:space:]]', '', 'g') = regexp_replace(?, '[[:space:]]', '', 'g')", title, public_body_id, body ], :include => [ :outgoing_messages ] ) - else - # For other databases (e.g. SQLite) not the end of the world being space-sensitive for this check - return InfoRequest.find(:first, :conditions => [ "title = ? and public_body_id = ? and outgoing_messages.body = ?", title, public_body_id, body ], :include => [ :outgoing_messages ] ) - end + return InfoRequest.find(:first, :conditions => [ "title = ? and public_body_id = ? and outgoing_messages.body = ?", title, public_body_id, body ], :include => [ :outgoing_messages ] ) end def find_existing_outgoing_message(body) @@ -435,11 +428,11 @@ public ActiveRecord::Base.transaction do raw_email = RawEmail.new - raw_email.data = raw_email_data incoming_message.raw_email = raw_email incoming_message.info_request = self - raw_email.save! incoming_message.save! + raw_email.data = raw_email_data + raw_email.save! self.awaiting_description = true self.log_event("response", { :incoming_message_id => incoming_message.id }) diff --git a/app/models/post_redirect.rb b/app/models/post_redirect.rb index 1ea3f1543..b111d019d 100644 --- a/app/models/post_redirect.rb +++ b/app/models/post_redirect.rb @@ -88,7 +88,7 @@ class PostRedirect < ActiveRecord::Base # Called from cron job delete-old-things def self.delete_old_post_redirects - PostRedirect.delete_all "updated_at < (now() - interval '6 months')" + PostRedirect.delete_all "updated_at < (now() - interval '2 months')" end end diff --git a/app/models/raw_email.rb b/app/models/raw_email.rb index 0b70d1786..4e1a69456 100644 --- a/app/models/raw_email.rb +++ b/app/models/raw_email.rb @@ -6,7 +6,6 @@ # id :integer not null, primary key # data_text :text # data_binary :binary -# # models/raw_email.rb: # The fat part of models/incoming_message.rb @@ -21,17 +20,50 @@ class RawEmail < ActiveRecord::Base has_one :incoming_message + before_destroy :destroy_file_representation! # We keep the old data_text field (which is of type text) for backwards # compatibility. We use the new data_binary field because only it works # properly in recent versions of PostgreSQL (get seg faults escaping # some binary strings). + def directory + request_id = self.incoming_message.info_request.id.to_s + File.join(MySociety::Config.get('RAW_EMAILS_LOCATION', + 'files/raw_emails'), + request_id[0..2], request_id) + end + + def filepath + File.join(self.directory, self.incoming_message.id.to_s) + end + def data=(d) - write_attribute(:data_binary, d) + if !File.exists?(self.directory) + FileUtils.mkdir_p self.directory + end + File.open(self.filepath, "wb") { |file| + file.write d + } end def data + if !File.exists?(self.filepath) + dbdata + else + File.open(self.filepath, "rb" ).read + end + end + + def destroy_file_representation! + File.delete(self.filepath) + end + + def dbdata=(d) + write_attribute(:data_binary, d) + end + + def dbdata d = read_attribute(:data_binary) if !d.nil? return d diff --git a/app/models/track_mailer.rb b/app/models/track_mailer.rb index 1b37709b9..6901a834d 100644 --- a/app/models/track_mailer.rb +++ b/app/models/track_mailer.rb @@ -26,7 +26,11 @@ class TrackMailer < ApplicationMailer @body = { :user => user, :email_about_things => email_about_things, :unsubscribe_url => unsubscribe_url } end - # Send email alerts for tracked things. + # Send email alerts for tracked things. Never more than one email + # a day, nor about events which are more than a week old, nor + # events about which emails have been sent within the last two + # weeks. + # Useful query to run by hand to see how many alerts are due: # User.find(:all, :conditions => [ "last_daily_track_email < ?", Time.now - 1.day ]).size def self.alert_tracks |