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-12-09 02:50:47 +01:00
|
|
|
const config = require('../config/speechConfig.js');
|
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-11-06 23:15:47 +01:00
|
|
|
let publishResults, certificateId, channelName;
|
|
|
|
// publish the file
|
2017-10-12 22:10:44 +02:00
|
|
|
return lbryApi.publishClaim(publishParams)
|
2018-02-22 02:02:57 +01:00
|
|
|
.then(tx => {
|
|
|
|
logger.info(`Successfully published ${publishParams.name} ${fileName}`, tx);
|
|
|
|
publishResults = tx;
|
|
|
|
// get the channel information
|
|
|
|
if (publishParams.channel_name) {
|
|
|
|
logger.debug(`this claim was published in channel: ${publishParams.channel_name}`);
|
|
|
|
return db.Channel.findOne({where: {channelName: publishParams.channel_name}});
|
|
|
|
} else {
|
|
|
|
logger.debug('this claim was not published in a channel');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(channel => {
|
2017-11-06 23:22:00 +01:00
|
|
|
// set channel information
|
2018-02-22 02:02:57 +01:00
|
|
|
certificateId = null;
|
|
|
|
channelName = null;
|
|
|
|
if (channel) {
|
|
|
|
certificateId = channel.channelClaimId;
|
|
|
|
channelName = channel.channelName;
|
|
|
|
}
|
|
|
|
logger.debug(`certificateId: ${certificateId}`);
|
|
|
|
})
|
|
|
|
.then(() => {
|
2017-11-06 23:15:47 +01:00
|
|
|
// create the File record
|
2018-02-22 02:02:57 +01:00
|
|
|
const fileRecord = {
|
|
|
|
name : publishParams.name,
|
|
|
|
claimId : publishResults.claim_id,
|
|
|
|
title : publishParams.metadata.title,
|
|
|
|
description: publishParams.metadata.description,
|
|
|
|
address : publishParams.claim_address,
|
|
|
|
outpoint : `${publishResults.txid}:${publishResults.nout}`,
|
|
|
|
height : 0,
|
|
|
|
fileName,
|
|
|
|
filePath : publishParams.file_path,
|
|
|
|
fileType,
|
|
|
|
nsfw : publishParams.metadata.nsfw,
|
|
|
|
};
|
|
|
|
// create the Claim record
|
|
|
|
const claimRecord = {
|
|
|
|
name : publishParams.name,
|
|
|
|
claimId : publishResults.claim_id,
|
|
|
|
title : publishParams.metadata.title,
|
|
|
|
description: publishParams.metadata.description,
|
|
|
|
address : publishParams.claim_address,
|
|
|
|
thumbnail : publishParams.metadata.thumbnail,
|
|
|
|
outpoint : `${publishResults.txid}:${publishResults.nout}`,
|
|
|
|
height : 0,
|
|
|
|
contentType: fileType,
|
|
|
|
nsfw : publishParams.metadata.nsfw,
|
|
|
|
amount : publishParams.bid,
|
|
|
|
certificateId,
|
|
|
|
channelName,
|
|
|
|
};
|
|
|
|
// upsert criteria
|
|
|
|
const upsertCriteria = {
|
|
|
|
name : publishParams.name,
|
|
|
|
claimId: publishResults.claim_id,
|
|
|
|
};
|
|
|
|
// upsert the records
|
|
|
|
return Promise.all([db.upsert(db.File, fileRecord, upsertCriteria, 'File'), db.upsert(db.Claim, claimRecord, upsertCriteria, 'Claim')]);
|
|
|
|
})
|
|
|
|
.then(([file, claim]) => {
|
|
|
|
logger.debug('File and Claim records successfully created');
|
|
|
|
return Promise.all([file.setClaim(claim), claim.setFile(file)]);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
logger.debug('File and Claim records successfully associated');
|
|
|
|
resolve(publishResults); // resolve the promise with the result from lbryApi.publishClaim;
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error('PUBLISH ERROR', error);
|
|
|
|
publishHelpers.deleteTemporaryFile(publishParams.file_path); // delete the local file
|
|
|
|
reject(error);
|
|
|
|
});
|
2017-07-03 23:48:35 +02:00
|
|
|
});
|
|
|
|
},
|
2018-03-06 00:38:26 +01:00
|
|
|
claimNameIsAvailable (name) {
|
2018-03-05 23:44:37 +01:00
|
|
|
// find any records where the name is used
|
2018-03-08 03:41:58 +01:00
|
|
|
return db.Claim
|
|
|
|
.findAll({
|
|
|
|
attributes: ['address'],
|
|
|
|
where : { name },
|
|
|
|
})
|
2018-03-05 23:44:37 +01:00
|
|
|
.then(result => {
|
|
|
|
if (result.length >= 1) {
|
|
|
|
const claimAddress = config.wallet.lbryClaimAddress;
|
2018-03-08 03:57:35 +01:00
|
|
|
// filter out any that were not published from this address
|
2018-03-05 23:44:37 +01:00
|
|
|
const filteredResult = result.filter((claim) => {
|
|
|
|
return (claim.address === claimAddress);
|
|
|
|
});
|
|
|
|
if (filteredResult.length >= 1) {
|
|
|
|
throw new Error('That claim is already in use');
|
|
|
|
};
|
|
|
|
return name;
|
|
|
|
};
|
|
|
|
return name;
|
2018-03-08 04:39:22 +01:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
throw error;
|
2018-03-05 23:44:37 +01:00
|
|
|
});
|
2017-12-09 02:50:47 +01:00
|
|
|
},
|
|
|
|
checkChannelAvailability (name) {
|
2018-03-08 04:39:22 +01:00
|
|
|
return db.Channel
|
|
|
|
.findAll({
|
|
|
|
where: { channelName: name },
|
|
|
|
})
|
|
|
|
.then(result => {
|
|
|
|
if (result.length >= 1) {
|
|
|
|
throw new Error('That channel has already been claimed');
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
throw error;
|
|
|
|
});
|
2017-12-09 02:50:47 +01:00
|
|
|
},
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|