From 1fa3aa0008d8cf7d91b66cb786a5c75359bc0dfc Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 27 Jul 2021 14:40:41 -0500 Subject: [PATCH] EditFederationModal: handle submission --- app/soapbox/actions/instance.js | 2 +- app/soapbox/actions/mrf.js | 44 +++++++++++++++++++ .../ui/components/edit_federation_modal.js | 12 ++++- 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 app/soapbox/actions/mrf.js diff --git a/app/soapbox/actions/instance.js b/app/soapbox/actions/instance.js index 23d628872..94b5da79d 100644 --- a/app/soapbox/actions/instance.js +++ b/app/soapbox/actions/instance.js @@ -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'])) { diff --git a/app/soapbox/actions/mrf.js b/app/soapbox/actions/mrf.js new file mode 100644 index 000000000..9cddf85f8 --- /dev/null +++ b/app/soapbox/actions/mrf.js @@ -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); + }); + }; +} diff --git a/app/soapbox/features/ui/components/edit_federation_modal.js b/app/soapbox/features/ui/components/edit_federation_modal.js index 4a914148b..abf7c2c3f 100644 --- a/app/soapbox/features/ui/components/edit_federation_modal.js +++ b/app/soapbox/features/ui/components/edit_federation_modal.js @@ -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() {