spee.ch/server/lbrynet/index.js

213 lines
6.5 KiB
JavaScript
Raw Normal View History

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');
const lbrynetUri = 'http://' + apiHost + ':' + apiPort;
const db = require('../models');
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');
module.exports = {
publishClaim(publishParams) {
2017-08-03 20:46:56 +02:00
logger.debug(`lbryApi >> Publishing claim to "${publishParams.name}"`);
const gaStartTime = Date.now();
return new Promise((resolve, reject) => {
axios
.post(lbrynetUri, {
method: 'publish',
params: publishParams,
})
.then(response => {
sendGATimingEvent(
'lbrynet',
'publish',
chooseGaLbrynetPublishLabel(publishParams),
gaStartTime,
Date.now()
);
handleLbrynetResponse(response, resolve, reject);
})
.catch(error => {
reject(error);
});
});
},
getClaim(uri) {
2017-08-03 20:46:56 +02:00
logger.debug(`lbryApi >> Getting Claim for "${uri}"`);
const gaStartTime = Date.now();
return new Promise((resolve, reject) => {
axios
.post(lbrynetUri, {
method: 'get',
2018-06-29 17:31:47 +02:00
params: {
uri,
timeout: getTimeout || 30,
},
})
.then(response => {
sendGATimingEvent('lbrynet', 'getClaim', 'GET', gaStartTime, Date.now());
handleLbrynetResponse(response, resolve, reject);
})
.catch(error => {
reject(error);
});
});
},
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);
});
});
},
async abandonClaim({ claimId }) {
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;
}
},
getClaimList(claimName) {
2017-08-04 20:32:21 +02:00
logger.debug(`lbryApi >> Getting claim_list for "${claimName}"`);
const gaStartTime = Date.now();
return new Promise((resolve, reject) => {
axios
.post(lbrynetUri, {
method: 'claim_list',
params: { name: claimName },
})
.then(response => {
sendGATimingEvent('lbrynet', 'getClaimList', 'CLAIM_LIST', gaStartTime, Date.now());
handleLbrynetResponse(response, resolve, reject);
})
.catch(error => {
reject(error);
});
});
},
resolveUri(uri) {
2017-08-03 20:46:56 +02:00
logger.debug(`lbryApi >> Resolving URI for "${uri}"`);
const gaStartTime = Date.now();
return new Promise((resolve, reject) => {
axios
.post(lbrynetUri, {
method: 'resolve',
2019-02-19 02:03:37 +01:00
params: { urls: uri },
})
.then(({ data }) => {
sendGATimingEvent('lbrynet', 'resolveUri', 'RESOLVE', gaStartTime, Date.now());
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`));
} else if (data.result[uri].error) {
// check for errors
reject(data.result[uri].error);
} else {
// if no errors, resolve
resolve(data.result[uri]);
}
})
.catch(error => {
reject(error);
});
});
},
getDownloadDirectory() {
2017-08-03 20:46:56 +02:00
logger.debug('lbryApi >> Retrieving the download directory path from lbry daemon...');
const gaStartTime = Date.now();
return new Promise((resolve, reject) => {
axios
.post(lbrynetUri, {
method: 'settings_get',
})
.then(({ data }) => {
sendGATimingEvent(
'lbrynet',
'getDownloadDirectory',
'SETTINGS_GET',
gaStartTime,
Date.now()
);
if (data.result) {
2019-03-01 09:20:53 +01:00
resolve(data.result.download_dir);
} else {
return new Error(
'Successfully connected to lbry daemon, but unable to retrieve the download directory.'
);
}
})
2017-09-14 18:35:58 +02:00
.catch(error => {
logger.error('Lbrynet Error:', error);
resolve('/home/lbry/Downloads/');
});
});
},
createChannel(name) {
logger.debug(`lbryApi >> Creating channel for ${name}...`);
const gaStartTime = Date.now();
2017-09-17 02:50:22 +02:00
return new Promise((resolve, reject) => {
axios
.post(lbrynetUri, {
method: 'channel_new',
params: {
channel_name: name,
amount: publishing.channelClaimBidAmount,
},
})
.then(response => {
sendGATimingEvent('lbrynet', 'createChannel', 'CHANNEL_NEW', gaStartTime, Date.now());
handleLbrynetResponse(response, resolve, reject);
})
2017-09-26 07:49:27 +02:00
.catch(error => {
reject(error);
});
2017-09-17 02:50:22 +02:00
});
},
getAccountBalance() {
const gaStartTime = Date.now();
return new Promise((resolve, reject) => {
axios
.post(lbrynetUri, {
method: 'account_balance',
})
.then(response => {
sendGATimingEvent(
'lbrynet',
'getAccountBalance',
'SETTINGS_GET',
gaStartTime,
Date.now()
);
handleLbrynetResponse(response, resolve, reject);
})
.catch(error => {
reject(error);
});
});
},
};