From d6be5c4dbefbec75c2386eb81185931f44e8e702 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 31 Oct 2017 10:05:15 -0700 Subject: [PATCH 01/10] 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); -- 2.45.2 From 409a9346deb42bb49df107b03c21461cf4430e47 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 31 Oct 2017 11:54:33 -0700 Subject: [PATCH 02/10] updated Claim.getAllChannelClaims --- controllers/serveController.js | 2 +- models/claim.js | 26 ++++++++++++++++++++++++-- models/index.js | 19 ------------------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/controllers/serveController.js b/controllers/serveController.js index 03eabc08..d65c2b90 100644 --- a/controllers/serveController.js +++ b/controllers/serveController.js @@ -168,7 +168,7 @@ module.exports = { return NO_CHANNEL; } shortChannelId = result; - return db.getAllChannelClaims(longChannelId); + return db.Claim.getAllChannelClaims(longChannelId); }) .then(result => { // 4. add extra data not available from Claim table if (result === NO_CHANNEL) { diff --git a/models/claim.js b/models/claim.js index 72ef0577..583e917e 100644 --- a/models/claim.js +++ b/models/claim.js @@ -181,11 +181,11 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D }; Claim.getShortClaimIdFromLongClaimId = function (claimId, claimName) { + logger.debug(`Claim.getShortClaimIdFromLongClaimId for ${claimId}#${claimId}`); return new Promise((resolve, reject) => { - logger.debug(`finding short claim id for ${claimId}#${claimId}`); this .findAll({ - where: {name: claimName}, + where: { name: claimName }, order: [['height', 'ASC']], }) .then(result => { @@ -202,5 +202,27 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D }); }; + Claim.getAllChannelClaims = function (channelId) { + logger.debug(`Claim.getAllChannelClaims for ${channelId}`); + return new Promise((resolve, reject) => { + this + .findAll({ + where: { certificateId: channelId }, + order: [['height', 'ASC']], + }) + .then(result => { + switch (result.length) { + case 0: + return resolve(null); + default: + return resolve(result); + } + }) + .catch(error => { + reject(error); + }); + }); + }; + return Claim; }; diff --git a/models/index.js b/models/index.js index c42606bb..4894f2a8 100644 --- a/models/index.js +++ b/models/index.js @@ -209,25 +209,6 @@ db['getClaimIdByLongChannelId'] = (channelId, claimName) => { }); }; -db['getAllChannelClaims'] = (channelId) => { - return new Promise((resolve, reject) => { - logger.debug(`finding all claims in channel "${channelId}"`); - db - .sequelize.query(`SELECT name, claimId, outpoint, height, address, contentType, title, description, license, thumbnail FROM Claim WHERE certificateId = '${channelId}' ORDER BY height DESC;`, { type: db.sequelize.QueryTypes.SELECT }) - .then(result => { - switch (result.length) { - case 0: - return resolve(null); - default: - return resolve(result); - } - }) - .catch(error => { - reject(error); - }); - }); -}; - db['getLongClaimId'] = (claimName, claimId) => { logger.debug(`getLongClaimId(${claimName}, ${claimId})`); if (claimId && (claimId.length === 40)) { // if a full claim id is provided -- 2.45.2 From c6878eb0a3df76d183186ec5a2b0e9fb4ae7e81d Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 31 Oct 2017 15:28:11 -0700 Subject: [PATCH 03/10] updated getClaimIdByLongChannelId --- controllers/serveController.js | 2 +- models/claim.js | 26 ++++++++++++++++++++++++ models/index.js | 37 ---------------------------------- 3 files changed, 27 insertions(+), 38 deletions(-) diff --git a/controllers/serveController.js b/controllers/serveController.js index d65c2b90..f0ed5f05 100644 --- a/controllers/serveController.js +++ b/controllers/serveController.js @@ -136,7 +136,7 @@ module.exports = { resolve(NO_CHANNEL); return; } - return db.getClaimIdByLongChannelId(result, claimName); + return db.Claim.getClaimIdByLongChannelId(result, claimName); }) .then(result => { // 3. get the asset using the long claim id logger.debug('asset claim id =', result); diff --git a/models/claim.js b/models/claim.js index 583e917e..c0df47a9 100644 --- a/models/claim.js +++ b/models/claim.js @@ -1,4 +1,5 @@ const logger = require('winston'); +const NO_CLAIM = 'NO_CLAIM'; function sortResult (result, longId) { let claimIndex; @@ -224,5 +225,30 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D }); }; + Claim.getClaimIdByLongChannelId = function (channelId, claimName) { + logger.debug(`finding claim id for claim ${claimName} from channel ${channelId}`); + return new Promise((resolve, reject) => { + Claim + .findAll({ + where: { name: claimName, certificateId: channelId }, + order: [['id', 'ASC']], + }) + .then(result => { + switch (result.length) { + case 0: + return resolve(NO_CLAIM); + case 1: + return resolve(result[0].claimId); + default: + logger.error(`${result.length} records found for ${claimName} from channel ${claimName}`); + return resolve(result[0].claimId); + } + }) + .catch(error => { + reject(error); + }); + }); + }; + return Claim; }; diff --git a/models/index.js b/models/index.js index 4894f2a8..fa527331 100644 --- a/models/index.js +++ b/models/index.js @@ -152,24 +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['getAllFreeClaims'] = (name) => { - return new Promise((resolve, reject) => { - db - .sequelize.query(`SELECT name, claimId, outpoint, height, address FROM Claim WHERE name = '${name}' ORDER BY amount DESC, height ASC`, { type: db.sequelize.QueryTypes.SELECT }) - .then(result => { - switch (result.length) { - case 0: - return resolve(null); - default: - return resolve(result); - } - }) - .catch(error => { - reject(error); - }); - }); -}; - db['resolveClaim'] = (name, claimId) => { return new Promise((resolve, reject) => { db @@ -190,25 +172,6 @@ db['resolveClaim'] = (name, claimId) => { }); }; -db['getClaimIdByLongChannelId'] = (channelId, claimName) => { - return new Promise((resolve, reject) => { - logger.debug(`finding claim id for claim "${claimName}" from channel "${channelId}"`); - db - .sequelize.query(`SELECT claimId FROM Claim WHERE name = '${claimName}' AND certificateId = '${channelId}' LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT }) - .then(result => { - switch (result.length) { - case 0: - return resolve(NO_CLAIM); - default: - return resolve(result[0].claimId); - } - }) - .catch(error => { - reject(error); - }); - }); -}; - db['getLongClaimId'] = (claimName, claimId) => { logger.debug(`getLongClaimId(${claimName}, ${claimId})`); if (claimId && (claimId.length === 40)) { // if a full claim id is provided -- 2.45.2 From cbfce3a01ef34ebd1f8c728ed4445913cae6cbe1 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 31 Oct 2017 16:00:12 -0700 Subject: [PATCH 04/10] getLongCLaimId updated --- controllers/serveController.js | 2 +- models/claim.js | 63 ++++++++++++++++++++++++++++++++-- models/index.js | 50 +-------------------------- 3 files changed, 63 insertions(+), 52 deletions(-) diff --git a/controllers/serveController.js b/controllers/serveController.js index f0ed5f05..023622f3 100644 --- a/controllers/serveController.js +++ b/controllers/serveController.js @@ -112,7 +112,7 @@ module.exports = { getAssetByClaim (claimName, claimId) { logger.debug(`getAssetByClaim(${claimName}, ${claimId})`); return new Promise((resolve, reject) => { - db.getLongClaimId(claimName, claimId) // 1. get the long claim id + db.Claim.getLongClaimId(claimName, claimId) // 1. get the long claim id .then(result => { // 2. get the asset using the long claim id logger.debug('long claim id ===', result); if (result === NO_CLAIM) { diff --git a/models/claim.js b/models/claim.js index c0df47a9..b61a389e 100644 --- a/models/claim.js +++ b/models/claim.js @@ -25,7 +25,7 @@ function sortResult (result, longId) { return shortId; } -module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, DOUBLE }) => { +module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, DOUBLE, Op }) => { const Claim = sequelize.define( 'Claim', { @@ -228,7 +228,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D Claim.getClaimIdByLongChannelId = function (channelId, claimName) { logger.debug(`finding claim id for claim ${claimName} from channel ${channelId}`); return new Promise((resolve, reject) => { - Claim + this .findAll({ where: { name: claimName, certificateId: channelId }, order: [['id', 'ASC']], @@ -250,5 +250,64 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D }); }; + // sequelize.query(`SELECT claimId FROM Claim WHERE name = '${name}' AND claimId LIKE '${shortId}%' ORDER BY height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT }) + Claim.getLongClaimIdFromShortClaimId = function (name, shortId) { + return new Promise((resolve, reject) => { + this + .findAll({ + where: { + name, + claimId: { + [Op.like]: `${shortId}%`, + }}, + order: [['height', 'ASC']], + }) + .then(result => { + switch (result.length) { + case 0: + return resolve(NO_CLAIM); + default: // note results must be sorted + return resolve(result[0].claimId); + } + }) + .catch(error => { + reject(error); + }); + }); + }; + + Claim.getTopFreeClaimIdByClaimName = function (name) { + return new Promise((resolve, reject) => { + this + .findAll({ + where: { name }, + order: [['effectiveAmount', 'DESC'], ['height', 'ASC']], // note: maybe height and effective amount need to switch? + }) + .then(result => { + switch (result.length) { + case 0: + return resolve(NO_CLAIM); + default: + logger.debug('getTopFreeClaimIdByClaimName result:', result); + return resolve(result[0].claimId); + } + }) + .catch(error => { + reject(error); + }); + }); + }; + + Claim.getLongClaimId = function (claimName, claimId) { + logger.debug(`getLongClaimId(${claimName}, ${claimId})`); + if (claimId && (claimId.length === 40)) { // if a full claim id is provided + return new Promise((resolve, reject) => resolve(claimId)); + } else if (claimId && claimId.length < 40) { + return this.getLongClaimIdFromShortClaimId(claimName, claimId); // if a short claim id is provided + } else { + return this.getTopFreeClaimIdByClaimName(claimName); // if no claim id is provided + } + }; + return Claim; }; diff --git a/models/index.js b/models/index.js index fa527331..ea227777 100644 --- a/models/index.js +++ b/models/index.js @@ -7,11 +7,11 @@ const db = {}; const logger = require('winston'); const NO_CHANNEL = 'NO_CHANNEL'; -const NO_CLAIM = 'NO_CLAIM'; const database = config.get('Database.Database'); const username = config.get('Database.Username'); const password = config.get('Database.Password'); + const sequelize = new Sequelize(database, username, password, { host : 'localhost', dialect: 'mysql', @@ -24,43 +24,6 @@ const sequelize = new Sequelize(database, username, password, { }, }); -function getLongClaimIdFromShortClaimId (name, shortId) { - return new Promise((resolve, reject) => { - db - .sequelize.query(`SELECT claimId FROM Claim WHERE name = '${name}' AND claimId LIKE '${shortId}%' ORDER BY height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT }) - .then(result => { - switch (result.length) { - case 0: - return resolve(NO_CLAIM); - default: // note results must be sorted - return resolve(result[0].claimId); - } - }) - .catch(error => { - reject(error); - }); - }); -} - -function getTopFreeClaimIdByClaimName (name) { - return new Promise((resolve, reject) => { - db - .sequelize.query(`SELECT claimId FROM Claim WHERE name = '${name}' ORDER BY effectiveAmount DESC, height ASC LIMIT 1`, { type: db.sequelize.QueryTypes.SELECT }) - .then(result => { - logger.debug('getTopFreeClaimIdByClaimName result:', result); - switch (result.length) { - case 0: - return resolve(NO_CLAIM); - default: - return resolve(result[0].claimId); - } - }) - .catch(error => { - reject(error); - }); - }); -}; - function getLongChannelIdFromShortChannelId (channelName, channelId) { return new Promise((resolve, reject) => { db @@ -172,17 +135,6 @@ db['resolveClaim'] = (name, claimId) => { }); }; -db['getLongClaimId'] = (claimName, claimId) => { - logger.debug(`getLongClaimId(${claimName}, ${claimId})`); - if (claimId && (claimId.length === 40)) { // if a full claim id is provided - return new Promise((resolve, reject) => resolve(claimId)); - } else if (claimId && claimId.length < 40) { - return getLongClaimIdFromShortClaimId(claimName, claimId); // if a short claim id is provided - } else { - return getTopFreeClaimIdByClaimName(claimName); // if no claim id is provided - } -}; - db['getLongChannelId'] = (channelName, channelId) => { logger.debug(`getLongChannelId (${channelName}, ${channelId})`); if (channelId && (channelId.length === 40)) { // if a full channel id is provided -- 2.45.2 From 913c24c3a90d483370da95fa64044f3a2b12e453 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 31 Oct 2017 16:23:11 -0700 Subject: [PATCH 05/10] updated getLongChannelId --- models/certificate.js | 64 ++++++++++++++++++++++++++++++++++++++++++- models/index.js | 50 --------------------------------- 2 files changed, 63 insertions(+), 51 deletions(-) diff --git a/models/certificate.js b/models/certificate.js index 89fa2323..e21f71b7 100644 --- a/models/certificate.js +++ b/models/certificate.js @@ -1,4 +1,5 @@ const logger = require('winston'); +const NO_CHANNEL = 'NO_CHANNEL'; function sortResult (result, longId) { let claimIndex; @@ -24,7 +25,7 @@ function sortResult (result, longId) { return shortId; } -module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, DOUBLE }) => { +module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, DOUBLE, Op }) => { const Certificate = sequelize.define( 'Certificate', { @@ -145,5 +146,66 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D }); }; + // 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 }) + Certificate.getLongChannelIdFromShortChannelId = function (channelName, channelId) { + return new Promise((resolve, reject) => { + this + .findAll({ + where: { + name : channelName, + claimId: { + [Op.like]: `${channelId}%`, + }, + order: [['height', 'ASC']], + }, + }) + .then(result => { + switch (result.length) { + case 0: + return resolve(NO_CHANNEL); + default: // note results must be sorted + return resolve(result[0].claimId); + } + }) + .catch(error => { + reject(error); + }); + }); + }; + + // sequelize.query(`SELECT claimId, amount, height FROM Certificate WHERE name = '${channelName}' ORDER BY effectiveAmount DESC, height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT }) + Certificate.getLongChannelIdFromChannelName = function (channelName) { + logger.debug(`getLongChannelIdFromChannelName(${channelName})`); + return new Promise((resolve, reject) => { + this + .findAll({ + where: { name: channelName }, + order: [['effectiveAmount', 'DESC'], ['height', 'ASC']], + }) + .then(result => { + switch (result.length) { + case 0: + return resolve(NO_CHANNEL); + default: + return resolve(result[0].claimId); + } + }) + .catch(error => { + reject(error); + }); + }); + }; + + Certificate.getLongChannelId = function (channelName, channelId) { + logger.debug(`getLongChannelId(${channelName}, ${channelId})`); + if (channelId && (channelId.length === 40)) { // if a full channel id is provided + return new Promise((resolve, reject) => resolve(channelId)); + } else if (channelId && channelId.length < 40) { // if a short channel id is provided + return this.getLongChannelIdFromShortChannelId(channelName, channelId); + } else { + return this.getLongChannelIdFromChannelName(channelName); // if no channel id provided + } + }; + return Certificate; }; diff --git a/models/index.js b/models/index.js index ea227777..3d187a24 100644 --- a/models/index.js +++ b/models/index.js @@ -6,8 +6,6 @@ const config = require('config'); const db = {}; const logger = require('winston'); -const NO_CHANNEL = 'NO_CHANNEL'; - const database = config.get('Database.Database'); const username = config.get('Database.Username'); const password = config.get('Database.Password'); @@ -24,43 +22,6 @@ const sequelize = new Sequelize(database, username, password, { }, }); -function getLongChannelIdFromShortChannelId (channelName, channelId) { - return new Promise((resolve, reject) => { - 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 => { - switch (result.length) { - case 0: - return resolve(NO_CHANNEL); - default: // note results must be sorted - return resolve(result[0].claimId); - } - }) - .catch(error => { - reject(error); - }); - }); -} - -function getLongChannelIdFromChannelName (channelName) { - logger.debug(`getLongChannelIdFromChannelName(${channelName})`); - return new Promise((resolve, reject) => { - db - .sequelize.query(`SELECT claimId, amount, height FROM Certificate WHERE name = '${channelName}' ORDER BY effectiveAmount DESC, height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT }) - .then(result => { - switch (result.length) { - case 0: - return resolve(NO_CHANNEL); - default: - return resolve(result[0].claimId); - } - }) - .catch(error => { - reject(error); - }); - }); -} - sequelize .authenticate() .then(() => { @@ -135,15 +96,4 @@ db['resolveClaim'] = (name, claimId) => { }); }; -db['getLongChannelId'] = (channelName, channelId) => { - logger.debug(`getLongChannelId (${channelName}, ${channelId})`); - if (channelId && (channelId.length === 40)) { // if a full channel id is provided - return new Promise((resolve, reject) => resolve(channelId)); - } else if (channelId && channelId.length < 40) { // if a short channel id is provided - return getLongChannelIdFromShortChannelId(channelName, channelId); - } else { - return getLongChannelIdFromChannelName(channelName); // if no channel id provided - } -}; - module.exports = db; -- 2.45.2 From b95f7ad5d40570bd81634c8ea4005e5ea3dd2455 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 31 Oct 2017 16:44:32 -0700 Subject: [PATCH 06/10] updated resolveClaim --- models/claim.js | 24 ++++++++++++++++++++++++ models/index.js | 34 ++++++++-------------------------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/models/claim.js b/models/claim.js index b61a389e..66cb3870 100644 --- a/models/claim.js +++ b/models/claim.js @@ -309,5 +309,29 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D } }; + // sequelize.query(`SELECT name, claimId, outpoint, height, address, title, description, thumbnail, certificateId, channelName FROM Claim WHERE name = '${name}' AND claimId = '${claimId}'`, { type: db.sequelize.QueryTypes.SELECT }) + Claim.resolveClaim = function (name, claimId) { + return new Promise((resolve, reject) => { + this + .findAll({ + where: { name, claimId }, + }) + .then(result => { + switch (result.length) { + case 0: + return resolve(null); + case 1: + return resolve(result[0]); + default: + logger.error('more than one entry matches that name and claimID'); + return resolve(result[0]); + } + }) + .catch(error => { + reject(error); + }); + }); + }; + return Claim; }; diff --git a/models/index.js b/models/index.js index 3d187a24..5e9bb620 100644 --- a/models/index.js +++ b/models/index.js @@ -31,6 +31,7 @@ sequelize logger.error('Sequelize was unable to connect to the database:', err); }); +// add each model to the db object fs .readdirSync(__dirname) .filter(file => { @@ -41,6 +42,7 @@ fs db[model.name] = model; }); +// run model.association for each model in the db object that has an association Object.keys(db).forEach(modelName => { if (db[modelName].associate) { logger.info('Associating model:', modelName); @@ -51,15 +53,15 @@ Object.keys(db).forEach(modelName => { db.sequelize = sequelize; db.Sequelize = Sequelize; -db['upsert'] = (Model, values, condition, tableName) => { +db.upsert = (Model, values, condition, tableName) => { return Model .findOne({ where: condition }) - .then(function (obj) { + .then(obj => { if (obj) { // update - logger.debug(`updating "${values.name}" "${values.claimId}" in db.${tableName}`); + logger.debug(`updating record in db.${tableName}`); return obj.update(values); } else { // insert - logger.debug(`creating "${values.name}" "${values.claimId}" in db.${tableName}`); + logger.debug(`creating record in db.${tableName}`); return Model.create(values); } }) @@ -68,32 +70,12 @@ db['upsert'] = (Model, values, condition, tableName) => { }); }; -db['getTrendingClaims'] = (startDate) => { +db.getTrendingClaims = (startDate) => { return 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 > "${startDate}" GROUP BY FileId ORDER BY COUNT(*) DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT }); }; -db['getRecentClaims'] = () => { +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['resolveClaim'] = (name, claimId) => { - return new Promise((resolve, reject) => { - db - .sequelize.query(`SELECT name, claimId, outpoint, height, address, title, description, thumbnail, certificateId, channelName FROM Claim WHERE name = '${name}' AND claimId = '${claimId}'`, { type: db.sequelize.QueryTypes.SELECT }) - .then(result => { - switch (result.length) { - case 0: - return resolve(null); - case 1: - return resolve(result[0]); - default: - throw new Error('more than one entry matches that name and claimID'); - } - }) - .catch(error => { - reject(error); - }); - }); -}; - module.exports = db; -- 2.45.2 From 4d23fbbe4ed184c869f9c30e7a9e64ea15bcdd98 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 31 Oct 2017 17:19:26 -0700 Subject: [PATCH 07/10] updated getRecentClaims --- controllers/serveController.js | 8 ++++---- controllers/statsController.js | 2 +- models/certificate.js | 2 -- models/claim.js | 1 - models/file.js | 8 ++++++++ models/index.js | 4 ---- 6 files changed, 13 insertions(+), 12 deletions(-) diff --git a/controllers/serveController.js b/controllers/serveController.js index 023622f3..b4f54b6d 100644 --- a/controllers/serveController.js +++ b/controllers/serveController.js @@ -64,7 +64,7 @@ function getAssetByLongClaimId (fullClaimId, name) { } logger.debug('no local file found for this name and claimId'); // 2. if no local claim, resolve and get the claim - db + db.Claim .resolveClaim(name, fullClaimId) .then(resolveResult => { logger.debug('resolve result >> ', resolveResult); @@ -130,7 +130,7 @@ module.exports = { getAssetByChannel (channelName, channelId, claimName) { logger.debug('getting asset by channel'); return new Promise((resolve, reject) => { - db.getLongChannelId(channelName, channelId) // 1. get the long channel id + db.Certificate.getLongChannelId(channelName, channelId) // 1. get the long channel id .then(result => { // 2. get the long claim Id if (result === NO_CHANNEL) { resolve(NO_CHANNEL); @@ -155,7 +155,7 @@ module.exports = { return new Promise((resolve, reject) => { let longChannelId; let shortChannelId; - db.getLongChannelId(channelName, channelId) // 1. get the long channel Id + db.Certificate.getLongChannelId(channelName, channelId) // 1. get the long channel Id .then(result => { // 2. get all claims for that channel if (result === NO_CHANNEL) { return NO_CHANNEL; @@ -220,7 +220,7 @@ module.exports = { .getShortClaimIdFromLongClaimId(fileInfo.claimId, fileInfo.name) .then(shortId => { fileInfo['shortId'] = shortId; - return db.resolveClaim(fileInfo.name, fileInfo.claimId); + return db.Claim.resolveClaim(fileInfo.name, fileInfo.claimId); }) .then(resolveResult => { logger.debug('resolve result >>', resolveResult); diff --git a/controllers/statsController.js b/controllers/statsController.js index b728ede6..50dcc36d 100644 --- a/controllers/statsController.js +++ b/controllers/statsController.js @@ -97,7 +97,7 @@ module.exports = { logger.debug('retrieving most recent claims'); return new Promise((resolve, reject) => { // get the raw requests data - db.getRecentClaims() + db.File.getRecentClaims() .then(results => { resolve(results); }) diff --git a/models/certificate.js b/models/certificate.js index e21f71b7..dafb8281 100644 --- a/models/certificate.js +++ b/models/certificate.js @@ -146,7 +146,6 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D }); }; - // 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 }) Certificate.getLongChannelIdFromShortChannelId = function (channelName, channelId) { return new Promise((resolve, reject) => { this @@ -173,7 +172,6 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D }); }; - // sequelize.query(`SELECT claimId, amount, height FROM Certificate WHERE name = '${channelName}' ORDER BY effectiveAmount DESC, height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT }) Certificate.getLongChannelIdFromChannelName = function (channelName) { logger.debug(`getLongChannelIdFromChannelName(${channelName})`); return new Promise((resolve, reject) => { diff --git a/models/claim.js b/models/claim.js index 66cb3870..6c29010c 100644 --- a/models/claim.js +++ b/models/claim.js @@ -309,7 +309,6 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D } }; - // sequelize.query(`SELECT name, claimId, outpoint, height, address, title, description, thumbnail, certificateId, channelName FROM Claim WHERE name = '${name}' AND claimId = '${claimId}'`, { type: db.sequelize.QueryTypes.SELECT }) Claim.resolveClaim = function (name, claimId) { return new Promise((resolve, reject) => { this diff --git a/models/file.js b/models/file.js index c1e86772..1ba0b5fa 100644 --- a/models/file.js +++ b/models/file.js @@ -55,5 +55,13 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER }) => { File.hasOne(db.Claim); }; + File.getRecentClaims = function () { + return this.findAll({ + where: { nsfw: false, trendingEligible: true }, + order: [['createdAt', 'DESC']], + limit: 25, + }); + }; + return File; }; diff --git a/models/index.js b/models/index.js index 5e9bb620..f8293431 100644 --- a/models/index.js +++ b/models/index.js @@ -74,8 +74,4 @@ db.getTrendingClaims = (startDate) => { return 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 > "${startDate}" GROUP BY FileId ORDER BY COUNT(*) DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT }); }; -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 }); -}; - module.exports = db; -- 2.45.2 From 713fa3f1e36ef56b60821d1beb72d14828520ac7 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 31 Oct 2017 17:24:51 -0700 Subject: [PATCH 08/10] cleaned up long long --- models/claim.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/claim.js b/models/claim.js index 6c29010c..8eaca620 100644 --- a/models/claim.js +++ b/models/claim.js @@ -288,7 +288,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D case 0: return resolve(NO_CLAIM); default: - logger.debug('getTopFreeClaimIdByClaimName result:', result); + logger.debug('getTopFreeClaimIdByClaimName result:', result.dataValues); return resolve(result[0].claimId); } }) -- 2.45.2 From d3316b416d94fd86985830e4474365fe426baedc Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 31 Oct 2017 17:42:51 -0700 Subject: [PATCH 09/10] removed instance log --- controllers/serveController.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/controllers/serveController.js b/controllers/serveController.js index b4f54b6d..fb52a8f2 100644 --- a/controllers/serveController.js +++ b/controllers/serveController.js @@ -67,7 +67,7 @@ function getAssetByLongClaimId (fullClaimId, name) { db.Claim .resolveClaim(name, fullClaimId) .then(resolveResult => { - logger.debug('resolve result >> ', resolveResult); + logger.debug('resolve result >> ', resolveResult.dataValues); // if no result, return early (claim doesn't exist or isn't free) if (!resolveResult) { resolve(NO_CLAIM); @@ -223,7 +223,7 @@ module.exports = { return db.Claim.resolveClaim(fileInfo.name, fileInfo.claimId); }) .then(resolveResult => { - logger.debug('resolve result >>', resolveResult); + logger.debug('resolve result >>', resolveResult.dataValues); fileInfo['thumbnail'] = chooseThumbnail(resolveResult, DEFAULT_THUMBNAIL); fileInfo['title'] = resolveResult.title; fileInfo['description'] = resolveResult.description; -- 2.45.2 From e48a7b596dc84e0b340815aabbeffb1a1d091c25 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 31 Oct 2017 20:35:33 -0700 Subject: [PATCH 10/10] fixed issue with Op.like --- models/certificate.js | 4 ++-- models/claim.js | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/models/certificate.js b/models/certificate.js index dafb8281..b68d6df5 100644 --- a/models/certificate.js +++ b/models/certificate.js @@ -153,10 +153,10 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D where: { name : channelName, claimId: { - [Op.like]: `${channelId}%`, + [sequelize.Op.like]: `${channelId}%`, }, - order: [['height', 'ASC']], }, + order: [['height', 'ASC']], }) .then(result => { switch (result.length) { diff --git a/models/claim.js b/models/claim.js index 8eaca620..64a54b02 100644 --- a/models/claim.js +++ b/models/claim.js @@ -250,7 +250,6 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D }); }; - // sequelize.query(`SELECT claimId FROM Claim WHERE name = '${name}' AND claimId LIKE '${shortId}%' ORDER BY height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT }) Claim.getLongClaimIdFromShortClaimId = function (name, shortId) { return new Promise((resolve, reject) => { this @@ -258,7 +257,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D where: { name, claimId: { - [Op.like]: `${shortId}%`, + [sequelize.Op.like]: `${shortId}%`, }}, order: [['height', 'ASC']], }) -- 2.45.2