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

71 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-04-27 18:54:36 +02:00
const { getClaim } = require('../../../../lbrynet');
const { createFileRecordDataAfterGet } = require('../../../../models/utils/createFileRecordData.js');
2018-04-27 18:54:36 +02:00
const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
2018-09-26 03:20:59 +02:00
const getClaimData = require('server/utils/getClaimData');
const chainquery = require('chainquery');
2018-04-27 18:54:36 +02:00
const db = require('../../../../models');
2018-03-29 02:35:41 +02:00
/*
route to get a claim
*/
2018-03-29 20:24:52 +02:00
const claimGet = ({ ip, originalUrl, params }, res) => {
const name = params.name;
const claimId = params.claimId;
2018-07-26 02:21:02 +02:00
let resolveResult;
let getResult;
2018-09-26 03:20:59 +02:00
chainquery.claim.queries.resolveClaim(name, claimId)
.then(result => {
if (!result) {
// could not find remote, return false to try local
return false;
}
return resolveResult = result;
})
.then(result => {
if (result === false) {
// Could not find remote, try local
return db.Claim.resolveClaim(name, claimId);
}
return result;
})
2018-07-26 02:21:02 +02:00
.then(result => {
if (!result) {
2018-03-29 20:24:52 +02:00
throw new Error('No matching uri found in Claim table');
}
2018-09-26 03:20:59 +02:00
return resolveResult = result;
2018-03-29 20:24:52 +02:00
})
2018-09-26 03:20:59 +02:00
.then(result => getClaim(`${name}#${claimId}`))
2018-07-26 02:21:02 +02:00
.then(result => {
2018-08-07 21:54:13 +02:00
if (!result) {
throw new Error(`Unable to Get ${name}#${claimId}`);
}
2018-07-26 02:21:02 +02:00
getResult = result;
if (result.completed) {
2018-09-26 03:20:59 +02:00
return createFileRecordDataAfterGet(getClaimData(resolveResult), getResult)
.then(fileData => {
const upsertCriteria = {name, claimId};
return db.upsert(db.File, fileData, upsertCriteria, 'File');
});
}
2018-03-29 20:24:52 +02:00
})
2018-07-26 02:21:02 +02:00
.then(() => {
2018-07-26 02:29:59 +02:00
const { message, completed } = getResult;
2018-07-26 02:21:02 +02:00
res.status(200).json({
success: true,
message,
completed,
});
2018-03-29 20:24:52 +02:00
})
.catch(error => {
handleErrorResponse(originalUrl, ip, error, res);
});
2018-03-29 02:35:41 +02:00
};
module.exports = claimGet;