thelounge/client/js/socket-events/init.js

142 lines
4 KiB
JavaScript
Raw Normal View History

2017-05-18 22:08:54 +02:00
"use strict";
const $ = require("jquery");
2017-08-28 22:06:28 +02:00
const escape = require("css.escape");
2017-05-18 22:08:54 +02:00
const socket = require("../socket");
2017-07-10 21:47:03 +02:00
const webpush = require("../webpush");
const slideoutMenu = require("../slideout");
2017-05-18 22:08:54 +02:00
const sidebar = $("#sidebar");
const storage = require("../localStorage");
2017-08-28 17:03:27 +02:00
const utils = require("../utils");
const {vueApp, initChannel} = require("../vue");
2017-05-18 22:08:54 +02:00
socket.on("init", function(data) {
2018-09-09 15:09:19 +02:00
vueApp.currentUserVisibleError = "Rendering…";
$("#loading-page-message").text(vueApp.currentUserVisibleError);
2017-08-28 17:03:27 +02:00
const previousActive = vueApp.activeChannel && vueApp.activeChannel.channel.id;
2017-05-18 22:08:54 +02:00
2018-07-08 22:08:08 +02:00
const networks = new Set(JSON.parse(storage.get("thelounge.networks.collapsed")));
for (const network of data.networks) {
2018-07-19 19:19:45 +02:00
network.isCollapsed = networks.has(network.uuid);
network.channels.forEach(initChannel);
2018-07-15 22:23:49 +02:00
const currentNetwork = vueApp.networks.find((n) => n.uuid === network.uuid);
2018-07-19 19:19:45 +02:00
// If we are reconnecting, merge existing state variables because they don't exist on the server
if (!currentNetwork) {
network.isJoinChannelShown = false;
2018-07-15 22:23:49 +02:00
2018-07-19 19:19:45 +02:00
continue;
}
2018-07-15 22:23:49 +02:00
2018-07-19 19:19:45 +02:00
network.isJoinChannelShown = currentNetwork.isJoinChannelShown;
2018-07-17 10:03:35 +02:00
2018-07-19 19:19:45 +02:00
for (const channel of network.channels) {
const currentChannel = currentNetwork.channels.find((c) => c.id === channel.id);
2018-07-15 22:23:49 +02:00
2018-07-19 19:19:45 +02:00
if (!currentChannel) {
continue;
2018-07-15 22:23:49 +02:00
}
2018-07-12 20:33:52 +02:00
2018-07-19 19:19:45 +02:00
channel.scrolledToBottom = currentChannel.scrolledToBottom;
channel.pendingMessage = currentChannel.pendingMessage;
// Reconnection only sends new messages, so merge it on the client
// Only concat if server sent us less than 100 messages so we don't introduce gaps
if (currentChannel.messages && channel.messages.length < 100) {
2018-07-19 19:19:45 +02:00
channel.messages = currentChannel.messages.concat(channel.messages);
}
if (currentChannel.moreHistoryAvailable) {
channel.moreHistoryAvailable = true;
}
}
2018-07-08 22:08:08 +02:00
}
vueApp.networks = data.networks;
2018-09-09 15:09:19 +02:00
vueApp.isConnected = true;
vueApp.currentUserVisibleError = null;
2018-07-08 19:53:23 +02:00
2018-07-15 22:23:49 +02:00
if (!vueApp.initialized) {
vueApp.initialized = true;
2017-08-28 17:03:27 +02:00
if (data.token) {
storage.set("token", data.token);
}
2017-05-18 22:08:54 +02:00
2017-08-28 17:03:27 +02:00
webpush.configurePushNotifications(data.pushSubscription, data.applicationServerKey);
2017-07-10 21:47:03 +02:00
slideoutMenu.enable();
const viewport = $("#viewport");
const viewportWidth = $(window).outerWidth();
let isUserlistOpen = storage.get("thelounge.state.userlist");
if (viewportWidth > utils.mobileViewportPixels) {
slideoutMenu.toggle(storage.get("thelounge.state.sidebar") !== "false");
}
// If The Lounge is opened on a small screen (less than 1024px), and we don't have stored
// user list state, close it by default
if (viewportWidth >= 1024 && isUserlistOpen !== "true" && isUserlistOpen !== "false") {
isUserlistOpen = "true";
}
viewport.toggleClass("userlist-open", isUserlistOpen === "true");
$(document.body).removeClass("signed-out");
2017-08-28 17:03:27 +02:00
$("#loading").remove();
$("#sign-in").remove();
2017-12-01 19:04:50 +01:00
if (window.g_LoungeErrorHandler) {
window.removeEventListener("error", window.g_LoungeErrorHandler);
window.g_LoungeErrorHandler = null;
}
2017-08-28 17:03:27 +02:00
}
2017-05-18 22:08:54 +02:00
2018-07-08 15:42:54 +02:00
vueApp.$nextTick(() => openCorrectChannel(previousActive, data.active));
2018-07-12 20:33:52 +02:00
utils.confirmExit();
utils.synchronizeNotifiedState();
2017-05-18 22:08:54 +02:00
});
2017-08-28 22:06:28 +02:00
function openCorrectChannel(clientActive, serverActive) {
let target = $();
2017-08-28 22:06:28 +02:00
// Open last active channel
if (clientActive > 0) {
target = sidebar.find(`.chan[data-id="${clientActive}"]`);
2017-08-28 22:06:28 +02:00
}
// Open window provided in location.hash
if (target.length === 0 && window.location.hash) {
2017-12-25 01:17:26 +01:00
target = $(`[data-target="${escape(window.location.hash)}"]`).first();
2017-08-28 22:06:28 +02:00
}
// Open last active channel according to the server
if (serverActive > 0 && target.length === 0) {
target = sidebar.find(`.chan[data-id="${serverActive}"]`);
2017-08-28 22:06:28 +02:00
}
// Open first available channel
if (target.length === 0) {
target = sidebar.find(".chan").first();
}
2017-08-28 22:06:28 +02:00
// If target channel is found, open it
if (target.length > 0) {
2017-08-28 22:06:28 +02:00
target.trigger("click", {
replaceHistory: true,
2017-08-28 22:06:28 +02:00
});
return;
}
// Open the connect window
$("#footer .connect").trigger("click", {
pushState: false,
2017-08-28 22:06:28 +02:00
});
}