Block used claims #68

Merged
bones7242 merged 6 commits from block-used-claims into master 2017-07-04 18:17:28 +02:00
3 changed files with 88 additions and 73 deletions
Showing only changes of commit 5083309edd - Show all commits

View file

@ -19,15 +19,53 @@ 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 = { module.exports = {
publish (publishParams, fileName, fileType) { publish (publishParams, fileName, fileType) {
const deferred = new Promise((resolve, reject) => { const deferred = new Promise((resolve, reject) => {
// 1. publish the file // 1. make sure the name is available
checkNameAvailability(publishParams.name)
.then(result => {
if (result === true) {
// 2. publish the file
lbryApi lbryApi
.publishClaim(publishParams) .publishClaim(publishParams)
.then(result => { .then(result => {
logger.info(`Successfully published ${fileName}`, 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) // 3. update old record or create new one (update is in case the claim has been published before by this daemon)
upsert( upsert(
db.File, db.File,
{ {
@ -61,34 +99,9 @@ module.exports = {
// reject the promise // reject the promise
reject(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 { } else {
resolve(true); 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);
});
} else {
resolve(true);
} }
}) })
.catch(error => { .catch(error => {
@ -97,4 +110,7 @@ module.exports = {
}); });
return deferred; return deferred;
}, },
checkNameAvailability (name) {
return checkNameAvailability(name);
},
}; };

View file

@ -3,7 +3,7 @@ const { postToStats } = require('../../controllers/statsController.js');
module.exports = { module.exports = {
handleRequestError (action, originalUrl, ip, error, res) { handleRequestError (action, originalUrl, ip, error, res) {
logger.error('Request Error >>', error); logger.error('Request Error >>', error.message);
if (error.response) { if (error.response) {
postToStats(action, originalUrl, ip, error.response.data.error.messsage); postToStats(action, originalUrl, ip, error.response.data.error.messsage);
res.status(error.response.status).send(error.response.data.error.message); 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.'); res.status(503).send('Connection refused. The daemon may not be running.');
} else { } else {
postToStats(action, originalUrl, ip, error); postToStats(action, originalUrl, ip, error);
res.status(400).send(JSON.stringify(error)); res.status(400).send(error.message);
} }
}, },
handlePublishError (error) { handlePublishError (error) {

View file

@ -123,7 +123,6 @@ document.getElementById('publish-submit').addEventListener('click', function(eve
} else { } else {
alert("That name has already been claimed by spee.ch. Please choose a different name."); alert("That name has already been claimed by spee.ch. Please choose a different name.");
} }
} else { } else {
console.log("request to check claim name failed with status:", this.status); console.log("request to check claim name failed with status:", this.status);
}; };