#!/usr/bin/env perl # send-comments: # Send comments/updates on reports to bodies # 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; BEGIN { use File::Basename qw(dirname); use File::Spec; my $d = dirname(File::Spec->rel2abs($0)); require "$d/../setenv.pl"; } use Digest::MD5; use Encode; use CronFns; use FixMyStreet::App; use Utils; use mySociety::Config; use Open311; # send_method config values found in by-area config data, for selecting to appropriate method use constant SEND_METHOD_EMAIL => 'email'; use constant SEND_METHOD_OPEN311 => 'Open311'; use constant COUNCIL_ID_OXFORDSHIRE => 2237; # Set up site, language etc. my ($verbose, $nomail) = CronFns::options(); my $base_url = mySociety::Config::get('BASE_URL'); my $site = CronFns::site($base_url); my $bodies = FixMyStreet::App->model('DB::Body')->search( { send_method => SEND_METHOD_OPEN311, send_comments => 1, } ); while ( my $body = $bodies->next ) { # Oxfordshire (OCC) is special: # we do *receive* service_request_updates (aka comments) for OCC, but we never *send* them, so skip this pass next if $body->areas->{+COUNCIL_ID_OXFORDSHIRE}; my $use_extended = 0; my $comments = FixMyStreet::App->model('DB::Comment')->search( { 'me.whensent' => undef, 'me.external_id' => undef, 'me.state' => 'confirmed', 'me.confirmed' => { '!=' => undef }, 'problem.whensent' => { '!=' => undef }, 'problem.external_id' => { '!=' => undef }, 'problem.bodies_str' => { -like => '%' . $body->id . '%' }, 'problem.send_method_used' => 'Open311', }, { join => 'problem', } ); if ( $body->areas->{2482} ) { $use_extended = 1; } my %open311_conf = ( endpoint => $body->endpoint, jurisdiction => $body->jurisdiction, api_key => $body->api_key, use_extended_updates => $use_extended, ); if ( $body->send_extended_statuses ) { $open311_conf{extended_statuses} = 1; } my $o = Open311->new( %open311_conf ); if ( $body->areas->{2482} ) { my $endpoints = $o->endpoints; $endpoints->{update} = 'update.xml'; $endpoints->{service_request_updates} = 'update.xml'; $o->endpoints( $endpoints ); } while ( my $comment = $comments->next ) { my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($comment->cobrand)->new(); # 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 my $sender = $cobrand->get_body_sender( $body, $comment->problem->category ); my $config = $sender->{config}; $o = Open311->new( endpoint => $config->endpoint, jurisdiction => $config->jurisdiction, api_key => $config->api_key, use_extended_updates => 1, # FMB uses extended updates ); } if ( $comment->send_fail_count ) { next if bromley_retry_timeout( $comment ); } if ( $body->areas->{2482} ) { my $extra = $comment->extra; if ( !$extra ) { $extra = {}; } unless ( $extra->{title} ) { $extra->{title} = $comment->user->title; $comment->extra( $extra ); } } my $id = $o->post_service_request_update( $comment ); if ( $id ) { $comment->update( { external_id => $id, whensent => \'current_timestamp', } ); } else { $comment->update( { send_fail_count => $comment->send_fail_count + 1, send_fail_timestamp => \'current_timestamp', send_fail_reason => 'Failed to post over Open311', } ); } } } sub bromley_retry_timeout { my $row = shift; my $tz = FixMyStreet->local_time_zone; my $now = DateTime->now( time_zone => $tz ); my $diff = $now - $row->send_fail_timestamp; if ( $diff->in_units( 'minutes' ) < 30 ) { return 1; } return 0; }