replaced getAllFreePublicClaims with calls to Claim table

This commit is contained in:
bill bittner 2017-08-15 15:35:03 -07:00
parent 13c5f5dfdb
commit 4981551c59
5 changed files with 72 additions and 145 deletions

View file

@ -1,9 +1,8 @@
const lbryApi = require('../helpers/lbryApi.js'); const lbryApi = require('../helpers/lbryApi.js');
const db = require('../models'); const db = require('../models');
const logger = require('winston'); const logger = require('winston');
const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicClaims.js');
const isFreeClaim = require('../helpers/functions/isFreeClaim.js'); const isFreeClaim = require('../helpers/functions/isFreeClaim.js');
const serveHelpers = require('../helpers/serveHelpers.js'); const { getTopFreeClaim, getFullClaimIdFromShortId } = require('../helpers/serveHelpers.js');
function checkForLocalAssetByClaimId (claimId, name) { function checkForLocalAssetByClaimId (claimId, name) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -94,7 +93,7 @@ module.exports = {
getAssetByShortId: function (shortId, name) { getAssetByShortId: function (shortId, name) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// get the full claimId // get the full claimId
serveHelpers.getFullClaimIdFromShortId(shortId, name) getFullClaimIdFromShortId(shortId, name)
// get the asset by the claimId // get the asset by the claimId
.then(claimId => { .then(claimId => {
resolve(getAssetByClaimId(claimId, name)); resolve(getAssetByClaimId(claimId, name));
@ -110,16 +109,15 @@ module.exports = {
getAssetByName (name) { getAssetByName (name) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// 1. get a list of the free public claims // 1. get a list of the free public claims
getAllFreePublicClaims(name) getTopFreeClaim(name)
// 2. check locally for the top claim // 2. check locally for the top claim
.then(freePublicClaimList => { .then(freePublicClaimList => {
// if no claims were found, return null // if no claims were found, return null
if (!freePublicClaimList) { if (!freePublicClaimList) {
resolve(null); return resolve(null);
return;
} }
// parse the result // parse the result
const claimId = freePublicClaimList[0].claim_id; const claimId = freePublicClaimList[0].claimId;
// get the asset // get the asset
resolve(getAssetByClaimId(claimId, name)); resolve(getAssetByClaimId(claimId, name));
}) })

View file

@ -146,15 +146,14 @@ module.exports = {
const dateTime = startDate.toISOString().slice(0, 19).replace('T', ' '); const dateTime = startDate.toISOString().slice(0, 19).replace('T', ' ');
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// get the raw requests data // get the raw requests data
db.sequelize db.sequelize.query(`SELECT COUNT(*), File.* FROM Request LEFT JOIN File ON Request.FileId = File.id WHERE FileId IS NOT NULL AND nsfw != 1 AND trendingEligible = 1 AND Request.createdAt > "${dateTime}" GROUP BY FileId ORDER BY COUNT(*) DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT })
.query(`SELECT COUNT(*), File.* FROM Request LEFT JOIN File ON Request.FileId = File.id WHERE FileId IS NOT NULL AND nsfw != 1 AND trendingEligible = 1 AND Request.createdAt > "${dateTime}" GROUP BY FileId ORDER BY COUNT(*) DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT }) .then(results => {
.then(results => { resolve(results);
resolve(results); })
}) .catch(error => {
.catch(error => { logger.error('sequelize error', error);
logger.error('sequelize error', error); reject(error);
reject(error); });
});
}); });
}, },
}; };

View file

@ -1,61 +0,0 @@
const isFreeClaim = require('./isFreeClaim.js');
const lbryApi = require('../lbryApi.js');
const logger = require('winston');
function filterForFreePublicClaims (claimsListArray) {
logger.debug('filtering claims list for free, public claims');
if (!claimsListArray) {
return null;
}
const freePublicClaims = claimsListArray.filter(claim => {
if (!claim.value) {
return false;
}
return isFreeClaim(claim);
});
return freePublicClaims;
}
function orderClaims (claimsListArray) {
logger.debug('ordering the free public claims');
claimsListArray.sort((a, b) => {
if (a.amount === b.amount) {
return a.height < b.height;
} else {
return a.amount < b.amount;
}
});
return claimsListArray;
}
module.exports = (claimName) => {
return new Promise((resolve, reject) => {
// make a call to the daemon to get the claims list
lbryApi
.getClaimList(claimName)
.then(({ claims }) => {
logger.debug(`Number of claims: ${claims.length}`);
// return early if no claims were found
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(claims);
// return early if no free, public claims were found
if (!freePublicClaims || freePublicClaims.length === 0) {
logger.debug('exiting due to lack of free or public claims');
resolve(null);
return;
}
// order the claims
const orderedPublicClaims = orderClaims(freePublicClaims);
// resolve the promise
resolve(orderedPublicClaims);
})
.catch(error => {
reject(error);
});
});
};

View file

@ -1,13 +1,12 @@
const logger = require('winston'); const logger = require('winston');
const db = require('../models'); const db = require('../models');
const lbryApi = require('./lbryApi');
function determineShortClaimId (claimId, height, claimList) { function determineShortClaimId (claimId, height, claimList) {
logger.debug('determining short url based on claim id and claim list'); logger.debug('determining short url based on claim id and claim list');
logger.debug('claimlist starting length:', claimList.length); logger.debug('claimlist starting length:', claimList.length);
// remove this claim from the claim list, if it exists // remove this claim from the claim list, if it exists
claimList = claimList.filter(claim => { claimList = claimList.filter(claim => {
return claim.claim_id !== claimId; return claim.claimId !== claimId;
}); });
logger.debug('claim list length without this claim:', claimList.length); logger.debug('claim list length without this claim:', claimList.length);
// If there are no other claims, return the first letter of the claim id... // If there are no other claims, return the first letter of the claim id...
@ -21,7 +20,7 @@ function determineShortClaimId (claimId, height, claimList) {
while (claimList.length !== 0) { while (claimList.length !== 0) {
i++; i++;
claimList = claimList.filter(claim => { claimList = claimList.filter(claim => {
const otherClaimIdSegmentToCompare = claim.claim_id.substring(0, i); const otherClaimIdSegmentToCompare = claim.claimId.substring(0, i);
const thisClaimIdSegmentToCompare = claimId.substring(0, i); const thisClaimIdSegmentToCompare = claimId.substring(0, i);
logger.debug('compare:', otherClaimIdSegmentToCompare, '===', thisClaimIdSegmentToCompare, '?'); logger.debug('compare:', otherClaimIdSegmentToCompare, '===', thisClaimIdSegmentToCompare, '?');
return (otherClaimIdSegmentToCompare === thisClaimIdSegmentToCompare); return (otherClaimIdSegmentToCompare === thisClaimIdSegmentToCompare);
@ -35,7 +34,7 @@ function determineShortClaimId (claimId, height, claimList) {
return claimId.substring(0, 1); return claimId.substring(0, 1);
} }
const allMatchingClaimsAtLastMatch = claimListCopy.filter(claim => { const allMatchingClaimsAtLastMatch = claimListCopy.filter(claim => {
return (claim.claim_id.substring(0, lastMatchIndex) === lastMatch); return (claim.claimId.substring(0, lastMatchIndex) === lastMatch);
}); });
// for those that share the longest shared prefix: see which came first in time. whichever is earliest, the others take the extra character // for those that share the longest shared prefix: see which came first in time. whichever is earliest, the others take the extra character
const sortedMatchingClaims = allMatchingClaimsAtLastMatch.sort((a, b) => { const sortedMatchingClaims = allMatchingClaimsAtLastMatch.sort((a, b) => {
@ -49,29 +48,6 @@ function determineShortClaimId (claimId, height, claimList) {
} }
} }
function checkLocalDbForClaims (name, shortId) {
return db.File
.findAll({
where: {
name,
claimId: { $like: `${shortId}%` },
},
})
.then(records => {
logger.debug('number of local search results:', records.length);
if (records.length === 0) {
return records;
}
const localClaims = records.map(record => { // format the data to match what lbry daemon would have returned
return { name: record.dataValues.name, claim_id: record.dataValues.claimId, height: record.dataValues.height };
});
return localClaims;
})
.catch(error => {
return error;
});
}
function createOpenGraphInfo ({ fileType, claimId, name, fileName, fileExt }) { function createOpenGraphInfo ({ fileType, claimId, name, fileName, fileExt }) {
return { return {
embedUrl : `https://spee.ch/embed/${claimId}/${name}`, embedUrl : `https://spee.ch/embed/${claimId}/${name}`,
@ -118,36 +94,13 @@ module.exports = {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
logger.debug('getting claim_id from short url'); logger.debug('getting claim_id from short url');
// use the daemon to check for claims list // use the daemon to check for claims list
lbryApi.getClaimList(name) db.sequelize.query(`SELECT claimId FROM Claim WHERE name = '${name}' AND claimId LIKE '${shortId}%' ORDER BY height ASC LIMIT 1;`)
.then(({ claims }) => { .then(result => {
logger.debug('Number of claims from getClaimList:', claims.length); switch (result.length) {
// if no claims were found, check locally for possible claims
if (claims.length === 0) {
return checkLocalDbForClaims(name, shortId);
} else {
return claims;
}
})
// handle the claims list
.then(claims => {
logger.debug('Claims ready for filtering');
const regex = new RegExp(`^${shortId}`);
const filteredClaimsList = claims.filter(claim => {
return regex.test(claim.claim_id);
});
switch (filteredClaimsList.length) {
case 0: case 0:
reject(new Error('That is an invalid short url')); return reject(new Error('That is an invalid Short Id'));
break; default: // note results must be sorted
case 1: return resolve(result[0].datavalues.claimId);
resolve(filteredClaimsList[0].claim_id);
break;
default:
const sortedClaimsList = filteredClaimsList.sort((a, b) => {
return a.height > b.height;
});
resolve(sortedClaimsList[0].claim_id);
break;
} }
}) })
.catch(error => { .catch(error => {
@ -158,13 +111,51 @@ module.exports = {
getShortIdFromClaimId (claimId, height, name) { getShortIdFromClaimId (claimId, height, name) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
logger.debug('finding short claim id from full claim id'); logger.debug('finding short claim id from full claim id');
// get a list of all the claims db.sequelize.query(`SELECT claimId, height FROM Claim WHERE name = '${name}' ORDER BY claimId;`)
lbryApi.getClaimList(name) .then(result => {
// find the smallest possible unique url for this claim switch (result.length) {
.then(({ claims }) => { case 0:
const shortId = determineShortClaimId(claimId, height, claims); return reject(new Error('That is an invalid Claim Id'));
logger.debug('short claim id ===', shortId); default: // note results must be sorted
resolve(shortId); const resultsArray = result.map(claim => {
return claim.dataValues;
});
const shortId = determineShortClaimId(claimId, height, resultsArray);
logger.debug('short claim id ===', shortId);
return resolve(shortId);
}
})
.catch(error => {
reject(error);
});
});
},
getAllFreeClaims (claimName) {
return new Promise((resolve, reject) => {
db.sequelize.query(`SELECT * FROM Claim WHERE name = '${claimName}' ORDER BY amount DESC, height ASC`)
.then(result => {
if (result.length === 0) {
logger.debug('exiting due to lack of claims');
return resolve(null);
}
const claims = result.map(claim => {
return claim.dataValues;
});
return resolve(claims);
})
.catch(error => {
reject(error);
});
});
},
getTopFreeClaim (claimName) {
return new Promise((resolve, reject) => {
db.sequelize.query(`SELECT * FROM Claim WHERE name = '${claimName}' ORDER BY amount DESC, height ASC LIMIT 1`)
.then(result => {
if (result.length === 0) {
return resolve(null);
}
return resolve(result[0].dataValues);
}) })
.catch(error => { .catch(error => {
reject(error); reject(error);

View file

@ -1,5 +1,5 @@
const errorHandlers = require('../helpers/errorHandlers.js'); const errorHandlers = require('../helpers/errorHandlers.js');
const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicClaims.js'); const { getAllFreeClaims } = require('../helpers/serveHelpers.js');
const { postToStats, getStatsSummary, getTrendingClaims } = require('../controllers/statsController.js'); const { postToStats, getStatsSummary, getTrendingClaims } = require('../controllers/statsController.js');
module.exports = (app) => { module.exports = (app) => {
@ -48,14 +48,14 @@ module.exports = (app) => {
// route to display all free public claims at a given name // route to display all free public claims at a given name
app.get('/:name/all', ({ ip, originalUrl, params }, res) => { app.get('/:name/all', ({ ip, originalUrl, params }, res) => {
// get and render the content // get and render the content
getAllFreePublicClaims(params.name) getAllFreeClaims(params.name)
.then(orderedFreePublicClaims => { .then(orderedFreeClaims => {
if (!orderedFreePublicClaims) { if (!orderedFreeClaims) {
res.status(307).render('noClaims'); res.status(307).render('noClaims');
return; return;
} }
postToStats('show', originalUrl, ip, null, null, 'success'); postToStats('show', originalUrl, ip, null, null, 'success');
res.status(200).render('allClaims', { claims: orderedFreePublicClaims }); res.status(200).render('allClaims', { claims: orderedFreeClaims });
}) })
.catch(error => { .catch(error => {
errorHandlers.handleRequestError('show', originalUrl, ip, error, res); errorHandlers.handleRequestError('show', originalUrl, ip, error, res);