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
|
|
|
|
|
|
|
|
connected_websockets = {}
|
|
|
|
|
2021-04-16 12:20:05 +01:00
|
|
|
|
|
|
|
@copilot_ext.websocket("/ws/panel/<copilot_id>")
|
2021-04-14 23:45:28 +01:00
|
|
|
async def ws_panel(copilot_id):
|
|
|
|
global connected_websockets
|
|
|
|
while True:
|
|
|
|
data = await websocket.receive()
|
2021-04-16 12:20:05 +01:00
|
|
|
connected_websockets[copilot_id] = shortuuid.uuid() + "-" + data
|
|
|
|
|
2021-04-16 22:22:36 +01:00
|
|
|
|
2021-04-16 12:20:05 +01:00
|
|
|
@copilot_ext.websocket("/ws/compose/<copilot_id>")
|
2021-04-14 23:45:28 +01:00
|
|
|
async def ws_compose(copilot_id):
|
|
|
|
global connected_websockets
|
2021-04-16 22:22:36 +01:00
|
|
|
|
2021-04-14 09:29:25 +01:00
|
|
|
while True:
|
2021-04-16 22:22:36 +01:00
|
|
|
|
2021-04-14 09:29:25 +01:00
|
|
|
data = await websocket.receive()
|
2021-04-14 23:45:28 +01:00
|
|
|
await websocket.send(connected_websockets[copilot_id])
|
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-13 19:20:12 +01:00
|
|
|
@copilot_ext.route("/cp/<copilot_id>")
|
|
|
|
async def compose(copilot_id):
|
|
|
|
copilot = await get_copilot(copilot_id) or abort(
|
|
|
|
HTTPStatus.NOT_FOUND, "Copilot link does not exist."
|
|
|
|
)
|
2021-04-15 09:39:27 +01:00
|
|
|
if copilot.lnurl_toggle:
|
2021-04-16 12:20:05 +01:00
|
|
|
return await render_template(
|
|
|
|
"copilot/compose.html",
|
|
|
|
copilot=copilot,
|
|
|
|
lnurl=copilot.lnurl,
|
|
|
|
lnurl_toggle=copilot.lnurl_toggle,
|
|
|
|
)
|
|
|
|
return await render_template(
|
|
|
|
"copilot/compose.html", copilot=copilot, lnurl_toggle=copilot.lnurl_toggle
|
|
|
|
)
|
|
|
|
|
2021-04-13 19:20:12 +01:00
|
|
|
|
2021-04-12 20:31:39 +01:00
|
|
|
@copilot_ext.route("/<copilot_id>")
|
2021-04-13 19:20:12 +01:00
|
|
|
async def panel(copilot_id):
|
2021-04-12 20:31:39 +01:00
|
|
|
copilot = await get_copilot(copilot_id) or abort(
|
2021-04-13 19:20:12 +01:00
|
|
|
HTTPStatus.NOT_FOUND, "Copilot link does not exist."
|
2021-04-12 20:31:39 +01:00
|
|
|
)
|
2021-04-16 12:20:05 +01:00
|
|
|
return await render_template("copilot/panel.html", copilot=copilot)
|
2021-04-20 00:37:51 +01:00
|
|
|
|
|
|
|
async def updater(data, comment, copilot):
|
|
|
|
global connected_websockets
|
|
|
|
connected_websockets[copilot] = (
|
|
|
|
shortuuid.uuid() + "-" + data + "-" + comment
|
|
|
|
)
|