From f7baf5aac19cb3deb91851a0829a8e0fd07c1eee Mon Sep 17 00:00:00 2001 From: bill bittner Date: Fri, 30 Jun 2017 14:37:47 -0700 Subject: [PATCH 1/2] simplified lbryApi.resolve to only return the single uri object --- controllers/serveController.js | 24 +++++++++++++++------ controllers/statsController.js | 2 +- helpers/functions/getAllFreePublicClaims.js | 4 ++-- helpers/libraries/lbryApi.js | 2 +- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/controllers/serveController.js b/controllers/serveController.js index 4017bd33..b83b68e7 100644 --- a/controllers/serveController.js +++ b/controllers/serveController.js @@ -5,15 +5,19 @@ const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicCla const isFreePublicClaim = require('../helpers/functions/isFreePublicClaim.js'); function updateFileIfNeeded (uri, claimName, claimId, localOutpoint, localHeight) { - logger.debug(`A mysql record was found for ${claimId}`); - logger.debug('initiating resolve on claim to check outpoint'); + logger.debug(`A mysql record was found for ${claimName}:${claimId}. Initiating resolve to check outpoint.`); // 1. resolve claim lbryApi .resolveUri(uri) .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); - const resolvedOutpoint = `${result[uri].claim.txid}:${result[uri].claim.nout}`; - const resolvedHeight = result[uri].claim.height; + const resolvedOutpoint = `${result.claim.txid}:${result.claim.nout}`; + const resolvedHeight = result.claim.height; logger.debug('database outpoint:', localOutpoint); logger.debug('resolved outpoint:', resolvedOutpoint); // 2. if the outpoint's match, no further work needed @@ -157,12 +161,18 @@ module.exports = { lbryApi .resolveUri(uri) .then(result => { + // check to make sure the result is a claim + if (!result.claim) { + logger.debug('resolve did not return a claim'); + reject('NO_FREE_PUBLIC_CLAIMS'); // note: should be a resolve not a reject! but I need routes to handle that properly. right now it is handled as an error. + return; + } // 4. check to see if the claim is free & public - if (isFreePublicClaim(result[uri].claim)) { + if (isFreePublicClaim(result.claim)) { // 5. get claim and serve - getClaimAndHandleResponse(uri, result[uri].claim.height, resolve, reject); + getClaimAndHandleResponse(uri, result.claim.height, resolve, reject); } else { - reject('NO_FREE_PUBLIC_CLAIMS'); + reject('NO_FREE_PUBLIC_CLAIMS'); // note: should be a resolve not a reject! but I need routes to handle that properly. right now it is handled as an error. } }) .catch(error => { diff --git a/controllers/statsController.js b/controllers/statsController.js index 0e736d66..0cd3da95 100644 --- a/controllers/statsController.js +++ b/controllers/statsController.js @@ -6,7 +6,7 @@ const googleApiKey = config.get('AnalyticsConfig.GoogleId'); module.exports = { 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 if (result && (typeof result !== 'string')) { result = result.toString(); diff --git a/helpers/functions/getAllFreePublicClaims.js b/helpers/functions/getAllFreePublicClaims.js index 0f9c30a2..b498bd09 100644 --- a/helpers/functions/getAllFreePublicClaims.js +++ b/helpers/functions/getAllFreePublicClaims.js @@ -38,7 +38,7 @@ module.exports = claimName => { logger.debug(`Number of claims: ${claimsList.length}`); // return early if no claims were found if (claimsList.length === 0) { - reject('NO_CLAIMS'); + reject('NO_CLAIMS'); // note: should be a resolve not a reject! but I need routes to handle that properly. right now it is handled as an error. logger.debug('exiting due to lack of claims'); return; } @@ -46,7 +46,7 @@ module.exports = claimName => { const freePublicClaims = filterForFreePublicClaims(claimsList); // return early if no free, public claims were found if (!freePublicClaims || freePublicClaims.length === 0) { - reject('NO_FREE_PUBLIC_CLAIMS'); + reject('NO_FREE_PUBLIC_CLAIMS'); // note: should be a resolve not a reject! but I need routes to handle that properly. right now it is handled as an error. logger.debug('exiting due to lack of free or public claims'); return; } diff --git a/helpers/libraries/lbryApi.js b/helpers/libraries/lbryApi.js index 026e1eee..b240b6c5 100644 --- a/helpers/libraries/lbryApi.js +++ b/helpers/libraries/lbryApi.js @@ -78,7 +78,7 @@ module.exports = { reject(data.result[uri].error); return; } - resolve(data.result); + resolve(data.result[uri]); }) .catch(error => { reject(error); -- 2.45.2 From 6dea616d4496974966bdc89cf5abea8546e099da Mon Sep 17 00:00:00 2001 From: bill bittner Date: Fri, 30 Jun 2017 15:31:23 -0700 Subject: [PATCH 2/2] the no-claims scenario is now a success case --- controllers/serveController.js | 11 ++++++++--- helpers/functions/getAllFreePublicClaims.js | 11 +++++------ helpers/libraries/errorHandlers.js | 5 +---- routes/serve-routes.js | 10 ++++++++++ routes/show-routes.js | 4 ++++ 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/controllers/serveController.js b/controllers/serveController.js index b83b68e7..3d068994 100644 --- a/controllers/serveController.js +++ b/controllers/serveController.js @@ -111,6 +111,11 @@ module.exports = { // 1. get the top free, public claims getAllFreePublicClaims(claimName) .then(freePublicClaimList => { + // check to make sure some claims were found + if (!freePublicClaimList) { + resolve(null); + return; + } const name = freePublicClaimList[0].name; const claimId = freePublicClaimList[0].claim_id; const uri = `${name}#${claimId}`; @@ -119,7 +124,7 @@ module.exports = { db.File .findOne({ where: { name, claimId } }) .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) { // serve the file resolve(claim.dataValues); @@ -164,7 +169,7 @@ module.exports = { // check to make sure the result is a claim if (!result.claim) { logger.debug('resolve did not return a claim'); - reject('NO_FREE_PUBLIC_CLAIMS'); // note: should be a resolve not a reject! but I need routes to handle that properly. right now it is handled as an error. + resolve(null); return; } // 4. check to see if the claim is free & public @@ -172,7 +177,7 @@ module.exports = { // 5. get claim and serve getClaimAndHandleResponse(uri, result.claim.height, resolve, reject); } else { - reject('NO_FREE_PUBLIC_CLAIMS'); // note: should be a resolve not a reject! but I need routes to handle that properly. right now it is handled as an error. + reject(null); } }) .catch(error => { diff --git a/helpers/functions/getAllFreePublicClaims.js b/helpers/functions/getAllFreePublicClaims.js index b498bd09..58d35157 100644 --- a/helpers/functions/getAllFreePublicClaims.js +++ b/helpers/functions/getAllFreePublicClaims.js @@ -34,20 +34,19 @@ module.exports = claimName => { lbryApi .getClaimsList(claimName) .then(({ claims }) => { - const claimsList = claims; - logger.debug(`Number of claims: ${claimsList.length}`); + logger.debug(`Number of claims: ${claims.length}`); // return early if no claims were found - if (claimsList.length === 0) { - reject('NO_CLAIMS'); // note: should be a resolve not a reject! but I need routes to handle that properly. right now it is handled as an error. + if (claims.length === 0) { logger.debug('exiting due to lack of claims'); + resolve(null); return; } // 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 if (!freePublicClaims || freePublicClaims.length === 0) { - reject('NO_FREE_PUBLIC_CLAIMS'); // note: should be a resolve not a reject! but I need routes to handle that properly. right now it is handled as an error. logger.debug('exiting due to lack of free or public claims'); + resolve(null); return; } // order the claims diff --git a/helpers/libraries/errorHandlers.js b/helpers/libraries/errorHandlers.js index 45264a86..4d922c3b 100644 --- a/helpers/libraries/errorHandlers.js +++ b/helpers/libraries/errorHandlers.js @@ -4,10 +4,7 @@ const { postToStats } = require('../../controllers/statsController.js'); module.exports = { handleRequestError (action, originalUrl, ip, error, res) { logger.error('Request Error >>', error); - if (error === 'NO_CLAIMS' || error === 'NO_FREE_PUBLIC_CLAIMS') { - postToStats(action, originalUrl, ip, 'success'); - res.status(307).render('noClaims'); - } else if (error.response) { + if (error.response) { postToStats(action, originalUrl, ip, error.response.data.error.messsage); res.status(error.response.status).send(error.response.data.error.message); } else if (error.code === 'ECONNREFUSED') { diff --git a/routes/serve-routes.js b/routes/serve-routes.js index cfbf11a3..f6ecb35a 100644 --- a/routes/serve-routes.js +++ b/routes/serve-routes.js @@ -47,6 +47,11 @@ module.exports = (app) => { serveController .getClaimByClaimId(params.name, params.claim_id) .then(fileInfo => { + // check to make sure a file was found + if (!fileInfo) { + res.status(307).render('noClaims'); + return; + } postToStats('serve', originalUrl, ip, 'success'); serveFile(fileInfo, res); }) @@ -61,6 +66,11 @@ module.exports = (app) => { serveController .getClaimByName(params.name) .then(fileInfo => { + // check to make sure a file was found + if (!fileInfo) { + res.status(307).render('noClaims'); + return; + } postToStats('serve', originalUrl, ip, 'success'); serveFile(fileInfo, res); }) diff --git a/routes/show-routes.js b/routes/show-routes.js index 945fc162..5e54c9b9 100644 --- a/routes/show-routes.js +++ b/routes/show-routes.js @@ -44,6 +44,10 @@ module.exports = (app) => { // get and render the content getAllClaims(params.name) .then(orderedFreePublicClaims => { + if (!orderedFreePublicClaims) { + res.status(307).render('noClaims'); + return; + } postToStats('show', originalUrl, ip, 'success'); res.status(200).render('allClaims', { claims: orderedFreePublicClaims }); }) -- 2.45.2