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-23 12:36:04 +05:30
|
|
|
import {LoadStatus, LoginFailure} from "../matrix/SessionContainer.js";
|
|
|
|
import {SyncStatus} from "../matrix/Sync.js";
|
2020-05-04 22:53:11 +05:30
|
|
|
import {ViewModel} from "./ViewModel.js";
|
2020-04-23 12:36:04 +05:30
|
|
|
|
2020-05-04 22:53:11 +05:30
|
|
|
export class SessionLoadViewModel extends ViewModel {
|
2020-05-06 02:44:58 +05:30
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2020-10-09 20:29:34 +05:30
|
|
|
const {createAndStartSessionContainer, ready, homeserver, deleteSessionOnCancel} = options;
|
2020-04-23 12:36:04 +05:30
|
|
|
this._createAndStartSessionContainer = createAndStartSessionContainer;
|
2020-10-09 20:29:34 +05:30
|
|
|
this._ready = ready;
|
2020-04-23 12:36:04 +05:30
|
|
|
this._homeserver = homeserver;
|
2020-04-29 13:40:20 +05:30
|
|
|
this._deleteSessionOnCancel = deleteSessionOnCancel;
|
2020-04-23 12:36:04 +05:30
|
|
|
this._loading = false;
|
|
|
|
this._error = null;
|
2021-05-18 18:57:31 +05:30
|
|
|
this.backUrl = this.urlCreator.urlForSegment("session", true);
|
2020-04-23 12:36:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
async start() {
|
|
|
|
if (this._loading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
this._loading = true;
|
2020-06-25 02:12:39 +05:30
|
|
|
this.emitChange("loading");
|
2021-07-30 13:45:35 +05:30
|
|
|
this._sessionContainer = await this._createAndStartSessionContainer();
|
2020-04-23 12:36:04 +05:30
|
|
|
this._waitHandle = this._sessionContainer.loadStatus.waitFor(s => {
|
2020-06-25 02:12:39 +05:30
|
|
|
this.emitChange("loadLabel");
|
2020-04-23 12:36:04 +05:30
|
|
|
// wait for initial sync, but not catchup sync
|
|
|
|
const isCatchupSync = s === LoadStatus.FirstSync &&
|
2020-05-04 23:08:23 +05:30
|
|
|
this._sessionContainer.sync.status.get() === SyncStatus.CatchupSync;
|
2020-04-23 12:36:04 +05:30
|
|
|
return isCatchupSync ||
|
|
|
|
s === LoadStatus.LoginFailed ||
|
|
|
|
s === LoadStatus.Error ||
|
|
|
|
s === LoadStatus.Ready;
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
await this._waitHandle.promise;
|
|
|
|
} catch (err) {
|
2020-04-29 13:40:20 +05:30
|
|
|
return; // aborted by goBack
|
2020-04-23 12:36:04 +05:30
|
|
|
}
|
|
|
|
// TODO: should we deal with no connection during initial sync
|
|
|
|
// and we're retrying as well here?
|
|
|
|
// e.g. show in the label what is going on wrt connectionstatus
|
|
|
|
// much like we will once you are in the app. Probably a good idea
|
|
|
|
|
|
|
|
// did it finish or get stuck at LoginFailed or Error?
|
|
|
|
const loadStatus = this._sessionContainer.loadStatus.get();
|
2020-10-09 20:29:34 +05:30
|
|
|
const loadError = this._sessionContainer.loadError;
|
2020-04-23 12:36:04 +05:30
|
|
|
if (loadStatus === LoadStatus.FirstSync || loadStatus === LoadStatus.Ready) {
|
2020-10-09 20:29:34 +05:30
|
|
|
const sessionContainer = this._sessionContainer;
|
|
|
|
// session container is ready,
|
|
|
|
// don't dispose it anymore when
|
|
|
|
// we get disposed
|
|
|
|
this._sessionContainer = null;
|
|
|
|
this._ready(sessionContainer);
|
2020-04-23 12:36:04 +05:30
|
|
|
}
|
2020-10-09 20:29:34 +05:30
|
|
|
if (loadError) {
|
|
|
|
console.error("session load error", loadError);
|
2020-09-02 18:24:57 +05:30
|
|
|
}
|
2020-04-23 12:36:04 +05:30
|
|
|
} catch (err) {
|
|
|
|
this._error = err;
|
2020-09-02 18:24:57 +05:30
|
|
|
console.error("error thrown during session load", err.stack);
|
2020-04-23 12:36:04 +05:30
|
|
|
} finally {
|
|
|
|
this._loading = false;
|
2020-06-27 02:56:24 +05:30
|
|
|
// loadLabel in case of sc.loadError also gets updated through this
|
2020-06-25 02:12:39 +05:30
|
|
|
this.emitChange("loading");
|
2020-04-23 12:36:04 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-09 20:29:34 +05:30
|
|
|
dispose() {
|
|
|
|
if (this._sessionContainer) {
|
|
|
|
this._sessionContainer.dispose();
|
|
|
|
this._sessionContainer = null;
|
|
|
|
}
|
|
|
|
if (this._waitHandle) {
|
|
|
|
// rejects with AbortError
|
|
|
|
this._waitHandle.dispose();
|
|
|
|
this._waitHandle = null;
|
2020-04-23 12:36:04 +05:30
|
|
|
}
|
2020-04-29 13:40:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// to show a spinner or not
|
|
|
|
get loading() {
|
|
|
|
return this._loading;
|
2020-04-23 12:36:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get loadLabel() {
|
|
|
|
const sc = this._sessionContainer;
|
|
|
|
const error = this._error || (sc && sc.loadError);
|
|
|
|
|
|
|
|
if (error || (sc && sc.loadStatus.get() === LoadStatus.Error)) {
|
|
|
|
return `Something went wrong: ${error && error.message}.`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sc) {
|
|
|
|
switch (sc.loadStatus.get()) {
|
|
|
|
case LoadStatus.NotLoading:
|
|
|
|
return `Preparing…`;
|
|
|
|
case LoadStatus.Login:
|
2021-08-15 12:26:45 +05:30
|
|
|
return `Checking your credentials…`;
|
2020-04-23 12:36:04 +05:30
|
|
|
case LoadStatus.LoginFailed:
|
|
|
|
switch (sc.loginFailure) {
|
|
|
|
case LoginFailure.LoginFailure:
|
2021-08-15 12:26:45 +05:30
|
|
|
return `Your credentials don't seem to be correct.`;
|
2020-04-23 12:36:04 +05:30
|
|
|
case LoginFailure.Connection:
|
|
|
|
return `Can't connect to ${this._homeserver}.`;
|
|
|
|
case LoginFailure.Unknown:
|
2021-08-15 12:26:45 +05:30
|
|
|
return `Something went wrong while checking your credentials.`;
|
2020-04-23 12:36:04 +05:30
|
|
|
}
|
|
|
|
break;
|
2020-09-08 20:46:34 +05:30
|
|
|
case LoadStatus.SessionSetup:
|
|
|
|
return `Setting up your encryption keys…`;
|
2020-04-23 12:36:04 +05:30
|
|
|
case LoadStatus.Loading:
|
|
|
|
return `Loading your conversations…`;
|
|
|
|
case LoadStatus.FirstSync:
|
|
|
|
return `Getting your conversations from the server…`;
|
|
|
|
default:
|
|
|
|
return this._sessionContainer.loadStatus.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return `Preparing…`;
|
|
|
|
}
|
|
|
|
}
|