pl-fe: Remove some immutable usage
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
@ -1,36 +1,33 @@
|
||||
import {
|
||||
Map as ImmutableMap,
|
||||
List as ImmutableList,
|
||||
Set as ImmutableSet,
|
||||
} from 'immutable';
|
||||
import trimStart from 'lodash/trimStart';
|
||||
import * as v from 'valibot';
|
||||
|
||||
import { mrfSimpleSchema } from 'pl-fe/schemas/pleroma';
|
||||
|
||||
type Config = ImmutableMap<string, any>;
|
||||
import type { PleromaConfig } from 'pl-api';
|
||||
|
||||
type Policy = Record<string, any>;
|
||||
type Config = PleromaConfig['configs'][0];
|
||||
|
||||
const find = (
|
||||
configs: ImmutableList<Config>,
|
||||
configs: PleromaConfig['configs'],
|
||||
group: string,
|
||||
key: string,
|
||||
): Config | undefined => configs.find(config =>
|
||||
config.isSuperset(ImmutableMap({ group, key })),
|
||||
config.group === group && config.key === key,
|
||||
);
|
||||
|
||||
const toSimplePolicy = (configs: ImmutableList<Config>) => {
|
||||
const toSimplePolicy = (configs: PleromaConfig['configs']) => {
|
||||
const config = find(configs, ':pleroma', ':mrf_simple');
|
||||
|
||||
const reducer = (acc: ImmutableMap<string, any>, curr: ImmutableMap<string, any>) => {
|
||||
const key = curr.getIn(['tuple', 0]) as string;
|
||||
const hosts = curr.getIn(['tuple', 1]) as ImmutableList<string>;
|
||||
return acc.set(trimStart(key, ':'), ImmutableSet(hosts));
|
||||
const reducer = (acc: Record<string, any>, curr: Record<string, any>) => {
|
||||
const key = curr.tuple?.[0] as string;
|
||||
const hosts = curr.tuple?.[1] as Array<string>;
|
||||
return acc[trimStart(key, ':')] = hosts;
|
||||
};
|
||||
|
||||
if (config?.get) {
|
||||
const value = config.get('value', ImmutableList());
|
||||
const result = value.reduce(reducer, ImmutableMap());
|
||||
if (config) {
|
||||
const value = config.value || [];
|
||||
const result = value.reduce(reducer, {});
|
||||
return v.parse(mrfSimpleSchema, result.toJS());
|
||||
} else {
|
||||
return v.parse(mrfSimpleSchema, {});
|
||||
@ -38,7 +35,7 @@ const toSimplePolicy = (configs: ImmutableList<Config>) => {
|
||||
};
|
||||
|
||||
const fromSimplePolicy = (simplePolicy: Policy) => {
|
||||
const mapper = ([key, hosts]: [key: string, hosts: ImmutableList<string>]) => ({ tuple: [`:${key}`, hosts] });
|
||||
const mapper = ([key, hosts]: [key: string, hosts: Array<string>]) => ({ tuple: [`:${key}`, hosts] });
|
||||
|
||||
const value = Object.entries(simplePolicy).map(mapper);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user