spee.ch/server/controllers/api/claim/longId/index.js

66 lines
2 KiB
JavaScript
Raw Normal View History

const db = require('server/models');
2018-12-14 18:42:37 +01:00
const chainquery = require('chainquery').default;
2018-04-29 22:00:41 +02:00
const { handleErrorResponse } = require('server/controllers/utils/errorHandlers.js');
2018-03-29 02:35:41 +02: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-03-29 20:24:52 +02:00
getClaimId(channelName, channelClaimId, claimName, claimId)
.then(fullClaimId => {
2018-04-29 22:00:41 +02:00
claimId = fullClaimId;
return chainquery.claim.queries.getOutpoint(claimName, fullClaimId).catch(() => {});
2018-09-26 03:20:59 +02:00
})
.then(outpointResult => {
if (!outpointResult) {
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(() => {
2019-03-18 04:13:00 +01:00
res.status(200).json({ success: true, data: claimId });
2018-03-29 20:24:52 +02:00
})
.catch(error => {
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',
});
}
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,
2019-03-18 04:13:00 +01:00
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.com/faq/dmca',
});
}
2018-03-29 20:24:52 +02:00
handleErrorResponse(originalUrl, ip, error, res);
});
2018-03-29 02:35:41 +02:00
};
module.exports = claimLongId;