From 28ab926ef9238762451345b827ba36e32560e2f8 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Mon, 21 Aug 2017 18:03:57 -0700 Subject: [PATCH] added support for channel with full id --- controllers/serveController.js | 25 ++++++++++++++++--------- helpers/serveHelpers.js | 18 ++++++++++++++++++ routes/serve-routes.js | 33 ++++++++++++++++++++------------- 3 files changed, 54 insertions(+), 22 deletions(-) diff --git a/controllers/serveController.js b/controllers/serveController.js index fe1ed0a6..88a7fb78 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 } = require('../helpers/serveHelpers.js'); +const { getTopFreeClaim, getFullClaimIdFromShortId, resolveAgainstClaimTable, getClaimIdByChannelId } = require('../helpers/serveHelpers.js'); function checkForLocalAssetByClaimId (claimId, name) { return new Promise((resolve, reject) => { @@ -82,14 +82,21 @@ function getAssetByClaimId (fullClaimId, name) { } module.exports = { - getAssetByChannel (channelName, name) { - logger.debug('...getting asset by channel...'); - return new Promise((resolve, reject) => { - // temporarily throw error - reject(new Error('channel names are not currently supported')); - // get the claim id - // get the asset by claim Id - }); + 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); + }); + }); + } else { + return new Error('this is not supported yet'); + } }, getAssetByShortId: function (shortId, name) { logger.debug('...getting asset by short id...'); diff --git a/helpers/serveHelpers.js b/helpers/serveHelpers.js index 188cf915..356b314c 100644 --- a/helpers/serveHelpers.js +++ b/helpers/serveHelpers.js @@ -127,6 +127,24 @@ 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 }) diff --git a/routes/serve-routes.js b/routes/serve-routes.js index cfbab943..94a3509e 100644 --- a/routes/serve-routes.js +++ b/routes/serve-routes.js @@ -7,19 +7,20 @@ const SERVE = 'SERVE'; const SHOW = 'SHOW'; const SHOWLITE = 'SHOWLITE'; const CHANNEL = 'CHANNEL'; -const SHORTURL = 'SHORTURL'; -const CLAIMID = 'CLAIMID'; -const NAME = 'NAME'; +const CLAIM_ID_SHORT = 'CLAIM_ID_SHORT'; +const CLAIM_ID_LONG = 'CLAIM_ID_LONG'; +const CLAIM_NAME = 'CLAIM_NAME'; +const CHANNELID_INDICATOR = ':'; -function getAsset (claimType, channelName, shortId, fullClaimId, name) { +function getAsset (claimType, channelName, channelId, shortId, fullClaimId, name) { switch (claimType) { case CHANNEL: - return getAssetByChannel(channelName, name); - case SHORTURL: + return getAssetByChannel(channelName, channelId, name); + case CLAIM_ID_SHORT: return getAssetByShortId(shortId, name); - case CLAIMID: + case CLAIM_ID_LONG: return getAssetByClaimId(fullClaimId, name); - case NAME: + case CLAIM_NAME: return getAssetByName(name); default: return new Error('that claim type was not found'); @@ -89,6 +90,7 @@ module.exports = (app) => { let channelName = null; let shortId = null; let fullClaimId = null; + let channelId = null; let method; let extension; // parse the name @@ -121,24 +123,29 @@ module.exports = (app) => { logger.debug('method =', method); // parse identifier for whether it is a channel, short url, or claim_id if (identifier.charAt(0) === '@') { - channelName = identifier.substring(1); + 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); + } } else if (identifier.length === 40) { fullClaimId = identifier; logger.debug('full claim id =', fullClaimId); - claimType = CLAIMID; + claimType = CLAIM_ID_LONG; } else if (identifier.length < 40) { shortId = identifier; logger.debug('short claim id =', shortId); - claimType = SHORTURL; + claimType = CLAIM_ID_SHORT; } else { logger.error('The URL provided could not be parsed'); res.send('that url is invalid'); return; }; // 1. retrieve the asset and information - getAsset(claimType, channelName, shortId, fullClaimId, name) + getAsset(claimType, channelName, channelId, shortId, fullClaimId, name) // 2. serve or show .then(fileInfo => { logger.debug('fileInfo', fileInfo); @@ -179,7 +186,7 @@ module.exports = (app) => { logger.debug('claim name = ', name); logger.debug('method =', method); // 1. retrieve the asset and information - getAsset(NAME, null, null, null, name) + getAsset(CLAIM_NAME, null, null, null, name) // 2. serve or show .then(fileInfo => { if (!fileInfo) {