2020-08-05 22:08:55 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
|
|
|
|
|
|
|
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-04-21 00:56:39 +05:30
|
|
|
import {createEnum} from "../utils/enum.js";
|
|
|
|
import {ObservableValue} from "../observable/ObservableValue.js";
|
|
|
|
import {HomeServerApi} from "./net/HomeServerApi.js";
|
2020-04-20 23:18:21 +05:30
|
|
|
import {Reconnector, ConnectionStatus} from "./net/Reconnector.js";
|
2020-04-21 00:56:39 +05:30
|
|
|
import {ExponentialRetryDelay} from "./net/ExponentialRetryDelay.js";
|
2020-04-20 23:18:21 +05:30
|
|
|
import {HomeServerError, ConnectionError, AbortError} from "./error.js";
|
|
|
|
import {Sync, SyncStatus} from "./Sync.js";
|
2020-04-21 00:56:39 +05:30
|
|
|
import {Session} from "./Session.js";
|
2020-04-10 02:49:49 +05:30
|
|
|
|
2020-04-18 22:46:16 +05:30
|
|
|
export const LoadStatus = createEnum(
|
|
|
|
"NotLoading",
|
|
|
|
"Login",
|
|
|
|
"LoginFailed",
|
2020-04-10 02:49:49 +05:30
|
|
|
"Loading",
|
|
|
|
"Migrating", //not used atm, but would fit here
|
2020-04-19 23:22:26 +05:30
|
|
|
"FirstSync",
|
2020-04-10 02:49:49 +05:30
|
|
|
"Error",
|
|
|
|
"Ready",
|
|
|
|
);
|
|
|
|
|
2020-04-18 22:46:16 +05:30
|
|
|
export const LoginFailure = createEnum(
|
2020-04-20 23:18:21 +05:30
|
|
|
"Connection",
|
2020-04-18 22:46:16 +05:30
|
|
|
"Credentials",
|
|
|
|
"Unknown",
|
|
|
|
);
|
|
|
|
|
|
|
|
export class SessionContainer {
|
2020-08-27 16:54:55 +05:30
|
|
|
constructor({clock, random, onlineStatus, request, storageFactory, sessionInfoStorage, olmPromise}) {
|
2020-04-18 22:46:16 +05:30
|
|
|
this._random = random;
|
|
|
|
this._clock = clock;
|
|
|
|
this._onlineStatus = onlineStatus;
|
|
|
|
this._request = request;
|
|
|
|
this._storageFactory = storageFactory;
|
2020-04-20 23:18:21 +05:30
|
|
|
this._sessionInfoStorage = sessionInfoStorage;
|
2020-04-18 22:46:16 +05:30
|
|
|
|
|
|
|
this._status = new ObservableValue(LoadStatus.NotLoading);
|
|
|
|
this._error = null;
|
|
|
|
this._loginFailure = null;
|
|
|
|
this._reconnector = null;
|
|
|
|
this._session = null;
|
|
|
|
this._sync = null;
|
2020-04-21 01:56:04 +05:30
|
|
|
this._sessionId = null;
|
|
|
|
this._storage = null;
|
2020-08-27 16:54:55 +05:30
|
|
|
this._olmPromise = olmPromise;
|
2020-04-10 02:49:49 +05:30
|
|
|
}
|
|
|
|
|
2020-04-21 02:19:14 +05:30
|
|
|
createNewSessionId() {
|
2020-04-18 22:46:16 +05:30
|
|
|
return (Math.floor(this._random() * Number.MAX_SAFE_INTEGER)).toString();
|
2020-04-10 02:49:49 +05:30
|
|
|
}
|
|
|
|
|
2020-04-18 22:46:16 +05:30
|
|
|
async startWithExistingSession(sessionId) {
|
|
|
|
if (this._status.get() !== LoadStatus.NotLoading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._status.set(LoadStatus.Loading);
|
|
|
|
try {
|
2020-04-20 23:18:21 +05:30
|
|
|
const sessionInfo = await this._sessionInfoStorage.get(sessionId);
|
2020-04-21 02:19:14 +05:30
|
|
|
if (!sessionInfo) {
|
|
|
|
throw new Error("Invalid session id: " + sessionId);
|
|
|
|
}
|
2020-08-27 22:42:06 +05:30
|
|
|
await this._loadSessionInfo(sessionInfo, false);
|
2020-04-18 22:46:16 +05:30
|
|
|
} catch (err) {
|
|
|
|
this._error = err;
|
|
|
|
this._status.set(LoadStatus.Error);
|
|
|
|
}
|
2020-04-10 02:49:49 +05:30
|
|
|
}
|
|
|
|
|
2020-04-18 22:46:16 +05:30
|
|
|
async startWithLogin(homeServer, username, password) {
|
|
|
|
if (this._status.get() !== LoadStatus.NotLoading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._status.set(LoadStatus.Login);
|
|
|
|
let sessionInfo;
|
|
|
|
try {
|
2020-05-06 02:46:51 +05:30
|
|
|
const hsApi = new HomeServerApi({homeServer, request: this._request, createTimeout: this._clock.createTimeout});
|
2020-04-18 22:46:16 +05:30
|
|
|
const loginData = await hsApi.passwordLogin(username, password).response();
|
2020-04-21 02:19:14 +05:30
|
|
|
const sessionId = this.createNewSessionId();
|
2020-04-18 22:46:16 +05:30
|
|
|
sessionInfo = {
|
|
|
|
id: sessionId,
|
|
|
|
deviceId: loginData.device_id,
|
|
|
|
userId: loginData.user_id,
|
|
|
|
homeServer: homeServer,
|
|
|
|
accessToken: loginData.access_token,
|
|
|
|
lastUsed: this._clock.now()
|
|
|
|
};
|
2020-04-20 23:18:21 +05:30
|
|
|
await this._sessionInfoStorage.add(sessionInfo);
|
2020-04-18 22:46:16 +05:30
|
|
|
} catch (err) {
|
|
|
|
this._error = err;
|
|
|
|
if (err instanceof HomeServerError) {
|
2020-04-23 00:19:21 +05:30
|
|
|
if (err.errcode === "M_FORBIDDEN") {
|
2020-04-18 22:46:16 +05:30
|
|
|
this._loginFailure = LoginFailure.Credentials;
|
|
|
|
} else {
|
|
|
|
this._loginFailure = LoginFailure.Unknown;
|
|
|
|
}
|
2020-04-23 00:19:03 +05:30
|
|
|
this._status.set(LoadStatus.LoginFailed);
|
2020-04-19 22:35:12 +05:30
|
|
|
} else if (err instanceof ConnectionError) {
|
2020-04-20 23:18:21 +05:30
|
|
|
this._loginFailure = LoginFailure.Connection;
|
2020-06-27 02:56:24 +05:30
|
|
|
this._status.set(LoadStatus.LoginFailed);
|
2020-04-18 22:46:16 +05:30
|
|
|
} else {
|
|
|
|
this._status.set(LoadStatus.Error);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// loading the session can only lead to
|
|
|
|
// LoadStatus.Error in case of an error,
|
|
|
|
// so separate try/catch
|
|
|
|
try {
|
2020-08-27 22:42:06 +05:30
|
|
|
await this._loadSessionInfo(sessionInfo, true);
|
2020-04-18 22:46:16 +05:30
|
|
|
} catch (err) {
|
|
|
|
this._error = err;
|
|
|
|
this._status.set(LoadStatus.Error);
|
2020-04-10 02:49:49 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 22:42:06 +05:30
|
|
|
async _loadSessionInfo(sessionInfo, isNewLogin) {
|
2020-04-18 22:46:16 +05:30
|
|
|
this._status.set(LoadStatus.Loading);
|
|
|
|
this._reconnector = new Reconnector({
|
|
|
|
onlineStatus: this._onlineStatus,
|
2020-04-23 00:18:25 +05:30
|
|
|
retryDelay: new ExponentialRetryDelay(this._clock.createTimeout),
|
2020-04-18 22:46:16 +05:30
|
|
|
createMeasure: this._clock.createMeasure
|
|
|
|
});
|
|
|
|
const hsApi = new HomeServerApi({
|
|
|
|
homeServer: sessionInfo.homeServer,
|
|
|
|
accessToken: sessionInfo.accessToken,
|
|
|
|
request: this._request,
|
|
|
|
reconnector: this._reconnector,
|
2020-05-06 02:46:51 +05:30
|
|
|
createTimeout: this._clock.createTimeout
|
2020-04-18 22:46:16 +05:30
|
|
|
});
|
2020-04-21 01:56:04 +05:30
|
|
|
this._sessionId = sessionInfo.id;
|
|
|
|
this._storage = await this._storageFactory.create(sessionInfo.id);
|
2020-04-18 22:46:16 +05:30
|
|
|
// no need to pass access token to session
|
|
|
|
const filteredSessionInfo = {
|
|
|
|
deviceId: sessionInfo.deviceId,
|
|
|
|
userId: sessionInfo.userId,
|
|
|
|
homeServer: sessionInfo.homeServer,
|
|
|
|
};
|
2020-08-27 16:54:55 +05:30
|
|
|
const olm = await this._olmPromise;
|
2020-09-02 18:00:18 +05:30
|
|
|
this._session = new Session({storage: this._storage,
|
|
|
|
sessionInfo: filteredSessionInfo, hsApi, olm, clock: this._clock});
|
2020-04-18 22:46:16 +05:30
|
|
|
await this._session.load();
|
2020-08-27 22:42:06 +05:30
|
|
|
await this._session.beforeFirstSync(isNewLogin);
|
2020-04-18 22:46:16 +05:30
|
|
|
|
2020-04-21 01:56:04 +05:30
|
|
|
this._sync = new Sync({hsApi, storage: this._storage, session: this._session});
|
2020-04-18 22:46:16 +05:30
|
|
|
// notify sync and session when back online
|
|
|
|
this._reconnectSubscription = this._reconnector.connectionStatus.subscribe(state => {
|
|
|
|
if (state === ConnectionStatus.Online) {
|
|
|
|
this._sync.start();
|
|
|
|
this._session.start(this._reconnector.lastVersionsResponse);
|
|
|
|
}
|
|
|
|
});
|
2020-04-19 22:32:10 +05:30
|
|
|
await this._waitForFirstSync();
|
2020-04-21 00:56:39 +05:30
|
|
|
|
2020-04-19 22:32:10 +05:30
|
|
|
this._status.set(LoadStatus.Ready);
|
|
|
|
|
2020-04-20 23:18:21 +05:30
|
|
|
// if the sync failed, and then the reconnector
|
|
|
|
// restored the connection, it would have already
|
|
|
|
// started to session, so check first
|
|
|
|
// to prevent an extra /versions request
|
2020-05-26 14:01:23 +05:30
|
|
|
|
|
|
|
// TODO: this doesn't look logical, but works. Why?
|
|
|
|
// I think because isStarted is true by default. That's probably not what we intend.
|
|
|
|
// I think there is a bug here, in that even if the reconnector already started the session, we'd still do this.
|
2020-05-07 22:16:45 +05:30
|
|
|
if (this._session.isStarted) {
|
2020-04-20 23:18:21 +05:30
|
|
|
const lastVersionsResponse = await hsApi.versions({timeout: 10000}).response();
|
|
|
|
this._session.start(lastVersionsResponse);
|
|
|
|
}
|
2020-04-19 22:32:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
async _waitForFirstSync() {
|
2020-04-18 22:46:16 +05:30
|
|
|
try {
|
2020-04-20 23:18:21 +05:30
|
|
|
this._sync.start();
|
2020-05-04 23:08:23 +05:30
|
|
|
this._status.set(LoadStatus.FirstSync);
|
2020-04-18 22:46:16 +05:30
|
|
|
} catch (err) {
|
2020-04-19 22:35:12 +05:30
|
|
|
// swallow ConnectionError here and continue,
|
2020-04-18 22:46:16 +05:30
|
|
|
// as the reconnector above will call
|
|
|
|
// sync.start again to retry in this case
|
2020-04-19 22:35:12 +05:30
|
|
|
if (!(err instanceof ConnectionError)) {
|
2020-04-18 22:46:16 +05:30
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// only transition into Ready once the first sync has succeeded
|
2020-06-27 02:56:24 +05:30
|
|
|
this._waitForFirstSyncHandle = this._sync.status.waitFor(s => s === SyncStatus.Syncing || s === SyncStatus.Stopped);
|
2020-04-19 22:32:10 +05:30
|
|
|
try {
|
|
|
|
await this._waitForFirstSyncHandle.promise;
|
2020-06-27 02:56:24 +05:30
|
|
|
if (this._sync.status.get() === SyncStatus.Stopped) {
|
|
|
|
if (this._sync.error) {
|
|
|
|
throw this._sync.error;
|
|
|
|
}
|
|
|
|
}
|
2020-04-19 22:32:10 +05:30
|
|
|
} catch (err) {
|
|
|
|
// if dispose is called from stop, bail out
|
|
|
|
if (err instanceof AbortError) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
} finally {
|
|
|
|
this._waitForFirstSyncHandle = null;
|
|
|
|
}
|
2020-04-18 22:46:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
get loadStatus() {
|
|
|
|
return this._status;
|
|
|
|
}
|
|
|
|
|
|
|
|
get loadError() {
|
|
|
|
return this._error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** only set at loadStatus InitialSync, CatchupSync or Ready */
|
2020-04-10 02:49:49 +05:30
|
|
|
get sync() {
|
|
|
|
return this._sync;
|
|
|
|
}
|
|
|
|
|
2020-04-18 22:46:16 +05:30
|
|
|
/** only set at loadStatus InitialSync, CatchupSync or Ready */
|
2020-04-10 02:49:49 +05:30
|
|
|
get session() {
|
|
|
|
return this._session;
|
|
|
|
}
|
|
|
|
|
2020-05-06 02:46:51 +05:30
|
|
|
get reconnector() {
|
|
|
|
return this._reconnector;
|
|
|
|
}
|
|
|
|
|
2020-04-18 22:46:16 +05:30
|
|
|
stop() {
|
2020-08-28 17:28:42 +05:30
|
|
|
if (this._reconnectSubscription) {
|
|
|
|
this._reconnectSubscription();
|
|
|
|
this._reconnectSubscription = null;
|
|
|
|
}
|
|
|
|
if (this._sync) {
|
|
|
|
this._sync.stop();
|
|
|
|
}
|
|
|
|
if (this._session) {
|
|
|
|
this._session.stop();
|
|
|
|
}
|
2020-04-19 22:32:10 +05:30
|
|
|
if (this._waitForFirstSyncHandle) {
|
|
|
|
this._waitForFirstSyncHandle.dispose();
|
|
|
|
this._waitForFirstSyncHandle = null;
|
|
|
|
}
|
2020-04-21 01:56:04 +05:30
|
|
|
if (this._storage) {
|
|
|
|
this._storage.close();
|
2020-04-21 02:19:14 +05:30
|
|
|
this._storage = null;
|
2020-04-21 01:56:04 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteSession() {
|
|
|
|
if (this._sessionId) {
|
|
|
|
// if one fails, don't block the other from trying
|
|
|
|
// also, run in parallel
|
|
|
|
await Promise.all([
|
|
|
|
this._storageFactory.delete(this._sessionId),
|
|
|
|
this._sessionInfoStorage.delete(this._sessionId),
|
|
|
|
]);
|
|
|
|
this._sessionId = null;
|
|
|
|
}
|
2020-04-10 02:49:49 +05:30
|
|
|
}
|
2020-04-18 22:46:16 +05:30
|
|
|
}
|