import { fetch } from "xhr" ; import { stringify } from "codec/query_string" ; interface SightEngineResponse { status : string ; request : { id : string ; timestampt : number ; operations : number ; } ; media : { id : string ; url : string ; } ; nudity : { raw : number ; partial : number ; safe : number ; } ; weapon : number ; alcohol : number ; drugs : number ; offensive : { prob : number ; } ; } const max = ( numbers ) : number => { return numbers . reduce ( ( running , current ) => current > running ? current : running ) ; } ; export const isSafe = async ( url , threshold ) : Promise < boolean > => { const secret = process . env . FUNCTIONS_SIGHTENGINE_API_SECRET ; const user = process . env . FUNCTIONS_SIGHTENGINE_API_USER ; if ( ! ( secret && user ) ) { return true ; } const options = { models : [ "nudity" , "wad" , "offensive" ] . join ( "," ) , api_user : user , api_secret : secret , url : url , } ; try { const response = await fetch ( `https://api.sightengine.com/1.0/check.json?${ stringify ( options ) }` ) ; if ( response . ok ) { const { nudity : { safe : noNudity } , offensive : { prob : offensive } , weapon , alcohol , drugs , } = JSON . parse ( response . body ) as SightEngineResponse ; return ( max ( [ 1 - noNudity , offensive , weapon , alcohol , drugs ] ) <= threshold ) ; } else { return false ; } } catch ( e ) { return false ; } } ;