2017-06-19 18:37:35 +02:00
|
|
|
const lbryApi = require('../helpers/libraries/lbryApi.js');
|
|
|
|
const db = require('../models');
|
2017-06-14 00:39:38 +02:00
|
|
|
|
2017-06-19 18:37:35 +02:00
|
|
|
const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicClaims.js');
|
|
|
|
const isFreePublicClaim = require('../helpers/functions/isFreePublicClaim.js');
|
2017-06-14 00:39:38 +02:00
|
|
|
|
2017-06-17 22:51:30 +02:00
|
|
|
function getClaimAndHandleResponse (claimUri, resolve, reject) {
|
|
|
|
lbryApi
|
|
|
|
.getClaim(claimUri)
|
|
|
|
.then(({ file_name, download_path, mime_type }) => {
|
|
|
|
resolve({
|
|
|
|
file_name,
|
|
|
|
file_path: download_path,
|
|
|
|
file_type: mime_type,
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
2017-06-16 21:29:02 +02:00
|
|
|
}
|
|
|
|
|
2017-06-14 00:39:38 +02:00
|
|
|
module.exports = {
|
2017-06-17 22:51:30 +02:00
|
|
|
getClaimByName (claimName) {
|
|
|
|
const deferred = new Promise((resolve, reject) => {
|
2017-06-19 18:37:35 +02:00
|
|
|
console.log('>> lbryHelpers >> getClaim BasedOnNameOnly:', claimName);
|
2017-06-17 22:51:30 +02:00
|
|
|
// get all free public claims
|
|
|
|
getAllFreePublicClaims(claimName)
|
|
|
|
.then(freePublicClaimList => {
|
2017-06-19 18:37:35 +02:00
|
|
|
const claimId = freePublicClaimList[0].claim_id;
|
|
|
|
const name = freePublicClaimList[0].name;
|
|
|
|
const freePublicClaimOutpoint = `${freePublicClaimList[0].txid}:${freePublicClaimList[0].nout}`;
|
|
|
|
const freePublicClaimUri = name + '#' + claimId;
|
|
|
|
console.log('>> Decided on public claim id:', claimId);
|
2017-06-17 22:51:30 +02:00
|
|
|
// check to see if the file is available locally
|
|
|
|
db.File
|
2017-06-17 23:00:24 +02:00
|
|
|
.findOne({ where: { name: name, claim_id: claimId } })
|
2017-06-17 22:51:30 +02:00
|
|
|
.then(claim => {
|
2017-06-17 23:00:24 +02:00
|
|
|
// if a matching claim is found locally...
|
2017-06-17 22:51:30 +02:00
|
|
|
if (claim) {
|
2017-06-19 18:37:35 +02:00
|
|
|
console.log('>> A matching claim_id was found locally');
|
2017-06-17 22:51:30 +02:00
|
|
|
// if the outpoint's match return it
|
|
|
|
if (claim.dataValues.outpoint === freePublicClaimOutpoint) {
|
2017-06-19 18:37:35 +02:00
|
|
|
console.log('>> Local outpoint matched');
|
|
|
|
resolve(claim.dataValues);
|
2017-06-17 22:51:30 +02:00
|
|
|
// if the outpoint's don't match, fetch updated claim
|
|
|
|
} else {
|
2017-06-19 18:37:35 +02:00
|
|
|
console.log('>> local outpoint did not match');
|
|
|
|
getClaimAndHandleResponse(freePublicClaimUri, resolve, reject);
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
|
|
|
// ... otherwise use daemon to retrieve it
|
|
|
|
} else {
|
|
|
|
// 'get' the claim
|
2017-06-19 18:37:35 +02:00
|
|
|
getClaimAndHandleResponse(freePublicClaimUri, resolve, reject);
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return deferred;
|
2017-06-17 22:51:30 +02:00
|
|
|
},
|
|
|
|
getClaimByClaimId (claimName, claimId) {
|
|
|
|
const deferred = new Promise((resolve, reject) => {
|
2017-06-19 18:37:35 +02:00
|
|
|
const uri = `${claimName}#${claimId}`;
|
|
|
|
console.log('>> lbryHelpers >> getClaimBasedOnUri:', uri);
|
2017-06-17 22:51:30 +02:00
|
|
|
// resolve the Uri
|
|
|
|
lbryApi
|
|
|
|
.resolveUri(uri) // note: use 'spread' and make parallel with db.File.findOne()
|
|
|
|
.then(result => {
|
|
|
|
// note should just be 'result' returned.
|
|
|
|
// get the outpoint
|
2017-06-19 18:37:35 +02:00
|
|
|
const resolvedOutpoint = `${result[uri].claim.txid}:${result[uri].claim.nout}`;
|
2017-06-17 22:51:30 +02:00
|
|
|
// check locally for the claim
|
|
|
|
db.File
|
|
|
|
.findOne({ where: { claim_id: claimId } })
|
|
|
|
.then(claim => {
|
|
|
|
// if a found locally...
|
|
|
|
if (claim) {
|
2017-06-19 18:37:35 +02:00
|
|
|
console.log('>> A matching claim_id was found locally');
|
2017-06-17 22:51:30 +02:00
|
|
|
// if the outpoint's match return it
|
|
|
|
if (claim.dataValues.outpoint === resolvedOutpoint) {
|
2017-06-19 18:37:35 +02:00
|
|
|
console.log('>> Local outpoint matched');
|
|
|
|
resolve(claim.dataValues);
|
2017-06-17 22:51:30 +02:00
|
|
|
// if the outpoint's don't match, fetch updated claim
|
|
|
|
} else {
|
2017-06-19 18:37:35 +02:00
|
|
|
console.log('>> Local outpoint did not match');
|
|
|
|
getClaimAndHandleResponse(uri, resolve, reject);
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
|
|
|
// ... otherwise use daemon to retrieve it
|
|
|
|
} else {
|
|
|
|
// check to make sure it is free and public (note: no need for another resolve?)
|
|
|
|
if (isFreePublicClaim(result[uri].claim)) {
|
|
|
|
// 'get' the claim
|
2017-06-19 18:37:35 +02:00
|
|
|
getClaimAndHandleResponse(uri, resolve, reject);
|
2017-06-17 22:51:30 +02:00
|
|
|
} else {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject('NO_FREE_PUBLIC_CLAIMS');
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return deferred;
|
2017-06-17 22:51:30 +02:00
|
|
|
},
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|