From 749e038a470b74d96b2f3a55b901dfe3370f84de Mon Sep 17 00:00:00 2001 From: Isaiah Becker-Mayer Date: Sun, 14 Aug 2022 11:05:13 -0700 Subject: [PATCH 1/3] typescript-ifying PasswordLoginViewModel --- src/domain/login/LoginViewModel.ts | 6 ++-- ...ViewModel.js => PasswordLoginViewModel.ts} | 32 ++++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-) rename src/domain/login/{PasswordLoginViewModel.js => PasswordLoginViewModel.ts} (66%) diff --git a/src/domain/login/LoginViewModel.ts b/src/domain/login/LoginViewModel.ts index 8eb11a9e..40f4b5f7 100644 --- a/src/domain/login/LoginViewModel.ts +++ b/src/domain/login/LoginViewModel.ts @@ -16,7 +16,7 @@ limitations under the License. import {Client} from "../../matrix/Client.js"; import {Options as BaseOptions, ViewModel} from "../ViewModel"; -import {PasswordLoginViewModel} from "./PasswordLoginViewModel.js"; +import {PasswordLoginViewModel} from "./PasswordLoginViewModel"; import {StartSSOLoginViewModel} from "./StartSSOLoginViewModel.js"; import {CompleteSSOLoginViewModel} from "./CompleteSSOLoginViewModel.js"; import {LoadStatus} from "../../matrix/Client.js"; @@ -60,7 +60,7 @@ export class LoginViewModel extends ViewModel { this._initViewModels(); } - get passwordLoginViewModel(): PasswordLoginViewModel { + get passwordLoginViewModel(): PasswordLoginViewModel | undefined { return this._passwordLoginViewModel; } @@ -285,7 +285,7 @@ export class LoginViewModel extends ViewModel { type ReadyFn = (client: Client) => void; // TODO: move to Client.js when its converted to typescript. -type LoginOptions = { +export type LoginOptions = { homeserver: string; password?: (username: string, password: string) => PasswordLoginMethod; sso?: SSOLoginHelper; diff --git a/src/domain/login/PasswordLoginViewModel.js b/src/domain/login/PasswordLoginViewModel.ts similarity index 66% rename from src/domain/login/PasswordLoginViewModel.js rename to src/domain/login/PasswordLoginViewModel.ts index 7c4ff78a..6b4d9978 100644 --- a/src/domain/login/PasswordLoginViewModel.js +++ b/src/domain/login/PasswordLoginViewModel.ts @@ -14,43 +14,53 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {ViewModel} from "../ViewModel"; import {LoginFailure} from "../../matrix/Client.js"; +import type {PasswordLoginMethod} from "../../matrix/login"; +import {Options as BaseOptions, ViewModel} from "../ViewModel"; +import type {LoginOptions} from "./LoginViewModel"; + +type Options = { + loginOptions: LoginOptions | undefined; + attemptLogin: (loginMethod: PasswordLoginMethod) => Promise; +} & BaseOptions export class PasswordLoginViewModel extends ViewModel { - constructor(options) { + private _loginOptions?: LoginOptions; + private _attemptLogin: (loginMethod: PasswordLoginMethod) => Promise; + private _isBusy = false; + private _errorMessage = ""; + + constructor(options: Options) { super(options); const {loginOptions, attemptLogin} = options; this._loginOptions = loginOptions; this._attemptLogin = attemptLogin; - this._isBusy = false; - this._errorMessage = ""; } - get isBusy() { return this._isBusy; } - get errorMessage() { return this._errorMessage; } + get isBusy(): boolean { return this._isBusy; } + get errorMessage(): string { return this._errorMessage; } - setBusy(status) { + setBusy(status: boolean): void { this._isBusy = status; this.emitChange("isBusy"); } - _showError(message) { + _showError(message: string): void { this._errorMessage = message; this.emitChange("errorMessage"); } - async login(username, password) { + async login(username: string, password: string): Promise{ this._errorMessage = ""; this.emitChange("errorMessage"); - const status = await this._attemptLogin(this._loginOptions.password(username, password)); + const status = await this._attemptLogin(this._loginOptions!.password!(username, password)); let error = ""; switch (status) { case LoginFailure.Credentials: error = this.i18n`Your username and/or password don't seem to be correct.`; break; case LoginFailure.Connection: - error = this.i18n`Can't connect to ${this._loginOptions.homeserver}.`; + error = this.i18n`Can't connect to ${this._loginOptions!.homeserver}.`; break; case LoginFailure.Unknown: error = this.i18n`Something went wrong while checking your login and password.`; From 53c4ecb69b36fa6963bd9a538c7d986c64059596 Mon Sep 17 00:00:00 2001 From: Isaiah Becker-Mayer Date: Sun, 14 Aug 2022 11:14:54 -0700 Subject: [PATCH 2/3] typescript-ifying StartSSOLoginViewModel --- src/domain/login/LoginViewModel.ts | 4 +-- ...ViewModel.js => StartSSOLoginViewModel.ts} | 30 ++++++++++++------- 2 files changed, 22 insertions(+), 12 deletions(-) rename src/domain/login/{StartSSOLoginViewModel.js => StartSSOLoginViewModel.ts} (56%) diff --git a/src/domain/login/LoginViewModel.ts b/src/domain/login/LoginViewModel.ts index 40f4b5f7..43f0334a 100644 --- a/src/domain/login/LoginViewModel.ts +++ b/src/domain/login/LoginViewModel.ts @@ -17,7 +17,7 @@ limitations under the License. import {Client} from "../../matrix/Client.js"; import {Options as BaseOptions, ViewModel} from "../ViewModel"; import {PasswordLoginViewModel} from "./PasswordLoginViewModel"; -import {StartSSOLoginViewModel} from "./StartSSOLoginViewModel.js"; +import {StartSSOLoginViewModel} from "./StartSSOLoginViewModel"; import {CompleteSSOLoginViewModel} from "./CompleteSSOLoginViewModel.js"; import {LoadStatus} from "../../matrix/Client.js"; import {SessionLoadViewModel} from "../SessionLoadViewModel.js"; @@ -64,7 +64,7 @@ export class LoginViewModel extends ViewModel { return this._passwordLoginViewModel; } - get startSSOLoginViewModel(): StartSSOLoginViewModel { + get startSSOLoginViewModel(): StartSSOLoginViewModel | undefined { return this._startSSOLoginViewModel; } diff --git a/src/domain/login/StartSSOLoginViewModel.js b/src/domain/login/StartSSOLoginViewModel.ts similarity index 56% rename from src/domain/login/StartSSOLoginViewModel.js rename to src/domain/login/StartSSOLoginViewModel.ts index dba0bcb5..b85bec71 100644 --- a/src/domain/login/StartSSOLoginViewModel.js +++ b/src/domain/login/StartSSOLoginViewModel.ts @@ -14,25 +14,35 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {ViewModel} from "../ViewModel"; +import type {SSOLoginHelper} from "../../matrix/login"; +import {Options as BaseOptions, ViewModel} from "../ViewModel"; +import type {LoginOptions} from "./LoginViewModel"; + + +type Options = { + loginOptions: LoginOptions | undefined; +} & BaseOptions; export class StartSSOLoginViewModel extends ViewModel{ - constructor(options) { + private _sso?: SSOLoginHelper; + private _isBusy = false; + + constructor(options: Options) { super(options); - this._sso = options.loginOptions.sso; + this._sso = options.loginOptions!.sso; this._isBusy = false; } - - get isBusy() { return this._isBusy; } - - setBusy(status) { + + get isBusy(): boolean { return this._isBusy; } + + setBusy(status: boolean): void { this._isBusy = status; this.emitChange("isBusy"); } - async startSSOLogin() { - await this.platform.settingsStorage.setString("sso_ongoing_login_homeserver", this._sso.homeserver); - const link = this._sso.createSSORedirectURL(this.urlCreator.createSSOCallbackURL()); + async startSSOLogin(): Promise { + await this.platform.settingsStorage.setString("sso_ongoing_login_homeserver", this._sso!.homeserver); + const link = this._sso!.createSSORedirectURL(this.urlCreator.createSSOCallbackURL()); this.platform.openUrl(link); } } From c1161e5332cc64ab3c407f27495149d84c8da272 Mon Sep 17 00:00:00 2001 From: Isaiah Becker-Mayer Date: Sun, 14 Aug 2022 11:27:25 -0700 Subject: [PATCH 3/3] typescript-ifying CompleteSSOLoginViewModel --- ...wModel.js => CompleteSSOLoginViewModel.ts} | 27 ++++++++++++++----- src/domain/login/LoginViewModel.ts | 4 +-- 2 files changed, 22 insertions(+), 9 deletions(-) rename src/domain/login/{CompleteSSOLoginViewModel.js => CompleteSSOLoginViewModel.ts} (72%) diff --git a/src/domain/login/CompleteSSOLoginViewModel.js b/src/domain/login/CompleteSSOLoginViewModel.ts similarity index 72% rename from src/domain/login/CompleteSSOLoginViewModel.js rename to src/domain/login/CompleteSSOLoginViewModel.ts index d41d53ec..b7be4642 100644 --- a/src/domain/login/CompleteSSOLoginViewModel.js +++ b/src/domain/login/CompleteSSOLoginViewModel.ts @@ -14,11 +14,24 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {ViewModel} from "../ViewModel"; +import {Options as BaseOptions, ViewModel} from "../ViewModel"; import {LoginFailure} from "../../matrix/Client.js"; +import type {TokenLoginMethod} from "../../matrix/login"; +import { Client } from "../../matrix/Client.js"; + +type Options = { + client: Client; + attemptLogin: (loginMethod: TokenLoginMethod) => Promise; + loginToken: string; +} & BaseOptions export class CompleteSSOLoginViewModel extends ViewModel { - constructor(options) { + private _loginToken: string; + private _client: Client; + private _attemptLogin: (loginMethod: TokenLoginMethod) => Promise; + private _errorMessage = ""; + + constructor(options: Options) { super(options); const { loginToken, @@ -29,22 +42,22 @@ export class CompleteSSOLoginViewModel extends ViewModel { this._client = client; this._attemptLogin = attemptLogin; this._errorMessage = ""; - this.performSSOLoginCompletion(); + void this.performSSOLoginCompletion(); } - get errorMessage() { return this._errorMessage; } + get errorMessage(): string { return this._errorMessage; } - _showError(message) { + _showError(message: string): void { this._errorMessage = message; this.emitChange("errorMessage"); } - async performSSOLoginCompletion() { + async performSSOLoginCompletion(): Promise { if (!this._loginToken) { return; } const homeserver = await this.platform.settingsStorage.getString("sso_ongoing_login_homeserver"); - let loginOptions; + let loginOptions: { token?: (loginToken: string) => TokenLoginMethod; }; try { loginOptions = await this._client.queryLogin(homeserver).result; } diff --git a/src/domain/login/LoginViewModel.ts b/src/domain/login/LoginViewModel.ts index 43f0334a..75d57880 100644 --- a/src/domain/login/LoginViewModel.ts +++ b/src/domain/login/LoginViewModel.ts @@ -18,7 +18,7 @@ import {Client} from "../../matrix/Client.js"; import {Options as BaseOptions, ViewModel} from "../ViewModel"; import {PasswordLoginViewModel} from "./PasswordLoginViewModel"; import {StartSSOLoginViewModel} from "./StartSSOLoginViewModel"; -import {CompleteSSOLoginViewModel} from "./CompleteSSOLoginViewModel.js"; +import {CompleteSSOLoginViewModel} from "./CompleteSSOLoginViewModel"; import {LoadStatus} from "../../matrix/Client.js"; import {SessionLoadViewModel} from "../SessionLoadViewModel.js"; import {SegmentType} from "../navigation/index"; @@ -68,7 +68,7 @@ export class LoginViewModel extends ViewModel { return this._startSSOLoginViewModel; } - get completeSSOLoginViewModel(): CompleteSSOLoginViewModel { + get completeSSOLoginViewModel(): CompleteSSOLoginViewModel | undefined { return this._completeSSOLoginViewModel; }