2021-04-14 09:21:20 +01:00
|
|
|
from quart import g, abort, render_template, jsonify, websocket
|
2021-04-12 20:31:39 +01:00
|
|
|
from http import HTTPStatus
|
2021-04-19 09:32:41 +01:00
|
|
|
import httpx
|
2021-04-12 20:31:39 +01:00
|
|
|
|
|
|
|
from lnbits.decorators import check_user_exists, validate_uuids
|
|
|
|
|
|
|
|
from . import copilot_ext
|
|
|
|
from .crud import get_copilot
|
|
|
|
|
2021-04-14 23:45:28 +01:00
|
|
|
from quart import g, abort, render_template, jsonify, websocket
|
|
|
|
from functools import wraps
|
2021-04-16 12:20:05 +01:00
|
|
|
import trio
|
2021-04-14 23:45:28 +01:00
|
|
|
import shortuuid
|
|
|
|
from . import copilot_ext
|
|
|
|
|
2021-04-16 12:20:05 +01:00
|
|
|
|
2021-04-12 20:31:39 +01:00
|
|
|
@copilot_ext.route("/")
|
|
|
|
@validate_uuids(["usr"], required=True)
|
|
|
|
@check_user_exists()
|
|
|
|
async def index():
|
|
|
|
return await render_template("copilot/index.html", user=g.user)
|
|
|
|
|
|
|
|
|
2021-04-20 14:33:41 +01:00
|
|
|
@copilot_ext.route("/cp/")
|
|
|
|
async def compose():
|
|
|
|
return await render_template("copilot/compose.html")
|
|
|
|
|
|
|
|
|
|
|
|
@copilot_ext.route("/pn/")
|
|
|
|
async def panel():
|
|
|
|
return await render_template("copilot/panel.html")
|
2021-04-20 00:37:51 +01:00
|
|
|
|
2021-04-20 08:50:53 +01:00
|
|
|
|
|
|
|
##################WEBSOCKET ROUTES########################
|
|
|
|
|
|
|
|
# socket_relay is a list where the control panel or
|
|
|
|
# lnurl endpoints can leave a message for the compose window
|
|
|
|
|
|
|
|
socket_relay = {}
|
|
|
|
|
|
|
|
|
|
|
|
@copilot_ext.websocket("/ws/panel/<copilot_id>")
|
|
|
|
async def ws_panel(copilot_id):
|
|
|
|
global socket_relay
|
|
|
|
while True:
|
|
|
|
data = await websocket.receive()
|
|
|
|
socket_relay[copilot_id] = shortuuid.uuid()[:5] + "-" + data + "-" + "none"
|
|
|
|
|
|
|
|
|
|
|
|
@copilot_ext.websocket("/ws/compose/<copilot_id>")
|
|
|
|
async def ws_compose(copilot_id):
|
|
|
|
global socket_relay
|
|
|
|
while True:
|
|
|
|
data = await websocket.receive()
|
|
|
|
await websocket.send(socket_relay[copilot_id])
|
|
|
|
|
|
|
|
|
2021-04-20 00:37:51 +01:00
|
|
|
async def updater(data, comment, copilot):
|
2021-04-20 08:50:53 +01:00
|
|
|
global socket_relay
|
2021-04-20 17:43:33 +01:00
|
|
|
socket_relay[copilot] = shortuuid.uuid()[:5] + "-" + str(data) + "-" + str(comment)
|
2021-06-23 09:20:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##################WEBSOCKET ROUTES########################
|
|
|
|
|
|
|
|
# socket_relay is a list where the control panel or
|
|
|
|
# lnurl endpoints can leave a message for the compose window
|
|
|
|
|
|
|
|
connected_websockets = set()
|
|
|
|
|
|
|
|
|
|
|
|
def collect_websocket(func):
|
|
|
|
@wraps(func)
|
|
|
|
async def wrapper(*args, **kwargs):
|
|
|
|
global connected_websockets
|
|
|
|
send_channel, receive_channel = trio.open_memory_channel(0)
|
|
|
|
connected_websockets.add(send_channel)
|
|
|
|
try:
|
|
|
|
return await func(receive_channel, *args, **kwargs)
|
|
|
|
finally:
|
|
|
|
connected_websockets.remove(send_channel)
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
@copilot_ext.websocket("/ws")
|
|
|
|
@collect_websocket
|
|
|
|
async def wss(receive_channel):
|
|
|
|
|
|
|
|
while True:
|
|
|
|
data = await receive_channel.receive()
|
|
|
|
await websocket.send(data)
|