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 dni ⚡
parent 2b67169512
commit a63ec981f1
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 loguru import logger
from lnbits.core.services import create_invoice from lnbits.core.services import create_invoice
from lnbits.core.views.api import api_payment from lnbits.core.views.api import api_payment
from lnbits.helpers import urlsafe_short_hash 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 ..watchonly.crud import get_config, get_fresh_address
from . import db from . import db
from .helpers import fetch_onchain_balance from .helpers import fetch_onchain_balance
from .models import Charges, CreateCharge from .models import Charges, CreateCharge, SatsPaySettings
###############CHARGES########################## ###############CHARGES##########################
@ -121,3 +124,36 @@ async def check_address_balance(charge_id: str) -> Optional[Charges]:
if invoice_status["paid"]: if invoice_status["paid"]:
return await update_charge(charge_id=charge_id, balance=charge.amount) return await update_charge(charge_id=charge_id, balance=charge.amount)
return await get_charge(charge_id) 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( await db.execute(
""" """
CREATE TABLE satspay.settings ( CREATE TABLE satspay.settings (
id TEXT NOT NULL PRIMARY KEY,
"user" TEXT, "user" TEXT,
custom_css TEXT custom_css TEXT
); );

View file

@ -72,3 +72,5 @@ class Charges(BaseModel):
def must_call_webhook(self): def must_call_webhook(self):
return self.webhook and self.paid and self.config.webhook_success == False 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" <q-btn unelevated color="primary" @click="formDialogCharge.show = true"
>New charge >New charge
</q-btn> </q-btn>
<q-btn unelevated color="primary" @click="formDialogSettings.show = true"
>SatsPay settings
</q-btn>
</q-card-section> </q-card-section>
</q-card> </q-card>
@ -394,6 +399,33 @@
</q-form> </q-form>
</q-card> </q-card>
</q-dialog> </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> </div>
{% endblock %} {% block scripts %} {{ window_vars(user) }} {% endblock %} {% block scripts %} {{ window_vars(user) }}
<!-- lnbits/static/vendor <!-- lnbits/static/vendor
@ -505,10 +537,20 @@
time: null, time: null,
amount: null amount: null
} }
},
formDialogSettings: {
show: false,
data: {
custom_css: '',
}
} }
} }
}, },
methods: { methods: {
cancelSettings: function(data) {
this.formDialogCharge.data.custom_css = ''
this.formDialogSettings.show = false
},
cancelCharge: function (data) { cancelCharge: function (data) {
this.formDialogCharge.data.description = '' this.formDialogCharge.data.description = ''
this.formDialogCharge.data.onchain = false this.formDialogCharge.data.onchain = false
@ -580,6 +622,12 @@
LNbits.utils.notifyApiError(error) 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 () { sendFormDataCharge: function () {
const wallet = this.g.user.wallets[0].inkey const wallet = this.g.user.wallets[0].inkey
const data = this.formDialogCharge.data const data = this.formDialogCharge.data
@ -651,6 +699,23 @@
this.rescanning = false 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) { createCharge: async function (wallet, data) {
try { try {
const resp = await LNbits.api.request( const resp = await LNbits.api.request(

View file

@ -1,6 +1,8 @@
import json import json
from http import HTTPStatus from http import HTTPStatus
from loguru import logger
import httpx
from fastapi.params import Depends from fastapi.params import Depends
from starlette.exceptions import HTTPException from starlette.exceptions import HTTPException
@ -19,9 +21,11 @@ from .crud import (
get_charge, get_charge,
get_charges, get_charges,
update_charge, update_charge,
save_settings,
) )
from .helpers import call_webhook, public_charge from .helpers import call_webhook, public_charge
from .models import CreateCharge from .helpers import compact_charge
from .models import CreateCharge, SatsPaySettings
#############################CHARGES########################## #############################CHARGES##########################
@ -125,4 +129,27 @@ async def api_charge_balance(charge_id):
extra = {**charge.config.dict(), **resp} extra = {**charge.config.dict(), **resp}
await update_charge(charge_id=charge.id, extra=json.dumps(extra)) 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)} 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