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-10-04 16:26:42 +02:00
|
|
|
const publish = async (publishParams, fileName, fileType, filePath) => {
|
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;
|
|
|
|
let newFile = Boolean(filePath);
|
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-12 08:00:07 +02:00
|
|
|
|
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) {
|
|
|
|
fileRecord = await createFileRecordDataAfterPublish(fileName, fileType, publishParams, publishResults);
|
|
|
|
} else {
|
|
|
|
fileRecord = await db.File.findOne({where: {claimId}});
|
|
|
|
}
|
|
|
|
|
|
|
|
const [file, claim] = await Promise.all([
|
|
|
|
db.upsert(db.File, fileRecord, upsertCriteria, 'File'),
|
|
|
|
db.upsert(db.Claim, claimRecord, upsertCriteria, 'Claim'),
|
|
|
|
]);
|
|
|
|
logger.info('File and Claim records successfully created');
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
file.setClaim(claim),
|
|
|
|
claim.setFile(file),
|
|
|
|
]);
|
|
|
|
logger.info('File and Claim records successfully associated');
|
|
|
|
|
|
|
|
return claimRecord;
|
|
|
|
} 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;
|
|
|
|
if (filePath) {
|
|
|
|
await deleteFile(filePath);
|
|
|
|
}
|
|
|
|
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;
|