From 3bc93aa636573ebbb9c5244438989e239afbc1c7 Mon Sep 17 00:00:00 2001 From: CristianSilvaGrosseli Date: Wed, 19 Aug 2020 19:28:31 -0300 Subject: [PATCH] bugfix: collab_client_vars's historicalAuthorData field don't being filled if users connect before someone write in pad. --- src/node/handler/PadMessageHandler.js | 42 ++++++++++++++++++++------- src/static/js/collab_client.js | 3 ++ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/node/handler/PadMessageHandler.js b/src/node/handler/PadMessageHandler.js index ffbb74ea8da..3678050607f 100644 --- a/src/node/handler/PadMessageHandler.js +++ b/src/node/handler/PadMessageHandler.js @@ -149,6 +149,18 @@ exports.handleDisconnect = async function(client) // Go through all user that are still on the pad, and send them the USER_LEAVE message client.broadcast.to(session.padId).json.send(messageToTheOtherUsers); + // If session has this user's writings, update historicalAuthorData of the users that are still connected + let historicalAuthorData = await _getHistoricalAuthorData(session.padId); + if (historicalAuthorData && _.contains(_.keys(historicalAuthorData), session.author)) { + let clientVars = { + "type": "HISTORICAL_AUTHOR_DATA", + "collab_client_vars": { + "historicalAuthorData": historicalAuthorData + } + } + client.broadcast.to(session.padId).json.send({type: "COLLABROOM", data: clientVars}); + } + // Allow plugins to hook into users leaving the pad hooks.callAll("userLeave", session); } @@ -927,17 +939,7 @@ async function handleClientReady(client, message) // get timestamp of latest revision needed for timeslider let currentTime = await pad.getRevisionDate(pad.getHeadRevisionNumber()); - // get all author data out of the database (in parallel) - let historicalAuthorData = {}; - await Promise.all(authors.map(authorId => { - return authorManager.getAuthor(authorId).then(author => { - if (!author) { - messageLogger.error("There is no author for authorId:", authorId); - } else { - historicalAuthorData[authorId] = { name: author.name, colorId: author.colorId }; // Filter author attribs (e.g. don't send author's pads to all clients) - } - }); - })); + let historicalAuthorData = await _getHistoricalAuthorData(pad.id); // glue the clientVars together, send them and tell the other clients that a new one is there @@ -1424,6 +1426,24 @@ async function composePadChangesets (padId, startNum, endNum) } } +// Get all author data out of the database (in parallel) +async function _getHistoricalAuthorData(padId) { + let pad = await padManager.getPad(padId); + let authors = pad.getAllAuthors(); + let historicalAuthorData = {}; + + await Promise.all(authors.map(authorId => { + return authorManager.getAuthor(authorId).then(author => { + if (!author) { + messageLogger.error("There is no author for authorId:", authorId); + } else { + historicalAuthorData[authorId] = { name: author.name, color: author.colorId }; // Filter author attribs (e.g. don't send author's pads to all clients) + } + }); + })); + return historicalAuthorData; +} + function _getRoomClients(padID) { var roomClients = []; var room = socketio.sockets.adapter.rooms[padID]; diff --git a/src/static/js/collab_client.js b/src/static/js/collab_client.js index 86148a81003..6e4ae973bc1 100644 --- a/src/static/js/collab_client.js +++ b/src/static/js/collab_client.js @@ -492,6 +492,9 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad) { callbacks.onServerMessage(msg.payload); } + else if(msg.type == "HISTORICAL_AUTHOR_DATA") { + clientVars.collab_client_vars.historicalAuthorData = msg.collab_client_vars.historicalAuthorData; + } //HACKISH: User messages do not have "payload" but "userInfo", so that all "handleClientMessage_USER_" hooks would work, populate payload //FIXME: USER_* messages to have "payload" property instead of "userInfo", seems like a quite a big work