diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/info_request.rb | 18 | ||||
-rw-r--r-- | app/models/info_request_event.rb | 32 | ||||
-rw-r--r-- | app/models/request_mailer.rb | 51 | ||||
-rw-r--r-- | app/models/user_info_request_sent_alert.rb | 5 |
4 files changed, 95 insertions, 11 deletions
diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 3c0e97107..ef6f4bc4d 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -22,7 +22,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.71 2008-03-21 14:45:38 francis Exp $ +# $Id: info_request.rb,v 1.72 2008-03-24 09:35:23 francis Exp $ require 'digest/sha1' @@ -356,17 +356,27 @@ public end # The last response is the default one people might want to reply to - def get_last_response + def get_last_response_event_id events = self.info_request_events.find(:all, :order => "created_at") events.reverse.each do |e| if e.event_type == 'response' - id = e.params[:incoming_message_id].to_i - return IncomingMessage.find(id) + return e.id end end return nil end + # The last response is the default one people might want to reply to + def get_last_response + event_id = self.get_last_response_event_id + if event_id.nil? + return nil + end + e = self.info_request_events.find(event_id) + incoming_message_id = e.params[:incoming_message_id].to_i + return IncomingMessage.find(incoming_message_id) + end + # The last outgoing message def get_last_outgoing_event events = self.info_request_events.find(:all, :order => "created_at") diff --git a/app/models/info_request_event.rb b/app/models/info_request_event.rb index fc4588596..a6f67c4fd 100644 --- a/app/models/info_request_event.rb +++ b/app/models/info_request_event.rb @@ -16,12 +16,14 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: info_request_event.rb,v 1.24 2008-03-21 14:45:38 francis Exp $ +# $Id: info_request_event.rb,v 1.25 2008-03-24 09:35:23 francis Exp $ class InfoRequestEvent < ActiveRecord::Base belongs_to :info_request validates_presence_of :info_request + belongs_to :info_request_event_id + validates_presence_of :event_type validates_inclusion_of :event_type, :in => [ 'sent', @@ -53,6 +55,34 @@ class InfoRequestEvent < ActiveRecord::Base YAML.load(self.params_yaml) end + # Find related incoming message + # XXX search for the find below and call this function more instead + def incoming_message + if not ['response'].include?(self.event_type) + raise "only call incoming_message for response events" + end + + if not self.params[:incoming_message_id] + raise "internal error, no incoming message id for response event" + end + + return IncomingMessage.find(self.params[:incoming_message_id].to_i) + end + + # Find related outgoing message + # XXX search for the find below and call this function more instead + def outgoing_message + if not [ 'edit_outgoing', 'sent', 'resent', 'followup_sent' ].include?(self.event_type) + raise "only call outgoing_message for appropriate event types" + end + + if not self.params[:outgoing_message_id] + raise "internal error, no outgoing message id for event type which expected one" + end + + return OutgoingMessage.find(self.params[:outgoing_message_id].to_i) + end + end diff --git a/app/models/request_mailer.rb b/app/models/request_mailer.rb index 8300422bb..58209e3b3 100644 --- a/app/models/request_mailer.rb +++ b/app/models/request_mailer.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: request_mailer.rb,v 1.27 2008-03-21 14:04:29 francis Exp $ +# $Id: request_mailer.rb,v 1.28 2008-03-24 09:35:23 francis Exp $ class RequestMailer < ApplicationMailer @@ -87,6 +87,21 @@ class RequestMailer < ApplicationMailer @body = { :info_request => info_request, :url => url } end + # Tell the requester that they need to say if the new response + # contains info or not + def new_response_reminder_alert(info_request, incoming_message) + post_redirect = PostRedirect.new( + :uri => describe_state_url(:id => info_request.id), + :user_id => info_request.user.id) + post_redirect.save! + url = confirm_url(:email_token => post_redirect.email_token) + + @from = contact_from_name_and_email + @recipients = info_request.user.name_and_email + @subject = "Did your recent FOI response contain information? - " + info_request.title + @body = { :incoming_message => incoming_message, :info_request => info_request, :url => url } + end + # Class function, called by script/mailin with all incoming responses. # [ This is a copy (Monkeypatch!) of function from action_mailer/base.rb, @@ -121,7 +136,7 @@ class RequestMailer < ApplicationMailer # Send email alerts for overdue requests def self.alert_overdue_requests() - #puts "alert_overdue_requests" + #STDERR.puts "alert_overdue_requests" info_requests = InfoRequest.find(:all, :conditions => [ "described_state = 'waiting_response' and not awaiting_description" ], :include => [ :user ] ) for info_request in info_requests # Only overdue requests @@ -130,19 +145,47 @@ class RequestMailer < ApplicationMailer sent_already = UserInfoRequestSentAlert.find(:first, :conditions => [ "alert_type = 'overdue_1' and user_id = ? and info_request_id = ?", info_request.user_id, info_request.id]) if sent_already.nil? # Alert not yet sent for this user - puts "sending overdue alert to info_request " + info_request.id.to_s + " user " + info_request.user_id.to_s + STDERR.puts "sending overdue alert to info_request " + info_request.id.to_s + " user " + info_request.user_id.to_s store_sent = UserInfoRequestSentAlert.new store_sent.info_request = info_request store_sent.user = info_request.user store_sent.alert_type = 'overdue_1' RequestMailer.deliver_overdue_alert(info_request, info_request.user) store_sent.save! - #puts "sent " + info_request.user.email + #STDERR.puts "sent " + info_request.user.email end end end + end + # Send email alerts for new responses which haven't been + # classified. Goes out 3 days after last update of event. + def self.alert_new_response_reminders() + #STDERR.puts "alert_new_response_reminders" + info_requests = InfoRequest.find(:all, :conditions => [ "awaiting_description and info_requests.updated_at < ?", Time.now() - 3.days ], :include => [ :user ], :order => "info_requests.id" ) + for info_request in info_requests + alert_event_id = info_request.get_last_response_event_id + last_response_message = info_request.get_last_response + if alert_event_id.nil? + raise "internal error, no last response while making alert new response reminder, request id " + info_request.id.to_s + end + # To the user who created the request + sent_already = UserInfoRequestSentAlert.find(:first, :conditions => [ "alert_type = 'new_response_reminder_1' and user_id = ? and info_request_id = ? and info_request_event_id = ?", info_request.user_id, info_request.id, alert_event_id]) + if sent_already.nil? + # Alert not yet sent for this user + STDERR.puts "sending new response reminder alert to info_request " + info_request.id.to_s + " user " + info_request.user_id.to_s + " event " + alert_event_id.to_s + store_sent = UserInfoRequestSentAlert.new + store_sent.info_request = info_request + store_sent.user = info_request.user + store_sent.alert_type = 'new_response_reminder_1' + store_sent.info_request_event_id = alert_event_id + RequestMailer.deliver_new_response_reminder_alert(info_request, last_response_message) + store_sent.save! + #STDERR.puts "sent " + info_request.user.email + end + end end + end diff --git a/app/models/user_info_request_sent_alert.rb b/app/models/user_info_request_sent_alert.rb index f58f6a58c..da4aeb94b 100644 --- a/app/models/user_info_request_sent_alert.rb +++ b/app/models/user_info_request_sent_alert.rb @@ -16,14 +16,15 @@ # Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: user_info_request_sent_alert.rb,v 1.7 2008-03-21 14:45:38 francis Exp $ +# $Id: user_info_request_sent_alert.rb,v 1.8 2008-03-24 09:35:23 francis Exp $ class UserInfoRequestSentAlert < ActiveRecord::Base belongs_to :user belongs_to :info_request validates_inclusion_of :alert_type, :in => [ - 'overdue_1' # tell user that info request has become overdue + 'overdue_1', # tell user that info request has become overdue + 'new_response_reminder_1' # reminder user to classify the recent response ] end |