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