2017-05-18 20:08:54 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const $ = require("jquery");
|
2017-08-28 20:06:28 +00:00
|
|
|
const escape = require("css.escape");
|
2017-05-18 20:08:54 +00:00
|
|
|
const socket = require("../socket");
|
2017-07-10 19:47:03 +00:00
|
|
|
const webpush = require("../webpush");
|
2018-03-06 11:29:28 +00:00
|
|
|
const slideoutMenu = require("../slideout");
|
2017-05-18 20:08:54 +00:00
|
|
|
const sidebar = $("#sidebar");
|
|
|
|
const storage = require("../localStorage");
|
2017-08-28 15:03:27 +00:00
|
|
|
const utils = require("../utils");
|
2018-07-18 18:40:09 +00:00
|
|
|
const {vueApp, initChannel} = require("../vue");
|
2017-05-18 20:08:54 +00:00
|
|
|
|
|
|
|
socket.on("init", function(data) {
|
2018-09-09 13:09:19 +00:00
|
|
|
vueApp.currentUserVisibleError = "Rendering…";
|
|
|
|
$("#loading-page-message").text(vueApp.currentUserVisibleError);
|
2017-08-28 15:03:27 +00:00
|
|
|
|
2018-07-17 08:21:08 +00:00
|
|
|
const previousActive = vueApp.activeChannel && vueApp.activeChannel.channel.id;
|
2017-05-18 20:08:54 +00:00
|
|
|
|
2019-07-15 19:26:32 +00:00
|
|
|
vueApp.networks = mergeNetworkData(data.networks);
|
2018-09-09 13:09:19 +00:00
|
|
|
vueApp.isConnected = true;
|
|
|
|
vueApp.currentUserVisibleError = null;
|
2018-07-08 17:53:23 +00:00
|
|
|
|
2018-07-15 20:23:49 +00:00
|
|
|
if (!vueApp.initialized) {
|
|
|
|
vueApp.initialized = true;
|
|
|
|
|
2017-08-28 15:03:27 +00:00
|
|
|
if (data.token) {
|
|
|
|
storage.set("token", data.token);
|
|
|
|
}
|
2017-05-18 20:08:54 +00:00
|
|
|
|
2017-08-28 15:03:27 +00:00
|
|
|
webpush.configurePushNotifications(data.pushSubscription, data.applicationServerKey);
|
2017-07-10 19:47:03 +00:00
|
|
|
|
2018-03-15 13:11:15 +00:00
|
|
|
slideoutMenu.enable();
|
|
|
|
|
|
|
|
const viewport = $("#viewport");
|
2018-06-27 07:05:37 +00:00
|
|
|
const viewportWidth = $(window).outerWidth();
|
|
|
|
let isUserlistOpen = storage.get("thelounge.state.userlist");
|
2018-03-15 13:11:15 +00:00
|
|
|
|
2018-09-18 19:27:35 +00:00
|
|
|
if (viewportWidth > utils.mobileViewportPixels) {
|
2018-06-28 07:45:40 +00:00
|
|
|
slideoutMenu.toggle(storage.get("thelounge.state.sidebar") !== "false");
|
2018-03-15 13:11:15 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 07:05:37 +00:00
|
|
|
// 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");
|
|
|
|
|
2018-02-23 19:22:05 +00:00
|
|
|
$(document.body).removeClass("signed-out");
|
2017-08-28 15:03:27 +00:00
|
|
|
$("#loading").remove();
|
|
|
|
$("#sign-in").remove();
|
2017-12-01 18:04:50 +00:00
|
|
|
|
|
|
|
if (window.g_LoungeErrorHandler) {
|
|
|
|
window.removeEventListener("error", window.g_LoungeErrorHandler);
|
|
|
|
window.g_LoungeErrorHandler = null;
|
|
|
|
}
|
2017-08-28 15:03:27 +00:00
|
|
|
}
|
2017-05-18 20:08:54 +00:00
|
|
|
|
2018-07-08 13:42:54 +00:00
|
|
|
vueApp.$nextTick(() => openCorrectChannel(previousActive, data.active));
|
2018-07-12 18:33:52 +00:00
|
|
|
|
|
|
|
utils.confirmExit();
|
2018-09-05 19:44:19 +00:00
|
|
|
utils.synchronizeNotifiedState();
|
2017-05-18 20:08:54 +00:00
|
|
|
});
|
2017-08-28 20:06:28 +00:00
|
|
|
|
|
|
|
function openCorrectChannel(clientActive, serverActive) {
|
2017-09-20 07:44:36 +00:00
|
|
|
let target = $();
|
2017-08-28 20:06:28 +00:00
|
|
|
|
|
|
|
// Open last active channel
|
|
|
|
if (clientActive > 0) {
|
2017-12-23 08:00:16 +00:00
|
|
|
target = sidebar.find(`.chan[data-id="${clientActive}"]`);
|
2017-08-28 20:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open window provided in location.hash
|
2017-09-20 07:44:36 +00:00
|
|
|
if (target.length === 0 && window.location.hash) {
|
2017-12-25 00:17:26 +00:00
|
|
|
target = $(`[data-target="${escape(window.location.hash)}"]`).first();
|
2017-08-28 20:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open last active channel according to the server
|
2017-09-20 07:44:36 +00:00
|
|
|
if (serverActive > 0 && target.length === 0) {
|
2017-12-23 08:00:16 +00:00
|
|
|
target = sidebar.find(`.chan[data-id="${serverActive}"]`);
|
2017-08-28 20:06:28 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 07:44:36 +00:00
|
|
|
// Open first available channel
|
|
|
|
if (target.length === 0) {
|
|
|
|
target = sidebar.find(".chan").first();
|
|
|
|
}
|
|
|
|
|
2017-08-28 20:06:28 +00:00
|
|
|
// If target channel is found, open it
|
2017-09-20 07:44:36 +00:00
|
|
|
if (target.length > 0) {
|
2017-08-28 20:06:28 +00:00
|
|
|
target.trigger("click", {
|
2017-11-15 06:35:15 +00:00
|
|
|
replaceHistory: true,
|
2017-08-28 20:06:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the connect window
|
|
|
|
$("#footer .connect").trigger("click", {
|
2017-11-15 06:35:15 +00:00
|
|
|
pushState: false,
|
2017-08-28 20:06:28 +00:00
|
|
|
});
|
|
|
|
}
|
2019-07-15 19:26:32 +00:00
|
|
|
|
|
|
|
function mergeNetworkData(newNetworks) {
|
|
|
|
const collapsedNetworks = new Set(JSON.parse(storage.get("thelounge.networks.collapsed")));
|
|
|
|
|
|
|
|
for (let n = 0; n < newNetworks.length; n++) {
|
|
|
|
const network = newNetworks[n];
|
|
|
|
const currentNetwork = vueApp.networks.find((net) => net.uuid === network.uuid);
|
|
|
|
|
|
|
|
// If this network is new, set some default variables and initalize channel variables
|
|
|
|
if (!currentNetwork) {
|
|
|
|
network.isJoinChannelShown = false;
|
|
|
|
network.isCollapsed = collapsedNetworks.has(network.uuid);
|
|
|
|
network.channels.forEach(initChannel);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge received network object into existing network object on the client
|
|
|
|
// so the object reference stays the same (e.g. for vueApp.currentChannel)
|
|
|
|
for (const key in network) {
|
|
|
|
if (!Object.prototype.hasOwnProperty.call(network, key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Channels require extra care to be merged correctly
|
|
|
|
if (key === "channels") {
|
2019-07-17 09:33:59 +00:00
|
|
|
currentNetwork.channels = mergeChannelData(
|
|
|
|
currentNetwork.channels,
|
|
|
|
network.channels
|
|
|
|
);
|
2019-07-15 19:26:32 +00:00
|
|
|
} else {
|
|
|
|
currentNetwork[key] = network[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newNetworks[n] = currentNetwork;
|
|
|
|
}
|
|
|
|
|
|
|
|
return newNetworks;
|
|
|
|
}
|
|
|
|
|
|
|
|
function mergeChannelData(oldChannels, newChannels) {
|
|
|
|
for (let c = 0; c < newChannels.length; c++) {
|
|
|
|
const channel = newChannels[c];
|
|
|
|
const currentChannel = oldChannels.find((chan) => chan.id === channel.id);
|
|
|
|
|
|
|
|
// This is a new channel that was joined while client was disconnected, initialize it
|
|
|
|
if (!currentChannel) {
|
|
|
|
initChannel(channel);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge received channel object into existing currentChannel
|
|
|
|
// so the object references are exactly the same (e.g. in vueApp.activeChannel)
|
|
|
|
for (const key in channel) {
|
|
|
|
if (!Object.prototype.hasOwnProperty.call(channel, key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Server sends an empty users array, client requests it whenever needed
|
|
|
|
if (key === "users") {
|
|
|
|
if (channel.type === "channel") {
|
2019-10-15 17:33:03 +00:00
|
|
|
if (vueApp.activeChannel && vueApp.activeChannel.channel === currentChannel) {
|
|
|
|
// For currently open channel, request the user list straight away
|
|
|
|
socket.emit("names", {
|
|
|
|
target: channel.id,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// For all other channels, mark the user list as outdated
|
|
|
|
// so an update will be requested whenever user switches to these channels
|
|
|
|
currentChannel.usersOutdated = true;
|
|
|
|
}
|
2019-07-15 19:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 (key === "messages" && currentChannel.messages && channel.messages.length < 100) {
|
|
|
|
currentChannel.messages = currentChannel.messages.concat(channel.messages);
|
|
|
|
} else {
|
|
|
|
currentChannel[key] = channel[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newChannels[c] = currentChannel;
|
|
|
|
}
|
|
|
|
|
|
|
|
return newChannels;
|
|
|
|
}
|