feat: fully working onboarding on login

This commit is contained in:
Paul Makles 2022-06-29 11:48:48 +01:00
parent 66ae518e51
commit 31220db8fe
3 changed files with 60 additions and 15 deletions

View file

@ -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(() => {

View file

@ -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;

View file

@ -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