Mostly working

This commit is contained in:
Arc 2024-11-18 21:54:27 +00:00
parent 5a2ba743cf
commit 08dad16611
5 changed files with 232 additions and 232 deletions

View file

@ -17,5 +17,11 @@ class MyExtension(BaseModel):
lnurlwithdrawamount: int lnurlwithdrawamount: int
wallet: str wallet: str
total: int total: int
lnurlpay: str lnurlpay: str = ""
lnurlwithdraw: str lnurlwithdraw: str = ""
class CreatePayment(BaseModel):
myextension_id: str
amount: int
memo: str

View file

@ -57,13 +57,17 @@ window.app = Vue.createApp({
this.g.user.wallets[0].inkey this.g.user.wallets[0].inkey
) )
.then(response => { .then(response => {
console.log(response.data)
this.myex = response.data this.myex = response.data
}) })
.catch(error => { .catch(err => {
console.error('Error fetching data:', error) LNbits.utils.notifyApiError(err)
}) })
} },
async openUrlDialog(tempid) {
this.urlDialog.data = _.findWhere(this.myex, {id: tempid})
this.qrValue = this.urlDialog.data.lnurlpay
await this.connectWebocket(this.urlDialog.data.id)
this.urlDialog.show = true
}, },
async sendMyExtensionData() { async sendMyExtensionData() {
const data = { const data = {
@ -82,6 +86,7 @@ window.app = Vue.createApp({
await this.createMyExtension(wallet, data) await this.createMyExtension(wallet, data)
} }
}, },
async updateMyExtensionForm(tempId) { async updateMyExtensionForm(tempId) {
const myextension = _.findWhere(this.myex, {id: tempId}) const myextension = _.findWhere(this.myex, {id: tempId})
this.formDialog.data = { this.formDialog.data = {
@ -106,6 +111,7 @@ window.app = Vue.createApp({
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
}) })
}, },
async updateMyExtension(wallet, data) { async updateMyExtension(wallet, data) {
await LNbits.api await LNbits.api
.request( .request(
@ -115,9 +121,7 @@ window.app = Vue.createApp({
data data
) )
.then(response => { .then(response => {
this.myex = _.reject(this.myex, obj => { this.myex = _.reject(this.myex, obj => obj.id == data.id)
return obj.id == data.id
})
this.myex.push(response.data) this.myex.push(response.data)
this.closeFormDialog() this.closeFormDialog()
}) })
@ -134,18 +138,18 @@ window.app = Vue.createApp({
.request( .request(
'DELETE', 'DELETE',
'/myextension/api/v1/myex/' + tempId, '/myextension/api/v1/myex/' + tempId,
_.findWhere(this.g.user.wallets, {id: myextension.wallet}).adminkey _.findWhere(this.g.user.wallets, {id: myextension.wallet})
.adminkey
) )
.then(() => { .then(() => {
this.myex = _.reject(this.myex, function (obj) { this.myex = _.reject(this.myex, obj => obj.id == tempId)
return obj.id == tempId
})
}) })
.catch(error => { .catch(error => {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
}) })
}) })
}, },
async exportCSV() { async exportCSV() {
await LNbits.utils.exportCSV(this.myexTable.columns, this.myex) await LNbits.utils.exportCSV(this.myexTable.columns, this.myex)
}, },
@ -172,33 +176,23 @@ window.app = Vue.createApp({
this.formDialog.show = false this.formDialog.show = false
this.formDialog.data = {} this.formDialog.data = {}
}, },
async openUrlDialog(id) {
this.urlDialog.data = _.findWhere(this.myex, {id}) async createInvoice(tempid) {
this.qrValue = this.urlDialog.data.lnurlpay
await this.connectWebocket(this.urlDialog.data.id)
this.urlDialog.show = true
},
async createInvoice(walletId, myextensionId) {
/////////////////////////////////////////////////// ///////////////////////////////////////////////////
///Simple call to the api to create an invoice///// ///Simple call to the api to create an invoice/////
/////////////////////////////////////////////////// ///////////////////////////////////////////////////
console.log(walletId) myex = _.findWhere(this.myex, {id: tempid})
const wallet = _.findWhere(this.g.user.wallets, { const wallet = _.findWhere(this.g.user.wallets, {id: myex.wallet})
id: walletId const data = {
}) myextension_id: tempid,
const dataToSend = {
out: false,
amount: this.invoiceAmount, amount: this.invoiceAmount,
memo: 'Invoice created by MyExtension', memo: 'MyExtension - ' + myex.name
extra: {
tag: 'MyExtension',
myextensionId: myextensionId
}
} }
await LNbits.api await LNbits.api
.request('POST', `/api/v1/payments`, wallet.inkey, dataToSend) .request('POST', `/myextension/api/v1/myex/payment`, wallet.inkey, data)
.then(response => { .then(async response => {
this.qrValue = response.data.payment_request this.qrValue = response.data.payment_request
await this.connectWebocket(wallet.id)
}) })
.catch(error => { .catch(error => {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
@ -258,8 +252,8 @@ window.app = Vue.createApp({
this.connection.onmessage = async function (e) { this.connection.onmessage = async function (e) {
await this.makeItRain() await this.makeItRain()
} }
}
}, },
/////////////////////////////////////////////////// ///////////////////////////////////////////////////
//////LIFECYCLE FUNCTIONS RUNNING ON PAGE LOAD///// //////LIFECYCLE FUNCTIONS RUNNING ON PAGE LOAD/////
/////////////////////////////////////////////////// ///////////////////////////////////////////////////

View file

@ -67,7 +67,7 @@
icon="launch" icon="launch"
:color="($q.dark.isActive) ? 'grey-7' : 'grey-5'" :color="($q.dark.isActive) ? 'grey-7' : 'grey-5'"
type="a" type="a"
:href="props.row.myextension" :href="props.row.id"
target="_blank" target="_blank"
><q-tooltip>Open public page</q-tooltip></q-btn ><q-tooltip>Open public page</q-tooltip></q-btn
></q-td ></q-td
@ -222,7 +222,7 @@
<template v-slot:append> <template v-slot:append>
<q-btn <q-btn
round round
@click="createInvoice(urlDialog.data.wallet, urlDialog.data.id)" @click="createInvoice(urlDialog.data.id)"
color="primary" color="primary"
flat flat
icon="add_circle" icon="add_circle"

View file

@ -10,6 +10,7 @@ from lnbits.helpers import template_renderer
from lnbits.settings import settings from lnbits.settings import settings
from .crud import get_myextension from .crud import get_myextension
from .helpers import lnurler
myextension_generic_router = APIRouter() myextension_generic_router = APIRouter()
@ -27,9 +28,9 @@ def myextension_renderer():
@myextension_generic_router.get("/", response_class=HTMLResponse) @myextension_generic_router.get("/", response_class=HTMLResponse)
async def index(request: Request, user: User = Depends(check_user_exists)): async def index(req: Request, user: User = Depends(check_user_exists)):
return myextension_renderer().TemplateResponse( return myextension_renderer().TemplateResponse(
"myextension/index.html", {"request": request, "user": user.json()} "myextension/index.html", {"request": req, "user": user.json()}
) )
@ -37,18 +38,18 @@ async def index(request: Request, user: User = Depends(check_user_exists)):
@myextension_generic_router.get("/{myextension_id}") @myextension_generic_router.get("/{myextension_id}")
async def myextension(request: Request, myextension_id): async def myextension(req: Request, myextension_id):
myextension = await get_myextension(myextension_id) myex = await get_myextension(myextension_id)
if not myextension: if not myex:
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="MyExtension does not exist." status_code=HTTPStatus.NOT_FOUND, detail="MyExtension does not exist."
) )
return myextension_renderer().TemplateResponse( return myextension_renderer().TemplateResponse(
"myextension/myextension.html", "myextension/myextension.html",
{ {
"request": request, "request": req,
"myextension_id": myextension_id, "myextension_id": myextension_id,
"lnurlpay": myextension.lnurlpay(request), "lnurlpay": lnurler(myex.id, "myextension.api_lnurl_pay", req),
"web_manifest": f"/myextension/manifest/{myextension_id}.webmanifest", "web_manifest": f"/myextension/manifest/{myextension_id}.webmanifest",
}, },
) )

View file

@ -18,7 +18,7 @@ from .crud import (
update_myextension, update_myextension,
) )
from .helpers import lnurler from .helpers import lnurler
from .models import CreateMyExtensionData, MyExtension from .models import CreateMyExtensionData, MyExtension, CreatePayment
myextension_api_router = APIRouter() myextension_api_router = APIRouter()
@ -67,6 +67,25 @@ async def api_myextension(myextension_id: str, req: Request) -> MyExtension:
return myex return myex
## Create a new record
@myextension_api_router.post("/api/v1/myex", status_code=HTTPStatus.CREATED)
async def api_myextension_create(
req: Request, # Withoutthe lnurl stuff this wouldnt be needed
data: CreateMyExtensionData,
wallet: WalletTypeInfo = Depends(require_admin_key),
) -> MyExtension:
myex = MyExtension(**data.dict(), wallet=wallet.wallet.id, id=urlsafe_short_hash())
myex = await create_myextension(myex)
# Populate lnurlpay and lnurlwithdraw.
# Withoutthe lnurl stuff this wouldnt be needed.
myex.lnurlpay = lnurler(myex.id, "myextension.api_lnurl_pay", req)
myex.lnurlwithdraw = lnurler(myex.id, "myextension.api_lnurl_withdraw", req)
return myex
## update a record ## update a record
@ -102,26 +121,6 @@ async def api_myextension_update(
return myex return myex
## Create a new record
@myextension_api_router.post("/api/v1/myex", status_code=HTTPStatus.CREATED)
async def api_myextension_create(
req: Request, # Withoutthe lnurl stuff this wouldnt be needed
data: CreateMyExtensionData,
wallet: WalletTypeInfo = Depends(require_admin_key),
) -> MyExtension:
myex = MyExtension(**data.dict(), wallet=wallet.wallet.id, id=urlsafe_short_hash())
myex = await create_myextension(myex)
# Populate lnurlpay and lnurlwithdraw.
# Withoutthe lnurl stuff this wouldnt be needed.
myex.lnurlpay = lnurler(myex.id, "myextension.api_lnurl_pay", req)
myex.lnurlwithdraw = lnurler(myex.id, "myextension.api_lnurl_withdraw", req)
return myex
## Delete a record ## Delete a record
@ -150,12 +149,12 @@ async def api_myextension_delete(
@myextension_api_router.post( @myextension_api_router.post(
"/api/v1/myex/payment/{myextension_id}", status_code=HTTPStatus.CREATED "/api/v1/myex/payment", status_code=HTTPStatus.CREATED
) )
async def api_myextension_create_invoice( async def api_myextension_create_invoice(
myextension_id: str, amount: int = Query(..., ge=1), memo: str = "" data: CreatePayment
) -> dict: ) -> dict:
myextension = await get_myextension(myextension_id) myextension = await get_myextension(data.myextension_id)
if not myextension: if not myextension:
raise HTTPException( raise HTTPException(
@ -167,11 +166,11 @@ async def api_myextension_create_invoice(
payment = await create_invoice( payment = await create_invoice(
wallet_id=myextension.wallet, wallet_id=myextension.wallet,
amount=amount, amount=data.amount,
memo=f"{memo} to {myextension.name}" if memo else f"{myextension.name}", memo=f"{data.memo} to {myextension.name}" if data.memo else f"{myextension.name}",
extra={ extra={
"tag": "myextension", "tag": "myextension",
"amount": amount, "amount": data.amount,
}, },
) )