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)
|
2017-09-21 21:05:04 +02:00
|
|
|
.then(tx => {
|
|
|
|
logger.info(`Successfully published ${fileName}`, tx);
|
|
|
|
publishResults = tx;
|
2017-11-06 23:15:47 +01:00
|
|
|
// get the channel information
|
2017-11-03 21:37:23 +01:00
|
|
|
if (publishParams.channel_name) {
|
|
|
|
logger.debug(`this claim was published in channel: ${publishParams.channel_name}`);
|
2017-11-03 22:23:54 +01:00
|
|
|
return db.Channel.findOne({where: {channelName: publishParams.channel_name}});
|
2017-10-05 23:48:08 +02:00
|
|
|
} else {
|
2017-11-03 21:37:23 +01:00
|
|
|
logger.debug('this claim was published in channel: n/a');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
})
|
2017-11-03 22:23:54 +01:00
|
|
|
.then(channel => {
|
2017-11-06 23:22:00 +01:00
|
|
|
// set channel information
|
2017-11-06 23:15:47 +01:00
|
|
|
certificateId = null;
|
|
|
|
channelName = null;
|
2017-11-03 22:23:54 +01:00
|
|
|
if (channel) {
|
|
|
|
certificateId = channel.channelClaimId;
|
|
|
|
channelName = channel.channelName;
|
|
|
|
}
|
2017-11-03 21:37:23 +01:00
|
|
|
logger.debug(`certificateId: ${certificateId}`);
|
2017-11-06 23:15:47 +01:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
// create the File record
|
2017-09-21 21:05:04 +02:00
|
|
|
const fileRecord = {
|
2017-08-21 02:45:29 +02:00
|
|
|
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,
|
2017-08-16 20:00:17 +02:00
|
|
|
fileName,
|
2017-08-21 02:45:29 +02:00
|
|
|
filePath : publishParams.file_path,
|
2017-08-16 20:00:17 +02:00
|
|
|
fileType,
|
2017-08-21 02:45:29 +02:00
|
|
|
nsfw : publishParams.metadata.nsfw,
|
2017-08-16 20:00:17 +02:00
|
|
|
};
|
2017-11-06 23:15:47 +01:00
|
|
|
// create the Claim record
|
2017-09-21 21:05:04 +02:00
|
|
|
const claimRecord = {
|
2017-10-05 23:48:08 +02:00
|
|
|
name : publishParams.name,
|
|
|
|
claimId : publishResults.claim_id,
|
|
|
|
title : publishParams.metadata.title,
|
|
|
|
description: publishParams.metadata.description,
|
|
|
|
address : publishParams.claim_address,
|
2017-10-26 21:49:06 +02:00
|
|
|
thumbnail : publishParams.metadata.thumbnail,
|
2017-10-05 23:48:08 +02:00
|
|
|
outpoint : `${publishResults.txid}:${publishResults.nout}`,
|
|
|
|
height : 0,
|
|
|
|
contentType: fileType,
|
|
|
|
nsfw : publishParams.metadata.nsfw,
|
|
|
|
amount : publishParams.bid,
|
2017-11-03 22:23:54 +01:00
|
|
|
certificateId,
|
|
|
|
channelName,
|
2017-09-21 21:05:04 +02:00
|
|
|
};
|
2017-11-06 23:22:00 +01:00
|
|
|
// upsert criteria
|
2017-09-21 21:05:04 +02:00
|
|
|
const upsertCriteria = {
|
2017-08-16 20:00:17 +02:00
|
|
|
name : publishParams.name,
|
|
|
|
claimId: publishResults.claim_id,
|
|
|
|
};
|
2017-11-06 23:22:00 +01:00
|
|
|
// upsert the records
|
2017-09-21 21:05:04 +02:00
|
|
|
return Promise.all([db.upsert(db.File, fileRecord, upsertCriteria, 'File'), db.upsert(db.Claim, claimRecord, upsertCriteria, 'Claim')]);
|
2017-08-16 20:00:17 +02:00
|
|
|
})
|
2017-09-25 20:55:56 +02:00
|
|
|
.then(([file, claim]) => {
|
2017-08-16 20:00:17 +02:00
|
|
|
logger.debug('File and Claim records successfully created');
|
2017-09-21 21:05:04 +02:00
|
|
|
return Promise.all([file.setClaim(claim), claim.setFile(file)]);
|
2017-09-21 20:00:06 +02:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
logger.debug('File and Claim records successfully associated');
|
2017-08-16 20:00:17 +02:00
|
|
|
resolve(publishResults); // resolve the promise with the result from lbryApi.publishClaim;
|
|
|
|
})
|
2017-07-04 09:02:47 +02:00
|
|
|
.catch(error => {
|
2017-08-16 20:00:17 +02:00
|
|
|
publishHelpers.deleteTemporaryFile(publishParams.file_path); // delete the local file
|
2017-07-04 09:02:47 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
2017-07-03 23:48:35 +02:00
|
|
|
});
|
|
|
|
},
|
2017-12-09 02:50:47 +01:00
|
|
|
checkClaimNameAvailability (name) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// find any records where the name is used
|
|
|
|
db.File.findAll({ where: { name } })
|
|
|
|
.then(result => {
|
|
|
|
if (result.length >= 1) {
|
|
|
|
const claimAddress = config.wallet.lbryClaimAddress;
|
|
|
|
// filter out any results that were not published from spee.ch's wallet address
|
|
|
|
const filteredResult = result.filter((claim) => {
|
|
|
|
return (claim.address === claimAddress);
|
|
|
|
});
|
|
|
|
// return based on whether any non-spee.ch claims were left
|
|
|
|
if (filteredResult.length >= 1) {
|
|
|
|
resolve(false);
|
|
|
|
} else {
|
|
|
|
resolve(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resolve(true);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
checkChannelAvailability (name) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// find any records where the name is used
|
|
|
|
db.Channel.findAll({ where: { channelName: name } })
|
|
|
|
.then(result => {
|
|
|
|
if (result.length >= 1) {
|
|
|
|
return resolve(false);
|
|
|
|
}
|
|
|
|
resolve(true);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|