Block used claims #68
3 changed files with 88 additions and 73 deletions
|
@ -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);
|
||||
},
|
||||
};
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue