2017-11-04 17:32:18 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const $ = require("jquery");
|
|
|
|
const socket = require("../socket");
|
2017-11-07 20:22:16 +00:00
|
|
|
const webpush = require("../webpush");
|
2018-09-03 07:30:05 +00:00
|
|
|
const upload = require("../upload");
|
2019-11-02 19:40:59 +00:00
|
|
|
const store = require("../store").default;
|
2019-11-08 18:50:28 +00:00
|
|
|
const {vueApp} = require("../vue");
|
2017-11-04 17:32:18 +00:00
|
|
|
|
2018-06-05 12:29:43 +00:00
|
|
|
window.addEventListener("beforeinstallprompt", (installPromptEvent) => {
|
|
|
|
$("#webapp-install-button")
|
|
|
|
.on("click", function() {
|
|
|
|
if (installPromptEvent && installPromptEvent.prompt) {
|
|
|
|
installPromptEvent.prompt();
|
|
|
|
}
|
|
|
|
|
|
|
|
$(this).prop("hidden", true);
|
|
|
|
})
|
|
|
|
.prop("hidden", false);
|
2018-08-18 19:48:58 +00:00
|
|
|
|
|
|
|
$("#native-app").prop("hidden", false);
|
2018-06-05 12:29:43 +00:00
|
|
|
});
|
|
|
|
|
2019-11-05 12:30:45 +00:00
|
|
|
socket.once("configuration", function(data) {
|
2019-11-02 19:40:59 +00:00
|
|
|
store.commit("isFileUploadEnabled", data.fileUpload);
|
|
|
|
store.commit("serverConfiguration", data);
|
2017-11-04 17:32:18 +00:00
|
|
|
|
2019-11-05 20:51:55 +00:00
|
|
|
// 'theme' setting depends on serverConfiguration.themes so
|
|
|
|
// settings cannot be applied before this point
|
|
|
|
store.dispatch("settings/applyAll");
|
|
|
|
|
2018-09-03 07:30:05 +00:00
|
|
|
if (data.fileUpload) {
|
2019-10-21 15:52:46 +00:00
|
|
|
upload.initialize(data.fileUploadMaxFileSize);
|
2018-09-03 07:30:05 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 20:51:55 +00:00
|
|
|
socket.emit("setting:get");
|
2017-11-07 20:22:16 +00:00
|
|
|
webpush.initialize();
|
|
|
|
|
2018-04-02 04:25:45 +00:00
|
|
|
// If localStorage contains a theme that does not exist on this server, switch
|
|
|
|
// back to its default theme.
|
2019-11-03 18:05:19 +00:00
|
|
|
const currentTheme = data.themes.find((t) => t.name === store.state.settings.theme);
|
2019-07-22 16:50:04 +00:00
|
|
|
|
|
|
|
if (currentTheme === undefined) {
|
2019-11-05 20:51:55 +00:00
|
|
|
store.commit("settings/update", {name: "theme", value: data.defaultTheme, sync: true});
|
2019-07-22 16:50:04 +00:00
|
|
|
} else if (currentTheme.themeColor) {
|
|
|
|
document.querySelector('meta[name="theme-color"]').content = currentTheme.themeColor;
|
2018-04-02 04:25:45 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 15:00:07 +00:00
|
|
|
if ("URLSearchParams" in window) {
|
2018-05-28 07:16:00 +00:00
|
|
|
const params = new URLSearchParams(document.location.search);
|
2018-04-22 18:28:00 +00:00
|
|
|
|
2019-11-08 18:50:28 +00:00
|
|
|
const cleanParams = () => {
|
|
|
|
// Remove query parameters from url without reloading the page
|
|
|
|
const cleanUri =
|
|
|
|
window.location.origin + window.location.pathname + window.location.hash;
|
|
|
|
window.history.replaceState({}, document.title, cleanUri);
|
|
|
|
};
|
|
|
|
|
2018-06-19 15:00:07 +00:00
|
|
|
if (params.has("uri")) {
|
2019-11-08 18:50:28 +00:00
|
|
|
// Set default connection settings from IRC protocol links
|
|
|
|
const uri =
|
|
|
|
params.get("uri") +
|
|
|
|
(location.hash.includes("#/") ? location.hash.split("#/")[0] : location.hash);
|
|
|
|
const queryParams = parseIrcUri(uri, data);
|
|
|
|
cleanParams();
|
|
|
|
vueApp.$router.push({path: "/connect", query: queryParams});
|
|
|
|
} else if (document.body.classList.contains("public") && document.location.search) {
|
|
|
|
// Set default connection settings from url params
|
|
|
|
const queryParams = document.location.search;
|
|
|
|
cleanParams();
|
|
|
|
vueApp.$router.push("/connect" + queryParams);
|
2018-06-19 15:00:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-04-22 18:28:00 +00:00
|
|
|
|
2019-11-08 18:50:28 +00:00
|
|
|
function parseIrcUri(stringUri) {
|
|
|
|
const data = {};
|
2018-06-19 15:00:07 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
// https://tools.ietf.org/html/draft-butcher-irc-url-04
|
|
|
|
const uri = new URL(stringUri);
|
2018-04-22 18:28:00 +00:00
|
|
|
|
2018-06-19 15:00:07 +00:00
|
|
|
// Replace protocol with a "special protocol" (that's what it's called in WHATWG spec)
|
|
|
|
// So that the uri can be properly parsed
|
|
|
|
if (uri.protocol === "irc:") {
|
|
|
|
uri.protocol = "http:";
|
2018-04-22 18:28:00 +00:00
|
|
|
|
2018-06-19 15:00:07 +00:00
|
|
|
if (!uri.port) {
|
|
|
|
uri.port = 6667;
|
2018-04-22 18:28:00 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 15:00:07 +00:00
|
|
|
data.tls = false;
|
|
|
|
} else if (uri.protocol === "ircs:") {
|
|
|
|
uri.protocol = "https:";
|
|
|
|
|
|
|
|
if (!uri.port) {
|
|
|
|
uri.port = 6697;
|
2018-04-22 18:28:00 +00:00
|
|
|
}
|
2018-06-19 15:00:07 +00:00
|
|
|
|
|
|
|
data.tls = true;
|
|
|
|
} else {
|
|
|
|
return;
|
2018-04-22 18:28:00 +00:00
|
|
|
}
|
2018-06-19 15:00:07 +00:00
|
|
|
|
|
|
|
data.host = data.name = uri.hostname;
|
|
|
|
data.port = uri.port;
|
|
|
|
data.username = window.decodeURIComponent(uri.username) || data.username;
|
|
|
|
data.password = window.decodeURIComponent(uri.password) || data.password;
|
|
|
|
|
|
|
|
let channel = (uri.pathname + uri.hash).substr(1);
|
|
|
|
const index = channel.indexOf(",");
|
|
|
|
|
|
|
|
if (index > -1) {
|
|
|
|
channel = channel.substring(0, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
data.join = channel;
|
|
|
|
} catch (e) {
|
|
|
|
// do nothing on invalid uri
|
2018-04-22 18:28:00 +00:00
|
|
|
}
|
2018-06-19 15:00:07 +00:00
|
|
|
|
2019-11-08 18:50:28 +00:00
|
|
|
return data;
|
2018-06-19 15:00:07 +00:00
|
|
|
}
|