2023-11-01 15:30:32 -07:00
|
|
|
import { Set as ImmutableSet } from 'immutable';
|
2022-06-15 13:11:36 -07:00
|
|
|
|
2022-11-15 12:46:23 -08:00
|
|
|
import ConfigDB from 'soapbox/utils/config-db';
|
2022-06-15 13:11:36 -07:00
|
|
|
|
|
|
|
import { fetchConfig, updateConfig } from './admin';
|
|
|
|
|
2023-09-23 18:41:24 -07:00
|
|
|
import type { MRFSimple } from 'soapbox/schemas/pleroma';
|
2022-06-15 13:11:36 -07:00
|
|
|
import type { AppDispatch, RootState } from 'soapbox/store';
|
|
|
|
|
2023-11-01 15:30:32 -07:00
|
|
|
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]) => {
|
2023-11-01 15:30:32 -07:00
|
|
|
const isRestricted = restrictions[key];
|
2022-06-15 13:11:36 -07:00
|
|
|
|
|
|
|
if (isRestricted) {
|
2023-09-23 18:41:24 -07:00
|
|
|
return [key, ImmutableSet(hosts).add(host).toJS()];
|
2022-06-15 13:11:36 -07:00
|
|
|
} else {
|
2023-09-23 18:41:24 -07:00
|
|
|
return [key, ImmutableSet(hosts).delete(host).toJS()];
|
2022-06-15 13:11:36 -07:00
|
|
|
}
|
|
|
|
});
|
2023-09-23 18:41:24 -07:00
|
|
|
|
|
|
|
return Object.fromEntries(entries);
|
2022-06-15 13:11:36 -07:00
|
|
|
};
|
|
|
|
|
2023-11-01 15:30:32 -07:00
|
|
|
const updateMrf = (host: string, restrictions: Record<string, any>) =>
|
2022-06-15 13:11:36 -07:00
|
|
|
(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);
|
2022-06-20 06:46:43 -07:00
|
|
|
return dispatch(updateConfig(config.toJS() as Array<Record<string, any>>));
|
2022-06-15 13:11:36 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
export { updateMrf };
|