diff --git a/src/node/handler/PadMessageHandler.js b/src/node/handler/PadMessageHandler.js index 899991a11..a1194ea91 100644 --- a/src/node/handler/PadMessageHandler.js +++ b/src/node/handler/PadMessageHandler.js @@ -1121,72 +1121,61 @@ const handleClientReady = async (socket, message, authorID) => { // Save the current revision in sessioninfos, should be the same as in clientVars sessionInfo.rev = pad.getHeadRevisionNumber(); + } - // prepare the notification for the other users on the pad, that this user joined - const messageToTheOtherUsers = { + // Notify other users about this new user. + socket.broadcast.to(padIds.padId).json.send({ + type: 'COLLABROOM', + data: { + type: 'USER_NEWINFO', + userInfo: { + colorId: authorColorId, + name: authorName, + userId: authorID, + }, + }, + }); + + // Notify this new user about other users. + await Promise.all(_getRoomSockets(pad.id).map(async (roomSocket) => { + if (roomSocket.id === socket.id) return; + + // sessioninfos might change while enumerating, so check if the sessionID is still assigned to a + // valid session. + const sessionInfo = sessioninfos[roomSocket.id]; + if (sessionInfo == null) return; + + // get the authorname & colorId + const authorId = sessionInfo.author; + // The authorId of this other user might be unknown if the other user just connected and has + // not yet sent a CLIENT_READY message. + if (authorId == null) return; + + // reuse previously created cache of author's data + const authorInfo = historicalAuthorData[authorId] || await authorManager.getAuthor(authorId); + if (authorInfo == null) { + messageLogger.error( + `Author ${authorId} connected via socket.io session ${roomSocket.id} is missing from ` + + 'the global author database. This should never happen because the author ID is ' + + 'generated by the same code that adds the author to the database.'); + // Don't bother telling the new user about this mystery author. + return; + } + + const msg = { type: 'COLLABROOM', data: { type: 'USER_NEWINFO', userInfo: { - colorId: authorColorId, - userId: authorID, + colorId: authorInfo.colorId, + name: authorInfo.name, + userId: authorId, }, }, }; - // Add the authorname of this new User, if avaiable - if (authorName != null) { - messageToTheOtherUsers.data.userInfo.name = authorName; - } - - // notify all existing users about new user - socket.broadcast.to(padIds.padId).json.send(messageToTheOtherUsers); - - // Get sessions for this pad and update them (in parallel) - await Promise.all(_getRoomSockets(pad.id).map(async (roomSocket) => { - // Jump over, if this session is the connection session - if (roomSocket.id === socket.id) { - return; - } - - // Since sessioninfos might change while being enumerated, check if the - // sessionID is still assigned to a valid session - const sessionInfo = sessioninfos[roomSocket.id]; - if (sessionInfo == null) return; - - // get the authorname & colorId - const authorId = sessionInfo.author; - // The authorId of this other user might be unknown if the other user just connected and has - // not yet sent a CLIENT_READY message. - if (authorId == null) return; - - // reuse previously created cache of author's data - const authorInfo = historicalAuthorData[authorId] || await authorManager.getAuthor(authorId); - if (authorInfo == null) { - messageLogger.error( - `Author ${authorId} connected via socket.io session ${roomSocket.id} is missing from ` + - 'the global author database. This should never happen because the author ID is ' + - 'generated by the same code that adds the author to the database.'); - // Don't bother telling the new user about this mystery author. - return; - } - - // Send the new User a Notification about this other user - const msg = { - type: 'COLLABROOM', - data: { - type: 'USER_NEWINFO', - userInfo: { - colorId: authorInfo.colorId, - name: authorInfo.name, - userId: authorId, - }, - }, - }; - - socket.json.send(msg); - })); - } + socket.json.send(msg); + })); }; /**