bigbuffet-rw/app/soapbox/actions/mrf.ts
marcin mikołajczak cfacc23f5c Actions: TypeScript
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
2022-06-15 22:11:36 +02:00

33 lines
1.1 KiB
TypeScript

import { Map as ImmutableMap, Set as ImmutableSet } from 'immutable';
import ConfigDB from 'soapbox/utils/config_db';
import { fetchConfig, updateConfig } from './admin';
import type { AppDispatch, RootState } from 'soapbox/store';
import type { Policy } from 'soapbox/utils/config_db';
const simplePolicyMerge = (simplePolicy: Policy, host: string, restrictions: ImmutableMap<string, any>) => {
return simplePolicy.map((hosts, key) => {
const isRestricted = restrictions.get(key);
if (isRestricted) {
return ImmutableSet(hosts).add(host);
} else {
return ImmutableSet(hosts).delete(host);
}
});
};
const updateMrf = (host: string, restrictions: ImmutableMap<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));
});
export { updateMrf };