diff options
Diffstat (limited to 'perllib/Open311')
-rw-r--r-- | perllib/Open311/GetServiceRequestUpdates.pm | 280 | ||||
-rw-r--r-- | perllib/Open311/GetServiceRequests.pm | 6 | ||||
-rw-r--r-- | perllib/Open311/GetUpdates.pm | 76 | ||||
-rw-r--r-- | perllib/Open311/PopulateServiceList.pm | 14 | ||||
-rwxr-xr-x | perllib/Open311/PostServiceRequestUpdates.pm | 49 | ||||
-rw-r--r-- | perllib/Open311/UpdatesBase.pm | 283 |
6 files changed, 373 insertions, 335 deletions
diff --git a/perllib/Open311/GetServiceRequestUpdates.pm b/perllib/Open311/GetServiceRequestUpdates.pm index 9fa81ac9e..b4f3f4430 100644 --- a/perllib/Open311/GetServiceRequestUpdates.pm +++ b/perllib/Open311/GetServiceRequestUpdates.pm @@ -1,92 +1,17 @@ package Open311::GetServiceRequestUpdates; use Moo; -use Open311; -use Parallel::ForkManager; -use FixMyStreet::DB; -use FixMyStreet::App::Model::PhotoSet; +extends 'Open311::UpdatesBase'; + use DateTime::Format::W3CDTF; -has system_user => ( is => 'rw' ); +has '+send_comments_flag' => ( default => 1 ); has start_date => ( is => 'ro', default => sub { undef } ); has end_date => ( is => 'ro', default => sub { undef } ); -has body => ( is => 'ro', default => sub { undef } ); -has suppress_alerts => ( is => 'rw', default => 0 ); -has verbose => ( is => 'ro', default => 0 ); -has schema => ( is =>'ro', lazy => 1, default => sub { FixMyStreet::DB->schema->connect } ); -has blank_updates_permitted => ( is => 'rw', default => 0 ); - -has current_body => ( is => 'rw' ); -has current_open311 => ( is => 'rw' ); Readonly::Scalar my $AREA_ID_BROMLEY => 2482; Readonly::Scalar my $AREA_ID_OXFORDSHIRE => 2237; -sub fetch { - my ($self, $open311) = @_; - - my $bodies = $self->schema->resultset('Body')->search( - { - send_method => 'Open311', - send_comments => 1, - comment_user_id => { '!=', undef }, - endpoint => { '!=', '' }, - } - ); - - if ( $self->body ) { - $bodies = $bodies->search( { name => $self->body } ); - } - - my $procs_min = FixMyStreet->config('FETCH_COMMENTS_PROCESSES_MIN') || 0; - my $procs_max = FixMyStreet->config('FETCH_COMMENTS_PROCESSES_MAX'); - my $procs_timeout = FixMyStreet->config('FETCH_COMMENTS_PROCESS_TIMEOUT'); - - my $pm = Parallel::ForkManager->new(FixMyStreet->test_mode ? 0 : $procs_min); - - if ($procs_max && $procs_timeout) { - my %workers; - $pm->run_on_wait(sub { - while (my ($pid, $started_at) = each %workers) { - next unless time() - $started_at > $procs_timeout; - next if $pm->max_procs == $procs_max; - $pm->set_max_procs($pm->max_procs + 1); - delete $workers{$pid}; # Only want to increase once per long-running thing - } - }, 1); - $pm->run_on_start(sub { my $pid = shift; $workers{$pid} = time(); }); - $pm->run_on_finish(sub { my $pid = shift; delete $workers{$pid}; }); - } - - while ( my $body = $bodies->next ) { - $pm->start and next; - - $self->current_body( $body ); - - my %open311_conf = ( - endpoint => $body->endpoint, - api_key => $body->api_key, - jurisdiction => $body->jurisdiction, - extended_statuses => $body->send_extended_statuses, - ); - - my $cobrand = $body->get_cobrand_handler; - $cobrand->call_hook(open311_config_updates => \%open311_conf) - if $cobrand; - - $self->current_open311( $open311 || Open311->new(%open311_conf) ); - - $self->suppress_alerts( $body->suppress_alerts ); - $self->blank_updates_permitted( $body->blank_updates_permitted ); - $self->system_user( $body->comment_user ); - $self->process_body(); - - $pm->finish; - } - - $pm->wait_all_children; -} - sub parse_dates { my $self = shift; my $body = $self->current_body; @@ -141,201 +66,12 @@ sub process_body { return 1; } -sub check_date { - my ($self, $request, @args) = @_; - - my $comment_time = eval { - DateTime::Format::W3CDTF->parse_datetime( $request->{updated_datetime} || "" ) - ->set_time_zone(FixMyStreet->local_time_zone); - }; - return if $@; - my $updated = DateTime::Format::W3CDTF->format_datetime($comment_time->clone->set_time_zone('UTC')); - return if @args && ($updated lt $args[0] || $updated gt $args[1]); - $request->{comment_time} = $comment_time; - return 1; -} - -sub find_problem { - my ($self, $request, @args) = @_; - - $self->check_date($request, @args) or return; - - my $body = $self->current_body; - my $request_id = $request->{service_request_id}; - - # If there's no request id then we can't work out - # what problem it belongs to so just skip - return unless $request_id || $request->{fixmystreet_id}; - - my $problem; - my $criteria = { - external_id => $request_id, - }; - - # in some cases we only have the FMS id and not the request id so use that - if ( $request->{fixmystreet_id} ) { - unless ( $request->{fixmystreet_id} =~ /^\d+$/ ) { - warn "skipping bad fixmystreet id in updates for " . $body->name . ": [" . $request->{fixmystreet_id} . "], external id is $request_id\n"; - return; - } - - $criteria = { - id => $request->{fixmystreet_id}, - }; - } - - $problem = $self->schema->resultset('Problem')->to_body($body)->search( $criteria ); +sub _find_problem { + my ($self, $criteria) = @_; + my $problem = $self->schema->resultset('Problem') + ->to_body($self->current_body) + ->search( $criteria ); return $problem->first; } -sub process_update { - my ($self, $request, $p) = @_; - my $open311 = $self->current_open311; - my $body = $self->current_body; - - my $state = $open311->map_state( $request->{status} ); - my $old_state = $p->state; - my $external_status_code = $request->{external_status_code} || ''; - my $customer_reference = $request->{customer_reference} || ''; - my $old_external_status_code = $p->get_extra_metadata('external_status_code') || ''; - my $comment = $self->schema->resultset('Comment')->new( - { - problem => $p, - user => $self->system_user, - external_id => $request->{update_id}, - text => $self->comment_text_for_request( - $request, $p, $state, $old_state, - $external_status_code, $old_external_status_code - ), - mark_fixed => 0, - mark_open => 0, - anonymous => 0, - name => $self->system_user->name, - confirmed => $request->{comment_time}, - created => $request->{comment_time}, - state => 'confirmed', - } - ); - - # Some Open311 services, e.g. Confirm via open311-adapter, provide - # a more fine-grained status code that we use within FMS for - # response templates. - if ( $external_status_code ) { - $comment->set_extra_metadata(external_status_code => $external_status_code); - $p->set_extra_metadata(external_status_code => $external_status_code); - } else { - $p->set_extra_metadata(external_status_code => ''); - } - - # if the customer reference to display in the report metadata is - # not the same as the external_id - if ( $customer_reference ) { - $p->set_extra_metadata( customer_reference => $customer_reference ); - } - - $open311->add_media($request->{media_url}, $comment) - if $request->{media_url}; - - # don't update state unless it's an allowed state - if ( FixMyStreet::DB::Result::Problem->visible_states()->{$state} && - # For Oxfordshire, don't allow changes back to Open from other open states - !( $body->areas->{$AREA_ID_OXFORDSHIRE} && $state eq 'confirmed' && $p->is_open ) && - # Don't let it change between the (same in the front end) fixed states - !( $p->is_fixed && FixMyStreet::DB::Result::Problem->fixed_states()->{$state} ) ) { - - $comment->problem_state($state); - - # we only want to update the problem state if that makes sense. We never want to unhide a problem. - # If the update is older than the last update then we also do not want to update the state. This - # is largely to avoid the situation where we miss some updates, make more updates and then catch - # the updates when we fetch the last 24 hours of updates. The exception to this is the first - # comment. This is to catch automated updates which happen faster than we get the external_id - # back from the endpoint and hence have an created time before the lastupdate. - if ( $p->is_visible && $p->state ne $state && - ( $comment->created >= $p->lastupdate || $p->comments->count == 0 ) ) { - $p->state($state); - } - } - - # If nothing to show (no text, photo, or state change), don't show this update - $comment->state('hidden') unless $comment->text || $comment->photo - || ($comment->problem_state && $state ne $old_state); - - my $cobrand = $body->get_cobrand_handler; - $cobrand->call_hook(open311_get_update_munging => $comment) - if $cobrand; - - # As comment->created has been looked at above, its time zone has been shifted - # to TIME_ZONE (if set). We therefore need to set it back to local before - # insertion. We also then need a clone, otherwise the setting of lastupdate - # will *also* reshift comment->created's time zone to TIME_ZONE. - my $created = $comment->created->set_time_zone(FixMyStreet->local_time_zone); - $p->lastupdate($created->clone); - $p->update; - $comment->insert(); - - if ( $self->suppress_alerts ) { - my @alerts = $self->schema->resultset('Alert')->search( { - alert_type => 'new_updates', - parameter => $p->id, - confirmed => 1, - user_id => $p->user->id, - } ); - - for my $alert (@alerts) { - my $alerts_sent = $self->schema->resultset('AlertSent')->find_or_create( { - alert_id => $alert->id, - parameter => $comment->id, - } ); - } - } - - return $comment; -} - -sub comment_text_for_request { - my ($self, $request, $problem, $state, $old_state, - $ext_code, $old_ext_code) = @_; - - # Response templates are only triggered if the state/external status has changed. - # And treat any fixed state as fixed. - my $state_changed = $state ne $old_state - && !( $problem->is_fixed && FixMyStreet::DB::Result::Problem->fixed_states()->{$state} ); - my $ext_code_changed = $ext_code ne $old_ext_code; - my $template; - if ($state_changed || $ext_code_changed) { - my $order; - my $state_params = { - 'me.state' => $state - }; - if ($ext_code) { - $state_params->{'me.external_status_code'} = $ext_code; - # make sure that empty string/nulls come last. - $order = { order_by => \"me.external_status_code DESC NULLS LAST" }; - }; - - if (my $t = $problem->response_templates->search({ - auto_response => 1, - -or => $state_params, - }, $order )->first) { - $template = $t->text; - } - } - - my $desc = $request->{description} || ''; - if ($desc && (!$template || $template !~ /\{\{description}}/)) { - return $desc; - } - - if ($template) { - $template =~ s/\{\{description}}/$desc/; - return $template; - } - - return "" if $self->blank_updates_permitted; - - print STDERR "Couldn't determine update text for $request->{update_id} (report " . $problem->id . ")\n"; - return ""; -} - 1; diff --git a/perllib/Open311/GetServiceRequests.pm b/perllib/Open311/GetServiceRequests.pm index e5fd6438e..7f98bbc4a 100644 --- a/perllib/Open311/GetServiceRequests.pm +++ b/perllib/Open311/GetServiceRequests.pm @@ -4,7 +4,6 @@ use Moo; use Open311; use FixMyStreet::DB; use FixMyStreet::MapIt; -use FixMyStreet::App::Model::PhotoSet; use DateTime::Format::W3CDTF; has system_user => ( is => 'rw' ); @@ -82,10 +81,10 @@ sub create_problems { } my $contacts = $self->schema->resultset('Contact') - ->active + ->not_deleted_admin ->search( { body_id => $body->id } ); - for my $request (@{$requests->{request}}) { + for my $request (@$requests) { # no point importing if we can't put it on the map unless ($request->{service_request_id} && $request->{lat} && $request->{long}) { warn "Not creating request '$request->{description}' for @{[$body->name]} as missing one of id, lat or long" @@ -186,6 +185,7 @@ sub create_problems { send_method_used => 'Open311', category => $contact, send_questionnaire => 0, + service => 'Open311', non_public => $non_public, } ); diff --git a/perllib/Open311/GetUpdates.pm b/perllib/Open311/GetUpdates.pm index f62acf4a8..352f2f218 100644 --- a/perllib/Open311/GetUpdates.pm +++ b/perllib/Open311/GetUpdates.pm @@ -1,67 +1,53 @@ package Open311::GetUpdates; use Moo; -use Open311; -use FixMyStreet::Cobrand; - -has body_list => ( is => 'ro' ); -has system_user => ( is => 'ro' ); +extends 'Open311::UpdatesBase'; -sub get_updates { - my $self = shift; +use Open311; - while ( my $body = $self->body_list->next ) { - my $open311 = Open311->new( - endpoint => $body->endpoint, - jurisdiction => $body->jurisdiction, - api_key => $body->api_key - ); +has '+send_comments_flag' => ( default => 0 ); +has ext_to_int_map => ( is => 'rw' ); - my $reports = $body->result_source->schema->resultset('Problem')->to_body($body)->search( - { - state => { 'IN', [qw/confirmed fixed/] }, - -and => [ - external_id => { '!=', undef }, - external_id => { '!=', '' }, - ], - } - ); +has report_criteria => ( is => 'ro', default => sub { { + state => [ FixMyStreet::DB::Result::Problem->visible_states() ], + external_id => { '!=', '' }, + } } ); - my @report_ids = (); - while ( my $report = $reports->next ) { - push @report_ids, $report->external_id; - } +sub process_body { + my ($self) = @_; - next unless @report_ids; + my $reports = $self->schema->resultset('Problem') + ->to_body($self->current_body) + ->search($self->report_criteria); - $self->update_reports( \@report_ids, $open311, $body ); - } + my @reports = $reports->all; + $self->update_reports(\@reports); } sub update_reports { - my ( $self, $report_ids, $open311, $body ) = @_; + my ( $self, $reports ) = @_; + return unless @$reports; - my $service_requests = $open311->get_service_requests( { report_ids => $report_ids } ); - my $requests = $service_requests->{request}; + my $requests = $self->current_open311->get_service_requests( { + report_ids => [ map { $_->external_id } @$reports ], + } ); + $self->ext_to_int_map({ map { $_->external_id => $_ } @$reports }); for my $request (@$requests) { - # if there's no updated date then we can't - # tell if it's newer than what we have so we should skip it - next unless $request->{updated_datetime}; - - my $request_id = $request->{service_request_id}; + $request->{description} = $request->{status_notes}; - my $problem = $body->result_source->schema->resultset('Problem') - ->search( { external_id => $request_id, } ); + my $p = $self->find_problem($request) or next; + next if $request->{comment_time} < $p->lastupdate; + # But what if update at our end later than update their end... - if (my $p = $problem->first) { - my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($p->cobrand)->new(); - $cobrand->set_lang_and_domain($p->lang, 1, FixMyStreet->path_to('locale')->stringify ); - $p->update_from_open311_service_request( $request, $body, $self->system_user ); - } + $self->process_update($request, $p); } +} - return 1; +sub _find_problem { + my ($self, $criteria) = @_; + my $problem = $self->ext_to_int_map->{$criteria->{external_id}}; + return $problem; } 1; diff --git a/perllib/Open311/PopulateServiceList.pm b/perllib/Open311/PopulateServiceList.pm index 9be17946e..a3672770c 100644 --- a/perllib/Open311/PopulateServiceList.pm +++ b/perllib/Open311/PopulateServiceList.pm @@ -145,6 +145,8 @@ sub _handle_existing_contact { my $service_name = $self->_normalize_service_name; my $protected = $contact->get_extra_metadata("open311_protect"); + return if $self->_current_body_cobrand && $self->_current_body_cobrand->call_hook(open311_skip_existing_contact => $contact); + print $self->_current_body->id . " already has a contact for service code " . $self->_current_service->{service_code} . "\n" if $self->verbose >= 2; if ( $contact->state eq 'deleted' || $service_name ne $contact->category || $self->_current_service->{service_code} ne $contact->email ) { @@ -350,10 +352,10 @@ sub _delete_contacts_not_in_service_list { ); if ($self->_current_body->can_be_devolved) { - # If the body has can_be_devolved switched on, it's most likely a - # combination of Open311/email, so ignore any email addresses. + # If the body has can_be_devolved switched on, ignore any + # contact with its own send method $found_contacts = $found_contacts->search( - { email => { -not_like => '%@%' } } + { send_method => [ "", undef ] }, ); } @@ -370,7 +372,11 @@ sub _delete_contacts_not_in_service_list { sub _delete_contacts_not_in_service_list_cobrand_overrides { my ( $self, $found_contacts ) = @_; - return $found_contacts; + if ($self->_current_body_cobrand && $self->_current_body_cobrand->can('open311_filter_contacts_for_deletion')) { + return $self->_current_body_cobrand->open311_filter_contacts_for_deletion($found_contacts); + } else { + return $found_contacts; + } } 1; diff --git a/perllib/Open311/PostServiceRequestUpdates.pm b/perllib/Open311/PostServiceRequestUpdates.pm index fadd063da..b9aa9ed50 100755 --- a/perllib/Open311/PostServiceRequestUpdates.pm +++ b/perllib/Open311/PostServiceRequestUpdates.pm @@ -14,14 +14,12 @@ use Open311; use constant SEND_METHOD_OPEN311 => 'Open311'; has verbose => ( is => 'ro', default => 0 ); -has current_open311 => ( is => 'rw' ); sub send { my $self = shift; my $bodies = $self->fetch_bodies; foreach my $body (values %$bodies) { - $self->construct_open311($body); $self->process_body($body); } } @@ -41,18 +39,25 @@ sub fetch_bodies { } sub construct_open311 { - my ($self, $body) = @_; - my $o = Open311->new($self->open311_params($body)); - $self->current_open311($o); + my ($self, $body, $comment) = @_; + my $o = Open311->new($self->open311_params($body, $comment)); + return $o; } sub open311_params { - my ($self, $body) = @_; + my ($self, $body, $comment) = @_; + + my $conf = $body; + if ($comment) { + my $cobrand_logged = $comment->get_cobrand_logged; + my $sender = $cobrand_logged->get_body_sender($body, $comment->problem); + $conf = $sender->{config}; + } my %open311_conf = ( - endpoint => $body->endpoint, - jurisdiction => $body->jurisdiction, - api_key => $body->api_key, + endpoint => $conf->endpoint, + jurisdiction => $conf->jurisdiction, + api_key => $conf->api_key, extended_statuses => $body->send_extended_statuses, fixmystreet_body => $body, ); @@ -119,9 +124,9 @@ sub process_update { return; } - my $o = $self->current_open311; + my $o = $self->construct_open311($body, $comment); - $cobrand->call_hook(open311_pre_send => $comment, $o); + $cobrand->call_hook(open311_pre_send_updates => $comment); my $id = $o->post_service_request_update( $comment ); @@ -141,6 +146,28 @@ sub process_update { } } +sub summary_failures { + my $self = shift; + my $bodies = $self->fetch_bodies; + my $params = $self->construct_query(1); + my $u = FixMyStreet::DB->resultset("Comment") + ->to_body([ keys %$bodies ]) + ->search({ "me.send_fail_count" => { '>', 0 } }) + ->search($params, { join => "problem" }); + + my $base_url = FixMyStreet->config('BASE_URL'); + my $sending_errors; + while (my $row = $u->next) { + my $url = $base_url . "/report/" . $row->problem_id; + $sending_errors .= "\n" . '=' x 80 . "\n\n" . "* $url, update " . $row->id . " failed " + . $row->send_fail_count . " times, last at " . $row->send_fail_timestamp + . ", reason " . $row->send_fail_reason . "\n"; + } + if ($sending_errors) { + print '=' x 80 . "\n\n" . "The following updates failed sending:\n$sending_errors"; + } +} + sub log { my ($self, $comment, $msg) = @_; return unless $self->verbose; diff --git a/perllib/Open311/UpdatesBase.pm b/perllib/Open311/UpdatesBase.pm new file mode 100644 index 000000000..c2f3fae0d --- /dev/null +++ b/perllib/Open311/UpdatesBase.pm @@ -0,0 +1,283 @@ +package Open311::UpdatesBase; + +use Moo; +use Open311; +use Parallel::ForkManager; +use FixMyStreet::DB; + +has send_comments_flag => ( is => 'ro' ); + +has system_user => ( is => 'rw' ); +has body => ( is => 'ro', default => sub { undef } ); +has verbose => ( is => 'ro', default => 0 ); +has schema => ( is =>'ro', lazy => 1, default => sub { FixMyStreet::DB->schema->connect } ); +has suppress_alerts => ( is => 'rw', default => 0 ); +has blank_updates_permitted => ( is => 'rw', default => 0 ); + +has current_body => ( is => 'rw' ); +has current_open311 => ( is => 'rwp', lazy => 1, builder => 1 ); +has open311_config => ( is => 'ro' ); # If we need to pass in a devolved contact + +Readonly::Scalar my $AREA_ID_OXFORDSHIRE => 2237; + +sub fetch { + my ($self, $open311) = @_; + + my $bodies = $self->schema->resultset('Body')->search( + { + send_method => 'Open311', + send_comments => $self->send_comments_flag, + comment_user_id => { '!=', undef }, + endpoint => { '!=', '' }, + } + ); + + if ( $self->body ) { + $bodies = $bodies->search( { name => $self->body } ); + } + + my $procs_min = FixMyStreet->config('FETCH_COMMENTS_PROCESSES_MIN') || 0; + my $procs_max = FixMyStreet->config('FETCH_COMMENTS_PROCESSES_MAX'); + my $procs_timeout = FixMyStreet->config('FETCH_COMMENTS_PROCESS_TIMEOUT'); + + my $pm = Parallel::ForkManager->new(FixMyStreet->test_mode ? 0 : $procs_min); + + if ($procs_max && $procs_timeout) { + my %workers; + $pm->run_on_wait(sub { + while (my ($pid, $started_at) = each %workers) { + next unless time() - $started_at > $procs_timeout; + next if $pm->max_procs == $procs_max; + $pm->set_max_procs($pm->max_procs + 1); + delete $workers{$pid}; # Only want to increase once per long-running thing + } + }, 1); + $pm->run_on_start(sub { my $pid = shift; $workers{$pid} = time(); }); + $pm->run_on_finish(sub { my $pid = shift; delete $workers{$pid}; }); + } + + while ( my $body = $bodies->next ) { + $pm->start and next; + + $self->current_body( $body ); + $self->_set_current_open311( $open311 || $self->_build_current_open311 ); + $self->suppress_alerts( $body->suppress_alerts ); + $self->blank_updates_permitted( $body->blank_updates_permitted ); + $self->system_user( $body->comment_user ); + $self->process_body(); + + $pm->finish; + } + + $pm->wait_all_children; +} + +sub _build_current_open311 { + my $self = shift; + + my $body = $self->current_body; + my $conf = $self->open311_config || $body; + my %open311_conf = ( + endpoint => $conf->endpoint || '', + api_key => $conf->api_key || '', + jurisdiction => $conf->jurisdiction || '', + extended_statuses => $body->send_extended_statuses, + ); + + my $cobrand = $body->get_cobrand_handler; + $cobrand->call_hook(open311_config_updates => \%open311_conf) + if $cobrand; + + return Open311->new(%open311_conf); +} + +sub check_date { + my ($self, $request, @args) = @_; + + my $comment_time = eval { + DateTime::Format::W3CDTF->parse_datetime( $request->{updated_datetime} || "" ) + ->set_time_zone(FixMyStreet->local_time_zone); + }; + return if $@; + my $updated = DateTime::Format::W3CDTF->format_datetime($comment_time->clone->set_time_zone('UTC')); + return if @args && ($updated lt $args[0] || $updated gt $args[1]); + $request->{comment_time} = $comment_time; + return 1; +} + +sub find_problem { + my ($self, $request, @args) = @_; + + $self->check_date($request, @args) or return; + + my $request_id = $request->{service_request_id}; + + # If there's no request id then we can't work out + # what problem it belongs to so just skip + return unless $request_id || $request->{fixmystreet_id}; + + my $criteria = { + external_id => $request_id, + }; + + # in some cases we only have the FMS id and not the request id so use that + if ( $request->{fixmystreet_id} ) { + unless ( $request->{fixmystreet_id} =~ /^\d+$/ ) { + warn "skipping bad fixmystreet id in updates for " . $self->current_body->name . ": [" . $request->{fixmystreet_id} . "], external id is $request_id\n"; + return; + } + + $criteria = { + id => $request->{fixmystreet_id}, + }; + } + + return $self->_find_problem($criteria); +} + +sub process_update { + my ($self, $request, $p) = @_; + my $open311 = $self->current_open311; + my $body = $self->current_body; + + my $state = $open311->map_state( $request->{status} ); + my $old_state = $p->state; + my $external_status_code = $request->{external_status_code} || ''; + my $customer_reference = $request->{customer_reference} || ''; + my $old_external_status_code = $p->get_extra_metadata('external_status_code') || ''; + my $comment = $self->schema->resultset('Comment')->new( + { + problem => $p, + user => $self->system_user, + external_id => $request->{update_id}, + text => $self->comment_text_for_request( + $request, $p, $state, $old_state, + $external_status_code, $old_external_status_code + ), + confirmed => $request->{comment_time}, + created => $request->{comment_time}, + } + ); + + # Some Open311 services, e.g. Confirm via open311-adapter, provide + # a more fine-grained status code that we use within FMS for + # response templates. + if ( $external_status_code ) { + $comment->set_extra_metadata(external_status_code => $external_status_code); + $p->set_extra_metadata(external_status_code => $external_status_code); + } else { + $p->set_extra_metadata(external_status_code => ''); + } + + # if the customer reference to display in the report metadata is + # not the same as the external_id + if ( $customer_reference ) { + $p->set_extra_metadata( customer_reference => $customer_reference ); + } + + $open311->add_media($request->{media_url}, $comment) + if $request->{media_url}; + + # don't update state unless it's an allowed state + if ( FixMyStreet::DB::Result::Problem->visible_states()->{$state} && + # For Oxfordshire, don't allow changes back to Open from other open states + !( $body->areas->{$AREA_ID_OXFORDSHIRE} && $state eq 'confirmed' && $p->is_open ) && + # Don't let it change between the (same in the front end) fixed states + !( $p->is_fixed && FixMyStreet::DB::Result::Problem->fixed_states()->{$state} ) ) { + + $comment->problem_state($state); + + # we only want to update the problem state if that makes sense. We never want to unhide a problem. + # If the update is older than the last update then we also do not want to update the state. This + # is largely to avoid the situation where we miss some updates, make more updates and then catch + # the updates when we fetch the last 24 hours of updates. The exception to this is the first + # comment. This is to catch automated updates which happen faster than we get the external_id + # back from the endpoint and hence have an created time before the lastupdate. + if ( $p->is_visible && $p->state ne $state && + ( $comment->created >= $p->lastupdate || $p->comments->count == 0 ) ) { + $p->state($state); + } + } + + # If nothing to show (no text, photo, or state change), don't show this update + $comment->state('hidden') unless $comment->text || $comment->photo + || ($comment->problem_state && $state ne $old_state); + + my $cobrand = $body->get_cobrand_handler; + $cobrand->call_hook(open311_get_update_munging => $comment) + if $cobrand; + + # As comment->created has been looked at above, its time zone has been shifted + # to TIME_ZONE (if set). We therefore need to set it back to local before + # insertion. We also then need a clone, otherwise the setting of lastupdate + # will *also* reshift comment->created's time zone to TIME_ZONE. + my $created = $comment->created->set_time_zone(FixMyStreet->local_time_zone); + $p->lastupdate($created->clone); + $p->update; + $comment->insert(); + + if ( $self->suppress_alerts ) { + my @alerts = $self->schema->resultset('Alert')->search( { + alert_type => 'new_updates', + parameter => $p->id, + confirmed => 1, + user_id => $p->user->id, + } ); + + for my $alert (@alerts) { + my $alerts_sent = $self->schema->resultset('AlertSent')->find_or_create( { + alert_id => $alert->id, + parameter => $comment->id, + } ); + } + } + + return $comment; +} + +sub comment_text_for_request { + my ($self, $request, $problem, $state, $old_state, + $ext_code, $old_ext_code) = @_; + + # Response templates are only triggered if the state/external status has changed. + # And treat any fixed state as fixed. + my $state_changed = $state ne $old_state + && !( $problem->is_fixed && FixMyStreet::DB::Result::Problem->fixed_states()->{$state} ); + my $ext_code_changed = $ext_code ne $old_ext_code; + my $template; + if ($state_changed || $ext_code_changed) { + my $order; + my $state_params = { + 'me.state' => $state + }; + if ($ext_code) { + $state_params->{'me.external_status_code'} = $ext_code; + # make sure that empty string/nulls come last. + $order = { order_by => \"me.external_status_code DESC NULLS LAST" }; + }; + + if (my $t = $problem->response_templates->search({ + auto_response => 1, + -or => $state_params, + }, $order )->first) { + $template = $t->text; + } + } + + my $desc = $request->{description} || ''; + if ($desc && (!$template || $template !~ /\{\{description}}/)) { + return $desc; + } + + if ($template) { + $template =~ s/\{\{description}}/$desc/; + return $template; + } + + return "" if $self->blank_updates_permitted; + + print STDERR "Couldn't determine update text for $request->{update_id} (report " . $problem->id . ")\n"; + return ""; +} + +1; |