From f27c2ebc2177c712546813aae326a328ea621adb Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sun, 21 Mar 2021 17:10:53 -0300 Subject: [PATCH] marginally improve the checkpending situation. --- lnbits/core/static/js/wallet.js | 33 +++++++++++++++++---------------- lnbits/core/views/api.py | 19 ++++++++++++------- lnbits/static/js/base.js | 12 +++++------- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/lnbits/core/static/js/wallet.js b/lnbits/core/static/js/wallet.js index 097a78195..58345ab0e 100644 --- a/lnbits/core/static/js/wallet.js +++ b/lnbits/core/static/js/wallet.js @@ -584,18 +584,16 @@ new Vue({ LNbits.href.deleteWallet(walletId, user) }) }, - fetchPayments: function (checkPending) { - return LNbits.api - .getPayments(this.g.wallet, checkPending) - .then(response => { - this.payments = response.data - .map(obj => { - return LNbits.map.payment(obj) - }) - .sort((a, b) => { - return b.time - a.time - }) - }) + fetchPayments: function () { + return LNbits.api.getPayments(this.g.wallet).then(response => { + this.payments = response.data + .map(obj => { + return LNbits.map.payment(obj) + }) + .sort((a, b) => { + return b.time - a.time + }) + }) }, fetchBalance: function () { LNbits.api.getWallet(this.g.wallet).then(response => { @@ -612,9 +610,12 @@ new Vue({ message: 'Checking pending transactions...' }) - this.fetchPayments(true).then(() => { - dismissMsg() - }) + LNbits.api + .checkPending(this.g.wallet) + .then(() => LNbits.api.fetchPayments) + .then(() => { + dismissMsg() + }) }, exportCSV: function () { LNbits.utils.exportCSV(this.paymentsTable.columns, this.payments) @@ -628,7 +629,7 @@ new Vue({ created: function () { this.fetchBalance() this.fetchPayments() - setTimeout(this.checkPendingPayments(), 1200) + this.checkPendingPayments() }, mounted: function () { // show disclaimer diff --git a/lnbits/core/views/api.py b/lnbits/core/views/api.py index 1225c4aa1..5a461c33a 100644 --- a/lnbits/core/views/api.py +++ b/lnbits/core/views/api.py @@ -3,7 +3,7 @@ import json import lnurl # type: ignore import httpx from urllib.parse import urlparse, urlunparse, urlencode, parse_qs, ParseResult -from quart import g, jsonify, request, make_response +from quart import g, jsonify, make_response from http import HTTPStatus from binascii import unhexlify from typing import Dict, Union @@ -32,15 +32,20 @@ async def api_wallet(): ) +@core_app.route("/api/v1/checkpending", methods=["POST"]) +@api_check_wallet_key("invoice") +async def api_checkpending(): + g.nursery.start_soon(delete_expired_invoices) + + for payment in await g.wallet.get_payments(complete=False, pending=True, exclude_uncheckable=True): + await payment.check_pending() + + return "", HTTPStatus.NO_CONTENT + + @core_app.route("/api/v1/payments", methods=["GET"]) @api_check_wallet_key("invoice") async def api_payments(): - if "check_pending" in request.args: - await delete_expired_invoices() - - for payment in await g.wallet.get_payments(complete=False, pending=True, exclude_uncheckable=True): - await payment.check_pending() - return jsonify(await g.wallet.get_payments(pending=True)), HTTPStatus.OK diff --git a/lnbits/static/js/base.js b/lnbits/static/js/base.js index ed0583e50..61d562482 100644 --- a/lnbits/static/js/base.js +++ b/lnbits/static/js/base.js @@ -52,13 +52,11 @@ window.LNbits = { getWallet: function (wallet) { return this.request('get', '/api/v1/wallet', wallet.inkey) }, - getPayments: function (wallet, checkPending) { - var query_param = checkPending ? '?check_pending' : '' - return this.request( - 'get', - ['/api/v1/payments', query_param].join(''), - wallet.inkey - ) + checkPending: function (wallet) { + return this.request('post', '/api/v1/checkpending', wallet.inkey) + }, + getPayments: function (wallet) { + return this.request('get', '/api/v1/payments', wallet.inkey) }, getPayment: function (wallet, paymentHash) { return this.request(