From 31220db8fe545d0b88235f3d2b8774ec335b3901 Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Wed, 29 Jun 2022 11:48:48 +0100 Subject: [PATCH] feat: fully working onboarding on login --- src/context/revoltjs/SyncManager.tsx | 12 ++++-- src/controllers/client/ClientController.tsx | 17 +++++--- src/controllers/client/Session.tsx | 46 ++++++++++++++++++--- 3 files changed, 60 insertions(+), 15 deletions(-) diff --git a/src/context/revoltjs/SyncManager.tsx b/src/context/revoltjs/SyncManager.tsx index 5d72f8c2..5022a373 100644 --- a/src/context/revoltjs/SyncManager.tsx +++ b/src/context/revoltjs/SyncManager.tsx @@ -9,21 +9,25 @@ import { reportError } from "../../lib/ErrorBoundary"; import { useApplicationState } from "../../mobx/State"; -import { useClient } from "../../controllers/client/ClientController"; +import { + useClient, + useSession, +} from "../../controllers/client/ClientController"; export default function SyncManager() { const client = useClient(); + const session = useSession(); const state = useApplicationState(); // Sync settings from Revolt. useEffect(() => { - if (client) { + if (session?.ready) { state.sync - .pull(client) + .pull(session.client!) .catch(console.error) .finally(() => state.changelog.checkForUpdates()); } - }, [client]); + }, [session?.ready]); // Take data updates from Revolt. useEffect(() => { diff --git a/src/controllers/client/ClientController.tsx b/src/controllers/client/ClientController.tsx index 9939df17..3012ba35 100644 --- a/src/controllers/client/ClientController.tsx +++ b/src/controllers/client/ClientController.tsx @@ -64,7 +64,7 @@ class ClientController { */ @action hydrate(auth: Auth) { for (const entry of auth.getAccounts()) { - this.addSession(entry); + this.addSession(entry, "existing"); } this.pickNextSession(); @@ -90,7 +90,10 @@ class ClientController { return this.current === null; } - @action addSession(entry: { session: SessionPrivate; apiUrl?: string }) { + @action addSession( + entry: { session: SessionPrivate; apiUrl?: string }, + knowledge: "new" | "existing", + ) { const user_id = entry.session.user_id!; const session = new Session(); @@ -102,6 +105,7 @@ class ClientController { session: entry.session, apiUrl: entry.apiUrl, configuration: this.configuration!, + knowledge, }) .catch((error) => { if (error === "Forbidden" || error === "Unauthorized") { @@ -177,9 +181,12 @@ class ClientController { } } - this.addSession({ - session, - }); + this.addSession( + { + session, + }, + "new", + ); /*const s = session; diff --git a/src/controllers/client/Session.tsx b/src/controllers/client/Session.tsx index e2e66b10..d171613b 100644 --- a/src/controllers/client/Session.tsx +++ b/src/controllers/client/Session.tsx @@ -1,6 +1,10 @@ import { action, computed, makeAutoObservable } from "mobx"; import { API, Client } from "revolt.js"; +import { state } from "../../mobx/State"; + +import { __thisIsAHack } from "../../context/intermediate/Intermediate"; + type State = "Ready" | "Connecting" | "Online" | "Disconnected" | "Offline"; type Transition = @@ -9,6 +13,8 @@ type Transition = apiUrl?: string; session: SessionPrivate; configuration?: API.RevoltConfig; + + knowledge: "new" | "existing"; } | { action: @@ -104,6 +110,17 @@ export default class Session { } } + private async continueLogin(data: Transition & { action: "LOGIN" }) { + try { + await this.client!.useExistingSession(data.session); + this.user_id = this.client!.user!._id; + state.auth.setSession(data.session); + } catch (err) { + this.state = "Ready"; + throw err; + } + } + @action async emit(data: Transition) { console.info(`[FSM ${this.user_id ?? "Anonymous"}]`, data); @@ -118,14 +135,31 @@ export default class Session { this.client!.configuration = data.configuration; } - try { - await this.client!.useExistingSession(data.session); - this.user_id = this.client!.user!._id; - } catch (err) { - this.state = "Ready"; - throw err; + if (data.knowledge === "new") { + await this.client!.fetchConfiguration(); + this.client!.session = data.session; + (this.client! as any).$updateHeaders(); + + const { onboarding } = await this.client!.api.get( + "/onboard/hello", + ); + + if (onboarding) { + __thisIsAHack({ + id: "onboarding", + callback: async (username: string) => + this.client!.completeOnboarding( + { username }, + false, + ).then(() => this.continueLogin(data)), + }); + + return; + } } + this.continueLogin(data); + break; } // Ready successfully received