pl-fe: migrate mastodon preload from immutable

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2024-11-15 15:01:37 +01:00
parent 3ee95491a2
commit 9e6ffa8df2

View file

@ -1,4 +1,3 @@
import { Map as ImmutableMap, fromJS } from 'immutable';
import trim from 'lodash/trim'; import trim from 'lodash/trim';
import { create, Draft } from 'mutative'; import { create, Draft } from 'mutative';
import { applicationSchema, PlApiClient, tokenSchema, type CredentialAccount, type CredentialApplication, type Token } from 'pl-api'; import { applicationSchema, PlApiClient, tokenSchema, type CredentialAccount, type CredentialApplication, type Token } from 'pl-api';
@ -6,6 +5,7 @@ import * as v from 'valibot';
import { MASTODON_PRELOAD_IMPORT, type PreloadAction } from 'pl-fe/actions/preload'; import { MASTODON_PRELOAD_IMPORT, type PreloadAction } from 'pl-fe/actions/preload';
import * as BuildConfig from 'pl-fe/build-config'; import * as BuildConfig from 'pl-fe/build-config';
import { coerceObject } from 'pl-fe/schemas/utils';
import KVStore from 'pl-fe/storage/kv-store'; import KVStore from 'pl-fe/storage/kv-store';
import { validId, isURL, parseBaseURL } from 'pl-fe/utils/auth'; import { validId, isURL, parseBaseURL } from 'pl-fe/utils/auth';
@ -27,6 +27,16 @@ import type { AnyAction } from 'redux';
const backendUrl = (isURL(BuildConfig.BACKEND_URL) ? BuildConfig.BACKEND_URL : ''); const backendUrl = (isURL(BuildConfig.BACKEND_URL) ? BuildConfig.BACKEND_URL : '');
const mastodonPreloadSchema = coerceObject({
meta: coerceObject({
access_token: v.string(),
me: v.string(),
}),
accounts: v.record(v.string(), v.object({
url: v.string(),
})),
});
const authUserSchema = v.object({ const authUserSchema = v.object({
access_token: v.string(), access_token: v.string(),
id: v.string(), id: v.string(),
@ -229,10 +239,11 @@ const deleteUser = (state: State | Draft<State>, account: Pick<AccountEntity, 'u
maybeShiftMe(state); maybeShiftMe(state);
}; };
const importMastodonPreload = (state: State | Draft<State>, data: ImmutableMap<string, any>) => { const importMastodonPreload = (state: State | Draft<State>, data: Record<string, any>) => {
const accountId = data.getIn(['meta', 'me']) as string; const parsedData = v.parse(mastodonPreloadSchema, data);
const accountUrl = data.getIn(['accounts', accountId, 'url']) as string; const accountId = parsedData.meta.me;
const accessToken = data.getIn(['meta', 'access_token']) as string; const accountUrl = parsedData.accounts[accountId]?.url;
const accessToken = parsedData.meta.access_token;
if (validId(accessToken) && validId(accountId) && isURL(accountUrl)) { if (validId(accessToken) && validId(accountId) && isURL(accountUrl)) {
state.tokens[accessToken] = v.parse(tokenSchema, { state.tokens[accessToken] = v.parse(tokenSchema, {
@ -335,7 +346,7 @@ const reducer = (state: State, action: AnyAction | AuthAction | MeAction | Prelo
}); });
case MASTODON_PRELOAD_IMPORT: case MASTODON_PRELOAD_IMPORT:
return updateState(state, (draft) => { return updateState(state, (draft) => {
importMastodonPreload(draft, fromJS<ImmutableMap<string, any>>(action.data)); importMastodonPreload(draft, action.data);
}); });
default: default:
return state; return state;