From c98910e1c5e9f221bc08b6facc2ec66124d3809d Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sat, 30 Oct 2021 01:40:20 -0400 Subject: [PATCH] PadMessageHandler: Populate session info as early as possible --- src/node/handler/PadMessageHandler.js | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/node/handler/PadMessageHandler.js b/src/node/handler/PadMessageHandler.js index 38c359a39..da8e97177 100644 --- a/src/node/handler/PadMessageHandler.js +++ b/src/node/handler/PadMessageHandler.js @@ -806,10 +806,13 @@ const handleClientReady = async (socket, message) => { // Check if the user has already disconnected. if (sessionInfo == null) return; - await hooks.aCallAll('clientReady', message); - - // Get ro/rw id:s const padIds = await readOnlyManager.getIds(sessionInfo.auth.padID); + sessionInfo.padId = padIds.padId; + sessionInfo.readOnlyPadId = padIds.readOnlyPadId; + sessionInfo.readonly = + padIds.readonly || !webaccess.userCanModify(sessionInfo.auth.padID, socket.client.request); + + await hooks.aCallAll('clientReady', message); // get all authordata of this new user assert(sessionInfo.author); @@ -817,7 +820,7 @@ const handleClientReady = async (socket, message) => { await authorManager.getAuthor(sessionInfo.author); // load the pad-object from the database - const pad = await padManager.getPad(padIds.padId); + const pad = await padManager.getPad(sessionInfo.padId); // these db requests all need the pad object (timestamp of latest revision, author data) const authors = pad.getAllAuthors(); @@ -840,9 +843,8 @@ const handleClientReady = async (socket, message) => { // glue the clientVars together, send them and tell the other clients that a new one is there - // Check that the client is still here. It might have disconnected between callbacks. - const sessionInfo = sessioninfos[socket.id]; - if (sessionInfo == null) return; + // Check if the user has disconnected during any of the above awaits. + if (sessionInfo !== sessioninfos[socket.id]) return; // Check if this author is already on the pad, if yes, kick the other sessions! const roomSockets = _getRoomSockets(pad.id); @@ -854,17 +856,11 @@ const handleClientReady = async (socket, message) => { if (sinfo && sinfo.author === sessionInfo.author) { // fix user's counter, works on page refresh or if user closes browser window and then rejoins sessioninfos[otherSocket.id] = {}; - otherSocket.leave(padIds.padId); + otherSocket.leave(sessionInfo.padId); otherSocket.json.send({disconnect: 'userdup'}); } } - // Save in sessioninfos that this session belonges to this pad - sessionInfo.padId = padIds.padId; - sessionInfo.readOnlyPadId = padIds.readOnlyPadId; - sessionInfo.readonly = - padIds.readonly || !webaccess.userCanModify(sessionInfo.auth.padID, socket.client.request); - const {session: {user} = {}} = socket.client.request; /* eslint-disable prefer-template -- it doesn't support breaking across multiple lines */ accessLogger.info(`[${pad.head > 0 ? 'ENTER' : 'CREATE'}]` +