2017-06-20 01:15:28 +02:00
|
|
|
const logger = require('winston');
|
2017-06-22 01:36:08 +02:00
|
|
|
const db = require('../models');
|
2017-08-03 02:13:02 +02:00
|
|
|
const lbryApi = require('../helpers/lbryApi.js');
|
|
|
|
const publishHelpers = require('../helpers/publishHelpers.js');
|
2017-06-03 05:56:33 +02:00
|
|
|
|
2017-06-22 01:36:08 +02:00
|
|
|
function upsert (Model, values, condition) {
|
|
|
|
return Model
|
|
|
|
.findOne({ where: condition })
|
|
|
|
.then(function (obj) {
|
|
|
|
if (obj) { // update
|
2017-07-01 02:28:53 +02:00
|
|
|
logger.silly(`updating ${values.name}:${values.claimId} in File db`);
|
2017-06-22 01:36:08 +02:00
|
|
|
return obj.update(values);
|
|
|
|
} else { // insert
|
2017-07-01 02:28:53 +02:00
|
|
|
logger.silly(`creating ${values.name}:${values.claimId} in File db`);
|
2017-06-22 01:36:08 +02:00
|
|
|
return Model.create(values);
|
|
|
|
}
|
|
|
|
}).catch(function (error) {
|
|
|
|
logger.error('Sequelize findOne error', error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-04 09:02:47 +02:00
|
|
|
function checkNameAvailability (name) {
|
2017-08-03 19:44:19 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-07-04 09:02:47 +02:00
|
|
|
// 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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-06-03 05:56:33 +02:00
|
|
|
module.exports = {
|
2017-07-04 03:27:35 +02:00
|
|
|
publish (publishParams, fileName, fileType) {
|
2017-08-03 19:44:19 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-07-04 09:02:47 +02:00
|
|
|
// 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,
|
2017-07-04 03:27:35 +02:00
|
|
|
}
|
2017-07-04 09:02:47 +02:00
|
|
|
).then(() => {
|
|
|
|
// resolve the promise with the result from lbryApi.publishClaim;
|
|
|
|
resolve(result);
|
2017-07-04 03:27:35 +02:00
|
|
|
})
|
2017-07-04 09:02:47 +02:00
|
|
|
.catch(error => {
|
|
|
|
logger.error('Sequelize findOne error', error);
|
|
|
|
// reject the promise
|
2017-07-04 03:27:35 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
2017-07-04 09:02:47 +02:00
|
|
|
})
|
|
|
|
.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);
|
|
|
|
});
|
2017-07-03 23:48:35 +02:00
|
|
|
});
|
|
|
|
},
|
2017-07-04 09:02:47 +02:00
|
|
|
checkNameAvailability (name) {
|
|
|
|
return checkNameAvailability(name);
|
|
|
|
},
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|