bigbuffet-rw/app/soapbox/actions/settings.js

183 lines
3.8 KiB
JavaScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
import { debounce } from 'lodash';
import { showAlertForError } from './alerts';
2020-05-28 15:52:07 -07:00
import { patchMe } from 'soapbox/actions/me';
2020-08-25 12:58:35 -07:00
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
2021-03-25 10:25:45 -07:00
import { isLoggedIn } from 'soapbox/utils/auth';
2020-09-27 17:43:42 -07:00
import uuid from '../uuid';
import { createSelector } from 'reselect';
2020-03-27 13:59:38 -07:00
export const SETTING_CHANGE = 'SETTING_CHANGE';
export const SETTING_SAVE = 'SETTING_SAVE';
export const FE_NAME = 'soapbox_fe';
2020-10-01 16:57:11 -07:00
export const defaultSettings = ImmutableMap({
2020-04-28 10:59:15 -07:00
onboarded: false,
skinTone: 1,
reduceMotion: false,
2020-05-28 13:43:17 -07:00
autoPlayGif: true,
2020-05-28 18:36:39 -07:00
displayMedia: 'default',
2020-04-28 10:59:15 -07:00
expandSpoilers: false,
unfollowModal: false,
boostModal: false,
deleteModal: true,
missingDescriptionModal: false,
2020-04-28 10:59:15 -07:00
defaultPrivacy: 'public',
defaultContentType: 'text/plain',
2020-06-02 10:16:26 -07:00
themeMode: 'light',
2020-06-04 15:29:00 -07:00
locale: navigator.language.split(/[-_]/)[0] || 'en',
showExplanationBox: true,
explanationBox: true,
2020-08-07 13:17:13 -07:00
otpEnabled: false,
2020-04-28 10:59:15 -07:00
systemFont: false,
dyslexicFont: false,
demetricator: false,
2020-08-25 12:58:35 -07:00
chats: ImmutableMap({
panes: ImmutableList(),
mainWindow: 'minimized',
sound: true,
2020-08-25 12:58:35 -07:00
}),
2020-04-28 10:59:15 -07:00
home: ImmutableMap({
shows: ImmutableMap({
reblog: true,
reply: true,
direct: false,
2020-04-28 10:59:15 -07:00
}),
regex: ImmutableMap({
body: '',
}),
}),
notifications: ImmutableMap({
alerts: ImmutableMap({
follow: true,
follow_request: false,
2020-04-28 10:59:15 -07:00
favourite: true,
reblog: true,
mention: true,
poll: true,
move: true,
'pleroma:emoji_reaction': true,
2020-04-28 10:59:15 -07:00
}),
quickFilter: ImmutableMap({
active: 'all',
show: true,
advanced: false,
}),
shows: ImmutableMap({
follow: true,
follow_request: false,
2020-04-28 10:59:15 -07:00
favourite: true,
reblog: true,
mention: true,
poll: true,
move: true,
'pleroma:emoji_reaction': true,
2020-04-28 10:59:15 -07:00
}),
sounds: ImmutableMap({
2020-05-18 17:43:58 -07:00
follow: false,
follow_request: false,
2020-05-18 17:43:58 -07:00
favourite: false,
reblog: false,
mention: false,
poll: false,
move: false,
'pleroma:emoji_reaction': false,
2020-04-28 10:59:15 -07:00
}),
}),
community: ImmutableMap({
2020-08-07 17:28:30 -07:00
shows: ImmutableMap({
reblog: false,
2020-08-07 17:28:30 -07:00
reply: true,
}),
2020-04-28 10:59:15 -07:00
other: ImmutableMap({
onlyMedia: false,
}),
regex: ImmutableMap({
body: '',
}),
}),
public: ImmutableMap({
2020-08-07 17:28:30 -07:00
shows: ImmutableMap({
reblog: true,
reply: true,
}),
2020-04-28 10:59:15 -07:00
other: ImmutableMap({
onlyMedia: false,
}),
regex: ImmutableMap({
body: '',
}),
}),
direct: ImmutableMap({
regex: ImmutableMap({
body: '',
}),
}),
trends: ImmutableMap({
show: true,
}),
2020-09-27 17:43:42 -07:00
columns: ImmutableList([
ImmutableMap({ id: 'COMPOSE', uuid: uuid(), params: {} }),
ImmutableMap({ id: 'HOME', uuid: uuid(), params: {} }),
ImmutableMap({ id: 'NOTIFICATIONS', uuid: uuid(), params: {} }),
]),
2020-04-28 10:59:15 -07:00
});
export const getSettings = createSelector([
state => state.getIn(['soapbox', 'defaultSettings']),
state => state.get('settings'),
], (soapboxSettings, settings) => {
2020-04-28 10:59:15 -07:00
return defaultSettings
.mergeDeep(soapboxSettings)
.mergeDeep(settings);
});
2020-04-28 10:59:15 -07:00
2020-03-27 13:59:38 -07:00
export function changeSetting(path, value) {
return dispatch => {
dispatch({
type: SETTING_CHANGE,
path,
value,
});
dispatch(saveSettings());
};
};
const debouncedSave = debounce((dispatch, getState) => {
2021-03-24 09:52:12 -07:00
if (!isLoggedIn(getState)) return;
2020-04-21 13:53:32 -07:00
const state = getState();
if (getSettings(state).getIn(['saved'])) return;
2020-04-21 13:53:32 -07:00
const data = state.get('settings').delete('saved').toJS();
2020-04-21 13:53:32 -07:00
dispatch(patchMe({
pleroma_settings_store: {
[FE_NAME]: data,
},
2020-04-21 13:53:32 -07:00
})).then(response => {
dispatch({ type: SETTING_SAVE });
}).catch(error => {
dispatch(showAlertForError(error));
});
2020-03-27 13:59:38 -07:00
}, 5000, { trailing: true });
export function saveSettings() {
return (dispatch, getState) => debouncedSave(dispatch, getState);
};