From 08dad16611eabed7ad194f8609b46599dadea7e1 Mon Sep 17 00:00:00 2001 From: Arc Date: Mon, 18 Nov 2024 21:54:27 +0000 Subject: [PATCH] Mostly working --- models.py | 10 +- static/js/index.js | 382 +++++++++++++++---------------- templates/myextension/index.html | 4 +- views.py | 15 +- views_api.py | 53 +++-- 5 files changed, 232 insertions(+), 232 deletions(-) diff --git a/models.py b/models.py index af878f5..e2a960e 100644 --- a/models.py +++ b/models.py @@ -17,5 +17,11 @@ class MyExtension(BaseModel): lnurlwithdrawamount: int wallet: str total: int - lnurlpay: str - lnurlwithdraw: str + lnurlpay: str = "" + lnurlwithdraw: str = "" + +class CreatePayment(BaseModel): + myextension_id: str + amount: int + memo: str + diff --git a/static/js/index.js b/static/js/index.js index 272c376..6c29705 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -57,209 +57,203 @@ window.app = Vue.createApp({ this.g.user.wallets[0].inkey ) .then(response => { - console.log(response.data) this.myex = response.data }) - .catch(error => { - console.error('Error fetching data:', error) + .catch(err => { + LNbits.utils.notifyApiError(err) }) - } - }, - async sendMyExtensionData() { - const data = { - name: this.formDialog.data.name, - lnurlwithdrawamount: this.formDialog.data.lnurlwithdrawamount, - lnurlpayamount: this.formDialog.data.lnurlpayamount - } - const wallet = _.findWhere(this.g.user.wallets, { - id: this.formDialog.data.wallet - }) - if (this.formDialog.data.id) { - data.id = this.formDialog.data.id - data.total = this.formDialog.data.total - await this.updateMyExtension(wallet, data) - } else { - await this.createMyExtension(wallet, data) - } - }, - async updateMyExtensionForm(tempId) { - const myextension = _.findWhere(this.myex, {id: tempId}) - this.formDialog.data = { - ...myextension - } - if (this.formDialog.data.tip_wallet != '') { - this.formDialog.advanced.tips = true - } - if (this.formDialog.data.withdrawlimit >= 1) { - this.formDialog.advanced.otc = true - } - this.formDialog.show = true - }, - async createMyExtension(wallet, data) { - await LNbits.api - .request('POST', '/myextension/api/v1/myex', wallet.adminkey, data) - .then(response => { - this.myex.push(response.data) - this.closeFormDialog() - }) - .catch(error => { - LNbits.utils.notifyApiError(error) - }) - }, - async updateMyExtension(wallet, data) { - await LNbits.api - .request( - 'PUT', - `/myextension/api/v1/myex/${data.id}`, - wallet.adminkey, - data - ) - .then(response => { - this.myex = _.reject(this.myex, obj => { - return obj.id == data.id - }) - this.myex.push(response.data) - this.closeFormDialog() - }) - .catch(error => { - LNbits.utils.notifyApiError(error) - }) - }, - async deleteMyExtension(tempId) { - var myextension = _.findWhere(this.myex, {id: tempId}) - await LNbits.utils - .confirmDialog('Are you sure you want to delete this MyExtension?') - .onOk(function () { - LNbits.api - .request( - 'DELETE', - '/myextension/api/v1/myex/' + tempId, - _.findWhere(this.g.user.wallets, {id: myextension.wallet}).adminkey - ) - .then(() => { - this.myex = _.reject(this.myex, function (obj) { - return obj.id == tempId - }) - }) - .catch(error => { - LNbits.utils.notifyApiError(error) - }) - }) - }, - async exportCSV() { - await LNbits.utils.exportCSV(this.myexTable.columns, this.myex) - }, - async itemsArray(tempId) { - const myextension = _.findWhere(this.myex, {id: tempId}) - return [...myextension.itemsMap.values()] - }, - async openformDialog(id) { - const [tempId, itemId] = id.split(':') - const myextension = _.findWhere(this.myex, {id: tempId}) - if (itemId) { - const item = myextension.itemsMap.get(id) - this.formDialog.data = { - ...item, - myextension: tempId + }, + 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() { + const data = { + name: this.formDialog.data.name, + lnurlwithdrawamount: this.formDialog.data.lnurlwithdrawamount, + lnurlpayamount: this.formDialog.data.lnurlpayamount } - } else { - this.formDialog.data.myextension = tempId - } - this.formDialog.data.currency = myextension.currency - this.formDialog.show = true - }, - async closeformDialog() { - this.formDialog.show = false - this.formDialog.data = {} - }, - async openUrlDialog(id) { - this.urlDialog.data = _.findWhere(this.myex, {id}) - 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///// - /////////////////////////////////////////////////// - console.log(walletId) - const wallet = _.findWhere(this.g.user.wallets, { - id: walletId - }) - const dataToSend = { - out: false, - amount: this.invoiceAmount, - memo: 'Invoice created by MyExtension', - extra: { - tag: 'MyExtension', - myextensionId: myextensionId - } - } - await LNbits.api - .request('POST', `/api/v1/payments`, wallet.inkey, dataToSend) - .then(response => { - this.qrValue = response.data.payment_request + const wallet = _.findWhere(this.g.user.wallets, { + id: this.formDialog.data.wallet }) - .catch(error => { - LNbits.utils.notifyApiError(error) - }) - }, - async makeItRain() { - document.getElementById('vue').disabled = true - var end = Date.now() + 2 * 1000 - var colors = ['#FFD700', '#ffffff'] - async function frame() { - confetti({ - particleCount: 2, - angle: 60, - spread: 55, - origin: {x: 0}, - colors: colors, - zIndex: 999999 - }) - confetti({ - particleCount: 2, - angle: 120, - spread: 55, - origin: {x: 1}, - colors: colors, - zIndex: 999999 - }) - if (Date.now() < end) { - requestAnimationFrame(frame) + if (this.formDialog.data.id) { + data.id = this.formDialog.data.id + data.total = this.formDialog.data.total + await this.updateMyExtension(wallet, data) } else { - document.getElementById('vue').disabled = false + await this.createMyExtension(wallet, data) + } + }, + + async updateMyExtensionForm(tempId) { + const myextension = _.findWhere(this.myex, {id: tempId}) + this.formDialog.data = { + ...myextension + } + if (this.formDialog.data.tip_wallet != '') { + this.formDialog.advanced.tips = true + } + if (this.formDialog.data.withdrawlimit >= 1) { + this.formDialog.advanced.otc = true + } + this.formDialog.show = true + }, + async createMyExtension(wallet, data) { + await LNbits.api + .request('POST', '/myextension/api/v1/myex', wallet.adminkey, data) + .then(response => { + this.myex.push(response.data) + this.closeFormDialog() + }) + .catch(error => { + LNbits.utils.notifyApiError(error) + }) + }, + + async updateMyExtension(wallet, data) { + await LNbits.api + .request( + 'PUT', + `/myextension/api/v1/myex/${data.id}`, + wallet.adminkey, + data + ) + .then(response => { + this.myex = _.reject(this.myex, obj => obj.id == data.id) + this.myex.push(response.data) + this.closeFormDialog() + }) + .catch(error => { + LNbits.utils.notifyApiError(error) + }) + }, + async deleteMyExtension(tempId) { + var myextension = _.findWhere(this.myex, {id: tempId}) + await LNbits.utils + .confirmDialog('Are you sure you want to delete this MyExtension?') + .onOk(function () { + LNbits.api + .request( + 'DELETE', + '/myextension/api/v1/myex/' + tempId, + _.findWhere(this.g.user.wallets, {id: myextension.wallet}) + .adminkey + ) + .then(() => { + this.myex = _.reject(this.myex, obj => obj.id == tempId) + }) + .catch(error => { + LNbits.utils.notifyApiError(error) + }) + }) + }, + + async exportCSV() { + await LNbits.utils.exportCSV(this.myexTable.columns, this.myex) + }, + async itemsArray(tempId) { + const myextension = _.findWhere(this.myex, {id: tempId}) + return [...myextension.itemsMap.values()] + }, + async openformDialog(id) { + const [tempId, itemId] = id.split(':') + const myextension = _.findWhere(this.myex, {id: tempId}) + if (itemId) { + const item = myextension.itemsMap.get(id) + this.formDialog.data = { + ...item, + myextension: tempId + } + } else { + this.formDialog.data.myextension = tempId + } + this.formDialog.data.currency = myextension.currency + this.formDialog.show = true + }, + async closeformDialog() { + this.formDialog.show = false + this.formDialog.data = {} + }, + + async createInvoice(tempid) { + /////////////////////////////////////////////////// + ///Simple call to the api to create an invoice///// + /////////////////////////////////////////////////// + myex = _.findWhere(this.myex, {id: tempid}) + const wallet = _.findWhere(this.g.user.wallets, {id: myex.wallet}) + const data = { + myextension_id: tempid, + amount: this.invoiceAmount, + memo: 'MyExtension - ' + myex.name + } + await LNbits.api + .request('POST', `/myextension/api/v1/myex/payment`, wallet.inkey, data) + .then(async response => { + this.qrValue = response.data.payment_request + await this.connectWebocket(wallet.id) + }) + .catch(error => { + LNbits.utils.notifyApiError(error) + }) + }, + async makeItRain() { + document.getElementById('vue').disabled = true + var end = Date.now() + 2 * 1000 + var colors = ['#FFD700', '#ffffff'] + async function frame() { + confetti({ + particleCount: 2, + angle: 60, + spread: 55, + origin: {x: 0}, + colors: colors, + zIndex: 999999 + }) + confetti({ + particleCount: 2, + angle: 120, + spread: 55, + origin: {x: 1}, + colors: colors, + zIndex: 999999 + }) + if (Date.now() < end) { + requestAnimationFrame(frame) + } else { + document.getElementById('vue').disabled = false + } + } + await frame() + }, + async connectWebocket(wallet_id) { + ////////////////////////////////////////////////// + ///wait for pay action to happen and do a thing//// + /////////////////////////////////////////////////// + if (location.protocol !== 'http:') { + localUrl = + 'wss://' + + document.domain + + ':' + + location.port + + '/api/v1/ws/' + + wallet_id + } else { + localUrl = + 'ws://' + + document.domain + + ':' + + location.port + + '/api/v1/ws/' + + wallet_id + } + this.connection = new WebSocket(localUrl) + this.connection.onmessage = async function (e) { + await this.makeItRain() } } - await frame() }, - async connectWebocket(wallet_id) { - ////////////////////////////////////////////////// - ///wait for pay action to happen and do a thing//// - /////////////////////////////////////////////////// - if (location.protocol !== 'http:') { - localUrl = - 'wss://' + - document.domain + - ':' + - location.port + - '/api/v1/ws/' + - wallet_id - } else { - localUrl = - 'ws://' + - document.domain + - ':' + - location.port + - '/api/v1/ws/' + - wallet_id - } - this.connection = new WebSocket(localUrl) - this.connection.onmessage = async function (e) { - await this.makeItRain() - } - }, - /////////////////////////////////////////////////// //////LIFECYCLE FUNCTIONS RUNNING ON PAGE LOAD///// /////////////////////////////////////////////////// diff --git a/templates/myextension/index.html b/templates/myextension/index.html index 3fcf632..038e712 100644 --- a/templates/myextension/index.html +++ b/templates/myextension/index.html @@ -67,7 +67,7 @@ icon="launch" :color="($q.dark.isActive) ? 'grey-7' : 'grey-5'" type="a" - :href="props.row.myextension" + :href="props.row.id" target="_blank" >Open public page MyExtension: 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 @@ -102,26 +121,6 @@ async def api_myextension_update( 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 @@ -150,12 +149,12 @@ async def api_myextension_delete( @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( - myextension_id: str, amount: int = Query(..., ge=1), memo: str = "" + data: CreatePayment ) -> dict: - myextension = await get_myextension(myextension_id) + myextension = await get_myextension(data.myextension_id) if not myextension: raise HTTPException( @@ -167,11 +166,11 @@ async def api_myextension_create_invoice( payment = await create_invoice( wallet_id=myextension.wallet, - amount=amount, - memo=f"{memo} to {myextension.name}" if memo else f"{myextension.name}", + amount=data.amount, + memo=f"{data.memo} to {myextension.name}" if data.memo else f"{myextension.name}", extra={ "tag": "myextension", - "amount": amount, + "amount": data.amount, }, )