From c4cb176f10eeb443f2c5a716db61374854e2c6e3 Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Sun, 24 Sep 2023 18:29:25 +0100 Subject: [PATCH] Fix Webhooks on withdraw (#2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * make webhook toggle not undefined * undefined toggle at update and stringify webhook * remove comment * remove json stringify * json validation serverside * Fix updating withdraw links with webhook info * make webhook toggle not undefined * undefined toggle at update and stringify webhook * remove comment * remove json stringify * Fix updating withdraw links with webhook info * fix: use arrow functions --------- Co-authored-by: dni ⚡ Co-authored-by: Vlad Stan --- static/js/index.js | 42 ++++++++++++++++-------------------------- views_api.py | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/static/js/index.js b/static/js/index.js index ced7843..0877b19 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -113,7 +113,8 @@ new Vue({ closeFormDialog: function () { this.formDialog.data = { is_unique: false, - use_custom: false + use_custom: false, + has_webhook: false } }, simplecloseFormDialog: function () { @@ -132,6 +133,7 @@ new Vue({ }, openUpdateDialog: function (linkId) { var link = _.findWhere(this.withdrawLinks, {id: linkId}) + link._data.has_webhook = link._data.webhook_url ? true : false this.formDialog.data = _.clone(link._data) this.formDialog.show = true }, @@ -156,6 +158,7 @@ new Vue({ minutes: 60, hours: 3600 }[this.formDialog.secondMultiplier] + if (data.id) { this.updateWithdrawLink(wallet, data) } else { @@ -189,27 +192,12 @@ new Vue({ }, updateWithdrawLink: function (wallet, data) { var self = this - const body = _.pick( - data, - 'title', - 'min_withdrawable', - 'max_withdrawable', - 'uses', - 'wait_time', - 'is_unique', - 'webhook_url', - 'webhook_headers', - 'webhook_body', - 'custom_url' - ) - - if (data.has_webhook) { - body = { - ...body, - webhook_url: data.webhook_url, - webhook_headers: data.webhook_headers, - webhook_body: data.webhook_body - } + + // Remove webhook info if toggle is set to false + if (!data.has_webhook) { + data.webhook_url = null + data.webhook_headers = null + data.webhook_body = null } LNbits.api @@ -217,14 +205,15 @@ new Vue({ 'PUT', '/withdraw/api/v1/links/' + data.id, wallet.adminkey, - body + data ) - .then(function (response) { + .then((response) => { self.withdrawLinks = _.reject(self.withdrawLinks, function (obj) { return obj.id === data.id }) self.withdrawLinks.push(mapWithdrawLink(response.data)) self.formDialog.show = false + this.closeFormDialog() }) .catch(function (error) { LNbits.utils.notifyApiError(error) @@ -235,10 +224,11 @@ new Vue({ LNbits.api .request('POST', '/withdraw/api/v1/links', wallet.adminkey, data) - .then(function (response) { + .then((response) => { self.withdrawLinks.push(mapWithdrawLink(response.data)) self.formDialog.show = false self.simpleformDialog.show = false + this.closeFormDialog() }) .catch(function (error) { LNbits.utils.notifyApiError(error) @@ -309,7 +299,7 @@ new Vue({ this.withdrawLinks, 'withdraw-links' ) - } + }, }, created: function () { if (this.g.user.wallets.length) { diff --git a/views_api.py b/views_api.py index 25df3c3..e2caf50 100644 --- a/views_api.py +++ b/views_api.py @@ -1,5 +1,6 @@ from http import HTTPStatus from typing import Optional +import json from fastapi import Depends, HTTPException, Query, Request from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl @@ -84,6 +85,24 @@ async def api_link_create_or_update( status_code=HTTPStatus.BAD_REQUEST, ) + if data.webhook_body: + try: + json.loads(data.webhook_body) + except: + raise HTTPException( + detail="`webhook_body` can not parse JSON.", + status_code=HTTPStatus.BAD_REQUEST, + ) + + if data.webhook_headers: + try: + json.loads(data.webhook_headers) + except: + raise HTTPException( + detail="`webhook_headers` can not parse JSON.", + status_code=HTTPStatus.BAD_REQUEST, + ) + if link_id: link = await get_withdraw_link(link_id, 0) if not link: