2018-04-27 08:42:22 +02:00
|
|
|
const logger = require('winston');
|
2018-07-26 22:48:53 +02:00
|
|
|
const db = require('../../../../models');
|
2018-08-21 14:53:17 +02:00
|
|
|
const { publishClaim } = require('../../../../lbrynet');
|
2018-07-26 22:48:53 +02:00
|
|
|
const { createFileRecordDataAfterPublish } = require('../../../../models/utils/createFileRecordData.js');
|
|
|
|
const { createClaimRecordDataAfterPublish } = require('../../../../models/utils/createClaimRecordData.js');
|
2018-04-27 08:42:22 +02:00
|
|
|
const deleteFile = require('./deleteFile.js');
|
|
|
|
|
2018-11-07 23:51:06 +01:00
|
|
|
const publish = async (publishParams, fileName, fileType) => {
|
2018-08-21 14:53:17 +02:00
|
|
|
let publishResults;
|
2018-09-25 15:57:42 +02:00
|
|
|
let channel;
|
2018-08-21 14:53:17 +02:00
|
|
|
let fileRecord;
|
2018-11-07 23:51:06 +01:00
|
|
|
let newFile = Boolean(publishParams.file_path);
|
2018-10-12 08:00:07 +02:00
|
|
|
|
2018-08-21 14:53:17 +02:00
|
|
|
try {
|
|
|
|
publishResults = await publishClaim(publishParams);
|
|
|
|
logger.info(`Successfully published ${publishParams.name} ${fileName}`, publishResults);
|
2018-10-22 16:39:13 +02:00
|
|
|
const outpoint = `${publishResults.output.txid}:${publishResults.output.nout}`;
|
2018-09-25 15:57:42 +02:00
|
|
|
// get the channel information
|
|
|
|
if (publishParams.channel_name) {
|
|
|
|
logger.debug(`this claim was published in channel: ${publishParams.channel_name}`);
|
|
|
|
channel = await db.Channel.findOne({
|
|
|
|
where: {
|
|
|
|
channelName: publishParams.channel_name,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
channel = null;
|
|
|
|
}
|
|
|
|
const certificateId = channel ? channel.channelClaimId : null;
|
|
|
|
const channelName = channel ? channel.channelName : null;
|
2018-08-21 14:53:17 +02:00
|
|
|
|
|
|
|
const claimRecord = await createClaimRecordDataAfterPublish(certificateId, channelName, fileName, fileType, publishParams, publishResults);
|
|
|
|
const {claimId} = claimRecord;
|
|
|
|
const upsertCriteria = {name: publishParams.name, claimId};
|
|
|
|
if (newFile) {
|
2018-11-07 23:51:06 +01:00
|
|
|
// this is the problem
|
|
|
|
//
|
2018-08-21 14:53:17 +02:00
|
|
|
fileRecord = await createFileRecordDataAfterPublish(fileName, fileType, publishParams, publishResults);
|
|
|
|
} else {
|
2018-11-07 23:51:06 +01:00
|
|
|
fileRecord = await db.File.findOne({where: {claimId}}).then(result => result.dataValues);
|
2018-08-21 14:53:17 +02:00
|
|
|
}
|
|
|
|
|
2018-11-07 23:51:06 +01:00
|
|
|
logger.info('fileRecord:', fileRecord);
|
|
|
|
logger.info('claimRecord:', claimRecord);
|
|
|
|
logger.info('upsertCriteria:', upsertCriteria);
|
|
|
|
|
2018-08-21 14:53:17 +02:00
|
|
|
const [file, claim] = await Promise.all([
|
|
|
|
db.upsert(db.File, fileRecord, upsertCriteria, 'File'),
|
|
|
|
db.upsert(db.Claim, claimRecord, upsertCriteria, 'Claim'),
|
|
|
|
]);
|
2018-11-07 23:51:06 +01:00
|
|
|
logger.info(`File and Claim records successfully created (${publishParams.name})`);
|
2018-08-21 14:53:17 +02:00
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
file.setClaim(claim),
|
|
|
|
claim.setFile(file),
|
|
|
|
]);
|
2018-11-07 23:51:06 +01:00
|
|
|
logger.info(`File and Claim records successfully associated (${publishParams.name})`);
|
2018-08-21 14:53:17 +02:00
|
|
|
|
2018-10-22 16:39:13 +02:00
|
|
|
return Object.assign({}, claimRecord, {outpoint});
|
2018-08-21 14:53:17 +02:00
|
|
|
} catch (err) {
|
2018-10-04 16:26:42 +02:00
|
|
|
// parse daemon response when err is a string
|
|
|
|
// this needs work
|
|
|
|
logger.info('publish/publish err:', err);
|
|
|
|
const error = typeof err === 'string' ? JSON.parse(err) : err;
|
2018-11-07 23:51:06 +01:00
|
|
|
if (publishParams.file_path) {
|
|
|
|
await deleteFile(publishParams.file_path);
|
2018-10-04 16:26:42 +02:00
|
|
|
}
|
|
|
|
const message = error.error && error.error.message ? error.error.message : 'Unknown publish error';
|
|
|
|
return {
|
|
|
|
error: true,
|
|
|
|
message,
|
|
|
|
};
|
2018-08-21 14:53:17 +02:00
|
|
|
}
|
2018-04-27 08:42:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = publish;
|