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,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 = { 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
lbryApi checkNameAvailability(publishParams.name)
.publishClaim(publishParams) .then(result => {
.then(result => { if (result === true) {
logger.info(`Successfully published ${fileName}`, result); // 2. publish the file
// 2. update old record or create new one (update is in case the claim has been published before by this daemon) lbryApi
upsert( .publishClaim(publishParams)
db.File, .then(result => {
{ logger.info(`Successfully published ${fileName}`, result);
name : publishParams.name, // 3. update old record or create new one (update is in case the claim has been published before by this daemon)
claimId : result.claim_id, upsert(
address : publishParams.claim_address, db.File,
outpoint: `${result.txid}:${result.nout}`, {
height : 0, name : publishParams.name,
fileName, claimId : result.claim_id,
filePath: publishParams.file_path, address : publishParams.claim_address,
fileType, outpoint: `${result.txid}:${result.nout}`,
nsfw : publishParams.metadata.nsfw, height : 0,
}, fileName,
{ filePath: publishParams.file_path,
name : publishParams.name, fileType,
claimId: result.claim_id, nsfw : publishParams.metadata.nsfw,
} },
).then(() => { {
// resolve the promise with the result from lbryApi.publishClaim; name : publishParams.name,
resolve(result); claimId: result.claim_id,
}) }
.catch(error => { ).then(() => {
logger.error('Sequelize findOne error', error); // resolve the promise with the result from lbryApi.publishClaim;
// reject the promise resolve(result);
reject(error); })
}); .catch(error => {
}) logger.error('Sequelize findOne error', error);
.catch(error => { // reject the promise
// delete the local file reject(error);
publishHelpers.deleteTemporaryFile(publishParams.file_path); });
// 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; return deferred;
}, },
checkNameAvailability (name) { checkNameAvailability (name) {
const deferred = new Promise((resolve, reject) => { return checkNameAvailability(name);
// 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;
}, },
}; };

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);
}; };