Skip to content

Commit

Permalink
bugfix: collab_client_vars's historicalAuthorData field don't being f…
Browse files Browse the repository at this point in the history
…illed if users connect before someone write in pad.
  • Loading branch information
CristianSilvaGrosseli authored and pedrobmarin committed Aug 24, 2020
1 parent 2ae6159 commit 3bc93aa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
42 changes: 31 additions & 11 deletions src/node/handler/PadMessageHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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];
Expand Down
3 changes: 3 additions & 0 deletions src/static/js/collab_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3bc93aa

Please sign in to comment.