Skip to content

Commit

Permalink
Add GET room ID handler, directly access file ID manager
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Oct 25, 2022
1 parent 8db6b68 commit 0053f1b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
13 changes: 10 additions & 3 deletions jupyter_server_ydoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import jupyter_server.serverapp
from jupyter_server.utils import url_path_join

from .ydoc import YDocWebSocketHandler
from .ydoc import YDocRoomIdHandler, YDocWebSocketHandler

__version__ = "0.1.12"

Expand All @@ -15,5 +15,12 @@ def _jupyter_server_extension_points() -> List[Dict[str, str]]:
def _load_jupyter_server_extension(serverapp: jupyter_server.serverapp.ServerApp) -> None:
web_app = serverapp.web_app
host_pattern = ".*$"
route_pattern = url_path_join(web_app.settings["base_url"], r"/api/yjs/(.*)")
web_app.add_handlers(host_pattern, [(route_pattern, YDocWebSocketHandler)])
yid_route_pattern = url_path_join(web_app.settings["base_url"], r"/api/yjs/roomid/(.*)")
yws_route_pattern = url_path_join(web_app.settings["base_url"], r"/api/yjs/(.*)")
web_app.add_handlers(
host_pattern,
[
(yid_route_pattern, YDocRoomIdHandler),
(yws_route_pattern, YDocWebSocketHandler),
],
)
45 changes: 39 additions & 6 deletions jupyter_server_ydoc/ydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import asyncio
from datetime import datetime
from pathlib import Path
from typing import Any, Callable, Dict, Optional, Tuple
from typing import Any, Dict, Optional, Tuple

from jupyter_server.base.handlers import JupyterHandler
from jupyter_server.auth import authorized
from jupyter_server.base.handlers import APIHandler, JupyterHandler
from jupyter_server.utils import ensure_async
from jupyter_ydoc import ydocs as YDOCS # type: ignore
from tornado import web
Expand Down Expand Up @@ -87,7 +88,6 @@ class YDocWebSocketHandler(WebSocketHandler, JupyterHandler):
saving_document: Optional["asyncio.Task[Any]"]
websocket_server: Optional[JupyterWebsocketServer] = None
_message_queue: "asyncio.Queue[Any]"
_id2path: Callable[[str], str]

# Override max_message size to 1GB
@property
Expand All @@ -113,8 +113,14 @@ def get_file_info(self) -> Tuple[str, str, str]:
file_path: str
file_id: str
file_format, file_type, file_id = room_name.split(":", 2)
file_path = self._id2path(file_id)
self.room.document.path = file_path
file_id_manager = self.settings.get("file_id_manager")
if file_id_manager is None:
# no file ID manager installed, the path is the ID
file_path = file_id
else:
file_path = file_id_manager.get_path(file_id)
if file_path != self.room.document.path:
self.room.document.path = file_path
return file_format, file_type, file_path

def set_file_info(self, value: str) -> None:
Expand All @@ -129,7 +135,6 @@ async def get(self, *args, **kwargs):
return await super().get(*args, **kwargs)

async def open(self, path):
self._id2path = self.settings.get("fileid2path", lambda x: x)
ystore_class = self.settings["collaborative_ystore_class"]
if self.websocket_server is None:
YDocWebSocketHandler.websocket_server = JupyterWebsocketServer(
Expand Down Expand Up @@ -275,3 +280,31 @@ async def maybe_save_document(self):

def check_origin(self, origin):
return True


class YDocRoomIdHandler(APIHandler):
auth_resource = "contents"

@web.authenticated
@authorized
async def get(self, path):
file_id_manager = self.settings.get("file_id_manager")
if file_id_manager is None:
# no file ID manager installed, the ID is the path
return self.finish(path)

idx = file_id_manager.get_id(path)
if idx is not None:
# index already exists
self.set_status(200)
return self.finish(str(idx))

# try indexing
idx = file_id_manager.index(path)
if idx is None:
# file does not exists
raise web.HTTPError(404, f"File {path!r} does not exist")

# index successfully created
self.set_status(201)
return self.finish(str(idx))

0 comments on commit 0053f1b

Please sign in to comment.