Skip to content

Commit

Permalink
[FMS][Open311] Send images when comment on private report
Browse files Browse the repository at this point in the history
If an image is on a comment which was made on a private report
Open311 can not download the image as it will not be visible.

In this case, send the photos directly to open311

mysociety/societyworks#3709
  • Loading branch information
MorayMySoc committed Jan 2, 2024
1 parent 90f85ad commit 2119843
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
1 change: 1 addition & 0 deletions perllib/FixMyStreet/Roles/Open311Alloy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ sub open311_config {

$params->{multi_photos} = 1;
$params->{upload_files} = 1;
$params->{upload_files_for_updates} = 1;
}

sub open311_extra_data_include {
Expand Down
33 changes: 29 additions & 4 deletions perllib/Open311.pm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ has extended_statuses => ( is => 'ro', isa => Bool, default => 0 );
has always_send_email => ( is => 'ro', isa => Bool, default => 0 );
has multi_photos => ( is => 'ro', isa => Bool, default => 0 );
has upload_files => ( is => 'ro', isa => Bool, default => 0 );
has upload_files_for_updates => ( is => 'ro', isa => Bool, default => 0 );
has always_upload_photos => ( is => 'ro', isa => Bool, default => 0 );
has use_customer_reference => ( is => 'ro', isa => Bool, default => 0 );
has mark_reopen => ( is => 'ro', isa => Bool, default => 0 );
Expand Down Expand Up @@ -234,12 +235,29 @@ sub _populate_service_request_uploads {
}
}

if ( $self->always_upload_photos || ( $problem->photo && $problem->non_public ) ) {
%$uploads = (%$uploads, %{$self->_add_photos_to_upload($problem, $params)});

return $uploads;
}

sub _add_photos_to_upload {
my ($self, $obj, $params) = @_;

my $non_public;
if (ref $obj eq 'FixMyStreet::DB::Result::Problem') {
$non_public = $obj->non_public
} elsif (ref $obj eq 'FixMyStreet::DB::Result::Comment') {
$non_public = $obj->problem->non_public;
};

my $uploads = {};

if ( $self->always_upload_photos || ( $obj->photo && $non_public ) ) {
# open311-adapter won't be able to download any photos if they're on
# a private report, so instead of sending the media_url parameter
# a private report or on a comment on a private report, so instead of sending the media_url parameter
# send the actual photo content with the POST request.
my $i = 0;
my $photoset = $problem->get_photoset;
my $photoset = $obj->get_photoset;
for ( $photoset->all_ids ) {
my $photo = $photoset->get_image_data( num => $i++, size => 'full' );
$uploads->{"photo$i"} = [ undef, $_, Content_Type => $photo->{content_type}, Content => $photo->{data} ];
Expand All @@ -250,6 +268,7 @@ sub _populate_service_request_uploads {
return $uploads;
}


sub _generate_service_request_description {
my $self = shift;
my $problem = shift;
Expand Down Expand Up @@ -342,7 +361,13 @@ sub post_service_request_update {

my $params = $self->_populate_service_request_update_params( $comment );

my $response = $self->_post( $self->endpoints->{update}, $params );
my $response;
if ($self->upload_files_for_updates && $comment->photo) {
my $uploads = $self->_add_photos_to_upload($comment, $params);
$response = $self->_post( $self->endpoints->{update}, $params, $uploads);
} else {
$response = $self->_post( $self->endpoints->{update}, $params);
}

if ( $response ) {
my $obj = $self->_get_xml_object( $response );
Expand Down
38 changes: 38 additions & 0 deletions t/open311.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use CGI::Simple;
use HTTP::Response;
use DateTime;
use DateTime::Format::W3CDTF;
use utf8;
use Encode;

use_ok( 'Open311' );

Expand Down Expand Up @@ -512,6 +514,42 @@ subtest 'check media url set' => sub {
};
};

subtest 'check images sent directly when comment on private report' => sub {
my $UPLOAD_DIR = tempdir( CLEANUP => 1 );

my $image_path = path('t/app/controller/sample.jpg');
$image_path->copy( path( $UPLOAD_DIR, '0123456789012345678901234567890123456789.jpeg' ) );

my $comment = make_comment('fixmystreet');
$comment->photo("0123456789012345678901234567890123456789");

FixMyStreet::override_config {
PHOTO_STORAGE_BACKEND => 'FileSystem',
PHOTO_STORAGE_OPTIONS => {
UPLOAD_DIR => $UPLOAD_DIR,
},
}, sub {
$problem->non_public(1);
my $results = make_update_req( $comment, '<?xml version="1.0" encoding="utf-8"?><service_request_updates><request_update><update_id>248</update_id></request_update></service_request_updates>', { upload_files_for_updates => 1 } );

is $results->{ res }, 248, 'got update id';
my $found = 0;
foreach ($results->{ req }->parts) {
my $cd = $_->header('Content-Disposition');
if ($cd =~ /description/) {
is decode_utf8($_->content), 'this is a comment', 'Correct description';
$found++;
}
if ($cd =~ /jpeg/) {
is $_->header('Content-Type'), 'image/jpeg', 'Correct image content type';
$found++;
}
}
is $found, 2, 'Found all tested headers';
$problem->non_public(0);
};
};

foreach my $test (
{
desc => 'comment with fixed state sends status of CLOSED',
Expand Down

0 comments on commit 2119843

Please sign in to comment.