diff --git a/app/soapbox/actions/accounts.js b/app/soapbox/actions/accounts.js index c1e2341fd..a5dbc9c11 100644 --- a/app/soapbox/actions/accounts.js +++ b/app/soapbox/actions/accounts.js @@ -84,6 +84,10 @@ export const FOLLOW_REQUEST_REJECT_REQUEST = 'FOLLOW_REQUEST_REJECT_REQUEST'; export const FOLLOW_REQUEST_REJECT_SUCCESS = 'FOLLOW_REQUEST_REJECT_SUCCESS'; export const FOLLOW_REQUEST_REJECT_FAIL = 'FOLLOW_REQUEST_REJECT_FAIL'; +export const NOTIFICATION_SETTINGS_REQUEST = 'NOTIFICATION_SETTINGS_REQUEST'; +export const NOTIFICATION_SETTINGS_SUCCESS = 'NOTIFICATION_SETTINGS_SUCCESS'; +export const NOTIFICATION_SETTINGS_FAIL = 'NOTIFICATION_SETTINGS_FAIL'; + function getFromDB(dispatch, getState, index, id) { return new Promise((resolve, reject) => { const request = index.get(id); @@ -806,6 +810,17 @@ export function unpinAccount(id) { }; }; +export function updateNotificationSettings(params) { + return (dispatch, getState) => { + dispatch({ type: NOTIFICATION_SETTINGS_REQUEST, params }); + return api(getState).put('/api/pleroma/notification_settings', params).then(({ data }) => { + dispatch({ type: NOTIFICATION_SETTINGS_SUCCESS, params, data }); + }).catch(error => { + dispatch({ type: NOTIFICATION_SETTINGS_FAIL, params, error }); + }); + }; +}; + export function pinAccountRequest(id) { return { type: ACCOUNT_PIN_REQUEST, diff --git a/app/soapbox/features/edit_profile/index.js b/app/soapbox/features/edit_profile/index.js index 566e9ee3e..e73ef1256 100644 --- a/app/soapbox/features/edit_profile/index.js +++ b/app/soapbox/features/edit_profile/index.js @@ -20,6 +20,7 @@ import { List as ImmutableList, } from 'immutable'; import { patchMe } from 'soapbox/actions/me'; +import { updateNotificationSettings } from 'soapbox/actions/accounts'; import { unescape } from 'lodash'; import { isVerified } from 'soapbox/utils/accounts'; import { getSoapboxConfig } from 'soapbox/actions/soapbox'; @@ -34,8 +35,10 @@ const messages = defineMessages({ const mapStateToProps = state => { const me = state.get('me'); const soapbox = getSoapboxConfig(state); + const meta = state.getIn(['meta', 'pleroma']); + const account = state.getIn(['accounts', me]).set('pleroma', meta); return { - account: state.getIn(['accounts', me]), + account, maxFields: state.getIn(['instance', 'pleroma', 'metadata', 'fields_limits', 'max_fields'], 4), verifiedCanEditName: soapbox.get('verifiedCanEditName'), }; @@ -73,10 +76,13 @@ class EditProfile extends ImmutablePureComponent { constructor(props) { super(props); - const initialState = props.account.withMutations(map => { + const { account } = this.props; + const strangerNotifications = account.getIn(['pleroma', 'notification_settings', 'block_from_strangers']); + const initialState = account.withMutations(map => { map.merge(map.get('source')); map.delete('source'); map.set('fields', normalizeFields(map.get('fields'), props.maxFields)); + map.set('stranger_notifications', strangerNotifications); unescapeParams(map, ['display_name', 'bio']); }); this.state = initialState.toObject(); @@ -127,13 +133,21 @@ class EditProfile extends ImmutablePureComponent { handleSubmit = (event) => { const { dispatch } = this.props; - dispatch(patchMe(this.getFormdata())).then(() => { + + const credentials = dispatch(patchMe(this.getFormdata())); + const notifications = dispatch(updateNotificationSettings({ + block_from_strangers: this.state.stranger_notifications || false, + })); + + this.setState({ isLoading: true }); + + Promise.all([credentials, notifications]).then(() => { this.setState({ isLoading: false }); dispatch(snackbar.success('Profile saved!')); }).catch((error) => { this.setState({ isLoading: false }); }); - this.setState({ isLoading: true }); + event.preventDefault(); } @@ -225,6 +239,13 @@ class EditProfile extends ImmutablePureComponent { checked={this.state.bot} onChange={this.handleCheckboxChange} /> + } + hint={} + name='stranger_notifications' + checked={this.state.stranger_notifications} + onChange={this.handleCheckboxChange} + />
diff --git a/app/soapbox/reducers/meta.js b/app/soapbox/reducers/meta.js index 0e784fe30..f32283eca 100644 --- a/app/soapbox/reducers/meta.js +++ b/app/soapbox/reducers/meta.js @@ -5,17 +5,20 @@ import { Map as ImmutableMap, fromJS } from 'immutable'; const initialState = ImmutableMap(); +const importAccount = (state, account) => { + return state.withMutations(state => { + if (account.has('pleroma')) { + const pleroPrefs = account.get('pleroma').delete('settings_store'); + state.mergeIn(['pleroma'], pleroPrefs); + } + }); +}; + export default function meta(state = initialState, action) { switch(action.type) { case ME_FETCH_SUCCESS: case ME_PATCH_SUCCESS: - const me = fromJS(action.me); - return state.withMutations(state => { - if (me.has('pleroma')) { - const pleroPrefs = me.get('pleroma').delete('settings_store'); - state.mergeIn(['pleroma'], pleroPrefs); - } - }); + return importAccount(state, fromJS(action.me)); default: return state; }