pleroma/src/actions/mrf.ts

36 lines
1.2 KiB
TypeScript
Raw Normal View History

import { Set as ImmutableSet } from 'immutable';
2022-11-15 12:46:23 -08:00
import ConfigDB from 'soapbox/utils/config-db';
import { fetchConfig, updateConfig } from './admin';
2023-09-23 18:41:24 -07:00
import type { MRFSimple } from 'soapbox/schemas/pleroma';
import type { AppDispatch, RootState } from 'soapbox/store';
const simplePolicyMerge = (simplePolicy: MRFSimple, host: string, restrictions: Record<string, any>) => {
2023-09-23 18:41:24 -07:00
const entries = Object.entries(simplePolicy).map(([key, hosts]) => {
const isRestricted = restrictions[key];
if (isRestricted) {
2023-09-23 18:41:24 -07:00
return [key, ImmutableSet(hosts).add(host).toJS()];
} else {
2023-09-23 18:41:24 -07:00
return [key, ImmutableSet(hosts).delete(host).toJS()];
}
});
2023-09-23 18:41:24 -07:00
return Object.fromEntries(entries);
};
const updateMrf = (host: string, restrictions: Record<string, any>) =>
(dispatch: AppDispatch, getState: () => RootState) =>
dispatch(fetchConfig())
.then(() => {
const configs = getState().admin.get('configs');
const simplePolicy = ConfigDB.toSimplePolicy(configs);
const merged = simplePolicyMerge(simplePolicy, host, restrictions);
const config = ConfigDB.fromSimplePolicy(merged);
return dispatch(updateConfig(config.toJS() as Array<Record<string, any>>));
});
export { updateMrf };