2020-03-25 14:53:28 +01:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
|
|
|
Copyright 2017 Vector Creations Ltd
|
|
|
|
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
|
2022-07-05 20:26:54 +02:00
|
|
|
Copyright 2018 - 2022 New Vector Ltd
|
2020-03-25 14:53:28 +01:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-08-29 02:14:51 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
2020-03-25 14:53:28 +01:00
|
|
|
// @ts-ignore
|
2021-05-21 22:10:48 +02:00
|
|
|
import olmWasmPath from "@matrix-org/olm/olm.wasm";
|
|
|
|
import Olm from '@matrix-org/olm';
|
2020-04-05 01:05:59 +02:00
|
|
|
import * as ReactDOM from "react-dom";
|
2020-04-08 20:47:52 +02:00
|
|
|
import * as React from "react";
|
2020-04-04 18:34:33 +02:00
|
|
|
import * as languageHandler from "matrix-react-sdk/src/languageHandler";
|
2020-03-25 14:55:25 +01:00
|
|
|
import SettingsStore from "matrix-react-sdk/src/settings/SettingsStore";
|
2020-04-04 18:34:33 +02:00
|
|
|
import PlatformPeg from "matrix-react-sdk/src/PlatformPeg";
|
2020-03-25 15:32:37 +01:00
|
|
|
import SdkConfig from "matrix-react-sdk/src/SdkConfig";
|
2021-06-30 14:28:31 +02:00
|
|
|
import { setTheme } from "matrix-react-sdk/src/theme";
|
2021-12-09 23:57:46 +01:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-07-05 20:26:54 +02:00
|
|
|
import { ModuleRunner } from "matrix-react-sdk/src/modules/ModuleRunner";
|
2020-04-04 18:34:33 +02:00
|
|
|
|
2021-12-09 23:57:46 +01:00
|
|
|
import ElectronPlatform from "./platform/ElectronPlatform";
|
|
|
|
import PWAPlatform from "./platform/PWAPlatform";
|
|
|
|
import WebPlatform from "./platform/WebPlatform";
|
2021-06-30 14:28:31 +02:00
|
|
|
import { initRageshake, initRageshakeStore } from "./rageshakesetup";
|
2022-07-05 20:26:54 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore - this path is created at runtime and therefore won't exist at typecheck time
|
|
|
|
import { INSTALLED_MODULES } from "../modules";
|
2020-04-04 18:34:33 +02:00
|
|
|
|
|
|
|
export const rageshakePromise = initRageshake();
|
2020-03-25 14:55:25 +01:00
|
|
|
|
2022-11-23 17:24:36 +01:00
|
|
|
export function preparePlatform(): void {
|
2021-01-13 16:39:18 +01:00
|
|
|
if (window.electron) {
|
2021-10-15 16:59:13 +02:00
|
|
|
logger.log("Using Electron platform");
|
2020-06-02 17:58:05 +02:00
|
|
|
PlatformPeg.set(new ElectronPlatform());
|
|
|
|
} else if (window.matchMedia('(display-mode: standalone)').matches) {
|
2021-10-15 16:59:13 +02:00
|
|
|
logger.log("Using PWA platform");
|
2020-06-02 17:58:05 +02:00
|
|
|
PlatformPeg.set(new PWAPlatform());
|
2020-03-25 15:32:37 +01:00
|
|
|
} else {
|
2021-10-15 16:59:13 +02:00
|
|
|
logger.log("Using Web platform");
|
2020-03-25 15:32:37 +01:00
|
|
|
PlatformPeg.set(new WebPlatform());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 17:24:36 +01:00
|
|
|
export function setupLogStorage(): Promise<void> {
|
2021-03-16 21:21:59 +01:00
|
|
|
if (SdkConfig.get().bug_report_endpoint_url) {
|
|
|
|
return initRageshakeStore();
|
|
|
|
}
|
2021-10-15 17:00:43 +02:00
|
|
|
logger.warn("No bug report endpoint set - logs will not be persisted");
|
2021-03-16 21:21:59 +01:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2022-11-23 17:24:36 +01:00
|
|
|
export async function loadConfig(): Promise<void> {
|
2020-04-08 20:35:16 +02:00
|
|
|
// XXX: We call this twice, once here and once in MatrixChat as a prop. We call it here to ensure
|
|
|
|
// granular settings are loaded correctly and to avoid duplicating the override logic for the theme.
|
|
|
|
//
|
|
|
|
// Note: this isn't called twice for some wrappers, like the Jitsi wrapper.
|
2022-03-18 17:12:44 +01:00
|
|
|
const platformConfig = await PlatformPeg.get().getConfig();
|
|
|
|
if (platformConfig) {
|
|
|
|
SdkConfig.put(platformConfig);
|
|
|
|
} else {
|
|
|
|
SdkConfig.unset(); // clears the config (sets to empty object)
|
|
|
|
}
|
2020-03-25 15:32:37 +01:00
|
|
|
}
|
|
|
|
|
2020-03-25 15:07:22 +01:00
|
|
|
export function loadOlm(): Promise<void> {
|
2020-03-25 14:53:28 +01:00
|
|
|
/* Load Olm. We try the WebAssembly version first, and then the legacy,
|
|
|
|
* asm.js version if that fails. For this reason we need to wait for this
|
|
|
|
* to finish before continuing to load the rest of the app. In future
|
|
|
|
* we could somehow pass a promise down to react-sdk and have it wait on
|
|
|
|
* that so olm can be loading in parallel with the rest of the app.
|
|
|
|
*
|
|
|
|
* We also need to tell the Olm js to look for its wasm file at the same
|
|
|
|
* level as index.html. It really should be in the same place as the js,
|
|
|
|
* ie. in the bundle directory, but as far as I can tell this is
|
|
|
|
* completely impossible with webpack. We do, however, use a hashed
|
|
|
|
* filename to avoid caching issues.
|
|
|
|
*/
|
|
|
|
return Olm.init({
|
|
|
|
locateFile: () => olmWasmPath,
|
|
|
|
}).then(() => {
|
2021-10-15 16:59:13 +02:00
|
|
|
logger.log("Using WebAssembly Olm");
|
2022-07-11 14:22:37 +02:00
|
|
|
}).catch((wasmLoadError) => {
|
|
|
|
logger.log("Failed to load Olm: trying legacy version", wasmLoadError);
|
2020-03-25 14:53:28 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const s = document.createElement('script');
|
|
|
|
s.src = 'olm_legacy.js'; // XXX: This should be cache-busted too
|
|
|
|
s.onload = resolve;
|
|
|
|
s.onerror = reject;
|
|
|
|
document.body.appendChild(s);
|
|
|
|
}).then(() => {
|
|
|
|
// Init window.Olm, ie. the one just loaded by the script tag,
|
|
|
|
// not 'Olm' which is still the failed wasm version.
|
|
|
|
return window.Olm.init();
|
|
|
|
}).then(() => {
|
2021-10-15 16:59:13 +02:00
|
|
|
logger.log("Using legacy Olm");
|
2022-07-11 14:22:37 +02:00
|
|
|
}).catch((legacyLoadError) => {
|
|
|
|
logger.log("Both WebAssembly and asm.js Olm failed!", legacyLoadError);
|
2020-03-25 14:53:28 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-03-25 14:55:25 +01:00
|
|
|
|
2022-11-23 17:24:36 +01:00
|
|
|
export async function loadLanguage(): Promise<void> {
|
2020-03-25 14:55:25 +01:00
|
|
|
const prefLang = SettingsStore.getValue("language", null, /*excludeDefault=*/true);
|
|
|
|
let langs = [];
|
|
|
|
|
|
|
|
if (!prefLang) {
|
|
|
|
languageHandler.getLanguagesFromBrowser().forEach((l) => {
|
|
|
|
langs.push(...languageHandler.getNormalizedLanguageKeys(l));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
langs = [prefLang];
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await languageHandler.setLanguage(langs);
|
|
|
|
document.documentElement.setAttribute("lang", languageHandler.getCurrentLanguage());
|
|
|
|
} catch (e) {
|
2021-10-15 16:56:22 +02:00
|
|
|
logger.error("Unable to set language", e);
|
2020-03-25 14:55:25 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-04 18:21:59 +02:00
|
|
|
|
2022-11-23 17:24:36 +01:00
|
|
|
export async function loadTheme(): Promise<void> {
|
2020-04-08 17:17:46 +02:00
|
|
|
setTheme();
|
|
|
|
}
|
|
|
|
|
2022-11-23 17:24:36 +01:00
|
|
|
export async function loadApp(fragParams: {}): Promise<void> {
|
2020-04-05 00:42:19 +02:00
|
|
|
// load app.js async so that its code is not executed immediately and we can catch any exceptions
|
2020-04-04 18:21:59 +02:00
|
|
|
const module = await import(
|
2020-11-25 18:01:19 +01:00
|
|
|
/* webpackChunkName: "element-web-app" */
|
2020-04-04 18:21:59 +02:00
|
|
|
/* webpackPreload: true */
|
|
|
|
"./app");
|
2020-04-08 22:41:22 +02:00
|
|
|
window.matrixChat = ReactDOM.render(await module.loadApp(fragParams),
|
2020-04-05 01:55:36 +02:00
|
|
|
document.getElementById('matrixchat'));
|
2020-04-04 18:21:59 +02:00
|
|
|
}
|
2020-04-08 20:47:52 +02:00
|
|
|
|
2022-11-23 17:24:36 +01:00
|
|
|
export async function showError(title: string, messages?: string[]): Promise<void> {
|
2020-04-08 20:47:52 +02:00
|
|
|
const ErrorView = (await import(
|
|
|
|
/* webpackChunkName: "error-view" */
|
2020-04-23 17:45:24 +02:00
|
|
|
"../async-components/structures/ErrorView")).default;
|
2020-04-08 21:03:45 +02:00
|
|
|
window.matrixChat = ReactDOM.render(<ErrorView title={title} messages={messages} />,
|
2020-04-08 20:47:52 +02:00
|
|
|
document.getElementById('matrixchat'));
|
|
|
|
}
|
|
|
|
|
2022-11-23 17:24:36 +01:00
|
|
|
export async function showIncompatibleBrowser(onAccept): Promise<void> {
|
2020-04-23 17:45:24 +02:00
|
|
|
const CompatibilityView = (await import(
|
|
|
|
/* webpackChunkName: "compatibility-view" */
|
|
|
|
"../async-components/structures/CompatibilityView")).default;
|
|
|
|
window.matrixChat = ReactDOM.render(<CompatibilityView onAccept={onAccept} />,
|
2020-04-08 22:41:22 +02:00
|
|
|
document.getElementById('matrixchat'));
|
|
|
|
}
|
|
|
|
|
2022-11-23 17:24:36 +01:00
|
|
|
export async function loadModules(): Promise<void> {
|
2022-07-05 20:26:54 +02:00
|
|
|
for (const InstalledModule of INSTALLED_MODULES) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore - we know the constructor exists even if TypeScript can't be convinced of that
|
|
|
|
ModuleRunner.instance.registerModule((api) => new InstalledModule(api));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 20:47:52 +02:00
|
|
|
export const _t = languageHandler._t;
|