Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/controllers/v1/computervision_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ const ComputervisionController = class ComputervisionController {
if ( !req.userSession && !req.applicationSession ) {
throw util.httpError( 401, "Unauthorized" );
}
if ( req.query.photo_id && Number.isNaN( req.query.photo_id ) ) {
throw util.httpError( 422, "photo_id is not a number" );
}
const obsID = Number( req.params.id );
if ( !obsID ) {
throw util.httpError( 422, "Missing observation ID or UUID" );
Expand All @@ -79,14 +82,17 @@ const ComputervisionController = class ComputervisionController {
let photoURL;
_.each( observation.photos, p => {
if ( photoURL ) { return; }
if ( req.query.photo_id && p.id !== Number( req.query.photo_id ) ) { return; }
if ( !p.url ) { return; }
photoURL = p.url;
if ( photoURL.match( /\/square\./i ) ) {
photoURL = p.url.replace( "/square.", "/medium." );
}
} );
if ( !photoURL ) {
if ( !photoURL && !req.query.photo_id ) {
throw util.httpError( 422, "Observation has no scorable photos" );
} else if ( !photoURL ) {
throw util.httpError( 422, "Observation has no scorable photos or no photo with the provided id" );
}
req.query.image_url = photoURL;
return ComputervisionController.scoreImageURL( req, { observation } );
Expand Down
6 changes: 6 additions & 0 deletions openapi/paths/v2/computervision/score_observation/{uuid}.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ module.exports = sendWrapper => {
.required( )
.description( "A single observation UUID" )
),
transform(
Joi.number( ).integer( )
.label( "photo_id" )
.meta( { in: "query" } )
.description( "The photo id associated with the observation to use for suggestions." )
),
transform(
Joi.string( ).label( "fields" ).meta( { in: "query" } )
.description( `
Expand Down
24 changes: 24 additions & 0 deletions test/integration/v2/computervision.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@ describe( "Computervision", ( ) => {
.set( "Authorization", token )
.expect( 401, done );
} );
it( "filters for photo id if provided", function ( done ) {
const token = jwt.sign(
{ application: "whatever" },
config.jwtApplicationSecret || "application_secret",
{ algorithm: "HS512" }
);
request( this.app ).get( `${url}?photo_id=1` )
.set( "Authorization", token )
.expect( 200 )
.expect( res => {
expect( res.body.results.length ).to.be.above( 0 );
} )
.expect( 200, done );
} );
it( "should fail when given a photo id not attributed to the observation", function ( done ) {
const token = jwt.sign(
{ application: "whatever" },
config.jwtApplicationSecret || "application_secret",
{ algorithm: "HS512" }
);
request( this.app ).get( `${url}?photo_id=2020101601` )
.set( "Authorization", token )
.expect( 422, done );
} );
} );
describe( "score_image", ( ) => {
const token = jwt.sign(
Expand Down