From d6be5c4dbefbec75c2386eb81185931f44e8e702 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 31 Oct 2017 10:05:15 -0700 Subject: [PATCH] moved getShortChannelId and getShortClaimId to class methods --- controllers/serveController.js | 6 ++-- helpers/authHelpers.js | 2 +- models/certificate.js | 48 ++++++++++++++++++++++++++ models/claim.js | 48 ++++++++++++++++++++++++++ models/index.js | 62 ---------------------------------- passport/local-login.js | 2 +- passport/local-signup.js | 2 +- routes/api-routes.js | 5 +-- 8 files changed, 105 insertions(+), 70 deletions(-) diff --git a/controllers/serveController.js b/controllers/serveController.js index bbc96dab..03eabc08 100644 --- a/controllers/serveController.js +++ b/controllers/serveController.js @@ -114,7 +114,7 @@ module.exports = { return new Promise((resolve, reject) => { db.getLongClaimId(claimName, claimId) // 1. get the long claim id .then(result => { // 2. get the asset using the long claim id - logger.debug('getLongClaimId result:', result); + logger.debug('long claim id ===', result); if (result === NO_CLAIM) { logger.debug('resolving NO_CLAIM'); resolve(NO_CLAIM); @@ -161,7 +161,7 @@ module.exports = { return NO_CHANNEL; } longChannelId = result; - return db.getShortChannelIdFromLongChannelId(longChannelId, channelName); + return db.Certificate.getShortChannelIdFromLongChannelId(longChannelId, channelName); }) .then(result => { // 3. get all Claim records for this channel if (result === NO_CHANNEL) { @@ -216,7 +216,7 @@ module.exports = { showFileLite(fileInfo, res); return fileInfo; case SHOW: - return db + return db.Claim .getShortClaimIdFromLongClaimId(fileInfo.claimId, fileInfo.name) .then(shortId => { fileInfo['shortId'] = shortId; diff --git a/helpers/authHelpers.js b/helpers/authHelpers.js index 92066cbd..455ddd9a 100644 --- a/helpers/authHelpers.js +++ b/helpers/authHelpers.js @@ -28,7 +28,7 @@ module.exports = { .then(channel => { userInfo['channelName'] = channel.channelName; userInfo['channelClaimId'] = channel.channelClaimId; - return db.getShortChannelIdFromLongChannelId(channel.channelClaimId, channel.channelName); + return db.Certificate.getShortChannelIdFromLongChannelId(channel.channelClaimId, channel.channelName); }) .then(shortChannelId => { userInfo['shortChannelId'] = shortChannelId; diff --git a/models/certificate.js b/models/certificate.js index 3cf35e7b..89fa2323 100644 --- a/models/certificate.js +++ b/models/certificate.js @@ -1,3 +1,29 @@ +const logger = require('winston'); + +function sortResult (result, longId) { + let claimIndex; + let shortId = longId.substring(0, 1); // default sort id is the first letter + let shortIdLength = 0; + // find the index of this certificate + claimIndex = result.findIndex(element => { + return element.claimId === longId; + }); + if (claimIndex < 0) { throw new Error('channelId not found in possible sorted list') } + // get an array of all certificates with lower height + let possibleMatches = result.slice(0, claimIndex); + // remove certificates with the same prefixes until none are left. + while (possibleMatches.length > 0) { + shortIdLength += 1; + shortId = longId.substring(0, shortIdLength); + possibleMatches = possibleMatches.filter(element => { + return (element.claimId.substring(0, shortIdLength) === shortId); + }); + } + // return the short Id + logger.debug('short channel id ===', shortId); + return shortId; +} + module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, DOUBLE }) => { const Certificate = sequelize.define( 'Certificate', @@ -97,5 +123,27 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D }); }; + Certificate.getShortChannelIdFromLongChannelId = function (longChannelId, channelName) { + logger.debug(`finding short channel id for ${channelName}:${longChannelId}`); + return new Promise((resolve, reject) => { + this + .findAll({ + where: {name: channelName}, + order: [['height', 'ASC']], + }) + .then(result => { + switch (result.length) { + case 0: + throw new Error('That is an invalid channel name'); + default: + return resolve(sortResult(result, longChannelId)); + } + }) + .catch(error => { + reject(error); + }); + }); + }; + return Certificate; }; diff --git a/models/claim.js b/models/claim.js index 804bb284..72ef0577 100644 --- a/models/claim.js +++ b/models/claim.js @@ -1,3 +1,29 @@ +const logger = require('winston'); + +function sortResult (result, longId) { + let claimIndex; + let shortId = longId.substring(0, 1); // default sort id is the first letter + let shortIdLength = 0; + // find the index of this certificate + claimIndex = result.findIndex(element => { + return element.claimId === longId; + }); + if (claimIndex < 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, claimIndex); + // remove certificates with the same prefixes until none are left. + while (possibleMatches.length > 0) { + shortIdLength += 1; + shortId = longId.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 shortId; +} + module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, DOUBLE }) => { const Claim = sequelize.define( 'Claim', @@ -154,5 +180,27 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D }); }; + Claim.getShortClaimIdFromLongClaimId = function (claimId, claimName) { + return new Promise((resolve, reject) => { + logger.debug(`finding short claim id for ${claimId}#${claimId}`); + this + .findAll({ + where: {name: claimName}, + order: [['height', 'ASC']], + }) + .then(result => { + switch (result.length) { + case 0: + throw new Error('That is an invalid claim name'); + default: + resolve(sortResult(result, claimId)); + } + }) + .catch(error => { + reject(error); + }); + }); + }; + return Claim; }; diff --git a/models/index.js b/models/index.js index 6fccf574..c42606bb 100644 --- a/models/index.js +++ b/models/index.js @@ -24,30 +24,6 @@ const sequelize = new Sequelize(database, username, password, { }, }); -function sortResult (result, longId) { - let claimIndex; - let shortId = longId.substring(0, 1); // default sort id is the first letter - let shortIdLength = 0; - // find the index of this certificate - claimIndex = result.findIndex(element => { - return element.claimId === longId; - }); - if (claimIndex < 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, claimIndex); - // remove certificates with the same prefixes until none are left. - while (possibleMatches.length > 0) { - shortIdLength += 1; - shortId = longId.substring(0, shortIdLength); - possibleMatches = possibleMatches.filter(element => { - return (element.claimId.substring(0, shortIdLength) === shortId); - }); - } - // return the short Id - logger.debug('short channel id ===', shortId); - return shortId; -} - function getLongClaimIdFromShortClaimId (name, shortId) { return new Promise((resolve, reject) => { db @@ -176,44 +152,6 @@ db['getRecentClaims'] = () => { return db.sequelize.query(`SELECT * FROM File WHERE nsfw != 1 AND trendingEligible = 1 ORDER BY createdAt DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT }); }; -db['getShortClaimIdFromLongClaimId'] = (claimId, claimName) => { - return new Promise((resolve, reject) => { - logger.debug('finding short channel id'); - db - .sequelize.query(`SELECT claimId, height FROM Claim WHERE name = '${claimName}' ORDER BY height;`, { type: db.sequelize.QueryTypes.SELECT }) - .then(result => { - switch (result.length) { - case 0: - throw new Error('That is an invalid claim name'); - default: - return resolve(sortResult(result, claimId)); - } - }) - .catch(error => { - reject(error); - }); - }); -}; - -db['getShortChannelIdFromLongChannelId'] = (longChannelId, channelName) => { - return new Promise((resolve, reject) => { - logger.debug(`finding short channel id for ${longChannelId} ${channelName}`); - db - .sequelize.query(`SELECT claimId, height FROM Certificate WHERE name = '${channelName}' ORDER BY height;`, { type: db.sequelize.QueryTypes.SELECT }) - .then(result => { - switch (result.length) { - case 0: - throw new Error('That is an invalid channel name'); - default: - return resolve(sortResult(result, longChannelId)); - } - }) - .catch(error => { - reject(error); - }); - }); -}; - db['getAllFreeClaims'] = (name) => { return new Promise((resolve, reject) => { db diff --git a/passport/local-login.js b/passport/local-login.js index f83dcc0e..473f875a 100644 --- a/passport/local-login.js +++ b/passport/local-login.js @@ -40,7 +40,7 @@ module.exports = new PassportLocalStrategy( .then(channel => { userInfo['channelName'] = channel.channelName; userInfo['channelClaimId'] = channel.channelClaimId; - return db.getShortChannelIdFromLongChannelId(channel.channelClaimId, channel.channelName); + return db.Certificate.getShortChannelIdFromLongChannelId(channel.channelClaimId, channel.channelName); }) .then(shortChannelId => { userInfo['shortChannelId'] = shortChannelId; diff --git a/passport/local-signup.js b/passport/local-signup.js index 177f388c..48f48e37 100644 --- a/passport/local-signup.js +++ b/passport/local-signup.js @@ -54,7 +54,7 @@ module.exports = new PassportLocalStrategy( }) .then(() => { logger.verbose('user and certificate successfully associated'); - return db.getShortChannelIdFromLongChannelId(userInfo.channelClaimId, userInfo.channelName); + return db.Certificate.getShortChannelIdFromLongChannelId(userInfo.channelClaimId, userInfo.channelName); }) .then(shortChannelId => { userInfo['shortChannelId'] = shortChannelId; diff --git a/routes/api-routes.js b/routes/api-routes.js index aabd348a..0c92879d 100644 --- a/routes/api-routes.js +++ b/routes/api-routes.js @@ -147,7 +147,8 @@ module.exports = (app) => { // route to get a short claim id from long claim Id app.get('/api/shortClaimId/:longId/:name', ({ originalUrl, ip, params }, res) => { // serve content - db.getShortClaimIdFromLongClaimId(params.longId, params.name) + db.Claim + .getShortClaimIdFromLongClaimId(params.longId, params.name) .then(shortId => { res.status(200).json(shortId); }) @@ -159,7 +160,7 @@ module.exports = (app) => { // route to get a short channel id from long channel Id app.get('/api/shortChannelId/:longId/:name', ({ params }, res) => { // serve content - db.getShortChannelIdFromLongChannelId(params.longId, params.name) + db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name) .then(shortId => { logger.debug('sending back short channel id', shortId); res.status(200).json(shortId);