From 1ca16816c2cceaccc89d165d866473f5b358f97c Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Thu, 17 Oct 2019 13:53:29 +0300 Subject: [PATCH] Synchronize open channel on client on reconnection --- client/js/socket-events/auth.js | 12 ++++++++---- src/server.js | 20 ++++++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/client/js/socket-events/auth.js b/client/js/socket-events/auth.js index 6ce7e47c..3fd5e66d 100644 --- a/client/js/socket-events/auth.js +++ b/client/js/socket-events/auth.js @@ -79,15 +79,19 @@ socket.on("auth", function(data) { for (const network of vueApp.networks) { for (const chan of network.channels) { - for (const msg of chan.messages) { - if (msg.id > lastMessage) { - lastMessage = msg.id; + if (chan.messages.length > 0) { + const id = chan.messages[chan.messages.length - 1].id; + + if (lastMessage < id) { + lastMessage = id; } } } } - socket.emit("auth", {user, token, lastMessage}); + const openChannel = (vueApp.activeChannel && vueApp.activeChannel.channel.id) || null; + + socket.emit("auth", {user, token, lastMessage, openChannel}); } } diff --git a/src/server.js b/src/server.js index 38f2e05b..51ea8d87 100644 --- a/src/server.js +++ b/src/server.js @@ -331,11 +331,23 @@ function indexRequest(req, res) { ); } -function initializeClient(socket, client, token, lastMessage) { +function initializeClient(socket, client, token, lastMessage, openChannel) { socket.emit("authorized"); client.clientAttach(socket.id, token); + // Client sends currently active channel on reconnect, + // pass it into `open` directly so it is verified and updated if necessary + if (openChannel) { + client.open(socket.id, openChannel); + + // If client provided channel passes checks, use it. if client has invalid + // channel open (or windows like settings) then use last known server active channel + openChannel = client.attachedClients[socket.id].openChannel || client.lastActiveChannel; + } else { + openChannel = client.lastActiveChannel; + } + if (Helper.config.fileUpload.enable) { new Uploader(socket); } @@ -624,9 +636,9 @@ function initializeClient(socket, client, token, lastMessage) { socket.emit("init", { applicationServerKey: manager.webPush.vapidKeys.publicKey, pushSubscription: client.config.sessions[token], - active: client.lastActiveChannel, + active: openChannel, networks: client.networks.map((network) => - network.getFilteredClone(client.lastActiveChannel, lastMessage) + network.getFilteredClone(openChannel, lastMessage) ), token: tokenToSend, }); @@ -705,7 +717,7 @@ function performAuthentication(data) { let token = null; const finalInit = () => { - initializeClient(socket, client, token, data.lastMessage || -1); + initializeClient(socket, client, token, data.lastMessage || -1, data.openChannel); if (!Helper.config.public) { client.manager.updateUser(client.name, {