diff --git a/plugs/collab/collab.ts b/plugs/collab/collab.ts index 31b1493f..e334ccd1 100644 --- a/plugs/collab/collab.ts +++ b/plugs/collab/collab.ts @@ -172,6 +172,7 @@ const clientId = store.get("collabClientId").then(async (clientId) => { return clientId; }); +let lastCollabPage: string | undefined; let currentCollabId: string | undefined; const localCollabServer = location.protocol === "http:" @@ -189,16 +190,18 @@ async function ping() { if (!collabId && currentCollabId) { // Stop collab console.log("Stopping collab"); - editor.flashNotification( - "Other users have left this page, switched back to single-user mode.", - ); + if (lastCollabPage === currentPage) { + editor.flashNotification( + "Other users have left this page, switched back to single-user mode.", + ); + } currentCollabId = undefined; await collab.stop(); } else if (collabId && collabId !== currentCollabId) { // Start collab console.log("Starting collab"); editor.flashNotification( - "Another device has joined this page, switched to multi-user mode.", + "Opening page in multi-user mode.", ); currentCollabId = collabId; await collab.start( @@ -207,6 +210,9 @@ async function ping() { "you", ); } + if (currentCollabId) { + lastCollabPage = currentPage; + } } catch (e: any) { // console.error("Ping error", e); if (e.message.includes("Failed to fetch") && currentCollabId) { diff --git a/server/collab.ts b/server/collab.ts index 46837974..e06caaa0 100644 --- a/server/collab.ts +++ b/server/collab.ts @@ -111,7 +111,20 @@ export class CollabServer { quiet: true, onLoadDocument: async (doc) => { console.log("[Hocuspocus]", "Requesting doc load", doc.documentName); - const pageName = doc.documentName.split("/").slice(1).join("/"); + const [collabId, ...pageNamePieces] = doc.documentName.split("/"); + const pageName = pageNamePieces.join("/"); + const collabPage = this.pages.get(pageName); + if (!collabPage || collabPage.collabId !== collabId) { + // This can happen after a server restart, where old clients are still trying to continue on an old session + // This will self-correct when the client discovers that the collabId has changed + // Until then: HARD PASS (meaning: don't send a document) + console.warn( + "[Hocuspocus]", + "Client tried to connect to old session", + doc.documentName, + ); + return; + } try { const yText = doc.document.getText("codemirror"); const { data } = await this.spacePrimitives.readFile(