2017-05-18 20:08:54 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const $ = require("jquery");
|
|
|
|
const socket = require("../socket");
|
|
|
|
const storage = require("../localStorage");
|
2019-11-02 19:40:59 +00:00
|
|
|
const {getActiveWindowComponent} = require("../vue");
|
|
|
|
const store = require("../store").default;
|
2019-11-03 11:23:03 +00:00
|
|
|
let lastServerHash = -1;
|
2017-05-18 20:08:54 +00:00
|
|
|
|
|
|
|
socket.on("auth", function(data) {
|
2017-08-28 20:06:28 +00:00
|
|
|
// If we reconnected and serverHash differs, that means the server restarted
|
2017-08-28 09:18:31 +00:00
|
|
|
// And we will reload the page to grab the latest version
|
2019-11-03 11:23:03 +00:00
|
|
|
if (lastServerHash > -1 && data.serverHash > -1 && data.serverHash !== lastServerHash) {
|
2017-08-28 09:18:31 +00:00
|
|
|
socket.disconnect();
|
2019-11-02 19:40:59 +00:00
|
|
|
store.commit("isConnected", false);
|
|
|
|
store.commit("currentUserVisibleError", "Server restarted, reloading…");
|
2017-08-28 09:18:31 +00:00
|
|
|
location.reload(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-27 17:39:16 +00:00
|
|
|
if (data.serverHash > -1) {
|
2019-11-03 11:23:03 +00:00
|
|
|
lastServerHash = data.serverHash;
|
2017-11-27 17:39:16 +00:00
|
|
|
} else {
|
2019-02-18 09:18:32 +00:00
|
|
|
getActiveWindowComponent().inFlight = false;
|
2017-11-27 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let token;
|
|
|
|
const user = storage.get("user");
|
2017-05-18 20:08:54 +00:00
|
|
|
|
|
|
|
if (!data.success) {
|
2019-11-02 19:40:59 +00:00
|
|
|
if (store.state.activeWindow !== "SignIn") {
|
2017-08-28 09:18:31 +00:00
|
|
|
socket.disconnect();
|
2019-11-02 19:40:59 +00:00
|
|
|
store.commit("isConnected", false);
|
|
|
|
store.commit("currentUserVisibleError", "Authentication failed, reloading…");
|
2017-08-28 09:18:31 +00:00
|
|
|
location.reload();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-18 20:08:54 +00:00
|
|
|
storage.remove("token");
|
|
|
|
|
2019-02-18 09:18:32 +00:00
|
|
|
getActiveWindowComponent().errorShown = true;
|
2017-06-21 07:58:29 +00:00
|
|
|
} else if (user) {
|
2017-05-18 20:08:54 +00:00
|
|
|
token = storage.get("token");
|
2017-08-28 09:18:31 +00:00
|
|
|
|
2017-05-18 20:08:54 +00:00
|
|
|
if (token) {
|
2019-11-02 19:40:59 +00:00
|
|
|
store.commit("currentUserVisibleError", "Authorizing…");
|
|
|
|
$("#loading-page-message").text(store.state.currentUserVisibleError);
|
2018-07-15 20:23:49 +00:00
|
|
|
|
|
|
|
let lastMessage = -1;
|
|
|
|
|
2019-11-02 19:40:59 +00:00
|
|
|
for (const network of store.state.networks) {
|
2018-07-15 20:23:49 +00:00
|
|
|
for (const chan of network.channels) {
|
2019-10-17 10:53:29 +00:00
|
|
|
if (chan.messages.length > 0) {
|
|
|
|
const id = chan.messages[chan.messages.length - 1].id;
|
|
|
|
|
|
|
|
if (lastMessage < id) {
|
|
|
|
lastMessage = id;
|
2018-07-15 20:23:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-02 19:40:59 +00:00
|
|
|
const openChannel =
|
|
|
|
(store.state.activeChannel && store.state.activeChannel.channel.id) || null;
|
2019-10-17 10:53:29 +00:00
|
|
|
|
|
|
|
socket.emit("auth", {user, token, lastMessage, openChannel});
|
2017-05-18 20:08:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-21 07:58:29 +00:00
|
|
|
|
2018-07-08 08:52:05 +00:00
|
|
|
$("#loading").remove();
|
2017-09-12 12:52:16 +00:00
|
|
|
$("#footer")
|
|
|
|
.find(".sign-in")
|
2017-05-18 20:08:54 +00:00
|
|
|
.trigger("click", {
|
|
|
|
pushState: false,
|
2017-09-12 12:52:16 +00:00
|
|
|
});
|
2017-05-18 20:08:54 +00:00
|
|
|
});
|