Skip to content

Commit fcab638

Browse files
committed
Allow to request a document from its document_id/room_id
1 parent 694f1e4 commit fcab638

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

projects/jupyter-server-ydoc/jupyter_server_ydoc/app.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,21 +136,35 @@ def initialize_handlers(self):
136136
async def get_document(
137137
self: YDocExtension,
138138
*,
139-
path: str,
140-
content_type: Literal["notebook", "file"],
141-
file_format: Literal["json", "text"],
139+
path: str | None = None,
140+
content_type: str | None = None,
141+
file_format: Literal["json", "text"] | None = None,
142+
document_id: str | None = None,
142143
copy: bool = True,
143144
) -> YBaseDoc | None:
144145
"""Get a view of the shared model for the matching document.
145146
147+
You need to provide either a ``document_id`` or the ``path``,
148+
the ``content_type`` and the ``file_format``.
149+
146150
If `copy=True`, the returned shared model is a fork, meaning that any changes
147151
made to it will not be propagated to the shared model used by the application.
148152
"""
149-
file_id_manager = self.serverapp.web_app.settings["file_id_manager"]
150-
file_id = file_id_manager.index(path)
151-
152-
encoded_path = encode_file_path(file_format, content_type, file_id)
153-
room_id = room_id_from_encoded_path(encoded_path)
153+
error_msg = "You need to provide either a ``document_id`` or the ``path``, the ``content_type`` and the ``file_format``."
154+
if document_id is None:
155+
if path is None or content_type is None or file_format is None:
156+
raise ValueError(error_msg)
157+
158+
file_id_manager = self.serverapp.web_app.settings["file_id_manager"]
159+
file_id = file_id_manager.index(path)
160+
161+
encoded_path = encode_file_path(file_format, content_type, file_id)
162+
room_id = room_id_from_encoded_path(encoded_path)
163+
164+
elif path is not None or content_type is not None or file_format is not None:
165+
raise ValueError(error_msg)
166+
else:
167+
room_id = document_id
154168

155169
try:
156170
room = await self.ywebsocket_server.get_room(room_id)
@@ -164,7 +178,7 @@ async def get_document(
164178
fork_ydoc = Doc()
165179
fork_ydoc.apply_update(update)
166180

167-
return YDOCS.get(content_type, YDOCS["file"])(fork_ydoc)
181+
return YDOCS.get(room.file_type, YDOCS["file"])(fork_ydoc)
168182
else:
169183
return room._document
170184

projects/jupyter-server-ydoc/jupyter_server_ydoc/rooms.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ def __init__(
5353
self._document.observe(self._on_document_change)
5454
self._file.observe(self.room_id, self._on_outofband_change)
5555

56+
@property
57+
def file_format(self) -> str:
58+
"""Document file format."""
59+
return self._file_format
60+
61+
@property
62+
def file_type(self) -> str:
63+
"""Document file type."""
64+
return self._file_type
65+
5666
@property
5767
def room_id(self) -> str:
5868
"""

0 commit comments

Comments
 (0)