From c3ebb5c428a6bbd5ce31eeb61631461b50ff8c4d Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 24 Mar 2024 17:50:54 -0500 Subject: [PATCH] accounts_meta: switch to immer, use the results for useOwnAccount --- src/actions/aliases.ts | 8 +++--- src/reducers/accounts-meta.ts | 46 ++++++++++++++++++++--------------- src/selectors/index.ts | 11 +++++++-- src/utils/permissions.ts | 12 ++++----- 4 files changed, 46 insertions(+), 31 deletions(-) diff --git a/src/actions/aliases.ts b/src/actions/aliases.ts index 652bde8b5..c582e70bb 100644 --- a/src/actions/aliases.ts +++ b/src/actions/aliases.ts @@ -105,8 +105,8 @@ const addToAliases = (account: Account) => const features = getFeatures(instance); if (!features.accountMoving) { - const me = state.me; - const alsoKnownAs = state.accounts_meta.get(me as string)!.pleroma.get('also_known_as'); + const me = state.me as string; + const alsoKnownAs = state.accounts_meta[me]?.pleroma?.also_known_as ?? []; dispatch(addToAliasesRequest()); @@ -156,8 +156,8 @@ const removeFromAliases = (account: string) => const features = getFeatures(instance); if (!features.accountMoving) { - const me = state.me; - const alsoKnownAs = state.accounts_meta.get(me as string)!.pleroma.get('also_known_as'); + const me = state.me as string; + const alsoKnownAs = state.accounts_meta[me]?.pleroma?.also_known_as ?? []; dispatch(removeFromAliasesRequest()); diff --git a/src/reducers/accounts-meta.ts b/src/reducers/accounts-meta.ts index 9410fc5cd..754c20334 100644 --- a/src/reducers/accounts-meta.ts +++ b/src/reducers/accounts-meta.ts @@ -3,40 +3,48 @@ * @module soapbox/reducers/accounts_meta */ -import { Map as ImmutableMap, Record as ImmutableRecord, fromJS } from 'immutable'; +import { produce } from 'immer'; import { VERIFY_CREDENTIALS_SUCCESS, AUTH_ACCOUNT_REMEMBER_SUCCESS } from 'soapbox/actions/auth'; import { ME_FETCH_SUCCESS, ME_PATCH_SUCCESS } from 'soapbox/actions/me'; +import { Account, accountSchema } from 'soapbox/schemas'; import type { AnyAction } from 'redux'; -const MetaRecord = ImmutableRecord({ - pleroma: ImmutableMap(), - role: null as ImmutableMap | null, - source: ImmutableMap(), -}); +interface AccountMeta { + pleroma: Account['pleroma']; + source: Account['source']; +} -export type Meta = ReturnType; -type State = ImmutableMap; +type State = Record; -const importAccount = (state: State, account: ImmutableMap) => { - const accountId = account.get('id'); +function importAccount(state: State, data: unknown): State { + const result = accountSchema.safeParse(data); - return state.set(accountId, MetaRecord({ - pleroma: account.get('pleroma', ImmutableMap()).delete('settings_store'), - role: account.get('role', null), - source: account.get('source', ImmutableMap()), - })); -}; + if (!result.success) { + return state; + } -export default function accounts_meta(state: State = ImmutableMap(), action: AnyAction) { + const account = result.data; + + return produce(state, draft => { + const existing = draft[account.id]; + + draft[account.id] = { + pleroma: account.pleroma ?? existing?.pleroma, + source: account.source ?? existing?.source, + }; + }); +} + +export default function accounts_meta(state: Readonly = {}, action: AnyAction): State { switch (action.type) { case ME_FETCH_SUCCESS: case ME_PATCH_SUCCESS: - return importAccount(state, ImmutableMap(fromJS(action.me))); + return importAccount(state, action.me); case VERIFY_CREDENTIALS_SUCCESS: case AUTH_ACCOUNT_REMEMBER_SUCCESS: - return importAccount(state, ImmutableMap(fromJS(action.account))); + return importAccount(state, action.account); default: return state; } diff --git a/src/selectors/index.ts b/src/selectors/index.ts index f569bb070..6d0809db8 100644 --- a/src/selectors/index.ts +++ b/src/selectors/index.ts @@ -38,14 +38,21 @@ export const accountIdsToAccts = (state: RootState, ids: string[]) => ids.map((i const getAccountBase = (state: RootState, id: string) => state.entities[Entities.ACCOUNTS]?.store[id] as Account | undefined; const getAccountRelationship = (state: RootState, id: string) => state.relationships.get(id); +const getAccountMeta = (state: RootState, id: string) => state.accounts_meta[id]; export const makeGetAccount = () => { return createSelector([ getAccountBase, getAccountRelationship, - ], (account, relationship) => { + getAccountMeta, + ], (account, relationship, meta) => { if (!account) return null; - return { ...account, relationship }; + return { + ...account, + relationship, + source: meta?.source ?? account.source, + pleroma: meta?.pleroma ?? account.pleroma, + }; }); }; diff --git a/src/utils/permissions.ts b/src/utils/permissions.ts index 9627074eb..fb728a56e 100644 --- a/src/utils/permissions.ts +++ b/src/utils/permissions.ts @@ -8,12 +8,12 @@ export const PERMISSION_MANAGE_REPORTS = 0x0000000000000010; type Permission = typeof PERMISSION_CREATE_GROUPS | typeof PERMISSION_INVITE_USERS | typeof PERMISSION_MANAGE_USERS | typeof PERMISSION_MANAGE_REPORTS export const hasPermission = (state: RootState, permission: Permission) => { - const account = state.accounts_meta.get(state.me as string)!; + return true; + // const role = state.accounts_meta[state.me as string]?.role; - if (!account?.role) return true; + // if (!role) return true; + // const { permissions } = role; - const permissions = account.getIn(['role', 'permissions']) as number; - - if (!permission) return true; - return (permissions & permission) === permission; + // if (!permission) return true; + // return (permissions & permission) === permission; };