adding websocket stuff

This commit is contained in:
benarc 2024-01-18 12:41:46 +00:00
parent 1fa7e9155c
commit addf8fec11
2 changed files with 45 additions and 16 deletions

View file

@ -38,7 +38,7 @@ async def on_invoice_paid(payment: Payment) -> None:
myextension = await get_myextension(myextension_id) myextension = await get_myextension(myextension_id)
assert myextension assert myextension
# update something # update something in the db
data_to_update = { data_to_update = {
"total": myextension.total + payment.amount "total": myextension.total + payment.amount
@ -46,8 +46,8 @@ async def on_invoice_paid(payment: Payment) -> None:
await update_myextension(myextension_id=myextension_id, **data_to_update.dict()) await update_myextension(myextension_id=myextension_id, **data_to_update.dict())
# here we could send some data to a websocket on wss://<your-lnbits>/api/v1/ws/<myextension_id> # here we could send some data to a websocket on wss://<your-lnbits>/api/v1/ws/<myextension_id>
# and then listen to it on the frontend, which we do with index.html connectWebocket()
some_payment_data = { some_payment_data = {
"name": myextension.name, "name": myextension.name,

View file

@ -195,9 +195,8 @@
</div> </div>
{% endblock %} {% block scripts %} {{ window_vars(user) }} {% endblock %} {% block scripts %} {{ window_vars(user) }}
<script> <script>
// The object returned here will be merged with the data object of the Vue instance // The object returned here will be merged with the data object of the Vue instance
const mapTemp = obj => { const mapMyExtension = obj => {
obj.date = Quasar.utils.date.formatDate( obj.date = Quasar.utils.date.formatDate(
new Date(obj.time * 1000), new Date(obj.time * 1000),
'YYYY-MM-DD HH:mm' 'YYYY-MM-DD HH:mm'
@ -257,7 +256,7 @@
withdrawbtwn: 10 withdrawbtwn: 10
} }
}, },
getTemps: function () { getMyExtensions: function () {
var self = this var self = this
LNbits.api LNbits.api
@ -268,11 +267,11 @@
) )
.then(function (response) { .then(function (response) {
self.temps = response.data.map(function (obj) { self.temps = response.data.map(function (obj) {
return mapTemp(obj) return mapMyExtension(obj)
}) })
}) })
}, },
sendTPosData() { sendMyExtensionData() {
const data = { const data = {
name: this.formDialog.data.name, name: this.formDialog.data.name,
lnurlwithdrawamount: this.formDialog.data.lnurlwithdrawamount, lnurlwithdrawamount: this.formDialog.data.lnurlwithdrawamount,
@ -284,12 +283,12 @@
console.log(data) console.log(data)
console.log(wallet) console.log(wallet)
if (data.id) { if (data.id) {
this.updateTPos(wallet, data) this.updateMyExtension(wallet, data)
} else { } else {
this.createTemp(wallet, data) this.createMyExtension(wallet, data)
} }
}, },
updateTPosForm(tempId) { updateMyExtensionForm(tempId) {
const myextension = _.findWhere(this.temps, {id: tempId}) const myextension = _.findWhere(this.temps, {id: tempId})
this.formDialog.data = { this.formDialog.data = {
...myextension, ...myextension,
@ -303,18 +302,18 @@
} }
this.formDialog.show = true this.formDialog.show = true
}, },
createTemp(wallet, data) { createMyExtension(wallet, data) {
LNbits.api LNbits.api
.request('POST', '/myextension/api/v1/temps', wallet.inkey, data) .request('POST', '/myextension/api/v1/temps', wallet.inkey, data)
.then(response => { .then(response => {
this.temps.push(mapTemp(response.data)) this.temps.push(mapMyExtension(response.data))
this.closeFormDialog() this.closeFormDialog()
}) })
.catch(error => { .catch(error => {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
}) })
}, },
updateTPos(wallet, data) { updateMyExtension(wallet, data) {
LNbits.api LNbits.api
.request( .request(
'PUT', 'PUT',
@ -333,7 +332,7 @@
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
}) })
}, },
deleteTemp: function (tempId) { deleteMyExtension: function (tempId) {
var self = this var self = this
var myextension = _.findWhere(this.temps, {id: tempId}) var myextension = _.findWhere(this.temps, {id: tempId})
@ -395,9 +394,13 @@
openUrlDialog(id) { openUrlDialog(id) {
this.urlDialog.data = _.findWhere(this.temps, {id}) this.urlDialog.data = _.findWhere(this.temps, {id})
this.qrValue = this.urlDialog.data.lnurlpay this.qrValue = this.urlDialog.data.lnurlpay
this.connectWebocket(this.urlDialog.data.id)
this.urlDialog.show = true this.urlDialog.show = true
}, },
createInvoice(walletId) { createInvoice(walletId) {
///////////////////////////////////////////////////
///Simple call to the api to create an invoice/////
///////////////////////////////////////////////////
console.log(walletId) console.log(walletId)
const wallet = _.findWhere(this.g.user.wallets, { const wallet = _.findWhere(this.g.user.wallets, {
id: walletId id: walletId
@ -420,10 +423,36 @@
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
}) })
}, },
connectWebocket(id){
//////////////////////////////////////////////////
///wait for pay action to happen and do a thing////
///////////////////////////////////////////////////
if (location.protocol !== 'http:') {
localUrl =
'wss://' +
document.domain +
':' +
location.port +
'/api/v1/ws/' +
id
} else {
localUrl =
'ws://' +
document.domain +
':' +
location.port +
'/api/v1/ws/' +
id
}
this.connection = new WebSocket(localUrl)
this.connection.onmessage = function (e) {
console.log(e)
}
}
}, },
created: function () { created: function () {
if (this.g.user.wallets.length) { if (this.g.user.wallets.length) {
this.getTemps() this.getMyExtensions()
} }
} }