Merge pull request #223 from talvasconcelos/jukebox/addlistener

Jukebox/addlistener
This commit is contained in:
Arc 2021-07-01 00:42:53 +01:00 committed by GitHub
commit 3562ceafe3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 81 additions and 74 deletions

1
.gitignore vendored
View File

@ -6,6 +6,7 @@ __pycache__
*$py.class
.mypy_cache
.vscode
*-lock.json
*.egg
*.egg-info

View File

@ -10,3 +10,8 @@ jukebox_ext: Blueprint = Blueprint(
from .views_api import * # noqa
from .views import * # noqa
from .tasks import register_listeners
from lnbits.tasks import record_async
jukebox_ext.record(record_async(register_listeners))

View File

@ -0,0 +1,27 @@
import json
import trio # type: ignore
from lnbits.core.models import Payment
from lnbits.core.crud import create_payment
from lnbits.core import db as core_db
from lnbits.tasks import register_invoice_listener, internal_invoice_paid
from lnbits.helpers import urlsafe_short_hash
from .crud import get_jukebox, update_jukebox_payment
async def register_listeners():
invoice_paid_chan_send, invoice_paid_chan_recv = trio.open_memory_channel(2)
register_invoice_listener(invoice_paid_chan_send)
await wait_for_paid_invoices(invoice_paid_chan_recv)
async def wait_for_paid_invoices(invoice_paid_chan: trio.MemoryReceiveChannel):
async for payment in invoice_paid_chan:
await on_invoice_paid(payment)
async def on_invoice_paid(payment: Payment) -> None:
if "jukebox" != payment.extra.get("tag"):
# not a jukebox invoice
return
await update_jukebox_payment(payment.payment_hash, paid=True)

View File

@ -134,14 +134,6 @@
}
},
methods: {
cancelPayment: function () {
this.paymentReq = null
clearInterval(this.paymentDialog.checker)
if (this.paymentDialog.dismissMsg) {
this.paymentDialog.dismissMsg()
}
},
closeReceiveDialog() {},
payForSong(song_id, name, artist, image) {
self = this
self.receive.name = name
@ -150,6 +142,49 @@
self.receive.id = song_id
self.receive.dialogues.first = true
},
startPaymentNotifier() {
this.cancelListener()
this.cancelListener = LNbits.events.onInvoicePaid(
this.selectedWallet,
payment => {
this.paid = true
this.receive.dialogues.first = false
this.receive.dialogues.second = false
LNbits.api
.request(
'GET',
'/jukebox/api/v1/jukebox/jb/invoicep/' +
this.receive.id +
'/{{ juke_id }}/' +
this.receive.paymentHash
)
.then(response1 => {
if (response1.data[2] == this.receive.id) {
setTimeout(() => {
this.getCurrent()
}, 500)
this.$q.notify({
color: 'green',
message:
'Success! "' +
this.receive.name +
'" will be played soon',
timeout: 3000
})
this.paid = false
response1 = []
}
})
.catch(err => {
LNbits.utils.notifyApiError(err)
self.paid = false
response1 = []
})
}
)
},
getInvoice(song_id) {
self = this
LNbits.api
@ -165,51 +200,9 @@
self.receive.paymentHash = response.data[0][0]
self.receive.dialogues.second = true
var paymentChecker = setInterval(function () {
if (!self.paid) {
self.checkInvoice(self.receive.paymentHash, '{{ juke_id }}')
}
if (self.paid) {
clearInterval(paymentChecker)
self.paid = true
self.receive.dialogues.first = false
self.receive.dialogues.second = false
self.$q.notify({
message: 'Processing'
})
LNbits.api
.request(
'GET',
'/jukebox/api/v1/jukebox/jb/invoicep/' +
song_id +
'/{{ juke_id }}/' +
self.receive.paymentHash
)
.then(function (response1) {
if (response1.data[2] == song_id) {
setTimeout(function () {
self.getCurrent()
}, 500)
self.$q.notify({
color: 'green',
message:
'Success! "' +
self.receive.name +
'" will be played soon',
timeout: 3000
})
self.paid = false
response1 = []
}
})
.catch(err => {
LNbits.utils.notifyApiError(err)
self.paid = false
response1 = []
})
}
}, 3000)
self.$q.notify({
message: 'Processing'
})
})
.catch(err => {
self.$q.notify({
@ -221,24 +214,6 @@
})
})
},
checkInvoice(juke_id, paymentHash) {
var self = this
LNbits.api
.request(
'GET',
'/jukebox/api/v1/jukebox/jb/checkinvoice/' +
juke_id +
'/' +
paymentHash,
'filla'
)
.then(function (response) {
self.paid = response.data.paid
})
.catch(function (error) {
LNbits.utils.notifyApiError(error)
})
},
getCurrent() {
LNbits.api
.request('GET', '/jukebox/api/v1/jukebox/jb/currently/{{juke_id}}')
@ -273,7 +248,8 @@
created() {
this.getCurrent()
this.playlists = JSON.parse('{{ playlists | tojson }}')
this.selectedWallet.inkey = '{{ inkey }}'
this.startPaymentNotifier()
self = this
LNbits.api
.request(
@ -289,8 +265,6 @@
.catch(err => {
LNbits.utils.notifyApiError(err)
})
// this.startPaymentNotifier()
}
})
</script>