2021-07-28 15:06:21 -07:00
|
|
|
import {
|
|
|
|
Map as ImmutableMap,
|
|
|
|
List as ImmutableList,
|
|
|
|
Set as ImmutableSet,
|
|
|
|
fromJS,
|
|
|
|
} from 'immutable';
|
2022-06-16 12:32:17 -07:00
|
|
|
import trimStart from 'lodash/trimStart';
|
2021-07-28 15:06:21 -07:00
|
|
|
|
2023-09-23 18:41:24 -07:00
|
|
|
import { type MRFSimple, mrfSimpleSchema } from 'soapbox/schemas/pleroma';
|
|
|
|
|
2022-03-31 16:10:34 -07:00
|
|
|
export type Config = ImmutableMap<string, any>;
|
2023-11-01 15:30:32 -07:00
|
|
|
export type Policy = Record<string, any>;
|
2022-03-20 17:40:08 -07:00
|
|
|
|
|
|
|
const find = (
|
|
|
|
configs: ImmutableList<Config>,
|
|
|
|
group: string,
|
|
|
|
key: string,
|
2024-05-12 16:18:04 -07:00
|
|
|
): Config | undefined => configs.find(config =>
|
|
|
|
config.isSuperset(ImmutableMap({ group, key })),
|
|
|
|
);
|
2021-07-28 15:06:21 -07:00
|
|
|
|
2023-09-23 18:41:24 -07:00
|
|
|
const toSimplePolicy = (configs: ImmutableList<Config>): MRFSimple => {
|
2021-07-28 15:06:21 -07:00
|
|
|
const config = find(configs, ':pleroma', ':mrf_simple');
|
|
|
|
|
2022-03-20 17:40:08 -07:00
|
|
|
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>;
|
2021-07-28 15:06:21 -07:00
|
|
|
return acc.set(trimStart(key, ':'), ImmutableSet(hosts));
|
|
|
|
};
|
|
|
|
|
2022-02-18 18:04:03 -08:00
|
|
|
if (config?.get) {
|
2021-07-28 15:06:21 -07:00
|
|
|
const value = config.get('value', ImmutableList());
|
2023-09-23 18:41:24 -07:00
|
|
|
const result = value.reduce(reducer, ImmutableMap());
|
|
|
|
return mrfSimpleSchema.parse(result.toJS());
|
2021-07-28 15:06:21 -07:00
|
|
|
} else {
|
2023-09-23 18:41:24 -07:00
|
|
|
return mrfSimpleSchema.parse({});
|
2021-07-28 15:06:21 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-20 17:40:08 -07:00
|
|
|
const fromSimplePolicy = (simplePolicy: Policy): ImmutableList<Config> => {
|
2023-11-01 15:30:32 -07:00
|
|
|
const mapper = ([key, hosts]: [key: string, hosts: ImmutableList<string>]) => fromJS({ tuple: [`:${key}`, hosts] });
|
2022-03-20 17:40:08 -07:00
|
|
|
|
2023-11-01 15:30:32 -07:00
|
|
|
const value = Object.entries(simplePolicy).map(mapper);
|
2021-07-28 15:06:21 -07:00
|
|
|
|
|
|
|
return ImmutableList([
|
|
|
|
ImmutableMap({
|
|
|
|
group: ':pleroma',
|
|
|
|
key: ':mrf_simple',
|
2023-11-01 15:30:32 -07:00
|
|
|
value: ImmutableList(value),
|
2021-07-28 15:06:21 -07:00
|
|
|
}),
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
2024-05-12 16:18:04 -07:00
|
|
|
const ConfigDB = {
|
2021-07-28 15:06:21 -07:00
|
|
|
find,
|
|
|
|
toSimplePolicy,
|
|
|
|
fromSimplePolicy,
|
2020-08-24 08:08:39 -07:00
|
|
|
};
|
|
|
|
|
2024-05-12 16:18:04 -07:00
|
|
|
export {
|
|
|
|
ConfigDB,
|
|
|
|
ConfigDB as default,
|
|
|
|
};
|