EditFederationModal: handle submission

This commit is contained in:
Alex Gleason 2021-07-27 14:40:41 -05:00
parent 9536fba7a9
commit 1fa3aa0008
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 56 additions and 2 deletions

View File

@ -9,7 +9,7 @@ export const NODEINFO_FETCH_FAIL = 'NODEINFO_FETCH_FAIL';
export function fetchInstance() {
return (dispatch, getState) => {
api(getState).get('/api/v1/instance').then(response => {
return api(getState).get('/api/v1/instance').then(response => {
dispatch(importInstance(response.data));
const v = parseVersion(get(response.data, 'version'));
if (v.software === 'Pleroma' && !get(response.data, ['pleroma', 'metadata'])) {

View File

@ -0,0 +1,44 @@
import { fetchInstance } from './instance';
import { updateConfig } from './admin';
import { Set as ImmutableSet } from 'immutable';
const simplePolicyMerge = (simplePolicy, host, restrictions) => {
return simplePolicy.map((hosts, key) => {
const isRestricted = restrictions.get(key);
if (isRestricted) {
return ImmutableSet(hosts).add(host);
} else {
return ImmutableSet(hosts).delete(host);
}
});
};
const simplePolicyToConfig = simplePolicy => {
const value = simplePolicy.map((hosts, key) => (
{ tuple: [`:${key}`, hosts.toJS()] }
)).toList();
return [{
group: ':pleroma',
key: ':mrf_simple',
value,
}];
};
export function updateMrf(host, restrictions) {
return (dispatch, getState) => {
return dispatch(fetchInstance())
.then(() => {
const simplePolicy = getState().getIn(['instance', 'pleroma', 'metadata', 'federation', 'mrf_simple']);
const merged = simplePolicyMerge(simplePolicy, host, restrictions);
const config = simplePolicyToConfig(merged);
dispatch(updateConfig(config));
// TODO: Make this less insane
setTimeout(() => {
dispatch(fetchInstance());
}, 1000);
});
};
}

View File

@ -7,6 +7,8 @@ import { defineMessages, injectIntl } from 'react-intl';
import { SimpleForm, Checkbox } from 'soapbox/features/forms';
import { makeGetRemoteInstance } from 'soapbox/selectors';
import { Map as ImmutableMap } from 'immutable';
import { updateMrf } from 'soapbox/actions/mrf';
import snackbar from 'soapbox/actions/snackbar';
const getRemoteInstance = makeGetRemoteInstance();
@ -17,6 +19,7 @@ const messages = defineMessages({
unlisted: { id: 'edit_federation.unlisted', defaultMessage: 'Force posts unlisted' },
followersOnly: { id: 'edit_federation.followers_only', defaultMessage: 'Hide posts except to followers' },
save: { id: 'edit_federation.save', defaultMessage: 'Save' },
success: { id: 'edit_federation.success', defaultMessage: '{host} federation was updated' },
});
const mapStateToProps = (state, { host }) => {
@ -61,7 +64,14 @@ class EditFederationModal extends ImmutablePureComponent {
}
handleSubmit = e => {
// TODO
const { intl, dispatch, host, onClose } = this.props;
const { data } = this.state;
dispatch(updateMrf(host, data))
.then(() => dispatch(snackbar.success(intl.formatMessage(messages.success, { host }))))
.catch(() => {});
onClose();
}
render() {