From c42fc55c6f5bce96521e2d9194ab43bd8dc7d8e2 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Sun, 15 Jul 2018 23:23:49 +0300 Subject: [PATCH] Fix reconnection state --- client/js/socket-events/auth.js | 15 +++++++++++++- client/js/socket-events/init.js | 35 ++++++++++++++++++++++++--------- client/js/socket-events/msg.js | 4 ---- client/js/utils.js | 2 -- client/js/vue.js | 1 + 5 files changed, 41 insertions(+), 16 deletions(-) diff --git a/client/js/socket-events/auth.js b/client/js/socket-events/auth.js index 8f527a82..57824bf0 100644 --- a/client/js/socket-events/auth.js +++ b/client/js/socket-events/auth.js @@ -5,6 +5,7 @@ const socket = require("../socket"); const storage = require("../localStorage"); const utils = require("../utils"); const templates = require("../../views"); +const {vueApp} = require("../vue"); socket.on("auth", function(data) { // If we reconnected and serverHash differs, that means the server restarted @@ -67,7 +68,19 @@ socket.on("auth", function(data) { if (token) { $("#loading-page-message, #connection-error").text("Authorizing…"); - const lastMessage = utils.lastMessageId; + + let lastMessage = -1; + + 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; + } + } + } + } + socket.emit("auth", {user, token, lastMessage}); } } diff --git a/client/js/socket-events/init.js b/client/js/socket-events/init.js index be1dc302..504111de 100644 --- a/client/js/socket-events/init.js +++ b/client/js/socket-events/init.js @@ -13,18 +13,33 @@ const {vueApp} = require("../vue"); socket.on("init", function(data) { $("#loading-page-message, #connection-error").text("Rendering…"); - const lastMessageId = utils.lastMessageId; - let previousActive = 0; - - if (lastMessageId > -1) { - previousActive = sidebar.find(".active").data("id"); - } + let previousActive = vueApp.activeChannel && vueApp.activeChannel.channel.id; const networks = new Set(JSON.parse(storage.get("thelounge.networks.collapsed"))); for (const network of data.networks) { - network.isJoinChannelShown = false; - network.isCollapsed = networks.has(network.uuid); + const currentNetwork = vueApp.networks.find((n) => n.uuid === network.uuid); + + // TODO: Make this updating more efficient + if (currentNetwork) { + network.isJoinChannelShown = currentNetwork.isJoinChannelShown; + network.isCollapsed = currentNetwork.isCollapsed; + + for (const channel of network.channels) { + const currentChannel = currentNetwork.channels.find((c) => c.id === channel.id); + + if (currentChannel && currentChannel.messages) { + channel.messages = currentChannel.messages.concat(channel.messages); + + if (currentChannel.moreHistoryAvailable) { + channel.moreHistoryAvailable = true; + } + } + } + } else { + network.isJoinChannelShown = false; + network.isCollapsed = networks.has(network.uuid); + } for (const channel of network.channels) { if (channel.type === "channel") { @@ -38,7 +53,9 @@ socket.on("init", function(data) { $("#connection-error").removeClass("shown"); - if (lastMessageId < 0) { + if (!vueApp.initialized) { + vueApp.initialized = true; + if (data.token) { storage.set("token", data.token); } diff --git a/client/js/socket-events/msg.js b/client/js/socket-events/msg.js index b6c8c8d3..bbc94cca 100644 --- a/client/js/socket-events/msg.js +++ b/client/js/socket-events/msg.js @@ -20,10 +20,6 @@ try { } socket.on("msg", function(data) { - if (utils.lastMessageId < data.msg.id) { - utils.lastMessageId = data.msg.id; - } - let targetId = data.chan; let {channel} = findChannel(data.chan); diff --git a/client/js/utils.js b/client/js/utils.js index cbd50c05..71bb5815 100644 --- a/client/js/utils.js +++ b/client/js/utils.js @@ -6,14 +6,12 @@ const viewport = $("#viewport"); const {vueApp} = require("./vue"); var serverHash = -1; // eslint-disable-line no-var -var lastMessageId = -1; // eslint-disable-line no-var module.exports = { // Same value as media query in CSS that forces sidebars to become overlays mobileViewportPixels: 768, findCurrentNetworkChan, serverHash, - lastMessageId, confirmExit, scrollIntoViewNicely, hasRoleInChannel, diff --git a/client/js/vue.js b/client/js/vue.js index 2c608b2f..8a693086 100644 --- a/client/js/vue.js +++ b/client/js/vue.js @@ -21,6 +21,7 @@ Vue.filter("roundBadgeNumber", roundBadgeNumber); const vueApp = new Vue({ el: "#viewport", data: { + initialized: false, connected: false, appName: document.title, activeChannel: null,