keanu-weblite/src/services/config.service.js

77 lines
3 KiB
JavaScript
Raw Normal View History

2025-03-31 16:33:54 +02:00
import * as defaultConfig from "@/assets/config.json";
2021-09-25 09:29:05 +02:00
export default {
2025-05-06 09:27:53 +02:00
install(app, defaultServerFromLocation, onloaded) {
var config = defaultConfig.default;
config["loaded"] = false;
const getRuntimeConfig = () => {
2023-10-13 09:31:45 +02:00
return fetch('./config.json?ms=' + Date.now()).then((res) => res.json()).catch(err => {
console.error("Failed to get config:", err);
return {};
});
2021-09-25 09:29:05 +02:00
}
config.promise = getRuntimeConfig().then((json) => {
2021-09-25 09:29:05 +02:00
// Reactively use all the config values
for (const key of Object.keys(json)) {
2025-05-06 09:27:53 +02:00
config[key] = json[key];
2021-09-25 09:29:05 +02:00
}
// If default server is not set, default to current server address
2023-12-01 08:20:03 +00:00
if (!json.defaultBaseUrl) {
if (json.defaultServer) {
// TODO - Only to migrate old values (defaultServer was renamed defaultBaseUrl), can be removed later...
2025-05-06 09:27:53 +02:00
config["defaultBaseUrl"] = defaultServerFromLocation;
2023-12-01 08:20:03 +00:00
} else {
2025-05-06 09:27:53 +02:00
config["defaultBaseUrl"] = json.defaultServer;
2023-12-01 08:20:03 +00:00
}
}
if (json.useFullyQualifiedDMLinks == undefined) {
2025-05-06 09:27:53 +02:00
config["useFullyQualifiedDMLinks"] = true; // Default to true
}
if (json.disableMediaSharing == undefined) {
config["disableMediaSharing"] = false;
}
if (!json.maxSizeAutoDownloads) {
2025-05-06 09:27:53 +02:00
config["maxSizeAutoDownloads"] = 10 * 1024 * 1024;
}
2025-01-14 11:14:11 +00:00
if (!json.roomTypes) {
let roomTypes = ["group_chat", "channel"];
const fileDropEnabled = (json.experimental_file_mode === undefined) ? true : !!json.experimental_file_mode;
if (fileDropEnabled) {
roomTypes.push("file_drop");
}
2025-05-06 09:27:53 +02:00
config["roomTypes"] = roomTypes;
2025-01-14 11:14:11 +00:00
}
2025-05-06 09:27:53 +02:00
config["loaded"] = true;
2025-03-31 16:33:54 +02:00
document.title = config.appName || "";
// Tell callback we are done loading runtime config
if (onloaded) {
onloaded(config);
}
return config;
2021-09-25 09:29:05 +02:00
});
2023-12-01 08:20:03 +00:00
/**
* If there is an explicit mapping for this MX domain in the config file, return the endpoint URL that it maps to.
* @param {*} domain
* @returns
*/
config.getMatrixDomainPartMapping = (domain) => {
console.log("Get domain endpoint mapping for", domain);
if (config.matrixDomainPartMapping && config.matrixDomainPartMapping[domain]) {
const mapping = config.matrixDomainPartMapping[domain];
if (Array.isArray(mapping)) {
return mapping[0]; //TODO - Use the first one for now, but maybe rotate somehow?
}
return mapping;
}
return undefined;
}
2025-05-06 09:27:53 +02:00
app.$config = config;
app.config.globalProperties.$config = config;
2021-09-25 09:29:05 +02:00
}
}