diff --git a/controllers/publishController.js b/controllers/publishController.js index db29902e..06e5f0eb 100644 --- a/controllers/publishController.js +++ b/controllers/publishController.js @@ -19,82 +19,98 @@ function upsert (Model, values, condition) { }); } +function checkNameAvailability (name) { + const deferred = new Promise((resolve, reject) => { + // find any records where the name is used + db.File + .findAll({ where: { name } }) + .then(result => { + if (result.length >= 1) { + // filter out any results that were not published from a spee.ch wallet address + lbryApi + .getWalletList() + .then((walletList) => { + const filteredResult = result.filter((claim) => { + return walletList.includes(claim.address); + }); + if (filteredResult.length >= 1) { + resolve(false); + } else { + resolve(true); + } + }) + .catch((error) => { + reject(error); + }); + } else { + resolve(true); + } + }) + .catch(error => { + reject(error); + }); + }); + return deferred; +}; + module.exports = { publish (publishParams, fileName, fileType) { const deferred = new Promise((resolve, reject) => { - // 1. publish the file - lbryApi - .publishClaim(publishParams) - .then(result => { - logger.info(`Successfully published ${fileName}`, result); - // 2. update old record or create new one (update is in case the claim has been published before by this daemon) - upsert( - db.File, - { - name : publishParams.name, - claimId : result.claim_id, - address : publishParams.claim_address, - outpoint: `${result.txid}:${result.nout}`, - height : 0, - fileName, - filePath: publishParams.file_path, - fileType, - nsfw : publishParams.metadata.nsfw, - }, - { - name : publishParams.name, - claimId: result.claim_id, - } - ).then(() => { - // resolve the promise with the result from lbryApi.publishClaim; - resolve(result); - }) - .catch(error => { - logger.error('Sequelize findOne error', error); - // reject the promise - reject(error); - }); - }) - .catch(error => { - // delete the local file - publishHelpers.deleteTemporaryFile(publishParams.file_path); - // reject the promise - reject(error); - }); + // 1. make sure the name is available + checkNameAvailability(publishParams.name) + .then(result => { + if (result === true) { + // 2. publish the file + lbryApi + .publishClaim(publishParams) + .then(result => { + logger.info(`Successfully published ${fileName}`, result); + // 3. update old record or create new one (update is in case the claim has been published before by this daemon) + upsert( + db.File, + { + name : publishParams.name, + claimId : result.claim_id, + address : publishParams.claim_address, + outpoint: `${result.txid}:${result.nout}`, + height : 0, + fileName, + filePath: publishParams.file_path, + fileType, + nsfw : publishParams.metadata.nsfw, + }, + { + name : publishParams.name, + claimId: result.claim_id, + } + ).then(() => { + // resolve the promise with the result from lbryApi.publishClaim; + resolve(result); + }) + .catch(error => { + logger.error('Sequelize findOne error', error); + // reject the promise + reject(error); + }); + }) + .catch(error => { + // delete the local file + publishHelpers.deleteTemporaryFile(publishParams.file_path); + // reject the promise + reject(error); + }); + } else { + const err = new Error('That name has already been claimed by spee.ch. Please choose a new claim name.'); + reject(err); + } + }) + .catch(error => { + reject(error); + }); }); return deferred; }, checkNameAvailability (name) { - const deferred = new Promise((resolve, reject) => { - // find any records where the name is used - db.File - .findAll({ where: { name } }) - .then(result => { - if (result.length >= 1) { - // filter out any results that were not published from a spee.ch wallet address - lbryApi - .getWalletList() - .then((walletList) => { - const filteredResult = result.filter((claim) => { - return walletList.includes(claim.address); - }); - if (filteredResult.length >= 1) { - resolve(false); - } else { - resolve(true); - } - }) - .catch((error) => { - reject(error); - }); - } else { - resolve(true); - } - }) - .catch(error => { - reject(error); - }); - }); - return deferred; + return checkNameAvailability(name); }, }; diff --git a/helpers/libraries/errorHandlers.js b/helpers/libraries/errorHandlers.js index 4d922c3b..c76ff38c 100644 --- a/helpers/libraries/errorHandlers.js +++ b/helpers/libraries/errorHandlers.js @@ -3,7 +3,7 @@ const { postToStats } = require('../../controllers/statsController.js'); module.exports = { handleRequestError (action, originalUrl, ip, error, res) { - logger.error('Request Error >>', error); + logger.error('Request Error >>', error.message); if (error.response) { postToStats(action, originalUrl, ip, error.response.data.error.messsage); res.status(error.response.status).send(error.response.data.error.message); @@ -12,7 +12,7 @@ module.exports = { res.status(503).send('Connection refused. The daemon may not be running.'); } else { postToStats(action, originalUrl, ip, error); - res.status(400).send(JSON.stringify(error)); + res.status(400).send(error.message); } }, handlePublishError (error) { diff --git a/public/assets/js/claimPublish.js b/public/assets/js/claimPublish.js index 534af660..e43424aa 100644 --- a/public/assets/js/claimPublish.js +++ b/public/assets/js/claimPublish.js @@ -123,7 +123,6 @@ document.getElementById('publish-submit').addEventListener('click', function(eve } else { alert("That name has already been claimed by spee.ch. Please choose a different name."); } - } else { console.log("request to check claim name failed with status:", this.status); };