aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/DB/ResultSet/Problem.pm
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet/DB/ResultSet/Problem.pm')
-rw-r--r--perllib/FixMyStreet/DB/ResultSet/Problem.pm157
1 files changed, 97 insertions, 60 deletions
diff --git a/perllib/FixMyStreet/DB/ResultSet/Problem.pm b/perllib/FixMyStreet/DB/ResultSet/Problem.pm
index 6da383d6c..c108f7e29 100644
--- a/perllib/FixMyStreet/DB/ResultSet/Problem.pm
+++ b/perllib/FixMyStreet/DB/ResultSet/Problem.pm
@@ -84,15 +84,16 @@ sub _recent {
my $key = $photos ? 'recent_photos' : 'recent';
$key .= ":$site_key:$num";
+ # unconfirmed might be returned for e.g. Zurich, but would mean in moderation, so no photo
+ my @states = grep { $_ ne 'unconfirmed' } FixMyStreet::DB::Result::Problem->visible_states();
my $query = {
non_public => 0,
- state => [ FixMyStreet::DB::Result::Problem->visible_states() ],
+ state => \@states,
};
$query->{photo} = { '!=', undef } if $photos;
my $attrs = {
- columns => [ 'id', 'title', 'confirmed' ],
- order_by => { -desc => 'confirmed' },
+ order_by => { -desc => 'coalesce(confirmed, created)' },
rows => $num,
};
@@ -134,10 +135,6 @@ sub around_map {
my ( $rs, $min_lat, $max_lat, $min_lon, $max_lon, $interval, $limit ) = @_;
my $attr = {
order_by => { -desc => 'created' },
- columns => [
- 'id', 'title', 'latitude', 'longitude', 'state', 'confirmed',
- { photo => 'photo is not null' },
- ],
};
$attr->{rows} = $limit if $limit;
@@ -159,7 +156,7 @@ sub around_map {
sub timeline {
my ( $rs ) = @_;
- my $prefetch =
+ my $prefetch =
FixMyStreet::App->model('DB')->schema->storage->sql_maker->quote_char ?
[ qw/user/ ] :
[];
@@ -219,46 +216,74 @@ sub categories_summary {
return \%categories;
}
+sub get_admin_url {
+ my ($rs, $cobrand, $row) = @_;
+ return $cobrand->admin_base_url . '/report_edit/' . $row->id;
+}
+
sub send_reports {
+ my ( $rs, $site_override ) = @_;
+
# Set up site, language etc.
- my ($verbose, $nomail) = CronFns::options();
+ my ($verbose, $nomail, $debug_mode) = CronFns::options();
+
my $base_url = mySociety::Config::get('BASE_URL');
- my $site = CronFns::site($base_url);
+ my $site = $site_override || CronFns::site($base_url);
+ my $states = [ 'confirmed', 'fixed' ];
+ $states = [ 'unconfirmed', 'confirmed', 'in progress', 'planned', 'closed' ] if $site eq 'zurich';
my $unsent = FixMyStreet::App->model("DB::Problem")->search( {
- state => [ 'confirmed', 'fixed' ],
+ state => $states,
whensent => undef,
- council => { '!=', undef },
+ bodies_str => { '!=', undef },
} );
my (%notgot, %note);
my $send_report = FixMyStreet::SendReport->new();
my $senders = $send_report->get_senders;
- my %sending_skipped_by_method;
+ my $debug_unsent_count = 0;
+ debug_print("starting to loop through unsent problem reports...") if $debug_mode;
while (my $row = $unsent->next) {
my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($row->cobrand)->new();
- # Cobranded and non-cobranded messages can share a database. In this case, the conf file
- # should specify a vhost to send the reports for each cobrand, so that they don't get sent
+ if ($debug_mode) {
+ $debug_unsent_count++;
+ print "\n";
+ debug_print("state=" . $row->state . ", bodies_str=" . $row->bodies_str . ($row->cobrand? ", cobrand=" . $row->cobrand : ""), $row->id);
+ }
+
+ # Cobranded and non-cobranded messages can share a database. In this case, the conf file
+ # should specify a vhost to send the reports for each cobrand, so that they don't get sent
# more than once if there are multiple vhosts running off the same database. The email_host
# call checks if this is the host that sends mail for this cobrand.
- next unless $cobrand->email_host();
+ if (! $cobrand->email_host()) {
+ debug_print("skipping because this host does not send reports for cobrand " . $cobrand->moniker, $row->id) if $debug_mode;
+ next;
+ }
+
$cobrand->set_lang_and_domain($row->lang, 1);
- if ( $row->is_from_abuser ) {
+ if ( $row->is_from_abuser) {
+ $row->update( { state => 'hidden' } );
+ debug_print("hiding because its sender is flagged as an abuser", $row->id) if $debug_mode;
+ next;
+ } elsif ( $row->title =~ /app store test/i ) {
$row->update( { state => 'hidden' } );
+ debug_print("hiding because it is an app store test message", $row->id) if $debug_mode;
next;
}
# Template variables for the email
my $email_base_url = $cobrand->base_url_for_report($row);
my %h = map { $_ => $row->$_ } qw/id title detail name category latitude longitude used_map/;
- map { $h{$_} = $row->user->$_ } qw/email phone/;
- $h{confirmed} = DateTime::Format::Pg->format_datetime( $row->confirmed->truncate (to => 'second' ) );
+ map { $h{$_} = $row->user->$_ || '' } qw/email phone/;
+ $h{confirmed} = DateTime::Format::Pg->format_datetime( $row->confirmed->truncate (to => 'second' ) )
+ if $row->confirmed;
$h{query} = $row->postcode;
$h{url} = $email_base_url . $row->url;
+ $h{admin_url} = $rs->get_admin_url($cobrand, $row);
$h{phone_line} = $h{phone} ? _('Phone:') . " $h{phone}\n\n" : '';
if ($row->photo) {
$h{has_photo} = _("This web page also contains a photo of the problem, provided by the user.") . "\n\n";
@@ -302,39 +327,42 @@ sub send_reports {
my ( $sender_count );
if ($site eq 'emptyhomes') {
- my $council = $row->council;
- my $areas_info = mySociety::MaPit::call('areas', $council);
+ my $body = $row->bodies_str;
+ $body = FixMyStreet::App->model("DB::Body")->find($body);
my $sender = "FixMyStreet::SendReport::EmptyHomes";
$reporters{ $sender } = $sender->new() unless $reporters{$sender};
- $reporters{ $sender }->add_council( $council, $areas_info->{$council} );
+ $reporters{ $sender }->add_body( $body );
+ $sender_count = 1;
} else {
# XXX Needs locks!
- my @all_councils = split /,|\|/, $row->council;
- my ($councils, $missing) = $row->council =~ /^([\d,]+)(?:\|([\d,]+))?/;
- my @councils = split(/,/, $councils);
- my $areas_info = mySociety::MaPit::call('areas', \@all_councils);
+ # XXX Only copes with at most one missing body
+ my ($bodies, $missing) = $row->bodies_str =~ /^([\d,]+)(?:\|(\d+))?/;
+ my @bodies = split(/,/, $bodies);
+ $bodies = FixMyStreet::App->model("DB::Body")->search(
+ { id => \@bodies },
+ { order_by => 'name' },
+ );
+ $missing = FixMyStreet::App->model("DB::Body")->find($missing) if $missing;
my @dear;
- foreach my $council (@councils) {
- my $name = $areas_info->{$council}->{name};
-
- my $sender_info = $cobrand->get_council_sender( $council, $areas_info->{$council}, $row->category );
+ while (my $body = $bodies->next) {
+ my $sender_info = $cobrand->get_body_sender( $body, $row->category );
my $sender = "FixMyStreet::SendReport::" . $sender_info->{method};
if ( ! exists $senders->{ $sender } ) {
- warn "No such sender [ $sender ] for council $name ( $council )";
+ warn "No such sender [ $sender ] for body $body->name ( $body->id )";
next;
}
$reporters{ $sender } ||= $sender->new();
if ( $reporters{ $sender }->should_skip( $row ) ) {
- $sending_skipped_by_method{ $sender }++ if
- $reporters{ $sender }->skipped;
+ debug_print("skipped by sender " . $sender_info->{method} . " (might be due to previous failed attempts?)", $row->id) if $debug_mode;
} else {
- push @dear, $name;
- $reporters{ $sender }->add_council( $council, $areas_info->{$council}, $sender_info->{config} );
+ debug_print("OK, adding recipient body " . $body->id . ":" . $body->name . ", " . $body->send_method, $row->id) if $debug_mode;
+ push @dear, $body->name;
+ $reporters{ $sender }->add_body( $body, $sender_info->{config} );
}
}
@@ -352,7 +380,7 @@ sub send_reports {
$h{subcategory_line} = "\n\n";
}
- $h{councils_name} = join(_(' and '), @dear);
+ $h{bodies_name} = join(_(' and '), @dear);
if ($h{category} eq _('Other')) {
$h{multiple} = @dear>1 ? "[ " . _("This email has been sent to both councils covering the location of the problem, as the user did not categorise it; please ignore it if you're not the correct council to deal with the issue, or let us know what category of problem this is so we can add it to our system.") . " ]\n\n"
: '';
@@ -360,11 +388,10 @@ sub send_reports {
$h{multiple} = @dear>1 ? "[ " . _("This email has been sent to several councils covering the location of the problem, as the category selected is provided for all of them; please ignore it if you're not the correct council to deal with the issue.") . " ]\n\n"
: '';
}
- $h{missing} = '';
+ $h{missing} = '';
if ($missing) {
- my $name = $areas_info->{$missing}->{name};
$h{missing} = '[ '
- . sprintf(_('We realise this problem might be the responsibility of %s; however, we don\'t currently have any contact details for them. If you know of an appropriate contact address, please do get in touch.'), $name)
+ . sprintf(_('We realise this problem might be the responsibility of %s; however, we don\'t currently have any contact details for them. If you know of an appropriate contact address, please do get in touch.'), $missing->name)
. " ]\n\n";
}
@@ -375,16 +402,16 @@ sub send_reports {
die 'Report not going anywhere for ID ' . $row->id . '!';
}
- next unless $sender_count;
+ if (! $sender_count) {
+ debug_print("can't send because sender count is zero", $row->id) if $debug_mode;
+ next;
+ }
- if (mySociety::Config::get('STAGING_SITE')) {
- # on a staging server send emails to ourselves rather than the councils
- my @testing_councils = split( '\|', mySociety::Config::get('TESTING_COUNCILS') );
- unless ( grep { $row->council eq $_ } @testing_councils ) {
- %reporters = map { $_ => $reporters{$_} } grep { /FixMyStreet::SendReport::(Email|NI)/ } keys %reporters;
- unless (%reporters) {
- %reporters = ( 'FixMyStreet::SendReport::Email' => FixMyStreet::SendReport::Email->new() );
- }
+ if (mySociety::Config::get('STAGING_SITE') && !mySociety::Config::get('SEND_REPORTS_ON_STAGING')) {
+ # on a staging server send emails to ourselves rather than the bodies
+ %reporters = map { $_ => $reporters{$_} } grep { /FixMyStreet::SendReport::(Email|NI|EmptyHomes)/ } keys %reporters;
+ unless (%reporters) {
+ %reporters = ( 'FixMyStreet::SendReport::Email' => FixMyStreet::SendReport::Email->new() );
}
}
@@ -392,6 +419,7 @@ sub send_reports {
my $result = -1;
for my $sender ( keys %reporters ) {
+ debug_print("sending using " . $sender, $row->id) if $debug_mode;
$result *= $reporters{ $sender }->send( $row, \%h );
if ( $reporters{ $sender }->unconfirmed_counts) {
foreach my $e (keys %{ $reporters{ $sender }->unconfirmed_counts } ) {
@@ -414,6 +442,7 @@ sub send_reports {
if ( $cobrand->report_sent_confirmation_email && !$h{anonymous_report}) {
_send_report_sent_email( $row, \%h, $nomail );
}
+ debug_print("send successful: OK", $row->id) if $debug_mode;
} else {
my @errors;
for my $sender ( keys %reporters ) {
@@ -422,34 +451,35 @@ sub send_reports {
}
}
$row->update_send_failed( join( '|', @errors ) );
+ debug_print("send FAILED: " . join( '|', @errors ), $row->id) if $debug_mode;
+ }
+ }
+ if ($debug_mode) {
+ print "\n";
+ if ($debug_unsent_count) {
+ debug_print("processed all unsent reports (total: $debug_unsent_count)");
+ } else {
+ debug_print("no unsent reports were found (must have whensent=null and suitable bodies_str & state) -- nothing to send");
}
}
- if ($verbose) {
+ if ($verbose || $debug_mode) {
print "Council email addresses that need checking:\n" if keys %notgot;
foreach my $e (keys %notgot) {
foreach my $c (keys %{$notgot{$e}}) {
print " " . $notgot{$e}{$c} . " problem, to $e category $c (" . $note{$e}{$c}. ")\n";
}
}
- if (keys %sending_skipped_by_method) {
- my $c = 0;
- print "\nProblem reports that send-reports did not attempt to send the following:\n";
- foreach my $send_method (sort keys %sending_skipped_by_method) {
- printf " %-24s %4d\n", "$send_method:", $sending_skipped_by_method{$send_method};
- $c+=$sending_skipped_by_method{$send_method};
- }
- printf " %-24s %4d\n", "Total:", $c;
- }
my $sending_errors = '';
my $unsent = FixMyStreet::App->model("DB::Problem")->search( {
state => [ 'confirmed', 'fixed' ],
whensent => undef,
- council => { '!=', undef },
+ bodies_str => { '!=', undef },
send_fail_count => { '>', 0 }
} );
while (my $row = $unsent->next) {
- $sending_errors .= "* http://www.fixmystreet.com/report/" . $row->id . ", failed "
+ my $base_url = mySociety::Config::get('BASE_URL');
+ $sending_errors .= "* " . $base_url . "/report/" . $row->id . ", failed "
. $row->send_fail_count . " times, last at " . $row->send_fail_timestamp
. ", reason " . $row->send_fail_reason . "\n";
}
@@ -485,4 +515,11 @@ sub _send_report_sent_email {
);
}
+sub debug_print {
+ my $msg = shift;
+ my $id = shift || '';
+ $id = "report $id: " if $id;
+ print "[] $id$msg\n";
+}
+
1;