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

175 lines
5.2 KiB
JavaScript
Raw Normal View History

2017-05-18 22:08:54 +02:00
"use strict";
const socket = require("../socket");
2017-07-10 21:47:03 +02:00
const webpush = require("../webpush");
2017-05-18 22:08:54 +02:00
const storage = require("../localStorage");
2019-10-17 18:56:44 +02:00
const constants = require("../constants");
const {vueApp, initChannel} = require("../vue");
2019-11-11 20:18:55 +01:00
const {switchToChannel, navigate} = require("../router");
2019-11-03 16:56:41 +01:00
const router = require("../router");
const store = require("../store").default;
2017-05-18 22:08:54 +02:00
socket.on("init", function(data) {
store.commit("networks", mergeNetworkData(data.networks));
store.commit("isConnected", true);
store.commit("currentUserVisibleError", null);
2018-07-08 19:53:23 +02:00
2019-11-05 20:29:51 +01:00
if (!store.state.appLoaded) {
2019-11-03 16:56:41 +01:00
router.initialize();
2019-11-05 20:29:51 +01:00
store.commit("appLoaded");
2018-07-15 22:23:49 +02:00
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
const viewportWidth = window.outerWidth;
let isUserlistOpen = storage.get("thelounge.state.userlist");
2019-10-17 18:56:44 +02:00
if (viewportWidth > constants.mobileViewportPixels) {
store.commit("sidebarOpen", 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";
}
store.commit("userlistOpen", isUserlistOpen === "true");
2019-11-05 20:29:51 +01:00
document.body.classList.remove("signed-out");
2017-12-01 19:04:50 +01:00
2019-11-05 20:29:51 +01:00
if (window.g_TheLoungeRemoveLoading) {
window.g_TheLoungeRemoveLoading();
2017-12-01 19:04:50 +01:00
}
2019-11-03 18:09:10 +01:00
if (!vueApp.$route.name || vueApp.$route.name === "SignIn") {
const channel = store.getters.findChannel(data.active);
if (channel) {
2019-11-11 20:18:55 +01:00
switchToChannel(channel.channel);
2019-11-03 18:09:10 +01:00
} else if (store.state.networks.length > 0) {
// Server is telling us to open a channel that does not exist
// For example, it can be unset if you first open the page after server start
2019-11-11 20:18:55 +01:00
switchToChannel(store.state.networks[0].channels[0]);
2019-11-03 18:09:10 +01:00
} else {
2019-11-11 20:18:55 +01:00
navigate("Connect");
2019-11-03 18:09:10 +01:00
}
}
2017-08-28 17:03:27 +02:00
}
2017-05-18 22:08:54 +02:00
2019-10-20 16:45:38 +02:00
if (document.body.classList.contains("public")) {
window.addEventListener(
"beforeunload",
() => "Are you sure you want to navigate away from this page?"
);
}
2017-05-18 22:08:54 +02:00
});
2017-08-28 22:06:28 +02: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 = store.getters.findNetwork(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 11:33:59 +02:00
currentNetwork.channels = mergeChannelData(
currentNetwork.channels,
network.channels
);
} 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 store.state.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") {
if (
store.state.activeChannel &&
store.state.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;
}
}
continue;
}
// Server sends total count of messages in memory, we compare it to amount of messages
// on the client, and decide whether theres more messages to load from server
if (key === "totalMessages") {
currentChannel.moreHistoryAvailable =
channel.totalMessages > currentChannel.messages.length;
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;
}