Handle failed API requests, reauth when token expired
This commit is contained in:
26
index.js
26
index.js
@@ -54,11 +54,11 @@ function waitForInvoicePaid (room, nick, invoice, retries=0) {
|
||||
}
|
||||
|
||||
lndhub.getInvoice(invoice.payment_hash).then(async data => {
|
||||
if (data.is_paid) {
|
||||
if (data && data.is_paid) {
|
||||
console.log(`Invoice paid: ${invoice.payment_hash}`);
|
||||
await grantVoice(room, nick);
|
||||
} else {
|
||||
setTimeout(waitForInvoicePaid, 3000, room, nick, invoice, retries++);
|
||||
if (!data) console.warn('Fetching invoice status failed');
|
||||
setTimeout(waitForInvoicePaid, timeout, room, nick, invoice, retries++);
|
||||
}
|
||||
});
|
||||
@@ -93,10 +93,14 @@ async function handleVoiceRequest (stanza) {
|
||||
console.log(`${nick} requested voice in ${room}`);
|
||||
|
||||
const invoice = await lndhub.createInvoice(config.amounts.voice, `Donation for ${room}`);
|
||||
console.log(`Created lightning invoice for ${nick}: ${invoice.payment_hash}`)
|
||||
|
||||
await respondToVoiceRequest(room, nick, invoice);
|
||||
waitForInvoicePaid(room, nick, invoice);
|
||||
if (invoice) {
|
||||
console.log(`Created lightning invoice for ${nick}: ${invoice.payment_hash}`)
|
||||
await respondToVoiceRequest(room, nick, invoice);
|
||||
waitForInvoicePaid(room, nick, invoice);
|
||||
} else {
|
||||
console.warn(`Invoice creation failed!`);
|
||||
// TODO notify ops contact
|
||||
}
|
||||
} catch(err) {
|
||||
console.error(err);
|
||||
}
|
||||
@@ -113,10 +117,14 @@ async function connectXmpp () {
|
||||
}
|
||||
|
||||
function connectLndhub () {
|
||||
return lndhub.auth(config.lndhub.username, config.lndhub.password).then(() => {
|
||||
console.log("Connected to Lndhub");
|
||||
return lndhub.auth(config.lndhub.username, config.lndhub.password).then(connected => {
|
||||
if (connected) {
|
||||
console.log("Connected to Lndhub");
|
||||
} else {
|
||||
process.exit(1);
|
||||
}
|
||||
}).catch(err => {
|
||||
console.warn(`Lndhub auth failed: ${err}`);
|
||||
console.error(`Lndhub connection failed: ${err}`);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user