2018-10-30 21:53:29 +01:00
const db = require ( 'server/models' ) ;
2018-09-26 03:20:59 +02:00
const chainquery = require ( 'chainquery' ) ;
2018-04-29 22:00:41 +02:00
2018-10-30 21:53:29 +01:00
const { handleErrorResponse } = require ( 'server/controllers/utils/errorHandlers.js' ) ;
2018-03-29 02:35:41 +02:00
2018-10-30 21:53:29 +01:00
const getClaimId = require ( 'server/controllers/utils/getClaimId.js' ) ;
2018-04-29 22:00:41 +02:00
2018-03-29 02:35:41 +02:00
const NO _CHANNEL = 'NO_CHANNEL' ;
const NO _CLAIM = 'NO_CLAIM' ;
2018-04-29 22:00:41 +02:00
const BLOCKED _CLAIM = 'BLOCKED_CLAIM' ;
2018-03-29 02:35:41 +02:00
/ *
route to get a long claim id
* /
2018-03-29 20:24:52 +02:00
const claimLongId = ( { ip , originalUrl , body , params } , res ) => {
const channelName = body . channelName ;
const channelClaimId = body . channelClaimId ;
const claimName = body . claimName ;
2018-04-29 22:00:41 +02:00
let claimId = body . claimId ;
2018-10-09 02:02:05 +02:00
2018-03-29 20:24:52 +02:00
getClaimId ( channelName , channelClaimId , claimName , claimId )
2018-04-29 21:17:23 +02:00
. then ( fullClaimId => {
2018-04-29 22:00:41 +02:00
claimId = fullClaimId ;
2018-10-09 02:02:05 +02:00
return chainquery . claim . queries . getOutpoint ( claimName , fullClaimId ) . catch ( ( ) => { } ) ;
2018-09-26 03:20:59 +02:00
} )
. then ( outpointResult => {
if ( ! outpointResult ) {
2018-10-09 02:02:05 +02:00
return db . Claim . getOutpoint ( claimName , claimId ) ;
2018-09-26 03:20:59 +02:00
}
return outpointResult ;
2018-07-02 22:53:52 +02:00
} )
. then ( outpoint => {
return db . Blocked . isNotBlocked ( outpoint ) ;
2018-04-29 22:00:41 +02:00
} )
. then ( ( ) => {
res . status ( 200 ) . json ( { success : true , data : claimId } ) ;
2018-03-29 20:24:52 +02:00
} )
. catch ( error => {
2018-04-29 21:17:23 +02:00
if ( error === NO _CLAIM ) {
return res . status ( 404 ) . json ( {
success : false ,
2018-04-29 22:00:41 +02:00
message : 'No matching claim id could be found for that url' ,
2018-04-29 21:17:23 +02:00
} ) ;
}
if ( error === NO _CHANNEL ) {
return res . status ( 404 ) . json ( {
success : false ,
2018-04-29 22:00:41 +02:00
message : 'No matching channel id could be found for that url' ,
} ) ;
}
if ( error === BLOCKED _CLAIM ) {
return res . status ( 410 ) . json ( {
success : false ,
message : 'In response to a complaint we received under the US Digital Millennium Copyright Act, we have blocked access to this content from our applications. For more details, see https://lbry.io/faq/dmca' ,
2018-04-29 21:17:23 +02:00
} ) ;
}
2018-03-29 20:24:52 +02:00
handleErrorResponse ( originalUrl , ip , error , res ) ;
} ) ;
2018-03-29 02:35:41 +02:00
} ;
module . exports = claimLongId ;