2016-08-02 15:04:20 +02:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2017-02-15 19:44:15 +01:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2018-04-27 12:25:13 +02:00
|
|
|
Copyright 2018 New Vector Ltd
|
2020-01-17 12:43:35 +01:00
|
|
|
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
2016-08-02 15:04:20 +02: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-10-07 18:18:19 +02:00
|
|
|
// @ts-ignore - XXX: tsc doesn't like this: our js-sdk imports are complex so this isn't surprising
|
2016-08-11 12:00:15 +02:00
|
|
|
import Matrix from 'matrix-js-sdk';
|
2020-10-07 13:14:36 +02:00
|
|
|
import { InvalidStoreError } from "matrix-js-sdk/src/errors";
|
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
2016-08-10 12:33:58 +02:00
|
|
|
|
2020-10-07 13:14:36 +02:00
|
|
|
import {IMatrixClientCreds, MatrixClientPeg} from './MatrixClientPeg';
|
2020-10-08 17:35:17 +02:00
|
|
|
import SecurityCustomisations from "./customisations/Security";
|
2019-11-19 12:52:12 +01:00
|
|
|
import EventIndexPeg from './indexing/EventIndexPeg';
|
2017-05-31 18:28:46 +02:00
|
|
|
import createMatrixClient from './utils/createMatrixClient';
|
2017-05-27 21:47:09 +02:00
|
|
|
import Analytics from './Analytics';
|
2017-01-20 15:22:27 +01:00
|
|
|
import Notifier from './Notifier';
|
2016-08-02 15:04:20 +02:00
|
|
|
import UserActivity from './UserActivity';
|
|
|
|
import Presence from './Presence';
|
2020-05-14 04:41:41 +02:00
|
|
|
import dis from './dispatcher/dispatcher';
|
2016-09-26 19:02:14 +02:00
|
|
|
import DMRoomMap from './utils/DMRoomMap';
|
2017-02-15 19:44:15 +01:00
|
|
|
import Modal from './Modal';
|
2019-12-20 02:19:56 +01:00
|
|
|
import * as sdk from './index';
|
2018-07-24 17:21:43 +02:00
|
|
|
import ActiveWidgetStore from './stores/ActiveWidgetStore';
|
2018-09-26 17:25:21 +02:00
|
|
|
import PlatformPeg from "./PlatformPeg";
|
2019-03-25 18:43:53 +01:00
|
|
|
import { sendLoginRequest } from "./Login";
|
|
|
|
import * as StorageManager from './utils/StorageManager';
|
2019-02-08 17:44:03 +01:00
|
|
|
import SettingsStore from "./settings/SettingsStore";
|
2019-06-27 06:36:55 +02:00
|
|
|
import TypingStore from "./stores/TypingStore";
|
2020-01-17 12:43:35 +01:00
|
|
|
import ToastStore from "./stores/ToastStore";
|
2019-08-10 01:35:59 +02:00
|
|
|
import {IntegrationManagers} from "./integrations/IntegrationManagers";
|
2019-10-31 20:28:00 +01:00
|
|
|
import {Mjolnir} from "./mjolnir/Mjolnir";
|
2020-01-17 12:43:35 +01:00
|
|
|
import DeviceListener from "./DeviceListener";
|
2020-04-07 00:04:41 +02:00
|
|
|
import {Jitsi} from "./widgets/Jitsi";
|
2020-06-25 22:59:46 +02:00
|
|
|
import {SSO_HOMESERVER_URL_KEY, SSO_ID_SERVER_URL_KEY} from "./BasePlatform";
|
2020-09-12 04:20:33 +02:00
|
|
|
import ThreepidInviteStore from "./stores/ThreepidInviteStore";
|
2020-10-30 11:26:15 +01:00
|
|
|
import CountlyAnalytics from "./CountlyAnalytics";
|
2020-11-23 17:20:15 +01:00
|
|
|
import CallHandler from './CallHandler';
|
2020-11-27 12:19:44 +01:00
|
|
|
import LifecycleCustomisations from "./customisations/Lifecycle";
|
2020-06-25 22:59:46 +02:00
|
|
|
|
2020-06-25 23:01:41 +02:00
|
|
|
const HOMESERVER_URL_KEY = "mx_hs_url";
|
|
|
|
const ID_SERVER_URL_KEY = "mx_is_url";
|
2016-08-02 15:04:20 +02:00
|
|
|
|
2020-10-07 13:14:36 +02:00
|
|
|
interface ILoadSessionOpts {
|
|
|
|
enableGuest?: boolean;
|
|
|
|
guestHsUrl?: string;
|
|
|
|
guestIsUrl?: string;
|
|
|
|
ignoreGuest?: boolean;
|
|
|
|
defaultDeviceDisplayName?: string;
|
|
|
|
fragmentQueryParams?: Record<string, string>;
|
|
|
|
}
|
|
|
|
|
2016-08-10 12:33:58 +02:00
|
|
|
/**
|
|
|
|
* Called at startup, to attempt to build a logged-in Matrix session. It tries
|
|
|
|
* a number of things:
|
|
|
|
*
|
2017-06-16 15:33:14 +02:00
|
|
|
* 1. if we have a guest access token in the fragment query params, it uses
|
2016-08-11 12:00:15 +02:00
|
|
|
* that.
|
2017-06-16 15:33:14 +02:00
|
|
|
* 2. if an access token is stored in local storage (from a previous session),
|
2016-08-10 12:33:58 +02:00
|
|
|
* it uses that.
|
2017-06-16 15:33:14 +02:00
|
|
|
* 3. it attempts to auto-register as a guest user.
|
2016-08-10 12:33:58 +02:00
|
|
|
*
|
2017-05-31 18:28:46 +02:00
|
|
|
* If any of steps 1-4 are successful, it will call {_doSetLoggedIn}, which in
|
2016-08-10 12:33:58 +02:00
|
|
|
* turn will raise on_logged_in and will_start_client events.
|
|
|
|
*
|
2020-10-07 13:14:36 +02:00
|
|
|
* @param {object} [opts]
|
|
|
|
* @param {object} [opts.fragmentQueryParams]: string->string map of the
|
2016-08-11 12:00:15 +02:00
|
|
|
* query-parameters extracted from the #-fragment of the starting URI.
|
2020-10-07 13:14:36 +02:00
|
|
|
* @param {boolean} [opts.enableGuest]: set to true to enable guest access
|
|
|
|
* tokens and auto-guest registrations.
|
|
|
|
* @param {string} [opts.guestHsUrl]: homeserver URL. Only used if enableGuest
|
|
|
|
* is true; defines the HS to register against.
|
|
|
|
* @param {string} [opts.guestIsUrl]: homeserver URL. Only used if enableGuest
|
|
|
|
* is true; defines the IS to use.
|
|
|
|
* @param {bool} [opts.ignoreGuest]: If the stored session is a guest account,
|
|
|
|
* ignore it and don't load it.
|
|
|
|
* @param {string} [opts.defaultDeviceDisplayName]: Default display name to use
|
|
|
|
* when registering as a guest.
|
2017-05-04 19:03:35 +02:00
|
|
|
* @returns {Promise} a promise which resolves when the above process completes.
|
2017-06-16 13:20:52 +02:00
|
|
|
* Resolves to `true` if we ended up starting a session, or `false` if we
|
|
|
|
* failed.
|
2016-08-10 12:33:58 +02:00
|
|
|
*/
|
2020-10-07 13:14:36 +02:00
|
|
|
export async function loadSession(opts: ILoadSessionOpts = {}): Promise<boolean> {
|
2018-04-27 18:49:53 +02:00
|
|
|
try {
|
|
|
|
let enableGuest = opts.enableGuest || false;
|
|
|
|
const guestHsUrl = opts.guestHsUrl;
|
|
|
|
const guestIsUrl = opts.guestIsUrl;
|
|
|
|
const fragmentQueryParams = opts.fragmentQueryParams || {};
|
|
|
|
const defaultDeviceDisplayName = opts.defaultDeviceDisplayName;
|
2018-04-27 15:20:09 +02:00
|
|
|
|
2019-08-02 12:08:37 +02:00
|
|
|
if (enableGuest && !guestHsUrl) {
|
2018-04-27 12:25:13 +02:00
|
|
|
console.warn("Cannot enable guest access: can't determine HS URL to use");
|
|
|
|
enableGuest = false;
|
|
|
|
}
|
2016-08-10 12:33:58 +02:00
|
|
|
|
2020-10-07 13:14:36 +02:00
|
|
|
if (
|
|
|
|
enableGuest &&
|
2018-04-27 12:25:13 +02:00
|
|
|
fragmentQueryParams.guest_user_id &&
|
|
|
|
fragmentQueryParams.guest_access_token
|
2020-10-07 13:14:36 +02:00
|
|
|
) {
|
2018-04-27 12:25:13 +02:00
|
|
|
console.log("Using guest access credentials");
|
2020-10-07 14:32:30 +02:00
|
|
|
return doSetLoggedIn({
|
2018-04-27 12:25:13 +02:00
|
|
|
userId: fragmentQueryParams.guest_user_id,
|
|
|
|
accessToken: fragmentQueryParams.guest_access_token,
|
|
|
|
homeserverUrl: guestHsUrl,
|
|
|
|
identityServerUrl: guestIsUrl,
|
|
|
|
guest: true,
|
|
|
|
}, true).then(() => true);
|
|
|
|
}
|
2020-10-07 14:32:30 +02:00
|
|
|
const success = await restoreFromLocalStorage({
|
2019-08-02 17:44:49 +02:00
|
|
|
ignoreGuest: Boolean(opts.ignoreGuest),
|
|
|
|
});
|
2017-02-15 19:44:15 +01:00
|
|
|
if (success) {
|
2017-06-16 13:20:52 +02:00
|
|
|
return true;
|
2017-02-15 19:44:15 +01:00
|
|
|
}
|
2016-08-10 12:33:58 +02:00
|
|
|
|
2017-02-15 19:44:15 +01:00
|
|
|
if (enableGuest) {
|
2020-10-07 14:32:30 +02:00
|
|
|
return registerAsGuest(guestHsUrl, guestIsUrl, defaultDeviceDisplayName);
|
2017-02-15 19:44:15 +01:00
|
|
|
}
|
2016-08-10 12:33:58 +02:00
|
|
|
|
2019-03-28 17:22:17 +01:00
|
|
|
// fall back to welcome screen
|
2017-06-16 13:20:52 +02:00
|
|
|
return false;
|
2018-04-27 18:49:53 +02:00
|
|
|
} catch (e) {
|
2019-03-28 17:22:17 +01:00
|
|
|
if (e instanceof AbortLoginAndRebuildStorage) {
|
|
|
|
// If we're aborting login because of a storage inconsistency, we don't
|
|
|
|
// need to show the general failure dialog. Instead, just go back to welcome.
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-07 14:32:30 +02:00
|
|
|
return handleLoadSessionFailure(e);
|
2018-04-27 18:49:53 +02:00
|
|
|
}
|
2016-08-10 12:33:58 +02:00
|
|
|
}
|
|
|
|
|
2019-03-08 01:04:50 +01:00
|
|
|
/**
|
|
|
|
* Gets the user ID of the persisted session, if one exists. This does not validate
|
|
|
|
* that the user's credentials still work, just that they exist and that a user ID
|
|
|
|
* is associated with them. The session is not loaded.
|
|
|
|
* @returns {String} The persisted session's owner, if an owner exists. Null otherwise.
|
|
|
|
*/
|
2020-10-07 13:14:36 +02:00
|
|
|
export function getStoredSessionOwner(): string {
|
2019-05-29 20:09:04 +02:00
|
|
|
const {hsUrl, userId, accessToken} = getLocalStorageSessionVars();
|
2019-03-08 01:04:50 +01:00
|
|
|
return hsUrl && userId && accessToken ? userId : null;
|
|
|
|
}
|
|
|
|
|
2019-05-14 12:59:38 +02:00
|
|
|
/**
|
|
|
|
* @returns {bool} True if the stored session is for a guest user or false if it is
|
|
|
|
* for a real user. If there is no stored session, return null.
|
|
|
|
*/
|
2020-10-07 13:14:36 +02:00
|
|
|
export function getStoredSessionIsGuest(): boolean {
|
2019-05-29 20:09:04 +02:00
|
|
|
const sessVars = getLocalStorageSessionVars();
|
2019-05-14 13:11:07 +02:00
|
|
|
return sessVars.hsUrl && sessVars.userId && sessVars.accessToken ? sessVars.isGuest : null;
|
2019-05-14 12:59:38 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 15:33:14 +02:00
|
|
|
/**
|
|
|
|
* @param {Object} queryParams string->string map of the
|
|
|
|
* query-parameters extracted from the real query-string of the starting
|
|
|
|
* URI.
|
|
|
|
*
|
|
|
|
* @param {String} defaultDeviceDisplayName
|
|
|
|
*
|
|
|
|
* @returns {Promise} promise which resolves to true if we completed the token
|
|
|
|
* login, else false
|
|
|
|
*/
|
2020-10-07 13:14:36 +02:00
|
|
|
export function attemptTokenLogin(
|
|
|
|
queryParams: Record<string, string>,
|
|
|
|
defaultDeviceDisplayName?: string,
|
|
|
|
): Promise<boolean> {
|
2017-06-16 15:33:14 +02:00
|
|
|
if (!queryParams.loginToken) {
|
2017-07-12 15:02:00 +02:00
|
|
|
return Promise.resolve(false);
|
2017-06-16 15:33:14 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 22:59:46 +02:00
|
|
|
const homeserver = localStorage.getItem(SSO_HOMESERVER_URL_KEY);
|
|
|
|
const identityServer = localStorage.getItem(SSO_ID_SERVER_URL_KEY);
|
2020-06-02 17:26:07 +02:00
|
|
|
if (!homeserver) {
|
2017-06-16 15:33:14 +02:00
|
|
|
console.warn("Cannot log in with token: can't determine HS URL to use");
|
2017-07-12 15:02:00 +02:00
|
|
|
return Promise.resolve(false);
|
2017-06-16 15:33:14 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 17:39:38 +01:00
|
|
|
return sendLoginRequest(
|
2020-06-02 17:26:07 +02:00
|
|
|
homeserver,
|
|
|
|
identityServer,
|
2016-08-11 17:15:42 +02:00
|
|
|
"m.login.token", {
|
|
|
|
token: queryParams.loginToken,
|
|
|
|
initial_device_display_name: defaultDeviceDisplayName,
|
|
|
|
},
|
2018-12-05 17:39:38 +01:00
|
|
|
).then(function(creds) {
|
2016-08-11 12:00:15 +02:00
|
|
|
console.log("Logged in with token");
|
2020-10-07 14:32:30 +02:00
|
|
|
return clearStorage().then(() => {
|
|
|
|
persistCredentialsToLocalStorage(creds);
|
2020-09-30 06:52:47 +02:00
|
|
|
// remember that we just logged in
|
2020-10-07 13:14:36 +02:00
|
|
|
sessionStorage.setItem("mx_fresh_login", String(true));
|
2017-06-16 15:33:14 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}).catch((err) => {
|
2016-08-11 12:00:15 +02:00
|
|
|
console.error("Failed to log in with login token: " + err + " " +
|
|
|
|
err.data);
|
2017-06-16 15:33:14 +02:00
|
|
|
return false;
|
2016-08-11 12:00:15 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-07 13:14:36 +02:00
|
|
|
export function handleInvalidStoreError(e: InvalidStoreError): Promise<void> {
|
|
|
|
if (e.reason === InvalidStoreError.TOGGLED_LAZY_LOADING) {
|
2018-10-10 18:07:17 +02:00
|
|
|
return Promise.resolve().then(() => {
|
|
|
|
const lazyLoadEnabled = e.value;
|
|
|
|
if (lazyLoadEnabled) {
|
|
|
|
const LazyLoadingResyncDialog =
|
|
|
|
sdk.getComponent("views.dialogs.LazyLoadingResyncDialog");
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
Modal.createDialog(LazyLoadingResyncDialog, {
|
|
|
|
onFinished: resolve,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// show warning about simultaneous use
|
|
|
|
// between LL/non-LL version on same host.
|
|
|
|
// as disabling LL when previously enabled
|
|
|
|
// is a strong indicator of this (/develop & /app)
|
|
|
|
const LazyLoadingDisabledDialog =
|
|
|
|
sdk.getComponent("views.dialogs.LazyLoadingDisabledDialog");
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
Modal.createDialog(LazyLoadingDisabledDialog, {
|
|
|
|
onFinished: resolve,
|
|
|
|
host: window.location.host,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}).then(() => {
|
|
|
|
return MatrixClientPeg.get().store.deleteAllData();
|
|
|
|
}).then(() => {
|
|
|
|
PlatformPeg.get().reload();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:32:30 +02:00
|
|
|
function registerAsGuest(
|
2020-10-07 13:14:36 +02:00
|
|
|
hsUrl: string,
|
|
|
|
isUrl: string,
|
|
|
|
defaultDeviceDisplayName: string,
|
|
|
|
): Promise<boolean> {
|
2017-06-20 18:38:02 +02:00
|
|
|
console.log(`Doing guest login on ${hsUrl}`);
|
2016-08-10 12:33:58 +02:00
|
|
|
|
2016-08-11 14:50:38 +02:00
|
|
|
// create a temporary MatrixClient to do the login
|
2017-05-04 19:03:35 +02:00
|
|
|
const client = Matrix.createClient({
|
2016-08-11 14:50:38 +02:00
|
|
|
baseUrl: hsUrl,
|
|
|
|
});
|
|
|
|
|
2016-08-11 17:15:42 +02:00
|
|
|
return client.registerGuest({
|
|
|
|
body: {
|
|
|
|
initial_device_display_name: defaultDeviceDisplayName,
|
|
|
|
},
|
|
|
|
}).then((creds) => {
|
2017-06-20 18:38:02 +02:00
|
|
|
console.log(`Registered as guest: ${creds.user_id}`);
|
2020-10-07 14:32:30 +02:00
|
|
|
return doSetLoggedIn({
|
2016-08-10 12:33:58 +02:00
|
|
|
userId: creds.user_id,
|
2016-08-11 15:21:52 +02:00
|
|
|
deviceId: creds.device_id,
|
2016-08-10 12:33:58 +02:00
|
|
|
accessToken: creds.access_token,
|
|
|
|
homeserverUrl: hsUrl,
|
|
|
|
identityServerUrl: isUrl,
|
|
|
|
guest: true,
|
2017-06-16 13:20:52 +02:00
|
|
|
}, true).then(() => true);
|
2016-08-10 12:33:58 +02:00
|
|
|
}, (err) => {
|
2019-06-12 15:04:08 +02:00
|
|
|
console.error("Failed to register as guest", err);
|
2017-06-16 13:20:52 +02:00
|
|
|
return false;
|
2016-08-10 12:33:58 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-07 13:14:36 +02:00
|
|
|
export interface ILocalStorageSession {
|
|
|
|
hsUrl: string;
|
|
|
|
isUrl: string;
|
|
|
|
accessToken: string;
|
|
|
|
userId: string;
|
|
|
|
deviceId: string;
|
|
|
|
isGuest: boolean;
|
|
|
|
}
|
|
|
|
|
2019-05-29 20:09:04 +02:00
|
|
|
/**
|
|
|
|
* Retrieves information about the stored session in localstorage. The session
|
|
|
|
* may not be valid, as it is not tested for consistency here.
|
|
|
|
* @returns {Object} Information about the session - see implementation for variables.
|
|
|
|
*/
|
2020-10-07 13:14:36 +02:00
|
|
|
export function getLocalStorageSessionVars(): ILocalStorageSession {
|
2020-06-03 21:23:01 +02:00
|
|
|
const hsUrl = localStorage.getItem(HOMESERVER_URL_KEY);
|
|
|
|
const isUrl = localStorage.getItem(ID_SERVER_URL_KEY);
|
2019-03-08 01:04:50 +01:00
|
|
|
const accessToken = localStorage.getItem("mx_access_token");
|
|
|
|
const userId = localStorage.getItem("mx_user_id");
|
|
|
|
const deviceId = localStorage.getItem("mx_device_id");
|
|
|
|
|
2019-05-14 12:59:38 +02:00
|
|
|
let isGuest;
|
|
|
|
if (localStorage.getItem("mx_is_guest") !== null) {
|
|
|
|
isGuest = localStorage.getItem("mx_is_guest") === "true";
|
|
|
|
} else {
|
|
|
|
// legacy key name
|
|
|
|
isGuest = localStorage.getItem("matrix-is-guest") === "true";
|
|
|
|
}
|
|
|
|
|
|
|
|
return {hsUrl, isUrl, accessToken, userId, deviceId, isGuest};
|
2019-03-08 01:04:50 +01:00
|
|
|
}
|
|
|
|
|
2017-02-15 19:44:15 +01:00
|
|
|
// returns a promise which resolves to true if a session is found in
|
|
|
|
// localstorage
|
2017-05-16 12:58:37 +02:00
|
|
|
//
|
|
|
|
// N.B. Lifecycle.js should not maintain any further localStorage state, we
|
|
|
|
// are moving towards using SessionStore to keep track of state related
|
|
|
|
// to the current session (which is typically backed by localStorage).
|
|
|
|
//
|
|
|
|
// The plan is to gradually move the localStorage access done here into
|
|
|
|
// SessionStore to avoid bugs where the view becomes out-of-sync with
|
2019-01-25 23:10:54 +01:00
|
|
|
// localStorage (e.g. isGuest etc.)
|
2020-10-07 14:32:30 +02:00
|
|
|
async function restoreFromLocalStorage(opts?: { ignoreGuest?: boolean }): Promise<boolean> {
|
2020-10-07 13:14:36 +02:00
|
|
|
const ignoreGuest = opts?.ignoreGuest;
|
2019-08-02 17:44:49 +02:00
|
|
|
|
2018-04-27 18:49:53 +02:00
|
|
|
if (!localStorage) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-29 20:09:04 +02:00
|
|
|
const {hsUrl, isUrl, accessToken, userId, deviceId, isGuest} = getLocalStorageSessionVars();
|
2016-08-10 11:33:27 +02:00
|
|
|
|
2018-04-27 18:49:53 +02:00
|
|
|
if (accessToken && userId && hsUrl) {
|
2019-08-02 12:22:42 +02:00
|
|
|
if (ignoreGuest && isGuest) {
|
|
|
|
console.log("Ignoring stored guest account: " + userId);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-28 06:05:45 +02:00
|
|
|
const pickleKey = await PlatformPeg.get().getPickleKey(userId, deviceId);
|
2020-07-28 23:31:27 +02:00
|
|
|
if (pickleKey) {
|
|
|
|
console.log("Got pickle key");
|
|
|
|
} else {
|
|
|
|
console.log("No pickle key available");
|
|
|
|
}
|
2020-05-28 06:05:45 +02:00
|
|
|
|
2020-10-07 13:14:36 +02:00
|
|
|
const freshLogin = sessionStorage.getItem("mx_fresh_login") === "true";
|
2020-09-30 06:52:47 +02:00
|
|
|
sessionStorage.removeItem("mx_fresh_login");
|
2020-09-03 22:28:42 +02:00
|
|
|
|
2018-04-27 18:49:53 +02:00
|
|
|
console.log(`Restoring session for ${userId}`);
|
2020-10-07 14:32:30 +02:00
|
|
|
await doSetLoggedIn({
|
2018-04-27 18:49:53 +02:00
|
|
|
userId: userId,
|
|
|
|
deviceId: deviceId,
|
|
|
|
accessToken: accessToken,
|
|
|
|
homeserverUrl: hsUrl,
|
|
|
|
identityServerUrl: isUrl,
|
|
|
|
guest: isGuest,
|
2020-05-28 06:05:45 +02:00
|
|
|
pickleKey: pickleKey,
|
2020-09-30 06:52:47 +02:00
|
|
|
freshLogin: freshLogin,
|
2018-04-27 18:49:53 +02:00
|
|
|
}, false);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
console.log("No previous session found.");
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-10 11:33:27 +02:00
|
|
|
}
|
2016-08-10 12:33:58 +02:00
|
|
|
|
2020-10-07 14:32:30 +02:00
|
|
|
async function handleLoadSessionFailure(e: Error): Promise<boolean> {
|
2019-03-28 17:22:17 +01:00
|
|
|
console.error("Unable to load session", e);
|
2017-02-15 19:44:15 +01:00
|
|
|
|
|
|
|
const SessionRestoreErrorDialog =
|
|
|
|
sdk.getComponent('views.dialogs.SessionRestoreErrorDialog');
|
|
|
|
|
2019-11-12 12:40:38 +01:00
|
|
|
const modal = Modal.createTrackedDialog('Session Restore Error', '', SessionRestoreErrorDialog, {
|
2017-06-21 22:49:41 +02:00
|
|
|
error: e.message,
|
2017-02-15 19:44:15 +01:00
|
|
|
});
|
|
|
|
|
2020-03-22 10:47:00 +01:00
|
|
|
const [success] = await modal.finished;
|
|
|
|
if (success) {
|
|
|
|
// user clicked continue.
|
2020-10-07 14:32:30 +02:00
|
|
|
await clearStorage();
|
2020-03-22 10:47:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-15 19:44:15 +01:00
|
|
|
|
2020-03-22 10:47:00 +01:00
|
|
|
// try, try again
|
|
|
|
return loadSession();
|
2017-02-15 19:44:15 +01:00
|
|
|
}
|
|
|
|
|
2016-08-02 19:52:56 +02:00
|
|
|
/**
|
2017-05-31 18:28:46 +02:00
|
|
|
* Transitions to a logged-in state using the given credentials.
|
|
|
|
*
|
|
|
|
* Starts the matrix client and all other react-sdk services that
|
|
|
|
* listen for events while a session is logged in.
|
|
|
|
*
|
|
|
|
* Also stops the old MatrixClient and clears old credentials/etc out of
|
|
|
|
* storage before starting the new client.
|
|
|
|
*
|
2016-08-03 17:31:42 +02:00
|
|
|
* @param {MatrixClientCreds} credentials The credentials to use
|
2017-06-19 11:22:18 +02:00
|
|
|
*
|
|
|
|
* @returns {Promise} promise which resolves to the new MatrixClient once it has been started
|
2016-08-02 19:52:56 +02:00
|
|
|
*/
|
2020-10-07 13:14:36 +02:00
|
|
|
export async function setLoggedIn(credentials: IMatrixClientCreds): Promise<MatrixClient> {
|
2020-09-30 06:52:47 +02:00
|
|
|
credentials.freshLogin = true;
|
2017-05-31 18:28:46 +02:00
|
|
|
stopMatrixClient();
|
2020-05-28 06:05:45 +02:00
|
|
|
const pickleKey = credentials.userId && credentials.deviceId
|
2020-10-07 13:14:36 +02:00
|
|
|
? await PlatformPeg.get().createPickleKey(credentials.userId, credentials.deviceId)
|
|
|
|
: null;
|
2020-05-28 06:05:45 +02:00
|
|
|
|
2020-07-28 23:31:27 +02:00
|
|
|
if (pickleKey) {
|
|
|
|
console.log("Created pickle key");
|
|
|
|
} else {
|
|
|
|
console.log("Pickle key not created");
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:32:30 +02:00
|
|
|
return doSetLoggedIn(Object.assign({}, credentials, {pickleKey}), true);
|
2017-05-31 18:28:46 +02:00
|
|
|
}
|
2017-05-04 19:04:47 +02:00
|
|
|
|
2019-07-05 00:45:40 +02:00
|
|
|
/**
|
|
|
|
* Hydrates an existing session by using the credentials provided. This will
|
|
|
|
* not clear any local storage, unlike setLoggedIn().
|
|
|
|
*
|
|
|
|
* Stops the existing Matrix client (without clearing its data) and starts a
|
|
|
|
* new one in its place. This additionally starts all other react-sdk services
|
|
|
|
* which use the new Matrix client.
|
|
|
|
*
|
2019-07-05 22:45:34 +02:00
|
|
|
* If the credentials belong to a different user from the session already stored,
|
|
|
|
* the old session will be cleared automatically.
|
|
|
|
*
|
2019-07-05 00:45:40 +02:00
|
|
|
* @param {MatrixClientCreds} credentials The credentials to use
|
|
|
|
*
|
|
|
|
* @returns {Promise} promise which resolves to the new MatrixClient once it has been started
|
|
|
|
*/
|
2020-10-07 13:14:36 +02:00
|
|
|
export function hydrateSession(credentials: IMatrixClientCreds): Promise<MatrixClient> {
|
2019-07-05 22:45:34 +02:00
|
|
|
const oldUserId = MatrixClientPeg.get().getUserId();
|
2019-07-08 23:35:34 +02:00
|
|
|
const oldDeviceId = MatrixClientPeg.get().getDeviceId();
|
2019-07-05 22:45:34 +02:00
|
|
|
|
|
|
|
stopMatrixClient(); // unsets MatrixClientPeg.get()
|
2019-07-05 00:45:40 +02:00
|
|
|
localStorage.removeItem("mx_soft_logout");
|
|
|
|
_isLoggingOut = false;
|
2019-07-05 22:45:34 +02:00
|
|
|
|
2019-07-08 23:35:34 +02:00
|
|
|
const overwrite = credentials.userId !== oldUserId || credentials.deviceId !== oldDeviceId;
|
2019-07-05 22:45:34 +02:00
|
|
|
if (overwrite) {
|
2020-01-29 16:48:25 +01:00
|
|
|
console.warn("Clearing all data: Old session belongs to a different user/session");
|
2019-07-05 22:45:34 +02:00
|
|
|
}
|
|
|
|
|
2020-10-07 14:32:30 +02:00
|
|
|
return doSetLoggedIn(credentials, overwrite);
|
2019-07-05 00:45:40 +02:00
|
|
|
}
|
|
|
|
|
2017-05-31 18:28:46 +02:00
|
|
|
/**
|
|
|
|
* fires on_logging_in, optionally clears localstorage, persists new credentials
|
|
|
|
* to localstorage, starts the new client.
|
|
|
|
*
|
|
|
|
* @param {MatrixClientCreds} credentials
|
|
|
|
* @param {Boolean} clearStorage
|
|
|
|
*
|
2017-06-19 11:22:18 +02:00
|
|
|
* @returns {Promise} promise which resolves to the new MatrixClient once it has been started
|
2017-05-31 18:28:46 +02:00
|
|
|
*/
|
2020-10-07 14:32:30 +02:00
|
|
|
async function doSetLoggedIn(
|
2020-10-07 13:14:36 +02:00
|
|
|
credentials: IMatrixClientCreds,
|
2020-10-07 16:10:11 +02:00
|
|
|
clearStorageEnabled: boolean,
|
2020-10-07 13:14:36 +02:00
|
|
|
): Promise<MatrixClient> {
|
2017-05-31 18:28:46 +02:00
|
|
|
credentials.guest = Boolean(credentials.guest);
|
2017-05-29 20:04:37 +02:00
|
|
|
|
2019-07-04 00:46:37 +02:00
|
|
|
const softLogout = isSoftLogout();
|
|
|
|
|
2017-05-04 19:04:47 +02:00
|
|
|
console.log(
|
2017-06-20 18:38:02 +02:00
|
|
|
"setLoggedIn: mxid: " + credentials.userId +
|
|
|
|
" deviceId: " + credentials.deviceId +
|
|
|
|
" guest: " + credentials.guest +
|
2019-07-04 00:46:37 +02:00
|
|
|
" hs: " + credentials.homeserverUrl +
|
|
|
|
" softLogout: " + softLogout,
|
2020-09-30 06:52:47 +02:00
|
|
|
" freshLogin: " + credentials.freshLogin,
|
2017-05-04 19:04:47 +02:00
|
|
|
);
|
2017-05-31 18:28:46 +02:00
|
|
|
|
2017-03-27 17:39:04 +02:00
|
|
|
// This is dispatched to indicate that the user is still in the process of logging in
|
2019-01-25 23:10:54 +01:00
|
|
|
// because async code may take some time to resolve, breaking the assumption that
|
2017-03-27 17:39:04 +02:00
|
|
|
// `setLoggedIn` takes an "instant" to complete, and dispatch `on_logged_in` a few ms
|
|
|
|
// later than MatrixChat might assume.
|
2017-07-11 18:09:06 +02:00
|
|
|
//
|
|
|
|
// we fire it *synchronously* to make sure it fires before on_logged_in.
|
|
|
|
// (dis.dispatch uses `setTimeout`, which does not guarantee ordering.)
|
|
|
|
dis.dispatch({action: 'on_logging_in'}, true);
|
2016-08-10 19:04:22 +02:00
|
|
|
|
2020-10-07 16:10:11 +02:00
|
|
|
if (clearStorageEnabled) {
|
2020-10-07 14:32:30 +02:00
|
|
|
await clearStorage();
|
2017-05-31 18:28:46 +02:00
|
|
|
}
|
|
|
|
|
2019-03-28 17:22:17 +01:00
|
|
|
const results = await StorageManager.checkConsistency();
|
|
|
|
// If there's an inconsistency between account data in local storage and the
|
|
|
|
// crypto store, we'll be generally confused when handling encrypted data.
|
|
|
|
// Show a modal recommending a full reset of storage.
|
2019-05-15 14:47:48 +02:00
|
|
|
if (results.dataInLocalStorage && results.cryptoInited && !results.dataInCryptoStore) {
|
2020-10-07 14:32:30 +02:00
|
|
|
const signOut = await showStorageEvictedDialog();
|
2019-03-28 17:22:17 +01:00
|
|
|
if (signOut) {
|
2020-10-07 14:32:30 +02:00
|
|
|
await clearStorage();
|
2019-03-28 17:22:17 +01:00
|
|
|
// This error feels a bit clunky, but we want to make sure we don't go any
|
|
|
|
// further and instead head back to sign in.
|
|
|
|
throw new AbortLoginAndRebuildStorage(
|
|
|
|
"Aborting login in progress because of storage inconsistency",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 18:43:53 +01:00
|
|
|
|
2020-02-14 18:36:14 +01:00
|
|
|
Analytics.setLoggedIn(credentials.guest, credentials.homeserverUrl);
|
2017-05-31 18:28:46 +02:00
|
|
|
|
2020-09-30 06:52:47 +02:00
|
|
|
MatrixClientPeg.replaceUsingCreds(credentials);
|
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
|
2020-10-02 23:43:49 +02:00
|
|
|
if (credentials.freshLogin && SettingsStore.getValue("feature_dehydration")) {
|
2020-09-30 06:52:47 +02:00
|
|
|
// If we just logged in, try to rehydrate a device instead of using a
|
|
|
|
// new device. If it succeeds, we'll get a new device ID, so make sure
|
|
|
|
// we persist that ID to localStorage
|
|
|
|
const newDeviceId = await client.rehydrateDevice();
|
|
|
|
if (newDeviceId) {
|
|
|
|
credentials.deviceId = newDeviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete credentials.freshLogin;
|
|
|
|
}
|
|
|
|
|
2016-08-10 19:04:22 +02:00
|
|
|
if (localStorage) {
|
|
|
|
try {
|
2020-10-07 14:32:30 +02:00
|
|
|
persistCredentialsToLocalStorage(credentials);
|
2020-09-30 06:52:47 +02:00
|
|
|
// make sure we don't think that it's a fresh login any more
|
|
|
|
sessionStorage.removeItem("mx_fresh_login");
|
2016-08-10 19:04:22 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.warn("Error using local storage: can't persist session!", e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.warn("No local storage available: can't persist session!");
|
|
|
|
}
|
|
|
|
|
2019-01-25 23:10:54 +01:00
|
|
|
dis.dispatch({ action: 'on_logged_in' });
|
2016-08-02 15:04:20 +02:00
|
|
|
|
2019-07-04 00:46:37 +02:00
|
|
|
await startMatrixClient(/*startSyncing=*/!softLogout);
|
2020-09-30 06:52:47 +02:00
|
|
|
return client;
|
2016-08-02 15:04:20 +02:00
|
|
|
}
|
|
|
|
|
2020-10-07 14:32:30 +02:00
|
|
|
function showStorageEvictedDialog(): Promise<boolean> {
|
2019-03-28 17:22:17 +01:00
|
|
|
const StorageEvictedDialog = sdk.getComponent('views.dialogs.StorageEvictedDialog');
|
|
|
|
return new Promise(resolve => {
|
|
|
|
Modal.createTrackedDialog('Storage evicted', '', StorageEvictedDialog, {
|
|
|
|
onFinished: resolve,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: Babel 6 requires the `transform-builtin-extend` plugin for this to satisfy
|
|
|
|
// `instanceof`. Babel 7 supports this natively in their class handling.
|
|
|
|
class AbortLoginAndRebuildStorage extends Error { }
|
|
|
|
|
2020-10-07 14:32:30 +02:00
|
|
|
function persistCredentialsToLocalStorage(credentials: IMatrixClientCreds): void {
|
2020-06-03 21:23:01 +02:00
|
|
|
localStorage.setItem(HOMESERVER_URL_KEY, credentials.homeserverUrl);
|
2019-08-07 12:41:00 +02:00
|
|
|
if (credentials.identityServerUrl) {
|
2020-06-03 21:23:01 +02:00
|
|
|
localStorage.setItem(ID_SERVER_URL_KEY, credentials.identityServerUrl);
|
2019-08-07 12:41:00 +02:00
|
|
|
}
|
2017-06-16 15:33:14 +02:00
|
|
|
localStorage.setItem("mx_user_id", credentials.userId);
|
|
|
|
localStorage.setItem("mx_access_token", credentials.accessToken);
|
|
|
|
localStorage.setItem("mx_is_guest", JSON.stringify(credentials.guest));
|
|
|
|
|
2020-07-28 23:31:27 +02:00
|
|
|
if (credentials.pickleKey) {
|
2020-10-07 13:14:36 +02:00
|
|
|
localStorage.setItem("mx_has_pickle_key", String(true));
|
2020-07-28 23:31:27 +02:00
|
|
|
} else {
|
|
|
|
if (localStorage.getItem("mx_has_pickle_key")) {
|
|
|
|
console.error("Expected a pickle key, but none provided. Encryption may not work.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-16 15:33:14 +02:00
|
|
|
// if we didn't get a deviceId from the login, leave mx_device_id unset,
|
|
|
|
// rather than setting it to "undefined".
|
|
|
|
//
|
|
|
|
// (in this case MatrixClient doesn't bother with the crypto stuff
|
|
|
|
// - that's fine for us).
|
|
|
|
if (credentials.deviceId) {
|
|
|
|
localStorage.setItem("mx_device_id", credentials.deviceId);
|
|
|
|
}
|
|
|
|
|
2020-10-08 17:35:17 +02:00
|
|
|
SecurityCustomisations.persistCredentials?.(credentials);
|
|
|
|
|
2017-06-20 18:38:02 +02:00
|
|
|
console.log(`Session persisted for ${credentials.userId}`);
|
2017-06-16 15:33:14 +02:00
|
|
|
}
|
|
|
|
|
2018-07-15 23:33:00 +02:00
|
|
|
let _isLoggingOut = false;
|
|
|
|
|
2016-08-02 19:55:13 +02:00
|
|
|
/**
|
|
|
|
* Logs the current session out and transitions to the logged-out state
|
|
|
|
*/
|
2020-10-07 13:14:36 +02:00
|
|
|
export function logout(): void {
|
2017-12-05 12:53:49 +01:00
|
|
|
if (!MatrixClientPeg.get()) return;
|
2020-10-30 11:26:15 +01:00
|
|
|
if (!CountlyAnalytics.instance.disabled) {
|
|
|
|
// user has logged out, fall back to anonymous
|
2020-10-30 15:27:14 +01:00
|
|
|
CountlyAnalytics.instance.enable(/* anonymous = */ true);
|
2020-10-30 11:26:15 +01:00
|
|
|
}
|
2017-12-05 12:38:25 +01:00
|
|
|
|
|
|
|
if (MatrixClientPeg.get().isGuest()) {
|
2016-08-02 15:04:20 +02:00
|
|
|
// logout doesn't work for guest sessions
|
2020-11-12 13:46:55 +01:00
|
|
|
// Also we sometimes want to re-log in a guest session if we abort the login.
|
|
|
|
// defer until next tick because it calls a synchronous dispatch and we are likely here from a dispatch.
|
|
|
|
setImmediate(() => onLoggedOut());
|
2016-08-02 15:04:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-15 23:33:00 +02:00
|
|
|
_isLoggingOut = true;
|
2020-05-28 06:05:45 +02:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
PlatformPeg.get().destroyPickleKey(client.getUserId(), client.getDeviceId());
|
|
|
|
client.logout().then(onLoggedOut,
|
2016-08-03 11:01:23 +02:00
|
|
|
(err) => {
|
|
|
|
// Just throwing an error here is going to be very unhelpful
|
|
|
|
// if you're trying to log out because your server's down and
|
|
|
|
// you want to log into a different server, so just forget the
|
|
|
|
// access token. It's annoying that this will leave the access
|
|
|
|
// token still valid, but we should fix this by having access
|
|
|
|
// tokens expire (and if you really think you've been compromised,
|
|
|
|
// change your password).
|
|
|
|
console.log("Failed to call logout API: token will not be invalidated");
|
2016-08-03 12:47:29 +02:00
|
|
|
onLoggedOut();
|
2017-05-04 19:03:35 +02:00
|
|
|
},
|
2019-11-18 11:03:05 +01:00
|
|
|
);
|
2016-08-02 15:04:20 +02:00
|
|
|
}
|
|
|
|
|
2020-10-07 13:14:36 +02:00
|
|
|
export function softLogout(): void {
|
2019-07-04 00:46:37 +02:00
|
|
|
if (!MatrixClientPeg.get()) return;
|
|
|
|
|
|
|
|
// Track that we've detected and trapped a soft logout. This helps prevent other
|
|
|
|
// parts of the app from starting if there's no point (ie: don't sync if we've
|
|
|
|
// been soft logged out, despite having credentials and data for a MatrixClient).
|
|
|
|
localStorage.setItem("mx_soft_logout", "true");
|
|
|
|
|
2019-10-02 17:45:53 +02:00
|
|
|
// Dev note: please keep this log line around. It can be useful for track down
|
|
|
|
// random clients stopping in the middle of the logs.
|
|
|
|
console.log("Soft logout initiated");
|
2019-07-04 00:46:37 +02:00
|
|
|
_isLoggingOut = true; // to avoid repeated flags
|
2019-10-08 18:06:12 +02:00
|
|
|
// Ensure that we dispatch a view change **before** stopping the client so
|
|
|
|
// so that React components unmount first. This avoids React soft crashes
|
|
|
|
// that can occur when components try to use a null client.
|
2019-07-04 00:46:37 +02:00
|
|
|
dis.dispatch({action: 'on_client_not_viable'}); // generic version of on_logged_out
|
2019-10-08 18:06:12 +02:00
|
|
|
stopMatrixClient(/*unsetClient=*/false);
|
2019-07-04 00:46:37 +02:00
|
|
|
|
|
|
|
// DO NOT CALL LOGOUT. A soft logout preserves data, logout does not.
|
|
|
|
}
|
|
|
|
|
2020-10-07 13:14:36 +02:00
|
|
|
export function isSoftLogout(): boolean {
|
2019-07-04 00:46:37 +02:00
|
|
|
return localStorage.getItem("mx_soft_logout") === "true";
|
|
|
|
}
|
|
|
|
|
2020-10-07 13:14:36 +02:00
|
|
|
export function isLoggingOut(): boolean {
|
2018-07-15 23:33:00 +02:00
|
|
|
return _isLoggingOut;
|
|
|
|
}
|
|
|
|
|
2016-08-02 19:56:12 +02:00
|
|
|
/**
|
|
|
|
* Starts the matrix client and all other react-sdk services that
|
|
|
|
* listen for events while a session is logged in.
|
2019-07-04 00:46:37 +02:00
|
|
|
* @param {boolean} startSyncing True (default) to actually start
|
|
|
|
* syncing the client.
|
2016-08-02 19:56:12 +02:00
|
|
|
*/
|
2020-10-07 13:14:36 +02:00
|
|
|
async function startMatrixClient(startSyncing = true): Promise<void> {
|
2017-07-05 17:20:21 +02:00
|
|
|
console.log(`Lifecycle: Starting MatrixClient`);
|
|
|
|
|
2016-08-02 15:04:20 +02:00
|
|
|
// dispatch this before starting the matrix client: it's used
|
|
|
|
// to add listeners for the 'sync' event so otherwise we'd have
|
|
|
|
// a race condition (and we need to dispatch synchronously for this
|
|
|
|
// to work).
|
|
|
|
dis.dispatch({action: 'will_start_client'}, true);
|
|
|
|
|
2020-05-22 22:55:43 +02:00
|
|
|
// reset things first just in case
|
|
|
|
TypingStore.sharedInstance().reset();
|
|
|
|
ToastStore.sharedInstance().reset();
|
|
|
|
|
2016-08-02 15:04:20 +02:00
|
|
|
Notifier.start();
|
2019-03-08 13:46:38 +01:00
|
|
|
UserActivity.sharedInstance().start();
|
2016-09-27 10:56:31 +02:00
|
|
|
DMRoomMap.makeShared().start();
|
2019-08-10 01:35:59 +02:00
|
|
|
IntegrationManagers.sharedInstance().startWatching();
|
2018-07-24 17:21:43 +02:00
|
|
|
ActiveWidgetStore.start();
|
2020-11-23 17:20:15 +01:00
|
|
|
CallHandler.sharedInstance().start();
|
2016-08-02 19:58:18 +02:00
|
|
|
|
2019-10-31 20:28:00 +01:00
|
|
|
// Start Mjolnir even though we haven't checked the feature flag yet. Starting
|
|
|
|
// the thing just wastes CPU cycles, but should result in no actual functionality
|
|
|
|
// being exposed to the user.
|
|
|
|
Mjolnir.sharedInstance().start();
|
|
|
|
|
2019-07-04 00:46:37 +02:00
|
|
|
if (startSyncing) {
|
2020-01-20 10:06:20 +01:00
|
|
|
// The client might want to populate some views with events from the
|
|
|
|
// index (e.g. the FilePanel), therefore initialize the event index
|
|
|
|
// before the client.
|
2019-11-12 13:29:07 +01:00
|
|
|
await EventIndexPeg.init();
|
2020-01-15 12:06:29 +01:00
|
|
|
await MatrixClientPeg.start();
|
2019-07-04 00:46:37 +02:00
|
|
|
} else {
|
|
|
|
console.warn("Caller requested only auxiliary services be started");
|
2019-07-05 00:45:40 +02:00
|
|
|
await MatrixClientPeg.assign();
|
2019-07-04 00:46:37 +02:00
|
|
|
}
|
2017-11-05 04:13:23 +01:00
|
|
|
|
2020-01-17 12:43:35 +01:00
|
|
|
// This needs to be started after crypto is set up
|
|
|
|
DeviceListener.sharedInstance().start();
|
2020-04-07 13:07:41 +02:00
|
|
|
// Similarly, don't start sending presence updates until we've started
|
|
|
|
// the client
|
|
|
|
if (!SettingsStore.getValue("lowBandwidth")) {
|
|
|
|
Presence.start();
|
|
|
|
}
|
2020-01-17 12:43:35 +01:00
|
|
|
|
2020-04-07 00:04:41 +02:00
|
|
|
// Now that we have a MatrixClientPeg, update the Jitsi info
|
2020-06-03 14:58:17 +02:00
|
|
|
await Jitsi.getInstance().start();
|
2020-04-07 00:04:41 +02:00
|
|
|
|
2017-11-05 04:13:23 +01:00
|
|
|
// dispatch that we finished starting up to wire up any other bits
|
|
|
|
// of the matrix client that cannot be set prior to starting up.
|
|
|
|
dis.dispatch({action: 'client_started'});
|
2019-07-04 00:46:37 +02:00
|
|
|
|
|
|
|
if (isSoftLogout()) {
|
|
|
|
softLogout();
|
|
|
|
}
|
2016-08-02 15:04:20 +02:00
|
|
|
}
|
|
|
|
|
2016-08-04 11:49:34 +02:00
|
|
|
/*
|
2017-05-31 18:28:46 +02:00
|
|
|
* Stops a running client and all related services, and clears persistent
|
|
|
|
* storage. Used after a session has been logged out.
|
2016-08-04 11:49:34 +02:00
|
|
|
*/
|
2020-10-07 13:14:36 +02:00
|
|
|
export async function onLoggedOut(): Promise<void> {
|
2018-07-15 23:33:00 +02:00
|
|
|
_isLoggingOut = false;
|
2019-10-08 18:06:12 +02:00
|
|
|
// Ensure that we dispatch a view change **before** stopping the client so
|
|
|
|
// so that React components unmount first. This avoids React soft crashes
|
|
|
|
// that can occur when components try to use a null client.
|
2019-10-25 17:37:57 +02:00
|
|
|
dis.dispatch({action: 'on_logged_out'}, true);
|
2016-08-11 14:50:38 +02:00
|
|
|
stopMatrixClient();
|
2020-10-07 14:32:30 +02:00
|
|
|
await clearStorage({deleteEverything: true});
|
2020-11-27 12:19:44 +01:00
|
|
|
LifecycleCustomisations.onLoggedOutAndStorageCleared?.();
|
2016-08-02 15:04:20 +02:00
|
|
|
}
|
|
|
|
|
2017-05-31 18:28:46 +02:00
|
|
|
/**
|
2020-09-12 05:09:42 +02:00
|
|
|
* @param {object} opts Options for how to clear storage.
|
2017-05-31 18:28:46 +02:00
|
|
|
* @returns {Promise} promise which resolves once the stores have been cleared
|
|
|
|
*/
|
2020-10-07 14:32:30 +02:00
|
|
|
async function clearStorage(opts?: { deleteEverything?: boolean }): Promise<void> {
|
2020-02-13 01:39:28 +01:00
|
|
|
Analytics.disable();
|
2017-05-31 18:28:46 +02:00
|
|
|
|
|
|
|
if (window.localStorage) {
|
2020-09-12 04:20:33 +02:00
|
|
|
// try to save any 3pid invites from being obliterated
|
|
|
|
const pendingInvites = ThreepidInviteStore.instance.getWireInvites();
|
|
|
|
|
2017-05-31 18:28:46 +02:00
|
|
|
window.localStorage.clear();
|
2020-09-12 04:20:33 +02:00
|
|
|
|
|
|
|
// now restore those invites
|
2020-09-12 04:24:51 +02:00
|
|
|
if (!opts?.deleteEverything) {
|
|
|
|
pendingInvites.forEach(i => {
|
|
|
|
const roomId = i.roomId;
|
|
|
|
delete i.roomId; // delete to avoid confusing the store
|
|
|
|
ThreepidInviteStore.instance.storeInvite(roomId, i);
|
|
|
|
});
|
|
|
|
}
|
2016-09-02 12:25:10 +02:00
|
|
|
}
|
2017-05-31 18:28:46 +02:00
|
|
|
|
2020-04-06 17:40:53 +02:00
|
|
|
if (window.sessionStorage) {
|
|
|
|
window.sessionStorage.clear();
|
|
|
|
}
|
|
|
|
|
2017-05-31 18:28:46 +02:00
|
|
|
// create a temporary client to clear out the persistent stores.
|
|
|
|
const cli = createMatrixClient({
|
|
|
|
// we'll never make any requests, so can pass a bogus HS URL
|
|
|
|
baseUrl: "",
|
|
|
|
});
|
2019-11-13 09:52:59 +01:00
|
|
|
|
2019-11-19 11:05:37 +01:00
|
|
|
await EventIndexPeg.deleteEventIndex();
|
|
|
|
await cli.clearStores();
|
2016-09-02 12:25:10 +02:00
|
|
|
}
|
|
|
|
|
2016-08-04 11:49:34 +02:00
|
|
|
/**
|
2017-05-31 18:28:46 +02:00
|
|
|
* Stop all the background processes related to the current client.
|
2019-07-04 00:46:37 +02:00
|
|
|
* @param {boolean} unsetClient True (default) to abandon the client
|
|
|
|
* on MatrixClientPeg after stopping.
|
2016-08-04 11:49:34 +02:00
|
|
|
*/
|
2020-10-07 13:14:36 +02:00
|
|
|
export function stopMatrixClient(unsetClient = true): void {
|
2016-08-02 15:04:20 +02:00
|
|
|
Notifier.stop();
|
2020-11-23 17:41:57 +01:00
|
|
|
CallHandler.sharedInstance().stop();
|
2019-03-08 16:09:44 +01:00
|
|
|
UserActivity.sharedInstance().stop();
|
2019-06-27 06:36:55 +02:00
|
|
|
TypingStore.sharedInstance().reset();
|
2016-08-02 15:04:20 +02:00
|
|
|
Presence.stop();
|
2018-07-24 17:39:30 +02:00
|
|
|
ActiveWidgetStore.stop();
|
2019-08-10 01:35:59 +02:00
|
|
|
IntegrationManagers.sharedInstance().stopWatching();
|
2019-10-31 20:28:00 +01:00
|
|
|
Mjolnir.sharedInstance().stop();
|
2020-01-17 12:43:35 +01:00
|
|
|
DeviceListener.sharedInstance().stop();
|
2016-12-09 11:32:56 +01:00
|
|
|
if (DMRoomMap.shared()) DMRoomMap.shared().stop();
|
2019-11-12 15:58:38 +01:00
|
|
|
EventIndexPeg.stop();
|
2017-05-04 19:03:35 +02:00
|
|
|
const cli = MatrixClientPeg.get();
|
2016-08-11 14:50:38 +02:00
|
|
|
if (cli) {
|
|
|
|
cli.stopClient();
|
|
|
|
cli.removeAllListeners();
|
2019-07-04 00:46:37 +02:00
|
|
|
|
|
|
|
if (unsetClient) {
|
|
|
|
MatrixClientPeg.unset();
|
2019-11-19 11:05:37 +01:00
|
|
|
EventIndexPeg.unset();
|
2019-07-04 00:46:37 +02:00
|
|
|
}
|
2016-08-11 14:50:38 +02:00
|
|
|
}
|
2016-08-02 15:04:20 +02:00
|
|
|
}
|