diff options
author | Marius Halden <marius.h@lden.org> | 2018-06-07 13:28:45 +0200 |
---|---|---|
committer | Marius Halden <marius.h@lden.org> | 2018-06-07 13:28:45 +0200 |
commit | 956f8b8a065824f9a9dc379eba1d0aa8b1b669cf (patch) | |
tree | 49f9ccb147b18dddee97500d4df7a3fb3dd3737c /bin/send-comments | |
parent | 782457d016084c8de04989dbc824a71899f8b41b (diff) | |
parent | 4dbf5371f79c5f290c08e561ba2c881e96b58669 (diff) |
Merge tag 'v2.3.3' into fiksgatami-dev
Diffstat (limited to 'bin/send-comments')
-rwxr-xr-x | bin/send-comments | 80 |
1 files changed, 71 insertions, 9 deletions
diff --git a/bin/send-comments b/bin/send-comments index aecedcb08..fc61169ef 100755 --- a/bin/send-comments +++ b/bin/send-comments @@ -5,13 +5,10 @@ # In Open311 parlance these are 'service request updates' and are sent using # mySociety's proposed extension to the Open311 Georeport v2 spec: # https://github.com/mysociety/fixmystreet/wiki/Open311-FMS---Proposed-differences-to-Open311 -# -# Copyright (c) 2011 UK Citizens Online Democracy. All rights reserved. -# Email: matthew@mysociety.org. WWW: http://www.mysociety.org use strict; use warnings; -require 5.8.0; +use v5.14; BEGIN { use File::Basename qw(dirname); @@ -26,6 +23,8 @@ use DateTime; use FixMyStreet; use FixMyStreet::Cobrand; use FixMyStreet::DB; +use FixMyStreet::Email; +use FixMyStreet::Map; use Open311; # send_method config values found in by-area config data, for selecting to appropriate method @@ -35,6 +34,7 @@ use constant SEND_METHOD_OPEN311 => 'Open311'; use constant COUNCIL_ID_OXFORDSHIRE => 2237; use constant COUNCIL_ID_BROMLEY => 2482; use constant COUNCIL_ID_LEWISHAM => 2492; +use constant COUNCIL_ID_BANES => 2551; # Set up site, language etc. my ($verbose, $nomail) = CronFns::options(); @@ -71,6 +71,7 @@ while ( my $body = $bodies->next ) { }, { join => 'problem', + order_by => [ 'confirmed', 'id' ], } ); @@ -100,7 +101,18 @@ while ( my $body = $bodies->next ) { } while ( my $comment = $comments->next ) { - my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($comment->cobrand)->new(); + my $cobrand = $body->get_cobrand_handler || + FixMyStreet::Cobrand->get_class_for_moniker($comment->cobrand)->new(); + + # Some cobrands (e.g. Buckinghamshire) don't want to receive updates + # from anyone except the original problem reporter. + if ($cobrand->call_hook(should_skip_sending_update => $comment)) { + unless (defined $comment->get_extra_metadata('cobrand_skipped_sending')) { + $comment->set_extra_metadata(cobrand_skipped_sending => 1); + $comment->update; + } + next; + } # TODO actually this should be OK for any devolved endpoint if original Open311->can_be_devolved, presumably if ( 0 ) { # Check can_be_devolved and do this properly if set @@ -114,9 +126,7 @@ while ( my $body = $bodies->next ) { ); } - if ( $comment->send_fail_count ) { - next if retry_timeout( $comment ); - } + next if !$verbose && $comment->send_fail_count && retry_timeout($comment); if ( $site eq 'fixmystreet.com' && $body->areas->{+COUNCIL_ID_BROMLEY} ) { my $extra = $comment->extra; @@ -133,6 +143,7 @@ while ( my $body = $bodies->next ) { my $id = $o->post_service_request_update( $comment ); if ( $id ) { + send_comment_email($comment, $cobrand) if $body->areas->{+COUNCIL_ID_BANES}; $comment->update( { external_id => $id, whensent => \'current_timestamp', @@ -141,8 +152,12 @@ while ( my $body = $bodies->next ) { $comment->update( { send_fail_count => $comment->send_fail_count + 1, send_fail_timestamp => \'current_timestamp', - send_fail_reason => 'Failed to post over Open311', + send_fail_reason => "Failed to post over Open311\n\n" . $o->error, } ); + + if ( $verbose && $o->error ) { + warn $o->error; + } } } } @@ -159,3 +174,50 @@ sub retry_timeout { return 0; } + +=head2 send_comment_email + +Some cobrands (e.g. BANES) want to receive an email for every update that's sent +via Open311. This function is called after each update is sent, and sends the +alert-update.txt templated email to the cobrand's update_email (or +contact_email if update_email isn't defined.) + +=cut +sub send_comment_email { + my ($comment, $cobrand) = @_; + + my $handler = $cobrand->call_hook(get_body_handler_for_problem => $comment->problem) or return; + + # Set up map/language so things don't error + FixMyStreet::Map::set_map_class($handler->map_type); + $handler->set_lang_and_domain( $comment->lang, 1, FixMyStreet->path_to('locale')->stringify ); + my $to = $cobrand->call_hook('update_email') || $cobrand->contact_email; + + # Construct the data the alert-update email template needs + # (bit annoying that we can't just put $comment in data!) + my %data = ( + cobrand => $handler, + hide_unsubscribe => 1, + data => [ { + item_photo => $comment->photo, + item_text => $comment->text, + item_name => $comment->name, + item_anonymous => $comment->anonymous, + confirmed => $comment->confirmed, + get_first_image_fp => sub { $comment->get_first_image_fp }, + } ], + report => $comment->problem, + problem_url => $handler->base_url_for_report($comment->problem) . $comment->problem->url, + ); + + FixMyStreet::Email::send_cron( + FixMyStreet::DB->schema, + "alert-update.txt", + \%data, + { To => $to }, + undef, + 0, + $handler, + $comment->lang, + ); +} |