SatsPay server save custom css settings now working

This commit is contained in:
Black Coffee 2022-10-24 15:33:49 +01:00 committed by Vlad Stan
parent 00cb7a8d21
commit 4e9132ec04
5 changed files with 132 additions and 3 deletions

View File

@ -3,6 +3,8 @@ from typing import List, Optional
from loguru import logger
from loguru import logger
from lnbits.core.services import create_invoice
from lnbits.core.views.api import api_payment
from lnbits.helpers import urlsafe_short_hash
@ -10,7 +12,8 @@ from lnbits.helpers import urlsafe_short_hash
from ..watchonly.crud import get_config, get_fresh_address
from . import db
from .helpers import fetch_onchain_balance
from .models import Charges, CreateCharge
from .models import Charges, CreateCharge, SatsPaySettings
###############CHARGES##########################
@ -121,3 +124,36 @@ async def check_address_balance(charge_id: str) -> Optional[Charges]:
if invoice_status["paid"]:
return await update_charge(charge_id=charge_id, balance=charge.amount)
return await get_charge(charge_id)
################## SETTINGS ###################
async def save_settings(user: str, data: SatsPaySettings):
# insert or update
row = await db.fetchone(
"""SELECT user FROM satspay.settings WHERE user = ?""", (user,)
)
if row:
await db.execute(
"""
UPDATE satspay.settings SET custom_css = ? WHERE user = ?
""",
(
data.custom_css,
user
),
)
else:
await db.execute(
"""
INSERT INTO satspay.settings (
user,
custom_css
)
VALUES (?, ?)
""",
(
user,
data.custom_css,
),
)
return True

View File

@ -46,7 +46,6 @@ async def m002_add_settings_table(db):
await db.execute(
"""
CREATE TABLE satspay.settings (
id TEXT NOT NULL PRIMARY KEY,
"user" TEXT,
custom_css TEXT
);

View File

@ -72,3 +72,5 @@ class Charges(BaseModel):
def must_call_webhook(self):
return self.webhook and self.paid and self.config.webhook_success == False
class SatsPaySettings(BaseModel):
custom_css: str = Query(None)

View File

@ -8,6 +8,11 @@
<q-btn unelevated color="primary" @click="formDialogCharge.show = true"
>New charge
</q-btn>
<q-btn unelevated color="primary" @click="formDialogSettings.show = true"
>SatsPay settings
</q-btn>
</q-card-section>
</q-card>
@ -394,6 +399,33 @@
</q-form>
</q-card>
</q-dialog>
<q-dialog v-model="formDialogSettings.show" position="top">
<q-card class="q-pa-lg q-pt-xl lnbits__dialog-card">
<q-form @submit="sendFormDataSettings" class="q-gutter-md">
<q-input
filled
dense
v-model.trim="formDialogSettings.data.custom_css"
type="textarea"
label="Custom CSS"
>
<q-tooltip>Custom CSS to apply styles to your SatsPay invoice</q-tooltip>
</q-input>
<div class="row q-mt-lg">
<q-btn
unelevated
color="primary"
type="submit"
>Save Settings</q-btn
>
<q-btn @click="cancelSettings" flat color="grey" class="q-ml-auto"
>Cancel</q-btn
>
</div>
</q-form>
</q-card>
</q-dialog>
</div>
{% endblock %} {% block scripts %} {{ window_vars(user) }}
<!-- lnbits/static/vendor
@ -505,10 +537,20 @@
time: null,
amount: null
}
},
formDialogSettings: {
show: false,
data: {
custom_css: '',
}
}
}
},
methods: {
cancelSettings: function(data) {
this.formDialogCharge.data.custom_css = ''
this.formDialogSettings.show = false
},
cancelCharge: function (data) {
this.formDialogCharge.data.description = ''
this.formDialogCharge.data.onchain = false
@ -580,6 +622,12 @@
LNbits.utils.notifyApiError(error)
}
},
sendFormDataSettings: function () {
const wallet = this.g.user.wallets[0].inkey
const data = this.formDialogSettings.data
data.custom_css = data.custom_css
this.saveSettings(wallet, data)
},
sendFormDataCharge: function () {
const wallet = this.g.user.wallets[0].inkey
const data = this.formDialogCharge.data
@ -651,6 +699,23 @@
this.rescanning = false
}
},
saveSettings: async function (wallet, data) {
try {
const resp = await LNbits.api.request(
'POST',
'/satspay/api/v1/settings',
wallet,
data
)
this.formDialogSettings.show = false
this.formDialogSettings.data = {
custom_css: ''
}
} catch (error) {
LNbits.utils.notifyApiError(error)
}
},
createCharge: async function (wallet, data) {
try {
const resp = await LNbits.api.request(

View File

@ -1,6 +1,8 @@
import json
from http import HTTPStatus
from loguru import logger
import httpx
from fastapi.params import Depends
from starlette.exceptions import HTTPException
@ -19,9 +21,11 @@ from .crud import (
get_charge,
get_charges,
update_charge,
save_settings,
)
from .helpers import call_webhook, public_charge
from .models import CreateCharge
from .helpers import compact_charge
from .models import CreateCharge, SatsPaySettings
#############################CHARGES##########################
@ -125,4 +129,27 @@ async def api_charge_balance(charge_id):
extra = {**charge.config.dict(), **resp}
await update_charge(charge_id=charge.id, extra=json.dumps(extra))
if charge.paid and charge.webhook:
async with httpx.AsyncClient() as client:
try:
r = await client.post(
charge.webhook,
json=compact_charge(charge),
timeout=40,
)
except AssertionError:
charge.webhook = None
return {**public_charge(charge)}
#############################CHARGES##########################
@satspay_ext.post("/api/v1/settings")
async def api_settings_save(
data: SatsPaySettings, wallet: WalletTypeInfo = Depends(require_invoice_key)
):
logger.debug("wallet.wallet.user")
logger.debug(wallet.wallet.user)
await save_settings(user=wallet.wallet.user, data=data)
return True