diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/application_mailer.rb | 7 | ||||
-rw-r--r-- | app/models/track_mailer.rb | 96 | ||||
-rw-r--r-- | app/models/track_thing.rb | 39 | ||||
-rw-r--r-- | app/models/user.rb | 3 |
4 files changed, 132 insertions, 13 deletions
diff --git a/app/models/application_mailer.rb b/app/models/application_mailer.rb index d41c68dfc..32c6ced26 100644 --- a/app/models/application_mailer.rb +++ b/app/models/application_mailer.rb @@ -4,7 +4,7 @@ # Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: application_mailer.rb,v 1.6 2008-02-28 14:25:51 francis Exp $ +# $Id: application_mailer.rb,v 1.7 2008-04-03 15:29:51 francis Exp $ class ApplicationMailer < ActionMailer::Base # Include all the functions views get, as emails call similar things. @@ -17,5 +17,10 @@ class ApplicationMailer < ActionMailer::Base def contact_from_name_and_email "WhatDoTheyKnow <"+MySociety::Config.get("CONTACT_EMAIL", 'contact@localhost')+">" end + + # URL generating functions are needed by all controllers (for redirects), + # views (for links) and mailers (for use in emails), so include them into + # all of all. + include LinkToHelper end diff --git a/app/models/track_mailer.rb b/app/models/track_mailer.rb new file mode 100644 index 000000000..0a7800ee6 --- /dev/null +++ b/app/models/track_mailer.rb @@ -0,0 +1,96 @@ +# models/track_mailer.rb: +# Emails which go to users who are tracking things. +# +# Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved. +# Email: francis@mysociety.org; WWW: http://www.mysociety.org/ +# +# $Id: track_mailer.rb,v 1.1 2008-04-03 15:29:51 francis Exp $ + +class TrackMailer < ApplicationMailer + def event_digest(user, email_about_things) + post_redirect = PostRedirect.new( + :uri => main_url(user_url(user)), + :user_id => user.id) + post_redirect.save! + unsubscribe_url = confirm_url(:email_token => post_redirect.email_token) + + @from = contact_from_name_and_email + @recipients = user.name_and_email + @subject = "Your WhatDoTheyKnow.com email alert" + @body = { :user => user, :email_about_things => email_about_things, :unsubscribe_url => unsubscribe_url } + end + + # Send email alerts for tracked things + def self.alert_tracks + now = Time.now() + users = User.find(:all, :conditions => [ "last_daily_track_email < ?", now - 1.day ]) + for user in users + #STDERR.puts "user " + user.url_name + + email_about_things = [] + track_things = TrackThing.find(:all, :conditions => [ "tracking_user_id = ?", user.id ]) + for track_thing in track_things + #STDERR.puts " track " + track_thing.track_query + + # What have we alerted on already? + done_info_request_events = {} + for t in track_thing.track_things_sent_emails + if not t.info_request_event_id.nil? + done_info_request_events[t.info_request_event_id] = 1 + end + end + + # Query for things in this track + # XXX remember to update in controllers/application.rb also + solr_object = InfoRequestEvent.multi_solr_search(track_thing.track_query, :models => [ PublicBody, User ], + :limit => 100, :offset => 0, + :highlight => { + :prefix => '*', :suffix => '*', + :fragsize => 250, + :fields => ["solr_text_main", "title", # InfoRequestEvent + "name", "short_name", # PublicBody + "name" # User + ]}, :order => "created_at desc" + ) + + # Go through looking for unalerted things + alert_results = [] + for result in solr_object.results + if result.class.to_s == "InfoRequestEvent" + if not done_info_request_events.include?(result.id) and track_thing.created_at < result.created_at + # OK alert this one + alert_results.push(result) + end + else + raise "need to add other types to TrackMailer.alert_tracks" + end + end + # If there were more alerts for this track, then store them + if alert_results.size > 0 + email_about_things.push([track_thing, alert_results]) + end + end + + # If we have anything to send, then send everything for the user in one mail + if email_about_things.size > 0 + STDERR.puts "sending alert for user " + user.url_name + TrackMailer.deliver_event_digest(user, email_about_things) + end + + # Record that we've now sent those alerts to that user + for track_thing, alert_results in email_about_things + for result in alert_results + track_things_sent_email = TrackThingsSentEmail.new + track_things_sent_email.track_thing_id = track_thing.id + track_things_sent_email.info_request_event_id = result.id + track_things_sent_email.save! + end + end + user.last_daily_track_email = now + user.save! + end + end + +end + + diff --git a/app/models/track_thing.rb b/app/models/track_thing.rb index 35f384e5c..deb391423 100644 --- a/app/models/track_thing.rb +++ b/app/models/track_thing.rb @@ -18,37 +18,54 @@ # Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: track_thing.rb,v 1.1 2008-04-01 16:40:37 francis Exp $ +# $Id: track_thing.rb,v 1.2 2008-04-03 15:29:51 francis Exp $ class TrackThing < ActiveRecord::Base belongs_to :user, :foreign_key => 'tracking_user_id' validates_presence_of :track_query + validates_presence_of :track_type belongs_to :info_request belongs_to :public_body belongs_to :user, :foreign_key => 'tracked_user_id' + has_many :track_things_sent_emails + + validates_inclusion_of :track_type, :in => [ + 'request_updates', + ] + validates_inclusion_of :track_medium, :in => [ 'email_daily', ] - attr_accessor :params - def TrackThing.create_track_for_request(info_request) track_thing = TrackThing.new + track_thing.track_type = 'request_updates' track_thing.info_request = info_request - track_thing.params = { - :title => "Track the request '" + CGI.escapeHTML(info_request.title) + "'", - :feed_sortby => 'described', - - :web => "To follow updates to the request '" + CGI.escapeHTML(info_request.title) + "'", - :email => "Then you will be emailed whenever the request '" + CGI.escapeHTML(info_request.title) + "' is updated.", - :email_subject => "Confirm you want to follow updates to the request '" + CGI.escapeHTML(info_request.title) + "'", - } track_thing.track_query = "request:" + info_request.url_title return track_thing end + # Return hash of text parameters describing the request etc. + def params + if @params.nil? + if self.track_type == 'request_updates' + @params = { + :title => "Track the request '" + CGI.escapeHTML(self.info_request.title) + "'", + :describe => "The request '" + CGI.escapeHTML(self.info_request.title) + "'", + :web => "To follow updates to the request '" + CGI.escapeHTML(self.info_request.title) + "'", + :email => "Then you will be emailed whenever the request '" + CGI.escapeHTML(self.info_request.title) + "' is updated.", + :email_subject => "Confirm you want to follow updates to the request '" + CGI.escapeHTML(self.info_request.title) + "'", + :feed_sortby => 'described', # for RSS, as newest would give a date for responses possibly days before description + } + else + raise "unknown tracking type " + self.track_type + end + end + return @params + end + end diff --git a/app/models/user.rb b/app/models/user.rb index 28b1e5b28..1efe0ad24 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -20,7 +20,7 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: user.rb,v 1.44 2008-04-01 16:40:37 francis Exp $ +# $Id: user.rb,v 1.45 2008-04-03 15:29:51 francis Exp $ require 'digest/sha1' @@ -35,6 +35,7 @@ class User < ActiveRecord::Base has_many :info_requests has_many :user_info_request_sent_alerts has_many :post_redirects + has_many :track_things, :foreign_key => 'tracking_user_id' attr_accessor :password_confirmation validates_confirmation_of :password, :message =>"^Please enter the same password twice" |