Merge branch 'stranger-notifications' into 'develop'

Support blocking notifications from strangers

See merge request soapbox-pub/soapbox-fe!519
This commit is contained in:
Alex Gleason 2021-06-07 03:59:53 +00:00
commit 2e4f193333
3 changed files with 50 additions and 11 deletions

View File

@ -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_SUCCESS = 'FOLLOW_REQUEST_REJECT_SUCCESS';
export const FOLLOW_REQUEST_REJECT_FAIL = 'FOLLOW_REQUEST_REJECT_FAIL'; 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) { function getFromDB(dispatch, getState, index, id) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const request = index.get(id); 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) { export function pinAccountRequest(id) {
return { return {
type: ACCOUNT_PIN_REQUEST, type: ACCOUNT_PIN_REQUEST,

View File

@ -20,6 +20,7 @@ import {
List as ImmutableList, List as ImmutableList,
} from 'immutable'; } from 'immutable';
import { patchMe } from 'soapbox/actions/me'; import { patchMe } from 'soapbox/actions/me';
import { updateNotificationSettings } from 'soapbox/actions/accounts';
import { unescape } from 'lodash'; import { unescape } from 'lodash';
import { isVerified } from 'soapbox/utils/accounts'; import { isVerified } from 'soapbox/utils/accounts';
import { getSoapboxConfig } from 'soapbox/actions/soapbox'; import { getSoapboxConfig } from 'soapbox/actions/soapbox';
@ -34,8 +35,10 @@ const messages = defineMessages({
const mapStateToProps = state => { const mapStateToProps = state => {
const me = state.get('me'); const me = state.get('me');
const soapbox = getSoapboxConfig(state); const soapbox = getSoapboxConfig(state);
const meta = state.getIn(['meta', 'pleroma']);
const account = state.getIn(['accounts', me]).set('pleroma', meta);
return { return {
account: state.getIn(['accounts', me]), account,
maxFields: state.getIn(['instance', 'pleroma', 'metadata', 'fields_limits', 'max_fields'], 4), maxFields: state.getIn(['instance', 'pleroma', 'metadata', 'fields_limits', 'max_fields'], 4),
verifiedCanEditName: soapbox.get('verifiedCanEditName'), verifiedCanEditName: soapbox.get('verifiedCanEditName'),
}; };
@ -73,10 +76,13 @@ class EditProfile extends ImmutablePureComponent {
constructor(props) { constructor(props) {
super(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.merge(map.get('source'));
map.delete('source'); map.delete('source');
map.set('fields', normalizeFields(map.get('fields'), props.maxFields)); map.set('fields', normalizeFields(map.get('fields'), props.maxFields));
map.set('stranger_notifications', strangerNotifications);
unescapeParams(map, ['display_name', 'bio']); unescapeParams(map, ['display_name', 'bio']);
}); });
this.state = initialState.toObject(); this.state = initialState.toObject();
@ -127,13 +133,21 @@ class EditProfile extends ImmutablePureComponent {
handleSubmit = (event) => { handleSubmit = (event) => {
const { dispatch } = this.props; 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 }); this.setState({ isLoading: false });
dispatch(snackbar.success('Profile saved!')); dispatch(snackbar.success('Profile saved!'));
}).catch((error) => { }).catch((error) => {
this.setState({ isLoading: false }); this.setState({ isLoading: false });
}); });
this.setState({ isLoading: true });
event.preventDefault(); event.preventDefault();
} }
@ -225,6 +239,13 @@ class EditProfile extends ImmutablePureComponent {
checked={this.state.bot} checked={this.state.bot}
onChange={this.handleCheckboxChange} onChange={this.handleCheckboxChange}
/> />
<Checkbox
label={<FormattedMessage id='edit_profile.fields.stranger_notifications_label' defaultMessage='Block notifications from strangers' />}
hint={<FormattedMessage id='edit_profile.hints.stranger_notifications' defaultMessage='Only show notifications from people you follow' />}
name='stranger_notifications'
checked={this.state.stranger_notifications}
onChange={this.handleCheckboxChange}
/>
</FieldsGroup> </FieldsGroup>
<FieldsGroup> <FieldsGroup>
<div className='fields-row__column fields-group'> <div className='fields-row__column fields-group'>

View File

@ -5,17 +5,20 @@ import { Map as ImmutableMap, fromJS } from 'immutable';
const initialState = ImmutableMap(); 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) { export default function meta(state = initialState, action) {
switch(action.type) { switch(action.type) {
case ME_FETCH_SUCCESS: case ME_FETCH_SUCCESS:
case ME_PATCH_SUCCESS: case ME_PATCH_SUCCESS:
const me = fromJS(action.me); return importAccount(state, fromJS(action.me));
return state.withMutations(state => {
if (me.has('pleroma')) {
const pleroPrefs = me.get('pleroma').delete('settings_store');
state.mergeIn(['pleroma'], pleroPrefs);
}
});
default: default:
return state; return state;
} }