diff --git a/lnbits/extensions/copilot/tasks.py b/lnbits/extensions/copilot/tasks.py index 7ff12e33..7dee007a 100644 --- a/lnbits/extensions/copilot/tasks.py +++ b/lnbits/extensions/copilot/tasks.py @@ -1,6 +1,8 @@ import trio # type: ignore import json import httpx +from quart import g, jsonify, url_for, websocket +from http import HTTPStatus from lnbits.core import db as core_db from lnbits.core.models import Payment diff --git a/lnbits/extensions/copilot/templates/copilot/compose.html b/lnbits/extensions/copilot/templates/copilot/compose.html index a8750eb7..8a28be55 100644 --- a/lnbits/extensions/copilot/templates/copilot/compose.html +++ b/lnbits/extensions/copilot/templates/copilot/compose.html @@ -87,6 +87,8 @@ counter: 1, colours: ['teal', 'purple', 'indigo', 'pink', 'green'], copilot: {}, + animQueue: [], + queue: false, lnurl: '' } }, @@ -133,36 +135,24 @@ console.log('Something went wrong!') }) }, - getPrice: function () { + launch(){ self = this - if (self.copilot.show_price != 'None') { - LNbits.api - .request('GET', 'https://api.opennode.com/v1/rates', 'filla') - .then(function (response) { - self.price = String( - new Intl.NumberFormat('en-US', { - style: 'currency', - currency: 'USD' - }).format(response.data.data.BTCUSD.USD) - ) + console.log("poo") + LNbits.api + .request( + 'GET', + '/copilot/api/v1/copilot/ws/' + self.copilot.id + '/launching/rocket') + .then(function (response1) { + self.$q.notify({ + color: 'green', + message: 'Sent!' }) - .catch(function (error) { - LNbits.utils.notifyApiError(error) - }) - } + }) + .catch(err => { + LNbits.utils.notifyApiError(err) + }) }, - animation1: function () { - self = this - setTimeout(function () { - setInterval(function () { - self.connection.send('') - self.counter++ - if (self.counter % 20 === 0) { - self.getPrice() - } - }, 1000) - }, 2000) - } + }, mounted() { this.initCamera() @@ -171,47 +161,45 @@ self = this self.copilot = JSON.parse(localStorage.getItem('copilot')) - LNbits.api - .request( - 'GET', - '/copilot/api/v1/copilot/ws/' + self.copilot.id + '/launching/rocket', - 'filla' - ) - .then(function (response) {}) - .catch(function (error) { - LNbits.utils.notifyApiError(error) + this.connectionBitStamp = new WebSocket('wss://ws.bitstamp.net') + + const obj = JSON.stringify({ + event: "bts:subscribe", + data: { channel: "live_trades_btcusd" }, }) + + this.connectionBitStamp.onmessage = function (e) { + console.log(JSON.parse(e.data).data.price) + self.price = String( + new Intl.NumberFormat('en-US', { + style: 'currency', + currency: 'USD' + }).format(JSON.parse(e.data).data.price) + ) + } + this.connectionBitStamp.onopen = () => this.connectionBitStamp.send(obj) + if (location.protocol !== 'http:') { - this.connection = new WebSocket( - 'wss://' + + localUrl = 'wss://' + document.domain + ':' + location.port + - '/copilot/ws/compose/' + - self.copilot.id - ) + '/copilot/ws/' + self.copilot.id + '/' } else { - this.connection = new WebSocket( - 'ws://' + + localUrl = 'ws://' + document.domain + ':' + location.port + - '/copilot/ws/compose/' + - self.copilot.id - ) + '/copilot/ws/' + self.copilot.id + '/' } - - this.connection.addEventListener('open', function (event) { - this.connection.send('') - }) - var showNotif = this.showNotif - - this.connection.addEventListener('message', function (event) { - res = event.data.split('-') - console.log(res[1]) - if (res[0] != this.oldRes) { - this.oldRes = res[0] - if (res[1] == 'rocket') { + this.connection = new WebSocket(localUrl) + this.connection.onmessage = function (e) { + console.log(e.data) + + res = e.data.split('-') + if (res[0] != this.oldRes) { + this.oldRes = res[0] + if (res[0] == 'rocket') { document.getElementById('animations').style.width = '40%' document.getElementById('animations').src = '/copilot/static/rocket.gif' @@ -219,7 +207,7 @@ document.getElementById('animations').src = '' }, 5000) } - if (res[1] == 'face') { + if (res[0] == 'face') { document.getElementById('animations').style.width = '35%' document.getElementById('animations').src = '/copilot/static/face.gif' @@ -227,7 +215,7 @@ document.getElementById('animations').src = '' }, 5000) } - if (res[1] == 'bitcoin') { + if (res[0] == 'bitcoin') { document.getElementById('animations').style.width = '30%' document.getElementById('animations').src = '/copilot/static/bitcoin.gif' @@ -235,7 +223,7 @@ document.getElementById('animations').src = '' }, 5000) } - if (res[1] == 'confetti') { + if (res[0] == 'confetti') { document.getElementById('animations').style.width = '100%' document.getElementById('animations').style.height = '100%' document.getElementById('animations').src = @@ -245,7 +233,7 @@ document.getElementById('animations').style.height = '' }, 5000) } - if (res[1] == 'martijn') { + if (res[0] == 'martijn') { document.getElementById('animations').style.width = '40%' document.getElementById('animations').src = '/copilot/static/martijn.gif' @@ -253,36 +241,29 @@ document.getElementById('animations').src = '' }, 5000) } - if (res[1] == 'rick') { - document.getElementById('animations').style.width = '40%' - document.getElementById('animations').src = - '/copilot/static/rick.gif' - setTimeout(function () { - document.getElementById('animations').src = '' - }, 5000) + if (res[0] == 'rick') { + this.animQueue.push({size: '40%', location: '/copilot/static/rick.gif'}) } - if (res[1] == 'true') { + if(self.queue == false){ + for() + + } + if (res[0] == 'true') { document.getElementById('videoCamera').style.width = '20%' self.initScreenShare() } - if (res[1] == 'false') { + if (res[0] == 'false') { document.getElementById('videoCamera').style.width = '100%' document.getElementById('videoScreen').src = null } - console.log(res[2]) - if (res[2] != 'none') { - showNotif(res[2]) + console.log(res[1]) + if (res[1] != 'none') { + showNotif(res[1]) } + this.oldRes = '' + } } - }) - - this.connection.addEventListener('close', function (event) { - console.log('The connection has been closed') - }) - var animation1 = this.animation1 - animation1() - var getPrice = this.getPrice - getPrice() + this.connection.onopen = () => this.launch } }) diff --git a/lnbits/extensions/copilot/templates/copilot/panel.html b/lnbits/extensions/copilot/templates/copilot/panel.html index 111b5e13..1cf74b40 100644 --- a/lnbits/extensions/copilot/templates/copilot/panel.html +++ b/lnbits/extensions/copilot/templates/copilot/panel.html @@ -110,8 +110,9 @@ this.connection.send(String(url)) }, fullscreenToggle: function () { + self = this console.log(this.fullscreen_cam) - this.connection.send(String(this.fullscreen_cam)) + self.animationBTN(String(this.fullscreen_cam)) if (this.fullscreen_cam) { this.fullscreen_cam = false } else { @@ -121,49 +122,28 @@ openCompose: function () { let params = 'scrollbars=no, resizable=no,status=no,location=no,toolbar=no,menubar=no,width=1200,height=644,left=410,top=100' - - open('./copilot/cp/', 'test', params) + open('../cp/', 'test', params) }, animationBTN: function (name) { - this.connection.send(name) + self = this + LNbits.api + .request( + 'GET', + '/copilot/api/v1/copilot/ws/' + self.copilot.id + '/none/' + name) + .then(function (response1) { + self.$q.notify({ + color: 'green', + message: 'Sent!' + }) + }) + .catch(err => { + LNbits.utils.notifyApiError(err) + }) }, - stfu: function (name) { - this.connection.send('') - } }, created: function () { self = this self.copilot = JSON.parse(localStorage.getItem('copilot')) - - if (location.protocol == 'https:') { - console.log(location.protocol) - self.connection = new WebSocket( - 'wss://' + - document.domain + - ':' + - location.port + - '/copilot/ws/panel/' + - self.copilot.id - ) - } else { - self.connection = new WebSocket( - 'ws://' + - document.domain + - ':' + - location.port + - '/copilot/ws/panel/' + - self.copilot.id - ) - } - self.connection.addEventListener('open', function (event) {}) - - self.connection.addEventListener('message', function (event) { - console.log('Message from server ', event.data) - }) - - self.connection.addEventListener('close', function (event) { - console.log('The connection has been closed') - }) } }) diff --git a/lnbits/extensions/copilot/views.py b/lnbits/extensions/copilot/views.py index 46a82ca6..b1c2d1ad 100644 --- a/lnbits/extensions/copilot/views.py +++ b/lnbits/extensions/copilot/views.py @@ -37,6 +37,7 @@ connected_websockets = defaultdict(set) @copilot_ext.websocket("/ws//") async def wss(id): copilot = await get_copilot(id) + print(copilot) if not copilot: return "", HTTPStatus.FORBIDDEN global connected_websockets @@ -53,6 +54,5 @@ async def updater(copilot_id, data, comment): copilot = await get_copilot(copilot_id) if not copilot: return - print(connected_websockets) for queue in connected_websockets[copilot_id]: await queue.send(f"{data + '-' + comment}") \ No newline at end of file diff --git a/lnbits/extensions/copilot/views_api.py b/lnbits/extensions/copilot/views_api.py index 76b2d54c..36c1e21f 100644 --- a/lnbits/extensions/copilot/views_api.py +++ b/lnbits/extensions/copilot/views_api.py @@ -71,14 +71,17 @@ async def api_copilots_retrieve(): @api_check_wallet_key("invoice") async def api_copilot_retrieve(copilot_id): copilot = await get_copilot(copilot_id) - if not copilot: return jsonify({"message": "copilot does not exist"}), HTTPStatus.NOT_FOUND - + if not copilot.lnurl_toggle: + return ( + jsonify({**copilot._asdict()}), + HTTPStatus.OK, + ) return ( - jsonify({**copilot._asdict(), **{"lnurl": copilot.lnurl}}), - HTTPStatus.OK, - ) + jsonify({**copilot._asdict(), **{"lnurl": copilot.lnurl}}), + HTTPStatus.OK, + ) @copilot_ext.route("/api/v1/copilot/", methods=["DELETE"]) @@ -97,7 +100,6 @@ async def api_copilot_delete(copilot_id): @copilot_ext.route("/api/v1/copilot/ws///", methods=["GET"]) async def api_copilot_ws_relay(copilot_id, comment, data): copilot = await get_copilot(copilot_id) - if not copilot: return jsonify({"message": "copilot does not exist"}), HTTPStatus.NOT_FOUND try: