From b785edf100ec126555a19be3334d17aaef4a1fb0 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Thu, 26 Apr 2018 19:19:11 -0700 Subject: [PATCH] moved remaining helpers except for publishHelpers --- index.js | 2 +- server/controllers/publishController.js | 2 +- server/helpers/configVarCheck.js | 17 --------- server/helpers/sequelizeHelpers.js | 25 ------------ server/helpers/statsHelpers.js | 38 ------------------- server/models/certificate.js | 2 +- server/models/claim.js | 2 +- server/models/utils/returnShortId.js | 25 ++++++++++++ server/routes/api/channelAvailability.js | 2 +- server/routes/api/claimAvailability.js | 2 +- server/routes/api/claimGet.js | 2 +- server/routes/api/claimList.js | 2 +- server/routes/api/claimPublish.js | 2 +- server/routes/api/claimResolve.js | 2 +- server/routes/api/index.js | 2 +- server/routes/assets/serveAssetByClaim.js | 2 +- .../assets/serveAssetByIdentifierAndClaim.js | 2 +- .../utils}/multipartMiddleware.js | 2 +- server/speechPassport/utils/local-signup.js | 2 +- server/{helpers => utils}/googleAnalytics.js | 0 server/{helpers => utils}/lbryApi.js | 0 21 files changed, 40 insertions(+), 95 deletions(-) delete mode 100644 server/helpers/configVarCheck.js delete mode 100644 server/helpers/sequelizeHelpers.js delete mode 100644 server/helpers/statsHelpers.js create mode 100644 server/models/utils/returnShortId.js rename server/{helpers => routes/utils}/multipartMiddleware.js (64%) rename server/{helpers => utils}/googleAnalytics.js (100%) rename server/{helpers => utils}/lbryApi.js (100%) diff --git a/index.js b/index.js index bcb6bb08..0e2a6efc 100644 --- a/index.js +++ b/index.js @@ -55,7 +55,7 @@ function Server () { app.use(requestLogger); // configure passport - const speechPassport = require('./server/speechPassport/index'); + const speechPassport = require('./server/speechPassport'); // initialize passport const sessionKey = siteConfig.auth.sessionKey; app.use(cookieSession({ diff --git a/server/controllers/publishController.js b/server/controllers/publishController.js index cee6dbbe..9037fccc 100644 --- a/server/controllers/publishController.js +++ b/server/controllers/publishController.js @@ -1,6 +1,6 @@ const logger = require('winston'); const db = require('../models'); -const lbryApi = require('../helpers/lbryApi.js'); +const lbryApi = require('../utils/lbryApi.js'); const publishHelpers = require('../helpers/publishHelpers.js'); const { publishing: { primaryClaimAddress, additionalClaimAddresses } } = require('../../config/siteConfig.js'); const Sequelize = require('sequelize'); diff --git a/server/helpers/configVarCheck.js b/server/helpers/configVarCheck.js deleted file mode 100644 index 642125ba..00000000 --- a/server/helpers/configVarCheck.js +++ /dev/null @@ -1,17 +0,0 @@ -const logger = require('winston'); - -module.exports = (config) => { - // get the config file - for (let configCategoryKey in config) { - if (config.hasOwnProperty(configCategoryKey)) { - // get the final variables for each config category - const configVariables = config[configCategoryKey]; - for (let configVarKey in configVariables) { - if (configVariables.hasOwnProperty(configVarKey)) { - // print each variable - logger.debug(`CONFIG CHECK: ${configCategoryKey}.${configVarKey} === ${configVariables[configVarKey]}`); - } - } - } - } -}; diff --git a/server/helpers/sequelizeHelpers.js b/server/helpers/sequelizeHelpers.js deleted file mode 100644 index 0297f676..00000000 --- a/server/helpers/sequelizeHelpers.js +++ /dev/null @@ -1,25 +0,0 @@ -module.exports = { - returnShortId: function (claimsArray, longId) { - let claimIndex; - let shortId = longId.substring(0, 1); // default short id is the first letter - let shortIdLength = 0; - // find the index of this claim id - claimIndex = claimsArray.findIndex(element => { - return element.claimId === longId; - }); - if (claimIndex < 0) { - throw new Error('claim id not found in claims list'); - } - // get an array of all claims with lower height - let possibleMatches = claimsArray.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 && (element.claimId.substring(0, shortIdLength) === shortId)); - }); - } - return shortId; - }, -}; diff --git a/server/helpers/statsHelpers.js b/server/helpers/statsHelpers.js deleted file mode 100644 index 9e471172..00000000 --- a/server/helpers/statsHelpers.js +++ /dev/null @@ -1,38 +0,0 @@ -const logger = require('winston'); -const db = require('../models'); - -module.exports = { - postToStats (action, url, ipAddress, name, claimId, result) { - logger.debug('action:', action); - // make sure the result is a string - if (result && (typeof result !== 'string')) { - result = result.toString(); - } - // make sure the ip address(es) are a string - if (ipAddress && (typeof ipAddress !== 'string')) { - ipAddress = ipAddress.toString(); - } - db.File - .findOne({where: { name, claimId }}) - .then(file => { - // create record in the db - let FileId; - if (file) { - FileId = file.dataValues.id; - } else { - FileId = null; - } - return db.Request - .create({ - action, - url, - ipAddress, - result, - FileId, - }); - }) - .catch(error => { - logger.error('Sequelize error >>', error); - }); - }, -}; diff --git a/server/models/certificate.js b/server/models/certificate.js index 7a83f66d..679f6312 100644 --- a/server/models/certificate.js +++ b/server/models/certificate.js @@ -1,5 +1,5 @@ const logger = require('winston'); -const { returnShortId } = require('../helpers/sequelizeHelpers.js'); +const returnShortId = require('./utils/returnShortId.js'); module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => { const Certificate = sequelize.define( diff --git a/server/models/claim.js b/server/models/claim.js index b7803cb6..1ab5f355 100644 --- a/server/models/claim.js +++ b/server/models/claim.js @@ -1,5 +1,5 @@ const logger = require('winston'); -const { returnShortId } = require('../helpers/sequelizeHelpers.js'); +const returnShortId = require('./utils/returnShortId.js'); const { assetDefaults: { thumbnail: defaultThumbnail }, details: { host } } = require('../../config/siteConfig.js'); function determineFileExtensionFromContentType (contentType) { diff --git a/server/models/utils/returnShortId.js b/server/models/utils/returnShortId.js new file mode 100644 index 00000000..9c362c61 --- /dev/null +++ b/server/models/utils/returnShortId.js @@ -0,0 +1,25 @@ +const returnShortId = (claimsArray, longId) => { + let claimIndex; + let shortId = longId.substring(0, 1); // default short id is the first letter + let shortIdLength = 0; + // find the index of this claim id + claimIndex = claimsArray.findIndex(element => { + return element.claimId === longId; + }); + if (claimIndex < 0) { + throw new Error('claim id not found in claims list'); + } + // get an array of all claims with lower height + let possibleMatches = claimsArray.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 && (element.claimId.substring(0, shortIdLength) === shortId)); + }); + } + return shortId; +}; + +module.exports = returnShortId; diff --git a/server/routes/api/channelAvailability.js b/server/routes/api/channelAvailability.js index 672bdcd0..6ea7a76b 100644 --- a/server/routes/api/channelAvailability.js +++ b/server/routes/api/channelAvailability.js @@ -1,5 +1,5 @@ const { checkChannelAvailability } = require('../../controllers/publishController.js'); -const { sendGATimingEvent } = require('../../helpers/googleAnalytics.js'); +const { sendGATimingEvent } = require('../../utils/googleAnalytics.js'); const { handleErrorResponse } = require('../utils/errorHandlers.js'); /* diff --git a/server/routes/api/claimAvailability.js b/server/routes/api/claimAvailability.js index c4d05cdd..575bd4d4 100644 --- a/server/routes/api/claimAvailability.js +++ b/server/routes/api/claimAvailability.js @@ -1,5 +1,5 @@ const { claimNameIsAvailable } = require('../../controllers/publishController.js'); -const { sendGATimingEvent } = require('../../helpers/googleAnalytics.js'); +const { sendGATimingEvent } = require('../../utils/googleAnalytics.js'); const { handleErrorResponse } = require('../utils/errorHandlers.js'); /* diff --git a/server/routes/api/claimGet.js b/server/routes/api/claimGet.js index 1551b7f2..77553397 100644 --- a/server/routes/api/claimGet.js +++ b/server/routes/api/claimGet.js @@ -1,4 +1,4 @@ -const { getClaim } = require('../../helpers/lbryApi.js'); +const { getClaim } = require('../../utils/lbryApi.js'); const { addGetResultsToFileData, createFileData } = require('../../helpers/publishHelpers.js'); const { handleErrorResponse } = require('../utils/errorHandlers.js'); const db = require('../../models'); diff --git a/server/routes/api/claimList.js b/server/routes/api/claimList.js index e5a54215..2502007a 100644 --- a/server/routes/api/claimList.js +++ b/server/routes/api/claimList.js @@ -1,4 +1,4 @@ -const { getClaimList } = require('../../helpers/lbryApi.js'); +const { getClaimList } = require('../../utils/lbryApi.js'); const { handleErrorResponse } = require('../utils/errorHandlers.js'); /* diff --git a/server/routes/api/claimPublish.js b/server/routes/api/claimPublish.js index c3074421..d1312fdc 100644 --- a/server/routes/api/claimPublish.js +++ b/server/routes/api/claimPublish.js @@ -1,7 +1,7 @@ const { createBasicPublishParams, createThumbnailPublishParams, parsePublishApiRequestBody, parsePublishApiRequestFiles } = require('../../helpers/publishHelpers.js'); const { claimNameIsAvailable, publish } = require('../../controllers/publishController.js'); const { authenticateUser } = require('../../auth/authentication.js'); -const { sendGATimingEvent } = require('../../helpers/googleAnalytics.js'); +const { sendGATimingEvent } = require('../../utils/googleAnalytics.js'); const { handleErrorResponse } = require('../utils/errorHandlers.js'); const { details: { host } } = require('../../../config/siteConfig.js'); diff --git a/server/routes/api/claimResolve.js b/server/routes/api/claimResolve.js index 8f8aeaf9..459efa51 100644 --- a/server/routes/api/claimResolve.js +++ b/server/routes/api/claimResolve.js @@ -1,4 +1,4 @@ -const { resolveUri } = require('../../helpers/lbryApi.js'); +const { resolveUri } = require('../../utils/lbryApi.js'); const { handleErrorResponse } = require('../utils/errorHandlers.js'); /* diff --git a/server/routes/api/index.js b/server/routes/api/index.js index b6ce0964..59e9e169 100644 --- a/server/routes/api/index.js +++ b/server/routes/api/index.js @@ -12,7 +12,7 @@ const claimShortId = require('./claimShortId'); const claimList = require('./claimList'); const fileAvailability = require('./fileAvailability'); -const multipartMiddleware = require('../../helpers/multipartMiddleware'); +const multipartMiddleware = require('../utils/multipartMiddleware'); module.exports = (app) => { // channel routes diff --git a/server/routes/assets/serveAssetByClaim.js b/server/routes/assets/serveAssetByClaim.js index fa73c031..63592043 100644 --- a/server/routes/assets/serveAssetByClaim.js +++ b/server/routes/assets/serveAssetByClaim.js @@ -1,4 +1,4 @@ -const { sendGAServeEvent } = require('../../helpers/googleAnalytics'); +const { sendGAServeEvent } = require('../../utils/googleAnalytics'); const { determineResponseType, logRequestData, diff --git a/server/routes/assets/serveAssetByIdentifierAndClaim.js b/server/routes/assets/serveAssetByIdentifierAndClaim.js index 71c2d413..f7bd8d18 100644 --- a/server/routes/assets/serveAssetByIdentifierAndClaim.js +++ b/server/routes/assets/serveAssetByIdentifierAndClaim.js @@ -1,4 +1,4 @@ -const { sendGAServeEvent } = require('../../helpers/googleAnalytics'); +const { sendGAServeEvent } = require('../../utils/googleAnalytics'); const { determineResponseType, flipClaimNameAndIdForBackwardsCompatibility, diff --git a/server/helpers/multipartMiddleware.js b/server/routes/utils/multipartMiddleware.js similarity index 64% rename from server/helpers/multipartMiddleware.js rename to server/routes/utils/multipartMiddleware.js index e85fc5c6..bd2e98f6 100644 --- a/server/helpers/multipartMiddleware.js +++ b/server/routes/utils/multipartMiddleware.js @@ -1,5 +1,5 @@ const multipart = require('connect-multiparty'); -const { publishing: { uploadDirectory } } = require('../../config/siteConfig.js'); +const { publishing: { uploadDirectory } } = require('../../../config/siteConfig.js'); const multipartMiddleware = multipart({uploadDir: uploadDirectory}); module.exports = multipartMiddleware; diff --git a/server/speechPassport/utils/local-signup.js b/server/speechPassport/utils/local-signup.js index cabb0b0f..0f4326e6 100644 --- a/server/speechPassport/utils/local-signup.js +++ b/server/speechPassport/utils/local-signup.js @@ -1,5 +1,5 @@ const PassportLocalStrategy = require('passport-local').Strategy; -const lbryApi = require('../../helpers/lbryApi.js'); +const lbryApi = require('../../utils/lbryApi.js'); const logger = require('winston'); const db = require('../../models/index'); diff --git a/server/helpers/googleAnalytics.js b/server/utils/googleAnalytics.js similarity index 100% rename from server/helpers/googleAnalytics.js rename to server/utils/googleAnalytics.js diff --git a/server/helpers/lbryApi.js b/server/utils/lbryApi.js similarity index 100% rename from server/helpers/lbryApi.js rename to server/utils/lbryApi.js