2021-08-11 16:23:42 -07:00
|
|
|
import { Map as ImmutableMap, List as ImmutableList, OrderedSet as ImmutableOrderedSet } from 'immutable';
|
2023-03-13 12:44:09 -07:00
|
|
|
import { defineMessage } 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:25:06 -08:00
|
|
|
|
2022-01-10 14:01:24 -08:00
|
|
|
import { patchMe } from 'soapbox/actions/me';
|
2021-11-08 18:21:41 -08:00
|
|
|
import messages from 'soapbox/locales/messages';
|
2022-12-20 07:47:46 -08:00
|
|
|
import toast from 'soapbox/toast';
|
2021-03-25 10:25:45 -07:00
|
|
|
import { isLoggedIn } from 'soapbox/utils/auth';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2022-06-19 12:26:57 -07:00
|
|
|
import type { AppDispatch, RootState } from 'soapbox/store';
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2023-06-28 15:53:17 -07:00
|
|
|
const SETTING_CHANGE = 'SETTING_CHANGE' as const;
|
|
|
|
const SETTING_SAVE = 'SETTING_SAVE' as const;
|
|
|
|
const SETTINGS_UPDATE = 'SETTINGS_UPDATE' as const;
|
2022-06-19 12:26:57 -07:00
|
|
|
|
|
|
|
const FE_NAME = 'soapbox_fe';
|
2020-04-18 11:58:51 -07:00
|
|
|
|
2022-06-22 10:54:36 -07:00
|
|
|
/** Options when changing/saving settings. */
|
|
|
|
type SettingOpts = {
|
|
|
|
/** Whether to display an alert when settings are saved. */
|
2023-02-15 13:26:27 -08:00
|
|
|
showAlert?: boolean
|
2022-06-22 10:54:36 -07:00
|
|
|
}
|
|
|
|
|
2023-03-13 12:44:09 -07:00
|
|
|
const saveSuccessMessage = defineMessage({ id: 'settings.save.success', defaultMessage: 'Your preferences have been saved!' });
|
2022-03-21 11:09:01 -07:00
|
|
|
|
2022-06-19 12:26:57 -07:00
|
|
|
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,
|
2021-07-28 07:03:14 -07:00
|
|
|
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,
|
2021-06-21 11:33:05 -07:00
|
|
|
missingDescriptionModal: false,
|
2020-04-28 10:59:15 -07:00
|
|
|
defaultPrivacy: 'public',
|
2020-12-24 12:20:07 -08:00
|
|
|
defaultContentType: 'text/plain',
|
2022-05-04 06:35:45 -07:00
|
|
|
themeMode: 'system',
|
2021-11-08 18:21:41 -08:00
|
|
|
locale: navigator.language || 'en',
|
2021-04-26 10:11:47 -07:00
|
|
|
showExplanationBox: true,
|
2020-07-26 15:30:54 -07:00
|
|
|
explanationBox: true,
|
2021-10-15 13:41:40 -07:00
|
|
|
autoloadTimelines: true,
|
2021-10-24 11:11:28 -07:00
|
|
|
autoloadMore: true,
|
2020-04-28 10:59:15 -07:00
|
|
|
|
|
|
|
systemFont: false,
|
|
|
|
demetricator: false,
|
|
|
|
|
2021-11-01 22:05:33 -07:00
|
|
|
isDeveloper: false,
|
|
|
|
|
2020-08-25 12:58:35 -07:00
|
|
|
chats: ImmutableMap({
|
|
|
|
panes: ImmutableList(),
|
|
|
|
mainWindow: 'minimized',
|
2020-09-04 19:43:14 -07:00
|
|
|
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,
|
2020-08-07 18:47:04 -07:00
|
|
|
direct: false,
|
2020-04-28 10:59:15 -07:00
|
|
|
}),
|
|
|
|
|
|
|
|
regex: ImmutableMap({
|
|
|
|
body: '',
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
|
|
|
|
notifications: ImmutableMap({
|
|
|
|
alerts: ImmutableMap({
|
|
|
|
follow: true,
|
2021-03-29 11:35:30 -07:00
|
|
|
follow_request: false,
|
2020-04-28 10:59:15 -07:00
|
|
|
favourite: true,
|
|
|
|
reblog: true,
|
|
|
|
mention: true,
|
|
|
|
poll: true,
|
2021-06-30 04:41:06 -07:00
|
|
|
move: true,
|
2021-01-19 16:50:08 -08:00
|
|
|
'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,
|
2021-06-30 04:41:06 -07:00
|
|
|
move: true,
|
2021-01-19 16:50:08 -08:00
|
|
|
'pleroma:emoji_reaction': true,
|
2020-04-28 10:59:15 -07:00
|
|
|
}),
|
|
|
|
|
|
|
|
sounds: ImmutableMap({
|
2020-05-18 17:43:58 -07:00
|
|
|
follow: false,
|
2021-03-29 11:35:30 -07:00
|
|
|
follow_request: false,
|
2020-05-18 17:43:58 -07:00
|
|
|
favourite: false,
|
|
|
|
reblog: false,
|
|
|
|
mention: false,
|
|
|
|
poll: false,
|
2021-06-30 04:41:06 -07:00
|
|
|
move: false,
|
2021-01-19 16:50:08 -08:00
|
|
|
'pleroma:emoji_reaction': false,
|
2020-04-28 10:59:15 -07:00
|
|
|
}),
|
2022-01-20 13:28:49 -08: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({
|
2020-08-14 10:42:03 -07:00
|
|
|
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: '',
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
|
2021-08-11 13:31:46 -07:00
|
|
|
account_timeline: ImmutableMap({
|
|
|
|
shows: ImmutableMap({
|
|
|
|
reblog: true,
|
2021-08-11 14:00:49 -07:00
|
|
|
pinned: true,
|
2021-12-12 20:34:10 -08:00
|
|
|
direct: false,
|
2021-08-11 13:31:46 -07:00
|
|
|
}),
|
|
|
|
}),
|
|
|
|
|
2022-12-11 12:37:00 -08:00
|
|
|
groups: ImmutableMap({}),
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2022-06-19 12:26:57 -07:00
|
|
|
const getSettings = createSelector([
|
|
|
|
(state: RootState) => state.soapbox.get('defaultSettings'),
|
|
|
|
(state: RootState) => state.settings,
|
2021-07-06 11:06:21 -07:00
|
|
|
], (soapboxSettings, settings) => {
|
2020-04-28 10:59:15 -07:00
|
|
|
return defaultSettings
|
|
|
|
.mergeDeep(soapboxSettings)
|
2021-07-06 11:06:21 -07:00
|
|
|
.mergeDeep(settings);
|
|
|
|
});
|
2020-04-28 10:59:15 -07:00
|
|
|
|
2023-06-28 15:53:17 -07:00
|
|
|
interface SettingChangeAction {
|
|
|
|
type: typeof SETTING_CHANGE
|
|
|
|
path: string[]
|
|
|
|
value: any
|
|
|
|
}
|
|
|
|
|
2022-06-22 10:54:36 -07:00
|
|
|
const changeSettingImmediate = (path: string[], value: any, opts?: SettingOpts) =>
|
2022-06-19 12:26:57 -07:00
|
|
|
(dispatch: AppDispatch) => {
|
2023-06-28 15:53:17 -07:00
|
|
|
const action: SettingChangeAction = {
|
2022-01-10 19:25:30 -08:00
|
|
|
type: SETTING_CHANGE,
|
|
|
|
path,
|
|
|
|
value,
|
2023-06-28 15:53:17 -07:00
|
|
|
};
|
2022-01-10 19:25:30 -08:00
|
|
|
|
2023-06-28 15:53:17 -07:00
|
|
|
dispatch(action);
|
2022-06-22 10:54:36 -07:00
|
|
|
dispatch(saveSettingsImmediate(opts));
|
2022-01-10 19:25:30 -08:00
|
|
|
};
|
|
|
|
|
2022-06-22 10:54:36 -07:00
|
|
|
const changeSetting = (path: string[], value: any, opts?: SettingOpts) =>
|
2022-06-19 12:26:57 -07:00
|
|
|
(dispatch: AppDispatch) => {
|
2023-06-28 15:53:17 -07:00
|
|
|
const action: SettingChangeAction = {
|
2020-03-27 13:59:38 -07:00
|
|
|
type: SETTING_CHANGE,
|
|
|
|
path,
|
|
|
|
value,
|
2023-06-28 15:53:17 -07:00
|
|
|
};
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2023-06-28 15:53:17 -07:00
|
|
|
dispatch(action);
|
2022-06-22 10:54:36 -07:00
|
|
|
return dispatch(saveSettings(opts));
|
2020-03-27 13:59:38 -07:00
|
|
|
};
|
|
|
|
|
2022-06-22 10:54:36 -07:00
|
|
|
const saveSettingsImmediate = (opts?: SettingOpts) =>
|
2022-06-19 12:26:57 -07:00
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
2022-01-10 19:25:30 -08:00
|
|
|
if (!isLoggedIn(getState)) return;
|
|
|
|
|
|
|
|
const state = getState();
|
|
|
|
if (getSettings(state).getIn(['saved'])) return;
|
|
|
|
|
2022-06-19 12:26:57 -07:00
|
|
|
const data = state.settings.delete('saved').toJS();
|
2022-01-10 19:25:30 -08:00
|
|
|
|
|
|
|
dispatch(patchMe({
|
|
|
|
pleroma_settings_store: {
|
|
|
|
[FE_NAME]: data,
|
|
|
|
},
|
2022-06-19 12:26:57 -07:00
|
|
|
})).then(() => {
|
2022-01-10 19:25:30 -08:00
|
|
|
dispatch({ type: SETTING_SAVE });
|
2022-03-21 11:09:01 -07:00
|
|
|
|
2022-06-22 10:54:36 -07:00
|
|
|
if (opts?.showAlert) {
|
2023-03-13 12:44:09 -07:00
|
|
|
toast.success(saveSuccessMessage);
|
2022-06-22 10:17:35 -07:00
|
|
|
}
|
2022-01-10 19:25:30 -08:00
|
|
|
}).catch(error => {
|
2022-12-20 09:45:46 -08:00
|
|
|
toast.showAlertForError(error);
|
2022-01-10 19:25:30 -08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-06-22 10:54:36 -07:00
|
|
|
const saveSettings = (opts?: SettingOpts) =>
|
|
|
|
(dispatch: AppDispatch) => dispatch(saveSettingsImmediate(opts));
|
2022-06-19 12:26:57 -07:00
|
|
|
|
2023-03-13 12:44:09 -07:00
|
|
|
const getLocale = (state: RootState, fallback = 'en') => {
|
|
|
|
const localeWithVariant = (getSettings(state).get('locale') as string).replace('_', '-');
|
|
|
|
const locale = localeWithVariant.split('-')[0];
|
|
|
|
return Object.keys(messages).includes(localeWithVariant) ? localeWithVariant : Object.keys(messages).includes(locale) ? locale : fallback;
|
|
|
|
};
|
|
|
|
|
2023-06-28 15:53:17 -07:00
|
|
|
type SettingsAction =
|
|
|
|
| SettingChangeAction
|
|
|
|
| { type: typeof SETTING_SAVE }
|
|
|
|
|
2022-06-19 12:26:57 -07:00
|
|
|
export {
|
|
|
|
SETTING_CHANGE,
|
|
|
|
SETTING_SAVE,
|
|
|
|
SETTINGS_UPDATE,
|
|
|
|
FE_NAME,
|
|
|
|
defaultSettings,
|
|
|
|
getSettings,
|
|
|
|
changeSettingImmediate,
|
|
|
|
changeSetting,
|
|
|
|
saveSettingsImmediate,
|
|
|
|
saveSettings,
|
2023-03-13 12:44:09 -07:00
|
|
|
getLocale,
|
2023-06-28 15:53:17 -07:00
|
|
|
type SettingsAction,
|
2022-06-19 12:26:57 -07:00
|
|
|
};
|