diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/info_request.rb | 34 | ||||
-rw-r--r-- | app/models/request_mailer.rb | 68 | ||||
-rw-r--r-- | app/models/user_info_request_sent_alert.rb | 3 |
3 files changed, 89 insertions, 16 deletions
diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 8e303eed2..5cc4afabf 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -23,7 +23,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.131 2008-08-26 16:03:36 francis Exp $ +# $Id: info_request.rb,v 1.132 2008-08-29 09:44:31 francis Exp $ require 'digest/sha1' require File.join(File.dirname(__FILE__),'../../vendor/plugins/acts_as_xapian/lib/acts_as_xapian') @@ -496,6 +496,16 @@ public info_request_event.save! end + # The last comment made, for alerts + def get_last_comment_event + for e in self.info_request_events.reverse + if e.event_type == 'comment' + return e + end + end + return nil + end + # The last response is the default one people might want to reply to def get_last_response_event_id for e in self.info_request_events.reverse @@ -505,25 +515,21 @@ public end return nil end - - # The last response is the default one people might want to reply to def get_last_response_event - info_request_event_id = get_last_response_event_id - if info_request_event_id.nil? - return nil - else - return InfoRequestEvent.find(info_request_event_id) + for e in self.info_request_events.reverse + if e.event_type == 'response' + return e + 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? + last_response_event = self.get_last_response_event + if last_response_event.nil? return nil + else + return last_response_event.incoming_message end - e = self.info_request_events.find(event_id) - return e.incoming_message end # The last outgoing message diff --git a/app/models/request_mailer.rb b/app/models/request_mailer.rb index 5e663ce90..72c826eac 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.43 2008-08-26 23:43:42 francis Exp $ +# $Id: request_mailer.rb,v 1.44 2008-08-29 09:44:31 francis Exp $ class RequestMailer < ApplicationMailer @@ -125,6 +125,20 @@ class RequestMailer < ApplicationMailer @body = { :incoming_message => incoming_message, :info_request => info_request, :url => url } end + # Tell the requester that they need to clarify their request + def comment_on_alert(info_request, comment) + @from = contact_from_name_and_email + @recipients = info_request.user.name_and_email + @subject = "Somebody added a note to your FOI request - " + info_request.title + @body = { :comment => comment, :info_request => info_request, :url => main_url(comment_url(comment)) } + end + def comment_on_alert_plural(info_request, count, earliest_unalerted_comment) + @from = contact_from_name_and_email + @recipients = info_request.user.name_and_email + @subject = "Some notes have been added to your FOI request - " + info_request.title + @body = { :count => @count, :info_request => info_request, :url => main_url(comment_url(earliest_unalerted_comment)) } + end + # Class function, called by script/mailin with all incoming responses. # [ This is a copy (Monkeypatch!) of function from action_mailer/base.rb, @@ -245,6 +259,58 @@ class RequestMailer < ApplicationMailer end end + # Send email alert to request submitter for new comments on the request. + def self.alert_comment_on_request() + #STDERR.puts "alert_comment_on_request" + # We only check comments made in the last month - this means if the + # cron jobs broke fro more than a month events would be lost, but no + # matter. I suspect the performance gain will be needed (with an index on updated_at) + info_requests = InfoRequest.find(:all, :conditions => [ "(select count(*) from info_request_events where event_type = 'comment' and info_request_events.info_request_id = info_requests.id and updated_at > ?) > 0", Time.now() - 1.month ], :include => [ { :info_request_events => :user_info_request_sent_alerts } ], :order => "info_requests.id" ) + for info_request in info_requests + #STDERR.puts "considering request " + info_request.id.to_s + + # Find the last comment, which we use as id to mark alert done + last_comment_event = info_request.get_last_comment_event + raise "expected comment event but got none" if last_comment_event.nil? + + # Count number of new comments to alert on + earliest_unalerted_comment_event = nil + count = 0 + for e in info_request.info_request_events.reverse + if e.event_type == 'comment' + alerted_for = e.user_info_request_sent_alerts.find(:first, :conditions => [ "alert_type = 'comment_1' and user_id = ?", info_request.user_id]) + if alerted_for.nil? + count = count + 1 + earliest_unalerted_comment_event = e + else + break + end + end + end + + # Alert needs sending if there are new comments + if count > 0 + store_sent = UserInfoRequestSentAlert.new + store_sent.info_request = info_request + store_sent.user = info_request.user + store_sent.alert_type = 'comment_1' + store_sent.info_request_event_id = last_comment_event.id + if count > 1 + STDERR.puts "sending multiple comment on request alert to info_request " + info_request.id.to_s + " user " + info_request.user_id.to_s + " count " + count.to_s + RequestMailer.deliver_comment_on_alert_plural(info_request, count, earliest_unalerted_comment_event.comment) + elsif count == 1 + STDERR.puts "sending comment on request alert to info_request " + info_request.id.to_s + " user " + info_request.user_id.to_s + " event " + last_comment_event.id.to_s + RequestMailer.deliver_comment_on_alert(info_request, last_comment_event.comment) + else + raise "internal error" + end + 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 af3c6e981..7ae76edd1 100644 --- a/app/models/user_info_request_sent_alert.rb +++ b/app/models/user_info_request_sent_alert.rb @@ -17,7 +17,7 @@ # 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.20 2008-08-09 15:19:01 francis Exp $ +# $Id: user_info_request_sent_alert.rb,v 1.21 2008-08-29 09:44:31 francis Exp $ class UserInfoRequestSentAlert < ActiveRecord::Base belongs_to :user @@ -28,6 +28,7 @@ class UserInfoRequestSentAlert < ActiveRecord::Base 'new_response_reminder_1', # reminder user to classify the recent response 'new_response_reminder_2', # repeat reminder user to classify the recent response 'not_clarified_1', # reminder that user has to explain part of the request + 'comment_1', # tell user that info request has a new comment ] end |