aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/controllers/application.rb10
-rw-r--r--app/controllers/track_controller.rb10
-rw-r--r--app/helpers/application_helper.rb7
-rw-r--r--app/models/application_mailer.rb7
-rw-r--r--app/models/track_mailer.rb96
-rw-r--r--app/models/track_thing.rb39
-rw-r--r--app/models/user.rb3
-rw-r--r--app/views/track/track_set.rhtml4
-rw-r--r--app/views/track_mailer/event_digest.rhtml57
-rw-r--r--app/views/user/show.rhtml17
10 files changed, 223 insertions, 27 deletions
diff --git a/app/controllers/application.rb b/app/controllers/application.rb
index a7ed76772..3b2b04d4f 100644
--- a/app/controllers/application.rb
+++ b/app/controllers/application.rb
@@ -6,7 +6,7 @@
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
-# $Id: application.rb,v 1.34 2008-04-01 05:43:40 francis Exp $
+# $Id: application.rb,v 1.35 2008-04-03 15:29:50 francis Exp $
class ApplicationController < ActionController::Base
@@ -30,7 +30,7 @@ class ApplicationController < ActionController::Base
# Called from test code, is a mimic of User.confirm, for use in following email
# links when in controller tests (since we don't have full integration tests that
- # can work over multiple controllers)0
+ # can work over multiple controllers)
def test_code_redirect_by_email_token(token, controller_example_group)
post_redirect = PostRedirect.find_by_email_token(token)
if post_redirect.nil?
@@ -144,6 +144,7 @@ class ApplicationController < ActionController::Base
@per_page = per_page
@page = (params[:page] || "1").to_i
+ # XXX remember to update in models/track_mailer.rb also
solr_object = InfoRequestEvent.multi_solr_search(@query, :models => [ PublicBody, User ],
:limit => @per_page, :offset => (@page - 1) * @per_page,
:highlight => {
@@ -167,8 +168,9 @@ class ApplicationController < ActionController::Base
@highlighting = solr_object.highlights
end
- # URL generating functions are needed by all controllers (for redirects)
- # and views (for links), so include them into all of both.
+ # 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/controllers/track_controller.rb b/app/controllers/track_controller.rb
index 0ad3ae740..37283d0d9 100644
--- a/app/controllers/track_controller.rb
+++ b/app/controllers/track_controller.rb
@@ -5,7 +5,7 @@
# Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
-# $Id: track_controller.rb,v 1.1 2008-04-01 16:40:37 francis Exp $
+# $Id: track_controller.rb,v 1.2 2008-04-03 15:29:50 francis Exp $
class TrackController < ApplicationController
@@ -21,10 +21,14 @@ class TrackController < ApplicationController
# Generic request tracker - set @track_thing before calling
def track_set
+ @track_thing.track_medium = 'email_daily'
+
@title = @track_thing.params[:title]
- @track_thing.track_medium = params[:track_thing][:track_medium]
+ if params[:track_thing]
+ @track_thing.track_medium = params[:track_thing][:track_medium]
+ end
- if not params[:submitted_track_request] or not @track_thing.valid?
+ if not params[:submitted_track] or not @track_thing.valid?
render :template => 'track/track_set'
return false
end
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index b6513155d..f655209ef 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -5,11 +5,12 @@
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
-# $Id: application_helper.rb,v 1.18 2008-03-19 05:46:52 francis Exp $
+# $Id: application_helper.rb,v 1.19 2008-04-03 15:29:51 francis Exp $
module ApplicationHelper
- # URL generating functions are needed by all controllers (for redirects)
- # views (for links), so include them into all of both.
+ # 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
# Contact email address
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"
diff --git a/app/views/track/track_set.rhtml b/app/views/track/track_set.rhtml
index 380d1dd64..7f89e98db 100644
--- a/app/views/track/track_set.rhtml
+++ b/app/views/track/track_set.rhtml
@@ -12,7 +12,7 @@
<label for="track_track_medium_email">Send me emails shortly after there are updates</label>
<br> -->
<%= radio_button "track_thing", "track_medium", "email_daily" %>
- <label for="track_thing_track_medium_email">Send me emails at most once a day</label>
+ <label for="track_thing_track_medium_email_daily">Send me emails at most once a day</label>
<!--
<br>
<%= radio_button "track_thing", "track_medium", "rss" %>
@@ -26,7 +26,7 @@
</p>
<div class="form_button">
- <%= hidden_field_tag 'submitted_track_request', 1 %>
+ <%= hidden_field_tag 'submitted_track', 1 %>
<%= submit_tag "Subscribe" %>
</div>
<% end %>
diff --git a/app/views/track_mailer/event_digest.rhtml b/app/views/track_mailer/event_digest.rhtml
new file mode 100644
index 000000000..856fdfb52
--- /dev/null
+++ b/app/views/track_mailer/event_digest.rhtml
@@ -0,0 +1,57 @@
+<%
+ # Construct the main text of the mail
+ main_text = ''
+ for track_thing, alert_results in @email_about_things
+ main_text += track_thing.params[:describe] + "\n"
+ main_text += ("=" * track_thing.params[:describe].size) + "\n\n"
+ for result in alert_results.reverse
+ if result.class.to_s == "InfoRequestEvent"
+ event = result
+
+ if not @highlighting.nil? and @highlighting['InfoRequestEvent'][event.id].size > 0
+ extract = @highlighting['InfoRequestEvent'][event.id].values.join(" ")
+ elsif not event.outgoing_message.nil?
+ extract = excerpt(event.outgoing_message.body_without_salutation, "", 150)
+ elsif not event.incoming_message.nil?
+ extract = excerpt(event.incoming_message.get_body_for_quoting, "", 150)
+ else
+ extract = excerpt(info_request.initial_request_text, "", 150)
+ end
+ extract.gsub!(/\s+/, ' ')
+
+ if event.event_type == 'response'
+ url = main_url(request_url(event.info_request))+"#incoming-"+event.incoming_message.id.to_s
+ main_text += event.info_request.public_body.name + " sent a response to " + event.info_request.user.name
+ elsif event.event_type == 'followup_sent'
+ url = main_url(request_url(event.info_request))+"#outgoing-"+event.outgoing_message.id.to_s
+ main_text += event.info_request.user.name + " sent a follow up message to " + event.info_request.public_body.name
+ elsif event.event_type == 'sent'
+ # this is unlikely to happen in real life, but happens in the test code
+ url = main_url(request_url(event.info_request))+"#outgoing-"+event.outgoing_message.id.to_s
+ main_text += event.info_request.user.name + " sent a request to " + event.info_request.public_body.name
+ else
+ raise "unknown type in event_digest " + event.event_type
+ end
+ main_text += " (" + simple_date(event.created_at) + ")\n"
+ main_text += MySociety::Format.wrap_email_body('"' + extract + '"').gsub(/\n+$/, "") + "\n"
+ main_text += url + "\n"
+
+ main_text += "\n"
+ else
+ raise "need to add other types to TrackMailer.event_digest"
+ end
+ end
+ main_text += "\n"
+ end
+
+ #STDERR.puts main_text
+ #STDERR.puts @unsubscribe_url
+%><%=main_text%>Alter your subscription
+=======================
+
+Please click on the link below to cancel or alter these emails.
+<%=@unsubscribe_url%>
+
+-- the WhatDoTheyKnow team
+
+
diff --git a/app/views/user/show.rhtml b/app/views/user/show.rhtml
index 3585f345f..be89d3899 100644
--- a/app/views/user/show.rhtml
+++ b/app/views/user/show.rhtml
@@ -21,6 +21,19 @@
<% end %>
</p>
+ <% if not @display_user.track_things.empty? %>
+ <h2>
+ <%= @is_you ? 'You are' : 'This person is' %> tracking
+ <%=pluralize(@display_user.track_things.size, "thing") %>
+ </h2>
+
+ <ul>
+ <% for track_thing in @display_user.track_things %>
+ <li><%= track_thing.params[:describe] %></li>
+ <% end %>
+ </ul>
+ <% end %>
+
<% if @display_user.info_requests.empty? %>
<h2>Freedom of Information requests made by <%= @is_you ? 'you' : 'this person' %></h2>
<p><%= @is_you ? 'You have' : 'This person has' %>
@@ -28,8 +41,8 @@
<% else %>
<h2>
- <%=pluralize(@display_user.info_requests.size, "Freedom of Information request") %>
- made by <%= @is_you ? 'you' : 'this person' %>
+ <%= @is_you ? 'You have' : 'This person has' %>
+ made <%=pluralize(@display_user.info_requests.size, "Freedom of Information request") %>
</h2>
<%= render :partial => 'request/request_listing', :locals => { :info_requests => @display_user.info_requests.sort { |a,b| b.created_at <=> a.created_at } } %>