From bb814413573f43e58b12c79b273e3c4349a94aa7 Mon Sep 17 00:00:00 2001 From: Shawn Date: Wed, 10 Oct 2018 11:28:53 -0500 Subject: [PATCH 1/2] Fix exceptions and incorrect logic around chainquery --- server/chainquery/bundle.debug.js | 33 ++++++++++++------- .../api/channel/claims/getChannelClaims.js | 3 +- server/render/build/handleShowRender.js | 7 ++-- server/render/src/handleShowRender.jsx | 7 ++-- 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/server/chainquery/bundle.debug.js b/server/chainquery/bundle.debug.js index cf51cf81..10b027d8 100644 --- a/server/chainquery/bundle.debug.js +++ b/server/chainquery/bundle.debug.js @@ -275,6 +275,7 @@ var ClaimModel = (sequelize, { BOOLEAN, DATE, DECIMAL, + ENUM, INTEGER, STRING, TEXT, @@ -378,12 +379,8 @@ var ClaimModel = (sequelize, { type: STRING, set() { }, }, - is_filtered: { - type: BOOLEAN, - set() { }, - }, bid_state: { - type: STRING, + type: ENUM('Active', 'Expired', 'Controlling', 'Spent', 'Accepted'), set() { }, }, created_at: { @@ -836,7 +833,22 @@ const isShortClaimId = (claimId) => { return (claimId && (claimId.length < 40)); }; -var claimQueries = (db, table) => ({ +var claimQueries = (db, table, sequelize) => ({ + + getClaimChannelName: async (publisher_id) => { + return await table.findAll({ + where : { claim_id: publisher_id }, + attributes: ['name'], + }).then(result => { + if(result.length === 0) { + throw new Error(`no record found for ${claimId}`); + } else if(result.length !== 1) { + logger$1.warn(`more than one record matches ${claimId} in db.Claim`); + } + + return result[0].name; + }); + }, getShortClaimIdFromLongClaimId: async (claimId, claimName) => { logger$1.debug(`claim.getShortClaimIdFromLongClaimId for ${claimName}#${claimId}`); @@ -857,7 +869,6 @@ var claimQueries = (db, table) => ({ return await table.findAll({ where: { publisher_id: channelClaimId }, order: [['height', 'DESC']], - raw : false, // returns an array of only data, not an array of instances }) .then(channelClaimsArray => { if(channelClaimsArray.length === 0) { @@ -1062,7 +1073,7 @@ if (!database || !username || !password) { } // set sequelize options -const sequelize$1 = new Sequelize(database, username, password, { +const sequelize = new Sequelize(database, username, password, { host : host$1, import : port, dialect : 'mysql', @@ -1086,8 +1097,8 @@ for(let i = 0; i < DATABASE_STRUCTURE_KEYS.length; i++) { let dbKey = DATABASE_STRUCTURE_KEYS[i]; let currentData = DATABASE_STRUCTURE[dbKey]; - db[dbKey] = currentData.table.createModel(sequelize$1, Sequelize); - db[dbKey].queries = currentData.queries(db, db[dbKey]); + db[dbKey] = currentData.table.createModel(sequelize, Sequelize); + db[dbKey].queries = currentData.queries(db, db[dbKey], sequelize); } // run model.association for each model in the db object that has an association @@ -1100,7 +1111,7 @@ DATABASE_STRUCTURE_KEYS.forEach(modelName => { }); // establish mysql connection -sequelize$1 +sequelize .authenticate() .then(() => { logger$2.info('Sequelize has established mysql connection for chainquery successfully.'); diff --git a/server/controllers/api/channel/claims/getChannelClaims.js b/server/controllers/api/channel/claims/getChannelClaims.js index f0252b53..d1ff3b96 100644 --- a/server/controllers/api/channel/claims/getChannelClaims.js +++ b/server/controllers/api/channel/claims/getChannelClaims.js @@ -7,7 +7,8 @@ const getChannelClaims = async (channelName, channelShortId, page) => { const channelId = await chainquery.claim.queries.getLongClaimId(channelName, channelShortId); const channelClaims = await chainquery.claim.queries.getAllChannelClaims(channelId); - const processedChannelClaims = await channelClaims.map((claim) => getClaimData(claim)); + const processingChannelClaims = channelClaims ? channelClaims.map((claim) => getClaimData(claim)) : []; + const processedChannelClaims = await Promise.all(processingChannelClaims); return returnPaginatedChannelClaims(channelName, channelId, processedChannelClaims, page); }; diff --git a/server/render/build/handleShowRender.js b/server/render/build/handleShowRender.js index d777c86a..854717dc 100644 --- a/server/render/build/handleShowRender.js +++ b/server/render/build/handleShowRender.js @@ -75,8 +75,11 @@ module.exports = function (req, res) { res.claimId = showState.assetList[assetKeys[0]].claimId; } else { var channelKeys = Object.keys(showState.channelList); - res.claimId = showState.channelList[channelKeys[0]].longId; - res.isChannel = true; + + if (channelKeys.length !== 0) { + res.claimId = showState.channelList[channelKeys[0]].longId; + res.isChannel = true; + } } // render component to a string diff --git a/server/render/src/handleShowRender.jsx b/server/render/src/handleShowRender.jsx index e4db93d1..721e0f60 100644 --- a/server/render/src/handleShowRender.jsx +++ b/server/render/src/handleShowRender.jsx @@ -40,8 +40,11 @@ module.exports = (req, res) => { res.claimId = showState.assetList[assetKeys[0]].claimId; } else { const channelKeys = Object.keys(showState.channelList); - res.claimId = showState.channelList[channelKeys[0]].longId; - res.isChannel = true; + + if(channelKeys.length !== 0) { + res.claimId = showState.channelList[channelKeys[0]].longId; + res.isChannel = true; + } } // render component to a string From 2e45314610af8a9053f59bbfca80abd2fe57c3de Mon Sep 17 00:00:00 2001 From: Shawn Date: Wed, 10 Oct 2018 12:19:51 -0500 Subject: [PATCH 2/2] Fix remaining chainquery exceptions and bad logic --- server/controllers/api/claim/data/index.js | 2 +- server/controllers/api/claim/shortId/index.js | 2 +- server/controllers/assets/utils/getClaimIdAndServeAsset.js | 4 ++-- server/controllers/utils/getClaimId.js | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/server/controllers/api/claim/data/index.js b/server/controllers/api/claim/data/index.js index 482be972..b72751a4 100644 --- a/server/controllers/api/claim/data/index.js +++ b/server/controllers/api/claim/data/index.js @@ -16,7 +16,7 @@ const claimData = async ({ ip, originalUrl, body, params }, res) => { try { let resolvedClaim = await chainquery.claim.queries.resolveClaim(claimName, claimId).catch(() => {}); - + if(!resolvedClaim) { resolvedClaim = await db.Claim.resolveClaim(claimName, claimId); } diff --git a/server/controllers/api/claim/shortId/index.js b/server/controllers/api/claim/shortId/index.js index 34bf6d8e..e8d820e2 100644 --- a/server/controllers/api/claim/shortId/index.js +++ b/server/controllers/api/claim/shortId/index.js @@ -10,7 +10,7 @@ const chainquery = require('chainquery'); const claimShortId = async ({ ip, originalUrl, body, params }, res) => { try { - let shortId = await chainquery.claim.queries.getShortClaimIdFromLongClaimId(params.longId, params.name); + let shortId = await chainquery.claim.queries.getShortClaimIdFromLongClaimId(params.longId, params.name).catch(() => {}); if(shortId === null) { shortId = await db.Claim.getShortClaimIdFromLongClaimId(params.longId, params.name); diff --git a/server/controllers/assets/utils/getClaimIdAndServeAsset.js b/server/controllers/assets/utils/getClaimIdAndServeAsset.js index cca00f74..4e4cbafe 100644 --- a/server/controllers/assets/utils/getClaimIdAndServeAsset.js +++ b/server/controllers/assets/utils/getClaimIdAndServeAsset.js @@ -25,11 +25,11 @@ const getClaimIdAndServeAsset = (channelName, channelClaimId, claimName, claimId }) .then(claim => { if (!claim) { - logger.debug('Full claim id:', fullClaimId); + logger.debug('Full claim id:', claimId); return db.Claim.findOne({ where: { name : claimName, - claimId: fullClaimId, + claimId, }, }); } diff --git a/server/controllers/utils/getClaimId.js b/server/controllers/utils/getClaimId.js index 3eab179e..76ea1f47 100644 --- a/server/controllers/utils/getClaimId.js +++ b/server/controllers/utils/getClaimId.js @@ -28,7 +28,7 @@ const getClaimId = async (channelName, channelClaimId, name, claimId) => { } else { let claimIdResult = await chainquery.claim.queries.getLongClaimId(name, claimId); - if(claimIdResult === null) { + if(!claimIdResult) { claimIdResult = await db.Claim.getLongClaimId(name, claimId); }