2017-06-19 18:37:35 +02:00
|
|
|
const axios = require('axios');
|
2017-06-20 00:25:14 +02:00
|
|
|
const logger = require('winston');
|
2018-06-29 17:31:47 +02:00
|
|
|
const { apiHost, apiPort, getTimeout } = require('@config/lbryConfig');
|
2018-04-27 08:42:22 +02:00
|
|
|
const lbrynetUri = 'http://' + apiHost + ':' + apiPort;
|
2018-10-04 16:26:42 +02:00
|
|
|
const db = require('../models');
|
2018-04-27 08:42:22 +02:00
|
|
|
const { chooseGaLbrynetPublishLabel, sendGATimingEvent } = require('../utils/googleAnalytics.js');
|
|
|
|
const handleLbrynetResponse = require('./utils/handleLbrynetResponse.js');
|
2018-11-01 04:59:01 +01:00
|
|
|
const { publishing } = require('@config/siteConfig');
|
2017-09-21 02:49:05 +02:00
|
|
|
|
2017-05-24 20:07:43 +02:00
|
|
|
module.exports = {
|
2019-02-09 22:18:09 +01:00
|
|
|
publishClaim(publishParams) {
|
2017-08-03 20:46:56 +02:00
|
|
|
logger.debug(`lbryApi >> Publishing claim to "${publishParams.name}"`);
|
2018-03-06 18:02:42 +01:00
|
|
|
const gaStartTime = Date.now();
|
2017-08-03 19:42:02 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-06-17 22:51:30 +02:00
|
|
|
axios
|
2018-04-27 08:42:22 +02:00
|
|
|
.post(lbrynetUri, {
|
2017-06-17 22:51:30 +02:00
|
|
|
method: 'publish',
|
|
|
|
params: publishParams,
|
|
|
|
})
|
|
|
|
.then(response => {
|
2019-02-09 22:18:09 +01:00
|
|
|
sendGATimingEvent(
|
|
|
|
'lbrynet',
|
|
|
|
'publish',
|
|
|
|
chooseGaLbrynetPublishLabel(publishParams),
|
|
|
|
gaStartTime,
|
|
|
|
Date.now()
|
|
|
|
);
|
2017-10-31 00:38:57 +01:00
|
|
|
handleLbrynetResponse(response, resolve, reject);
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
},
|
2019-02-09 22:18:09 +01:00
|
|
|
getClaim(uri) {
|
2017-08-03 20:46:56 +02:00
|
|
|
logger.debug(`lbryApi >> Getting Claim for "${uri}"`);
|
2018-03-06 18:02:42 +01:00
|
|
|
const gaStartTime = Date.now();
|
2017-08-03 19:42:02 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-06-17 22:51:30 +02:00
|
|
|
axios
|
2018-04-27 08:42:22 +02:00
|
|
|
.post(lbrynetUri, {
|
2017-06-17 22:51:30 +02:00
|
|
|
method: 'get',
|
2018-06-29 17:31:47 +02:00
|
|
|
params: {
|
|
|
|
uri,
|
|
|
|
timeout: getTimeout || 30,
|
|
|
|
},
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
2017-09-21 02:49:05 +02:00
|
|
|
.then(response => {
|
2018-03-06 18:02:42 +01:00
|
|
|
sendGATimingEvent('lbrynet', 'getClaim', 'GET', gaStartTime, Date.now());
|
2017-10-31 00:38:57 +01:00
|
|
|
handleLbrynetResponse(response, resolve, reject);
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
},
|
2019-02-27 02:53:48 +01:00
|
|
|
getFileListFileByOutpoint(outpoint) {
|
|
|
|
logger.debug(`lbryApi >> Getting File_List for "${outpoint}"`);
|
|
|
|
const gaStartTime = Date.now();
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
axios
|
|
|
|
.post(lbrynetUri, {
|
|
|
|
method: 'file_list',
|
|
|
|
params: {
|
|
|
|
outpoint,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
sendGATimingEvent('lbrynet', 'getFileList', 'FILE_LIST', gaStartTime, Date.now());
|
|
|
|
handleLbrynetResponse(response, resolve, reject);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2019-02-09 22:18:09 +01:00
|
|
|
async abandonClaim({ claimId }) {
|
2018-08-21 14:53:17 +02:00
|
|
|
logger.debug(`lbryApi >> Abandon claim "${claimId}"`);
|
|
|
|
const gaStartTime = Date.now();
|
|
|
|
try {
|
|
|
|
const abandon = await axios.post(lbrynetUri, {
|
|
|
|
method: 'claim_abandon',
|
|
|
|
params: { claim_id: claimId },
|
|
|
|
});
|
|
|
|
sendGATimingEvent('lbrynet', 'abandonClaim', 'ABANDON_CLAIM', gaStartTime, Date.now());
|
|
|
|
return abandon.data;
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(error);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
},
|
2019-02-09 22:18:09 +01:00
|
|
|
getClaimList(claimName) {
|
2017-08-04 20:32:21 +02:00
|
|
|
logger.debug(`lbryApi >> Getting claim_list for "${claimName}"`);
|
2018-03-06 18:02:42 +01:00
|
|
|
const gaStartTime = Date.now();
|
2017-08-03 19:42:02 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-06-17 22:51:30 +02:00
|
|
|
axios
|
2018-04-27 08:42:22 +02:00
|
|
|
.post(lbrynetUri, {
|
2017-06-17 22:51:30 +02:00
|
|
|
method: 'claim_list',
|
|
|
|
params: { name: claimName },
|
|
|
|
})
|
2017-09-21 02:49:05 +02:00
|
|
|
.then(response => {
|
2018-03-06 18:02:42 +01:00
|
|
|
sendGATimingEvent('lbrynet', 'getClaimList', 'CLAIM_LIST', gaStartTime, Date.now());
|
2017-10-31 00:38:57 +01:00
|
|
|
handleLbrynetResponse(response, resolve, reject);
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
},
|
2019-02-09 22:18:09 +01:00
|
|
|
resolveUri(uri) {
|
2017-08-03 20:46:56 +02:00
|
|
|
logger.debug(`lbryApi >> Resolving URI for "${uri}"`);
|
2018-03-06 18:02:42 +01:00
|
|
|
const gaStartTime = Date.now();
|
2017-08-03 19:42:02 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-06-17 22:51:30 +02:00
|
|
|
axios
|
2018-04-27 08:42:22 +02:00
|
|
|
.post(lbrynetUri, {
|
2017-06-17 22:51:30 +02:00
|
|
|
method: 'resolve',
|
2019-02-19 02:03:37 +01:00
|
|
|
params: { urls: uri },
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.then(({ data }) => {
|
2018-03-06 18:02:42 +01:00
|
|
|
sendGATimingEvent('lbrynet', 'resolveUri', 'RESOLVE', gaStartTime, Date.now());
|
2018-10-04 16:26:42 +02:00
|
|
|
if (Object.keys(data.result).length === 0 && data.result.constructor === Object) {
|
|
|
|
// workaround for daemon returning empty result object
|
|
|
|
// https://github.com/lbryio/lbry/issues/1485
|
|
|
|
db.Claim.findOne({ where: { claimId: uri.split('#')[1] } })
|
|
|
|
.then(() => reject('This claim has not yet been confirmed on the LBRY blockchain'))
|
|
|
|
.catch(() => reject(`Claim ${uri} does not exist`));
|
2019-02-09 22:18:09 +01:00
|
|
|
} else if (data.result[uri].error) {
|
|
|
|
// check for errors
|
2017-06-22 01:36:08 +02:00
|
|
|
reject(data.result[uri].error);
|
2019-02-09 22:18:09 +01:00
|
|
|
} else {
|
|
|
|
// if no errors, resolve
|
2017-07-19 18:11:08 +02:00
|
|
|
resolve(data.result[uri]);
|
2017-06-22 01:36:08 +02:00
|
|
|
}
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
},
|
2019-02-09 22:18:09 +01:00
|
|
|
getDownloadDirectory() {
|
2017-08-03 20:46:56 +02:00
|
|
|
logger.debug('lbryApi >> Retrieving the download directory path from lbry daemon...');
|
2018-03-06 18:02:42 +01:00
|
|
|
const gaStartTime = Date.now();
|
2017-08-03 19:42:02 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-07-18 18:52:18 +02:00
|
|
|
axios
|
2018-04-27 08:42:22 +02:00
|
|
|
.post(lbrynetUri, {
|
2017-07-18 18:52:18 +02:00
|
|
|
method: 'settings_get',
|
|
|
|
})
|
|
|
|
.then(({ data }) => {
|
2019-02-09 22:18:09 +01:00
|
|
|
sendGATimingEvent(
|
|
|
|
'lbrynet',
|
|
|
|
'getDownloadDirectory',
|
|
|
|
'SETTINGS_GET',
|
|
|
|
gaStartTime,
|
|
|
|
Date.now()
|
|
|
|
);
|
2017-07-18 18:52:18 +02:00
|
|
|
if (data.result) {
|
2019-03-01 09:20:53 +01:00
|
|
|
resolve(data.result.download_dir);
|
2017-07-18 18:52:18 +02:00
|
|
|
} else {
|
2019-02-09 22:18:09 +01:00
|
|
|
return new Error(
|
|
|
|
'Successfully connected to lbry daemon, but unable to retrieve the download directory.'
|
|
|
|
);
|
2017-07-18 18:52:18 +02:00
|
|
|
}
|
|
|
|
})
|
2017-09-14 18:35:58 +02:00
|
|
|
.catch(error => {
|
2017-09-14 20:34:13 +02:00
|
|
|
logger.error('Lbrynet Error:', error);
|
2017-08-03 19:42:02 +02:00
|
|
|
resolve('/home/lbry/Downloads/');
|
2017-07-18 18:52:18 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2019-02-09 22:18:09 +01:00
|
|
|
createChannel(name) {
|
2018-03-06 18:02:42 +01:00
|
|
|
logger.debug(`lbryApi >> Creating channel for ${name}...`);
|
|
|
|
const gaStartTime = Date.now();
|
2017-09-17 02:50:22 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-09-21 02:49:05 +02:00
|
|
|
axios
|
2018-04-27 08:42:22 +02:00
|
|
|
.post(lbrynetUri, {
|
2019-05-24 19:09:07 +02:00
|
|
|
method: 'channel_create',
|
2017-09-21 02:49:05 +02:00
|
|
|
params: {
|
2019-05-24 19:09:07 +02:00
|
|
|
name: name,
|
|
|
|
bid: publishing.channelClaimBidAmount,
|
2017-09-21 02:49:05 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(response => {
|
2018-03-06 18:02:42 +01:00
|
|
|
sendGATimingEvent('lbrynet', 'createChannel', 'CHANNEL_NEW', gaStartTime, Date.now());
|
2017-10-31 00:38:57 +01:00
|
|
|
handleLbrynetResponse(response, resolve, reject);
|
2017-09-21 02:49:05 +02:00
|
|
|
})
|
2017-09-26 07:49:27 +02:00
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2017-09-17 02:50:22 +02:00
|
|
|
});
|
|
|
|
},
|
2019-02-09 22:18:09 +01:00
|
|
|
getAccountBalance() {
|
2018-05-18 03:11:22 +02:00
|
|
|
const gaStartTime = Date.now();
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
axios
|
|
|
|
.post(lbrynetUri, {
|
2019-02-09 22:18:09 +01:00
|
|
|
method: 'account_balance',
|
2018-05-18 03:11:22 +02:00
|
|
|
})
|
|
|
|
.then(response => {
|
2019-02-09 22:18:09 +01:00
|
|
|
sendGATimingEvent(
|
|
|
|
'lbrynet',
|
|
|
|
'getAccountBalance',
|
|
|
|
'SETTINGS_GET',
|
|
|
|
gaStartTime,
|
|
|
|
Date.now()
|
|
|
|
);
|
2018-05-18 03:11:22 +02:00
|
|
|
handleLbrynetResponse(response, resolve, reject);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|