Fix Webhooks on withdraw (#2)

* 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  <office@dnilabs.com>
Co-authored-by: Vlad Stan <stan.v.vlad@gmail.com>
This commit is contained in:
Tiago Vasconcelos 2023-09-24 18:29:25 +01:00 committed by GitHub
commit c4cb176f10
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 26 deletions

View file

@ -113,7 +113,8 @@ new Vue({
closeFormDialog: function () { closeFormDialog: function () {
this.formDialog.data = { this.formDialog.data = {
is_unique: false, is_unique: false,
use_custom: false use_custom: false,
has_webhook: false
} }
}, },
simplecloseFormDialog: function () { simplecloseFormDialog: function () {
@ -132,6 +133,7 @@ new Vue({
}, },
openUpdateDialog: function (linkId) { openUpdateDialog: function (linkId) {
var link = _.findWhere(this.withdrawLinks, {id: 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.data = _.clone(link._data)
this.formDialog.show = true this.formDialog.show = true
}, },
@ -156,6 +158,7 @@ new Vue({
minutes: 60, minutes: 60,
hours: 3600 hours: 3600
}[this.formDialog.secondMultiplier] }[this.formDialog.secondMultiplier]
if (data.id) { if (data.id) {
this.updateWithdrawLink(wallet, data) this.updateWithdrawLink(wallet, data)
} else { } else {
@ -189,27 +192,12 @@ new Vue({
}, },
updateWithdrawLink: function (wallet, data) { updateWithdrawLink: function (wallet, data) {
var self = this var self = this
const body = _.pick(
data, // Remove webhook info if toggle is set to false
'title', if (!data.has_webhook) {
'min_withdrawable', data.webhook_url = null
'max_withdrawable', data.webhook_headers = null
'uses', data.webhook_body = null
'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
}
} }
LNbits.api LNbits.api
@ -217,14 +205,15 @@ new Vue({
'PUT', 'PUT',
'/withdraw/api/v1/links/' + data.id, '/withdraw/api/v1/links/' + data.id,
wallet.adminkey, wallet.adminkey,
body data
) )
.then(function (response) { .then((response) => {
self.withdrawLinks = _.reject(self.withdrawLinks, function (obj) { self.withdrawLinks = _.reject(self.withdrawLinks, function (obj) {
return obj.id === data.id return obj.id === data.id
}) })
self.withdrawLinks.push(mapWithdrawLink(response.data)) self.withdrawLinks.push(mapWithdrawLink(response.data))
self.formDialog.show = false self.formDialog.show = false
this.closeFormDialog()
}) })
.catch(function (error) { .catch(function (error) {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
@ -235,10 +224,11 @@ new Vue({
LNbits.api LNbits.api
.request('POST', '/withdraw/api/v1/links', wallet.adminkey, data) .request('POST', '/withdraw/api/v1/links', wallet.adminkey, data)
.then(function (response) { .then((response) => {
self.withdrawLinks.push(mapWithdrawLink(response.data)) self.withdrawLinks.push(mapWithdrawLink(response.data))
self.formDialog.show = false self.formDialog.show = false
self.simpleformDialog.show = false self.simpleformDialog.show = false
this.closeFormDialog()
}) })
.catch(function (error) { .catch(function (error) {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
@ -309,7 +299,7 @@ new Vue({
this.withdrawLinks, this.withdrawLinks,
'withdraw-links' 'withdraw-links'
) )
} },
}, },
created: function () { created: function () {
if (this.g.user.wallets.length) { if (this.g.user.wallets.length) {

View file

@ -1,5 +1,6 @@
from http import HTTPStatus from http import HTTPStatus
from typing import Optional from typing import Optional
import json
from fastapi import Depends, HTTPException, Query, Request from fastapi import Depends, HTTPException, Query, Request
from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl
@ -84,6 +85,24 @@ async def api_link_create_or_update(
status_code=HTTPStatus.BAD_REQUEST, 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: if link_id:
link = await get_withdraw_link(link_id, 0) link = await get_withdraw_link(link_id, 0)
if not link: if not link: