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.
|
|
|
|
*/
|
|
|
|
|
2021-12-22 21:49:37 +05:30
|
|
|
import {Client} from "../matrix/Client.js";
|
2020-04-21 00:56:39 +05:30
|
|
|
import {SessionViewModel} from "./session/SessionViewModel.js";
|
2020-10-08 21:53:37 +05:30
|
|
|
import {SessionLoadViewModel} from "./SessionLoadViewModel.js";
|
2021-08-15 16:21:00 +05:30
|
|
|
import {LoginViewModel} from "./login/LoginViewModel.js";
|
2022-01-17 21:00:22 +05:30
|
|
|
import {LogoutViewModel} from "./LogoutViewModel.js";
|
2020-04-21 00:56:39 +05:30
|
|
|
import {SessionPickerViewModel} from "./SessionPickerViewModel.js";
|
2020-05-04 22:53:11 +05:30
|
|
|
import {ViewModel} from "./ViewModel.js";
|
2019-07-30 02:09:56 +05:30
|
|
|
|
2020-10-09 13:29:59 +05:30
|
|
|
export class RootViewModel extends ViewModel {
|
2020-05-06 02:44:58 +05:30
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2019-07-30 02:09:56 +05:30
|
|
|
this._error = null;
|
|
|
|
this._sessionPickerViewModel = null;
|
2020-10-09 20:33:38 +05:30
|
|
|
this._sessionLoadViewModel = null;
|
|
|
|
this._loginViewModel = null;
|
2022-01-17 21:00:22 +05:30
|
|
|
this._logoutViewModel = null;
|
2020-10-09 20:33:38 +05:30
|
|
|
this._sessionViewModel = null;
|
2021-12-22 21:39:52 +05:30
|
|
|
this._pendingClient = null;
|
2020-10-08 21:53:37 +05:30
|
|
|
}
|
|
|
|
|
2020-10-16 16:16:14 +05:30
|
|
|
async load() {
|
2020-10-09 20:33:38 +05:30
|
|
|
this.track(this.navigation.observe("login").subscribe(() => this._applyNavigation()));
|
|
|
|
this.track(this.navigation.observe("session").subscribe(() => this._applyNavigation()));
|
2021-08-01 20:41:57 +05:30
|
|
|
this.track(this.navigation.observe("sso").subscribe(() => this._applyNavigation()));
|
2021-05-18 17:57:09 +05:30
|
|
|
this._applyNavigation(true);
|
2020-10-13 18:13:31 +05:30
|
|
|
}
|
|
|
|
|
2021-05-18 17:57:09 +05:30
|
|
|
async _applyNavigation(shouldRestoreLastUrl) {
|
2022-01-17 21:00:22 +05:30
|
|
|
const isLogin = this.navigation.path.get("login");
|
|
|
|
const logoutSessionId = this.navigation.path.get("logout")?.value;
|
2021-08-20 22:53:15 +05:30
|
|
|
const sessionId = this.navigation.path.get("session")?.value;
|
|
|
|
const loginToken = this.navigation.path.get("sso")?.value;
|
2020-10-13 18:13:31 +05:30
|
|
|
if (isLogin) {
|
|
|
|
if (this.activeSection !== "login") {
|
|
|
|
this._showLogin();
|
|
|
|
}
|
2022-01-17 21:00:22 +05:30
|
|
|
} else if (logoutSessionId) {
|
|
|
|
if (this.activeSection !== "logout") {
|
|
|
|
this._showLogout(logoutSessionId);
|
|
|
|
}
|
2020-10-13 18:13:31 +05:30
|
|
|
} else if (sessionId === true) {
|
|
|
|
if (this.activeSection !== "picker") {
|
|
|
|
this._showPicker();
|
|
|
|
}
|
|
|
|
} else if (sessionId) {
|
|
|
|
if (!this._sessionViewModel || this._sessionViewModel.id !== sessionId) {
|
2021-12-22 21:39:52 +05:30
|
|
|
// see _showLogin for where _pendingClient comes from
|
|
|
|
if (this._pendingClient && this._pendingClient.sessionId === sessionId) {
|
|
|
|
const client = this._pendingClient;
|
|
|
|
this._pendingClient = null;
|
|
|
|
this._showSession(client);
|
2020-10-16 19:16:06 +05:30
|
|
|
} else {
|
|
|
|
// this should never happen, but we want to be sure not to leak it
|
2021-12-22 21:39:52 +05:30
|
|
|
if (this._pendingClient) {
|
|
|
|
this._pendingClient.dispose();
|
|
|
|
this._pendingClient = null;
|
2020-10-16 19:16:06 +05:30
|
|
|
}
|
|
|
|
this._showSessionLoader(sessionId);
|
|
|
|
}
|
2020-10-13 18:13:31 +05:30
|
|
|
}
|
2021-08-20 22:53:15 +05:30
|
|
|
} else if (loginToken) {
|
2021-08-15 16:21:00 +05:30
|
|
|
this.urlCreator.normalizeUrl();
|
|
|
|
if (this.activeSection !== "login") {
|
2021-08-20 22:53:15 +05:30
|
|
|
this._showLogin(loginToken);
|
2021-08-15 16:21:00 +05:30
|
|
|
}
|
2021-07-31 17:07:44 +05:30
|
|
|
}
|
|
|
|
else {
|
2020-10-09 20:33:38 +05:30
|
|
|
try {
|
2021-05-18 17:57:09 +05:30
|
|
|
if (!(shouldRestoreLastUrl && this.urlCreator.tryRestoreLastUrl())) {
|
2020-10-26 21:01:30 +05:30
|
|
|
const sessionInfos = await this.platform.sessionInfoStorage.getAll();
|
2020-10-16 16:16:14 +05:30
|
|
|
if (sessionInfos.length === 0) {
|
|
|
|
this.navigation.push("login");
|
|
|
|
} else if (sessionInfos.length === 1) {
|
|
|
|
this.navigation.push("session", sessionInfos[0].id);
|
|
|
|
} else {
|
|
|
|
this.navigation.push("session");
|
|
|
|
}
|
2020-10-14 18:40:32 +05:30
|
|
|
}
|
2020-10-09 20:33:38 +05:30
|
|
|
} catch (err) {
|
|
|
|
this._setSection(() => this._error = err);
|
2020-10-07 13:10:51 +05:30
|
|
|
}
|
2020-10-09 20:33:38 +05:30
|
|
|
}
|
|
|
|
}
|
2019-07-30 02:09:56 +05:30
|
|
|
|
|
|
|
async _showPicker() {
|
2020-04-10 02:49:49 +05:30
|
|
|
this._setSection(() => {
|
2020-10-26 20:14:11 +05:30
|
|
|
this._sessionPickerViewModel = new SessionPickerViewModel(this.childOptions());
|
2019-07-30 02:09:56 +05:30
|
|
|
});
|
|
|
|
try {
|
|
|
|
await this._sessionPickerViewModel.load();
|
|
|
|
} catch (err) {
|
2020-04-10 02:49:49 +05:30
|
|
|
this._setSection(() => this._error = err);
|
2019-07-30 02:09:56 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-17 17:53:59 +05:30
|
|
|
_showLogin(loginToken) {
|
2020-04-10 02:49:49 +05:30
|
|
|
this._setSection(() => {
|
2020-10-09 20:33:38 +05:30
|
|
|
this._loginViewModel = new LoginViewModel(this.childOptions({
|
2021-08-23 22:56:39 +05:30
|
|
|
defaultHomeserver: this.platform.config["defaultHomeServer"],
|
2021-12-22 21:39:52 +05:30
|
|
|
ready: client => {
|
2020-10-16 19:16:06 +05:30
|
|
|
// we don't want to load the session container again,
|
|
|
|
// but we also want the change of screen to go through the navigation
|
|
|
|
// so we store the session container in a temporary variable that will be
|
|
|
|
// consumed by _applyNavigation, triggered by the navigation change
|
|
|
|
//
|
|
|
|
// Also, we should not call _setSection before the navigation is in the correct state,
|
|
|
|
// as url creation (e.g. in RoomTileViewModel)
|
|
|
|
// won't be using the correct navigation base path.
|
2021-12-22 21:39:52 +05:30
|
|
|
this._pendingClient = client;
|
|
|
|
this.navigation.push("session", client.sessionId);
|
2020-10-09 20:33:38 +05:30
|
|
|
},
|
2021-08-20 22:53:15 +05:30
|
|
|
loginToken
|
2020-10-09 20:33:38 +05:30
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-17 21:00:22 +05:30
|
|
|
_showLogout(sessionId) {
|
|
|
|
this._setSection(() => {
|
|
|
|
this._logoutViewModel = new LogoutViewModel(this.childOptions({sessionId}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-22 21:39:52 +05:30
|
|
|
_showSession(client) {
|
2020-10-09 20:33:38 +05:30
|
|
|
this._setSection(() => {
|
2021-12-22 21:39:52 +05:30
|
|
|
this._sessionViewModel = new SessionViewModel(this.childOptions({client}));
|
2020-10-09 20:33:38 +05:30
|
|
|
this._sessionViewModel.start();
|
2020-10-08 21:53:37 +05:30
|
|
|
});
|
|
|
|
}
|
2019-07-30 02:09:56 +05:30
|
|
|
|
2020-10-08 21:53:37 +05:30
|
|
|
_showSessionLoader(sessionId) {
|
2021-12-22 21:49:37 +05:30
|
|
|
const client = new Client(this.platform);
|
2021-12-22 21:39:52 +05:30
|
|
|
client.startWithExistingSession(sessionId);
|
2020-10-08 21:53:37 +05:30
|
|
|
this._setSection(() => {
|
2021-05-18 18:57:31 +05:30
|
|
|
this._sessionLoadViewModel = new SessionLoadViewModel(this.childOptions({
|
2021-12-22 21:39:52 +05:30
|
|
|
client,
|
|
|
|
ready: client => this._showSession(client)
|
2021-05-18 18:57:31 +05:30
|
|
|
}));
|
2020-10-08 21:53:37 +05:30
|
|
|
this._sessionLoadViewModel.start();
|
|
|
|
});
|
2019-07-30 02:09:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get activeSection() {
|
|
|
|
if (this._error) {
|
|
|
|
return "error";
|
|
|
|
} else if (this._sessionViewModel) {
|
|
|
|
return "session";
|
|
|
|
} else if (this._loginViewModel) {
|
|
|
|
return "login";
|
2022-01-17 21:00:22 +05:30
|
|
|
} else if (this._logoutViewModel) {
|
|
|
|
return "logout";
|
2020-10-08 21:53:37 +05:30
|
|
|
} else if (this._sessionPickerViewModel) {
|
2019-07-30 02:09:56 +05:30
|
|
|
return "picker";
|
2020-10-08 21:53:37 +05:30
|
|
|
} else if (this._sessionLoadViewModel) {
|
|
|
|
return "loading";
|
|
|
|
} else {
|
|
|
|
return "redirecting";
|
2019-07-30 02:09:56 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 02:49:49 +05:30
|
|
|
_setSection(setter) {
|
|
|
|
// clear all members the activeSection depends on
|
|
|
|
this._error = null;
|
2020-10-09 20:33:38 +05:30
|
|
|
this._sessionPickerViewModel = this.disposeTracked(this._sessionPickerViewModel);
|
|
|
|
this._sessionLoadViewModel = this.disposeTracked(this._sessionLoadViewModel);
|
|
|
|
this._loginViewModel = this.disposeTracked(this._loginViewModel);
|
2022-01-17 21:00:22 +05:30
|
|
|
this._logoutViewModel = this.disposeTracked(this._logoutViewModel);
|
2020-10-09 20:33:38 +05:30
|
|
|
this._sessionViewModel = this.disposeTracked(this._sessionViewModel);
|
2020-04-10 02:49:49 +05:30
|
|
|
// now set it again
|
|
|
|
setter();
|
2020-10-09 20:33:38 +05:30
|
|
|
this._sessionPickerViewModel && this.track(this._sessionPickerViewModel);
|
|
|
|
this._sessionLoadViewModel && this.track(this._sessionLoadViewModel);
|
|
|
|
this._loginViewModel && this.track(this._loginViewModel);
|
2022-01-17 21:00:22 +05:30
|
|
|
this._logoutViewModel && this.track(this._logoutViewModel);
|
2020-10-09 20:33:38 +05:30
|
|
|
this._sessionViewModel && this.track(this._sessionViewModel);
|
2020-05-04 22:53:11 +05:30
|
|
|
this.emitChange("activeSection");
|
2020-04-10 02:49:49 +05:30
|
|
|
}
|
|
|
|
|
2020-04-21 02:40:13 +05:30
|
|
|
get error() { return this._error; }
|
2019-07-30 02:09:56 +05:30
|
|
|
get sessionViewModel() { return this._sessionViewModel; }
|
|
|
|
get loginViewModel() { return this._loginViewModel; }
|
2022-01-17 21:00:22 +05:30
|
|
|
get logoutViewModel() { return this._logoutViewModel; }
|
2019-07-30 02:09:56 +05:30
|
|
|
get sessionPickerViewModel() { return this._sessionPickerViewModel; }
|
2020-10-08 21:53:37 +05:30
|
|
|
get sessionLoadViewModel() { return this._sessionLoadViewModel; }
|
2019-07-30 02:09:56 +05:30
|
|
|
}
|