diff --git a/controllers/serveController.js b/controllers/serveController.js index 88a7fb78..3f6d010c 100644 --- a/controllers/serveController.js +++ b/controllers/serveController.js @@ -1,7 +1,7 @@ const lbryApi = require('../helpers/lbryApi.js'); const db = require('../models'); const logger = require('winston'); -const { getTopFreeClaim, getFullClaimIdFromShortId, resolveAgainstClaimTable, getClaimIdByChannelId } = require('../helpers/serveHelpers.js'); +const { getTopFreeClaim, getFullClaimIdFromShortId, resolveAgainstClaimTable, getClaimIdByChannel } = require('../helpers/serveHelpers.js'); function checkForLocalAssetByClaimId (claimId, name) { return new Promise((resolve, reject) => { @@ -82,21 +82,18 @@ function getAssetByClaimId (fullClaimId, name) { } module.exports = { - getAssetByChannel (channelName, channelId, name) { - if (channelId) { - return new Promise((resolve, reject) => { - getClaimIdByChannelId(channelId, name) - .then(claimId => { - logger.debug('claim id = ', claimId); - resolve(getAssetByClaimId(claimId, name)); - }) - .catch(error => { - reject(error); - }); + getAssetByChannel (channelName, channelId, claimName) { + logger.debug('channelId =', channelId); + return new Promise((resolve, reject) => { + getClaimIdByChannel(channelName, channelId) + .then(claimId => { + logger.debug('claim id = ', claimId); + resolve(getAssetByClaimId(claimId, claimName)); + }) + .catch(error => { + reject(error); }); - } else { - return new Error('this is not supported yet'); - } + }); }, getAssetByShortId: function (shortId, name) { logger.debug('...getting asset by short id...'); @@ -138,4 +135,10 @@ module.exports = { }); }); }, + getChannelByName (name) { + logger.debug('...getting channel by channel name...'); + return new Promise((resolve, reject) => { + reject(new Error('This feature is not yet supported')); + }); + }, }; diff --git a/helpers/serveHelpers.js b/helpers/serveHelpers.js index 356b314c..914d27f6 100644 --- a/helpers/serveHelpers.js +++ b/helpers/serveHelpers.js @@ -1,6 +1,101 @@ const logger = require('winston'); const db = require('../models'); +// function determineShortChannelId (name, longChannelId) { +// return new Promise((resolve, reject) => { +// logger.debug('finding short channel id'); +// db.sequelize.query(`SELECT claimId, height FROM Certificate WHERE name = '${name}' ORDER BY height;`, { type: db.sequelize.QueryTypes.SELECT }) +// .then(result => { +// switch (result.length) { +// case 0: +// return reject(new Error('That is an invalid channel name')); +// default: +// let certificateIndex; +// let shortId = longChannelId.substring(0, 1); // default sort id is the first letter +// let shortIdLength = 0; +// // find the index of this certificate +// certificateIndex = result.findIndex(element => { +// return element.claimId === longChannelId; +// }); +// if (certificateIndex < 0) { throw new Error('claimid not found in possible sorted list') } +// // get an array of all certificates with lower height +// let possibleMatches = result.slice(0, certificateIndex); +// // remove certificates with the same prefixes until none are left. +// while (possibleMatches.length > 0) { +// shortIdLength += 1; +// shortId = longChannelId.substring(0, shortIdLength); +// possibleMatches = possibleMatches.filter(element => { +// return (element.claimId.substring(0, shortIdLength) === shortId); +// }); +// } +// // return the short Id +// logger.debug('short claim id ===', shortId); +// return resolve(shortId); +// } +// }) +// .catch(error => { +// reject(error); +// }); +// }); +// } + +function getLongChannelId (channelName, channelId) { + if (channelId && (channelId.length === 40)) { // full channel id + return channelId; + } else if (channelId && channelId.length < 40) { // short channel id + return getLongChannelIdFromShortChannelId(channelName, channelId); + } else { + return getChannelIdFromChannelName(channelName); + } +}; + +function getClaimIdByLongChannelId (channelId, name) { + return new Promise((resolve, reject) => { + logger.debug('finding claim id from channel id and claim name'); + db.sequelize.query(`SELECT claimId FROM Claim WHERE name = '${name}' AND certificateId = '${channelId}' LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT }) + .then(result => { + switch (result.length) { + case 0: + return reject(new Error('There is no such claim for that channel')); + default: + return resolve(result[0].claimId); + } + }) + .catch(error => { + reject(error); + }); + }); +} + +function getLongChannelIdFromShortChannelId (channelName, channelId) { + return new Promise((resolve, reject) => { + logger.debug(`finding long channel id for ${channelName}:${channelId}`); + // get the long channel Id + db.sequelize.query(`SELECT claimId, height FROM Certificate WHERE name = '${channelName}' AND claimId LIKE '${channelId}%' ORDER BY height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT }) + .then(result => { + logger.debug('result >>', result); + switch (result.length) { + case 0: + throw new Error('That is an invalid Short Channel Id'); + default: // note results must be sorted + return (result[0].claimId); + } + }) + // return the long channel id + .then(longChannelId => { + logger.debug('channelId =', longChannelId); + return resolve(longChannelId); + }) + .catch(error => { + reject(error); + }); + }); +} + +function getChannelIdFromChannelName (channelName, claimName) { + // select the top top channel id +} + function determineShortClaimId (claimId, height, claimList) { logger.debug('determining short url based on claim id and claim list'); logger.debug('claimlist starting length:', claimList.length); @@ -98,7 +193,7 @@ module.exports = { .then(result => { switch (result.length) { case 0: - return reject(new Error('That is an invalid Short Id')); + return reject(new Error('That is an invalid Short Claim Id')); default: // note results must be sorted return resolve(result[0].claimId); } @@ -115,7 +210,7 @@ module.exports = { .then(result => { switch (result.length) { case 0: - return reject(new Error('That is an invalid Claim Id')); + return reject(new Error('That is an invalid claim name')); default: // note results must be sorted const shortId = determineShortClaimId(claimId, height, result); logger.debug('short claim id ===', shortId); @@ -127,24 +222,6 @@ module.exports = { }); }); }, - getClaimIdByChannelId (channelId, name) { - return new Promise((resolve, reject) => { - logger.debug('finding claim id from channel id'); - db.sequelize.query(`SELECT claimId FROM Claim WHERE name = '${name}' AND certificateId = '${channelId}' LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT }) - .then(result => { - switch (result.length) { - case 0: - return reject(new Error('That is an invalid Channel Id')); - default: // note results must be sorted - logger.debug('found result', result); - return resolve(result[0].claimId); - } - }) - .catch(error => { - reject(error); - }); - }); - }, getAllFreeClaims (name) { return new Promise((resolve, reject) => { db.sequelize.query(`SELECT * FROM Claim WHERE name = '${name}' ORDER BY amount DESC, height ASC`, { type: db.sequelize.QueryTypes.SELECT }) @@ -195,4 +272,20 @@ module.exports = { }); }); }, + getClaimIdByChannel (channelName, channelId, claimName) { + return new Promise((resolve, reject) => { + // 1. get the long channel id + getLongChannelId(channelName, channelId) + // 2. get the claim Id + .then(longChannelId => { + return getClaimIdByLongChannelId(longChannelId, claimName); + }) + .then(claimId => { + return resolve(claimId); + }) + .catch(error => { + reject(error); + }); + }); + }, }; diff --git a/routes/serve-routes.js b/routes/serve-routes.js index 7a851103..d21de474 100644 --- a/routes/serve-routes.js +++ b/routes/serve-routes.js @@ -124,13 +124,13 @@ module.exports = (app) => { // parse identifier for whether it is a channel, short url, or claim_id if (identifier.charAt(0) === '@') { channelName = identifier; - logger.debug('channel name =', channelName); claimType = CHANNEL; const channelIdIndex = channelName.indexOf(CHANNELID_INDICATOR); if (channelIdIndex !== -1) { channelId = channelName.substring(channelIdIndex + 1); channelName = channelName.substring(0, channelIdIndex); } + logger.debug('channel name =', channelName); } else if (identifier.length === 40) { fullClaimId = identifier; logger.debug('full claim id =', fullClaimId);