Connection status/error messages

This commit is contained in:
Leo Vasanko
2023-11-07 18:01:34 +00:00
parent 54d6ea6332
commit d5e1304c0d
10 changed files with 124 additions and 75 deletions

View File

@@ -4,7 +4,7 @@ import typing
import msgspec
from sanic import Blueprint
from cista import watching
from cista import __version__, config, watching
from cista.fileio import FileServer
from cista.protocol import ControlTypes, FileRange, StatusMsg
from cista.util.apphelpers import asend, websocket_wrapper
@@ -83,6 +83,23 @@ async def control(req, ws):
@bp.websocket("watch")
@websocket_wrapper
async def watch(req, ws):
await ws.send(
msgspec.json.encode(
{
"server": {
"name": "Cista", # Should be configurable
"version": __version__,
"public": config.config.public,
},
"user": {
"username": req.ctx.username,
"privileged": req.ctx.user.privileged,
}
if req.ctx.user
else None,
}
).decode()
)
try:
with watching.tree_lock:
q = watching.pubsub[ws] = asyncio.Queue()

View File

@@ -38,8 +38,10 @@ async def main_stop(app, loop):
async def use_session(req):
req.ctx.session = session.get(req)
try:
req.ctx.username = req.ctx.session["username"]
req.ctx.user = config.config.users[req.ctx.session["username"]] # type: ignore
except (AttributeError, KeyError, TypeError):
req.ctx.username = None
req.ctx.user = None
# CSRF protection
if req.method == "GET" and req.headers.upgrade != "websocket":