Dealing with lack of claims as a success #64

Merged
bones7242 merged 2 commits from no-claims-found into master 2017-07-01 00:47:19 +02:00
7 changed files with 45 additions and 20 deletions

View file

@ -5,15 +5,19 @@ const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicCla
const isFreePublicClaim = require('../helpers/functions/isFreePublicClaim.js'); const isFreePublicClaim = require('../helpers/functions/isFreePublicClaim.js');
function updateFileIfNeeded (uri, claimName, claimId, localOutpoint, localHeight) { function updateFileIfNeeded (uri, claimName, claimId, localOutpoint, localHeight) {
logger.debug(`A mysql record was found for ${claimId}`); logger.debug(`A mysql record was found for ${claimName}:${claimId}. Initiating resolve to check outpoint.`);
logger.debug('initiating resolve on claim to check outpoint');
// 1. resolve claim // 1. resolve claim
lbryApi lbryApi
.resolveUri(uri) .resolveUri(uri)
.then(result => { .then(result => {
// check to make sure the result is a claim
if (!result.claim) {
logger.debug('resolve did not return a claim');
return;
}
// logger.debug('resolved result:', result); // logger.debug('resolved result:', result);
const resolvedOutpoint = `${result[uri].claim.txid}:${result[uri].claim.nout}`; const resolvedOutpoint = `${result.claim.txid}:${result.claim.nout}`;
const resolvedHeight = result[uri].claim.height; const resolvedHeight = result.claim.height;
logger.debug('database outpoint:', localOutpoint); logger.debug('database outpoint:', localOutpoint);
logger.debug('resolved outpoint:', resolvedOutpoint); logger.debug('resolved outpoint:', resolvedOutpoint);
// 2. if the outpoint's match, no further work needed // 2. if the outpoint's match, no further work needed
@ -107,6 +111,11 @@ module.exports = {
// 1. get the top free, public claims // 1. get the top free, public claims
getAllFreePublicClaims(claimName) getAllFreePublicClaims(claimName)
.then(freePublicClaimList => { .then(freePublicClaimList => {
// check to make sure some claims were found
if (!freePublicClaimList) {
resolve(null);
return;
}
const name = freePublicClaimList[0].name; const name = freePublicClaimList[0].name;
const claimId = freePublicClaimList[0].claim_id; const claimId = freePublicClaimList[0].claim_id;
const uri = `${name}#${claimId}`; const uri = `${name}#${claimId}`;
@ -115,7 +124,7 @@ module.exports = {
db.File db.File
.findOne({ where: { name, claimId } }) .findOne({ where: { name, claimId } })
.then(claim => { .then(claim => {
// 3. if a matching claim_id is found locally, serve it // 3. if a matching record is found locally, serve it
if (claim) { if (claim) {
// serve the file // serve the file
resolve(claim.dataValues); resolve(claim.dataValues);
@ -157,12 +166,18 @@ module.exports = {
lbryApi lbryApi
.resolveUri(uri) .resolveUri(uri)
.then(result => { .then(result => {
// check to make sure the result is a claim
if (!result.claim) {
logger.debug('resolve did not return a claim');
resolve(null);
return;
}
// 4. check to see if the claim is free & public // 4. check to see if the claim is free & public
if (isFreePublicClaim(result[uri].claim)) { if (isFreePublicClaim(result.claim)) {
// 5. get claim and serve // 5. get claim and serve
getClaimAndHandleResponse(uri, result[uri].claim.height, resolve, reject); getClaimAndHandleResponse(uri, result.claim.height, resolve, reject);
} else { } else {
reject('NO_FREE_PUBLIC_CLAIMS'); reject(null);
} }
}) })
.catch(error => { .catch(error => {

View file

@ -6,7 +6,7 @@ const googleApiKey = config.get('AnalyticsConfig.GoogleId');
module.exports = { module.exports = {
postToStats: (action, url, ipAddress, result) => { postToStats: (action, url, ipAddress, result) => {
logger.silly('creating record for statistics db'); logger.silly(`creating ${action} record for statistics db`);
// make sure the result is a string // make sure the result is a string
if (result && (typeof result !== 'string')) { if (result && (typeof result !== 'string')) {
result = result.toString(); result = result.toString();

View file

@ -34,20 +34,19 @@ module.exports = claimName => {
lbryApi lbryApi
.getClaimsList(claimName) .getClaimsList(claimName)
.then(({ claims }) => { .then(({ claims }) => {
const claimsList = claims; logger.debug(`Number of claims: ${claims.length}`);
logger.debug(`Number of claims: ${claimsList.length}`);
// return early if no claims were found // return early if no claims were found
if (claimsList.length === 0) { if (claims.length === 0) {
reject('NO_CLAIMS');
logger.debug('exiting due to lack of claims'); logger.debug('exiting due to lack of claims');
resolve(null);
return; return;
} }
// filter the claims to return only free, public claims // filter the claims to return only free, public claims
const freePublicClaims = filterForFreePublicClaims(claimsList); const freePublicClaims = filterForFreePublicClaims(claims);
// return early if no free, public claims were found // return early if no free, public claims were found
if (!freePublicClaims || freePublicClaims.length === 0) { if (!freePublicClaims || freePublicClaims.length === 0) {
reject('NO_FREE_PUBLIC_CLAIMS');
logger.debug('exiting due to lack of free or public claims'); logger.debug('exiting due to lack of free or public claims');
resolve(null);
return; return;
} }
// order the claims // order the claims

View file

@ -4,10 +4,7 @@ const { postToStats } = require('../../controllers/statsController.js');
module.exports = { module.exports = {
handleRequestError (action, originalUrl, ip, error, res) { handleRequestError (action, originalUrl, ip, error, res) {
logger.error('Request Error >>', error); logger.error('Request Error >>', error);
if (error === 'NO_CLAIMS' || error === 'NO_FREE_PUBLIC_CLAIMS') { if (error.response) {
postToStats(action, originalUrl, ip, 'success');
res.status(307).render('noClaims');
} else if (error.response) {
postToStats(action, originalUrl, ip, error.response.data.error.messsage); postToStats(action, originalUrl, ip, error.response.data.error.messsage);
res.status(error.response.status).send(error.response.data.error.message); res.status(error.response.status).send(error.response.data.error.message);
} else if (error.code === 'ECONNREFUSED') { } else if (error.code === 'ECONNREFUSED') {

View file

@ -78,7 +78,7 @@ module.exports = {
reject(data.result[uri].error); reject(data.result[uri].error);
return; return;
} }
resolve(data.result); resolve(data.result[uri]);
}) })
.catch(error => { .catch(error => {
reject(error); reject(error);

View file

@ -47,6 +47,11 @@ module.exports = (app) => {
serveController serveController
.getClaimByClaimId(params.name, params.claim_id) .getClaimByClaimId(params.name, params.claim_id)
.then(fileInfo => { .then(fileInfo => {
// check to make sure a file was found
if (!fileInfo) {
res.status(307).render('noClaims');
return;
}
postToStats('serve', originalUrl, ip, 'success'); postToStats('serve', originalUrl, ip, 'success');
serveFile(fileInfo, res); serveFile(fileInfo, res);
}) })
@ -61,6 +66,11 @@ module.exports = (app) => {
serveController serveController
.getClaimByName(params.name) .getClaimByName(params.name)
.then(fileInfo => { .then(fileInfo => {
// check to make sure a file was found
if (!fileInfo) {
res.status(307).render('noClaims');
return;
}
postToStats('serve', originalUrl, ip, 'success'); postToStats('serve', originalUrl, ip, 'success');
serveFile(fileInfo, res); serveFile(fileInfo, res);
}) })

View file

@ -44,6 +44,10 @@ module.exports = (app) => {
// get and render the content // get and render the content
getAllClaims(params.name) getAllClaims(params.name)
.then(orderedFreePublicClaims => { .then(orderedFreePublicClaims => {
if (!orderedFreePublicClaims) {
res.status(307).render('noClaims');
return;
}
postToStats('show', originalUrl, ip, 'success'); postToStats('show', originalUrl, ip, 'success');
res.status(200).render('allClaims', { claims: orderedFreePublicClaims }); res.status(200).render('allClaims', { claims: orderedFreePublicClaims });
}) })