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

230 lines
4.8 KiB
JavaScript
Raw Normal View History

2021-08-11 16:23:42 -07:00
import { Map as ImmutableMap, List as ImmutableList, OrderedSet as ImmutableOrderedSet } from 'immutable';
2022-03-21 11:09:01 -07:00
import { defineMessages } from 'react-intl';
2022-01-10 14:01:24 -08:00
import { createSelector } from 'reselect';
2022-04-15 18:00:03 -07:00
import { v4 as uuid } from 'uuid';
2022-01-10 14:01:24 -08:00
import { patchMe } from 'soapbox/actions/me';
2021-03-25 10:25:45 -07:00
import { isLoggedIn } from 'soapbox/utils/auth';
2022-01-10 14:01:24 -08:00
import { showAlertForError } from './alerts';
2022-03-21 11:09:01 -07:00
import snackbar from './snackbar';
2020-03-27 13:59:38 -07:00
export const SETTING_CHANGE = 'SETTING_CHANGE';
export const SETTING_SAVE = 'SETTING_SAVE';
export const SETTINGS_UPDATE = 'SETTINGS_UPDATE';
2020-03-27 13:59:38 -07:00
export const FE_NAME = 'soapbox_fe';
2022-03-21 11:09:01 -07:00
const messages = defineMessages({
saveSuccess: { id: 'settings.save.success', defaultMessage: 'Your preferences have been saved!' },
});
2020-10-01 16:57:11 -07:00
export const defaultSettings = ImmutableMap({
2022-05-02 13:55:52 -07:00
onboarded: false,
2020-04-28 10:59:15 -07:00
skinTone: 1,
reduceMotion: false,
underlineLinks: 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',
2022-05-04 06:35:45 -07:00
themeMode: 'system',
2020-06-04 15:29:00 -07:00
locale: navigator.language.split(/[-_]/)[0] || 'en',
showExplanationBox: true,
explanationBox: true,
autoloadTimelines: true,
autoloadMore: true,
2020-04-28 10:59:15 -07:00
systemFont: false,
dyslexicFont: false,
demetricator: false,
isDeveloper: 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,
2022-04-16 17:03:33 -07:00
follow_request: true,
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
}),
birthdays: ImmutableMap({
show: true,
}),
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,
2021-12-12 20:34:10 -08:00
direct: false,
2020-08-07 17:28:30 -07:00
}),
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,
2021-12-12 20:34:10 -08:00
direct: false,
2020-08-07 17:28:30 -07:00
}),
2020-04-28 10:59:15 -07:00
other: ImmutableMap({
onlyMedia: false,
}),
regex: ImmutableMap({
body: '',
}),
}),
direct: ImmutableMap({
regex: ImmutableMap({
body: '',
}),
}),
account_timeline: ImmutableMap({
shows: ImmutableMap({
reblog: true,
pinned: true,
2021-12-12 20:34:10 -08:00
direct: false,
}),
}),
2020-04-28 10:59:15 -07:00
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: {} }),
]),
2021-08-11 16:23:42 -07:00
remote_timeline: ImmutableMap({
pinnedHosts: ImmutableOrderedSet(),
}),
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
export function changeSettingImmediate(path, value) {
return dispatch => {
dispatch({
type: SETTING_CHANGE,
path,
value,
});
dispatch(saveSettingsImmediate());
};
}
2022-03-21 11:09:01 -07:00
export function changeSetting(path, value, intl) {
2020-03-27 13:59:38 -07:00
return dispatch => {
dispatch({
type: SETTING_CHANGE,
path,
value,
});
2022-03-21 11:09:01 -07:00
return dispatch(saveSettings(intl));
2020-03-27 13:59:38 -07:00
};
2021-08-03 12:22:51 -07:00
}
2020-03-27 13:59:38 -07:00
2022-03-21 11:09:01 -07:00
export function saveSettingsImmediate(intl) {
return (dispatch, getState) => {
if (!isLoggedIn(getState)) return;
const state = getState();
if (getSettings(state).getIn(['saved'])) return;
const data = state.get('settings').delete('saved').toJS();
dispatch(patchMe({
pleroma_settings_store: {
[FE_NAME]: data,
},
})).then(response => {
dispatch({ type: SETTING_SAVE });
2022-03-21 11:09:01 -07:00
if (intl) {
dispatch(snackbar.success(intl.formatMessage(messages.saveSuccess)));
}
}).catch(error => {
dispatch(showAlertForError(error));
});
};
}
2022-03-21 11:09:01 -07:00
export function saveSettings(intl) {
return (dispatch, getState) => dispatch(saveSettingsImmediate(intl));
2021-08-03 12:22:51 -07:00
}