diff --git a/app/gabsocial/actions/importer/index.js b/app/gabsocial/actions/importer/index.js index 1c82d93b0..db808d3e2 100644 --- a/app/gabsocial/actions/importer/index.js +++ b/app/gabsocial/actions/importer/index.js @@ -65,9 +65,9 @@ export function importFetchedStatuses(statuses) { function processStatus(status) { const normalOldStatus = getState().getIn(['statuses', status.id]); - const readingPrefs = getState().getIn(['preferences', 'reading']); + const expandSpoilers = getState().getIn(['settings', 'expandSpoilers']); - pushUnique(normalStatuses, normalizeStatus(status, normalOldStatus, readingPrefs)); + pushUnique(normalStatuses, normalizeStatus(status, normalOldStatus, expandSpoilers)); pushUnique(accounts, status.account); if (status.reblog && status.reblog.id) { diff --git a/app/gabsocial/actions/importer/normalizer.js b/app/gabsocial/actions/importer/normalizer.js index f1f4d7a2f..0edac3e5c 100644 --- a/app/gabsocial/actions/importer/normalizer.js +++ b/app/gabsocial/actions/importer/normalizer.js @@ -34,9 +34,8 @@ export function normalizeAccount(account) { return account; } -export function normalizeStatus(status, normalOldStatus, readingPrefs) { +export function normalizeStatus(status, normalOldStatus, expandSpoilers) { const normalStatus = { ...status }; - const expandSpoilers = readingPrefs.getIn(['expand', 'spoilers']); normalStatus.account = status.account.id; diff --git a/app/gabsocial/actions/preferences.js b/app/gabsocial/actions/preferences.js deleted file mode 100644 index 23b9d9081..000000000 --- a/app/gabsocial/actions/preferences.js +++ /dev/null @@ -1,62 +0,0 @@ -import api from '../api'; -import { fetchMeSuccess } from 'gabsocial/actions/me'; -import { debounce } from 'lodash'; -import { showAlertForError } from './alerts'; - -export const MASTO_PREFS_FETCH_SUCCESS = 'MASTO_PREFS_FETCH_SUCCESS'; -export const PREFERENCE_CHANGE = 'PREFERENCE_CHANGE'; -export const PREFERENCE_SAVE = 'PREFERENCE_SAVE'; - -export const FE_NAME = 'soapbox_fe'; - -export function fetchMastoPreferences() { - return (dispatch, getState) => { - api(getState).get('/api/v1/preferences').then(response => { - dispatch(mastoFetchPrefsSuccess(response.data)); - }).catch(e => { - console.error(e); - console.error('Could not fetch Mastodon preferences.'); - }); - }; -} - -export function mastoFetchPrefsSuccess(prefs) { - return { - type: MASTO_PREFS_FETCH_SUCCESS, - prefs, - }; -} - -export function changePreference(path, value) { - return dispatch => { - dispatch({ - type: PREFERENCE_CHANGE, - path, - value, - }); - - dispatch(savePreferences()); - }; -}; - -const debouncedSave = debounce((dispatch, getState) => { - if (!getState().get('me')) return; - if (getState().getIn(['preferences', 'saved'])) return; - - const data = getState().get('preferences').delete('saved').toJS(); - - api(getState).patch('/api/v1/accounts/update_credentials', { - pleroma_settings_store: { - [FE_NAME]: data, - }, - }).then(response => { - dispatch({ type: PREFERENCE_SAVE }); - dispatch(fetchMeSuccess(response.data)); - }).catch(error => { - dispatch(showAlertForError(error)); - }); -}, 5000, { trailing: true }); - -export function savePreferences() { - return (dispatch, getState) => debouncedSave(dispatch, getState); -}; diff --git a/app/gabsocial/actions/settings.js b/app/gabsocial/actions/settings.js index bfda2ddd1..f2aa32802 100644 --- a/app/gabsocial/actions/settings.js +++ b/app/gabsocial/actions/settings.js @@ -1,10 +1,13 @@ import api from '../api'; import { debounce } from 'lodash'; import { showAlertForError } from './alerts'; +import { fetchMeSuccess } from 'gabsocial/actions/me'; export const SETTING_CHANGE = 'SETTING_CHANGE'; export const SETTING_SAVE = 'SETTING_SAVE'; +export const FE_NAME = 'soapbox_fe'; + export function changeSetting(path, value) { return dispatch => { dispatch({ @@ -19,16 +22,20 @@ export function changeSetting(path, value) { const debouncedSave = debounce((dispatch, getState) => { if (!getState().get('me')) return; + if (getState().getIn(['settings', 'saved'])) return; - if (getState().getIn(['settings', 'saved'])) { - return; - } + const data = getState().get('settings').delete('saved').toJS(); - const data = getState().get('settings').filter((_, path) => path !== 'saved').toJS(); - - api().put('/api/web/settings', { data }) - .then(() => dispatch({ type: SETTING_SAVE })) - .catch(error => dispatch(showAlertForError(error))); + api(getState).patch('/api/v1/accounts/update_credentials', { + pleroma_settings_store: { + [FE_NAME]: data, + }, + }).then(response => { + dispatch({ type: SETTING_SAVE }); + dispatch(fetchMeSuccess(response.data)); + }).catch(error => { + dispatch(showAlertForError(error)); + }); }, 5000, { trailing: true }); export function saveSettings() { diff --git a/app/gabsocial/components/avatar.js b/app/gabsocial/components/avatar.js index 00da9a1ab..c3c8bb49f 100644 --- a/app/gabsocial/components/avatar.js +++ b/app/gabsocial/components/avatar.js @@ -4,7 +4,7 @@ import PropTypes from 'prop-types'; import ImmutablePropTypes from 'react-immutable-proptypes'; const mapStateToProps = state => ({ - animate: state.getIn(['preferences', 'auto_play_gif']), + animate: state.getIn(['settings', 'autoPlayGif']), }); export default @connect(mapStateToProps) diff --git a/app/gabsocial/components/avatar_composite.js b/app/gabsocial/components/avatar_composite.js index ada6448c7..a483cfed0 100644 --- a/app/gabsocial/components/avatar_composite.js +++ b/app/gabsocial/components/avatar_composite.js @@ -4,7 +4,7 @@ import PropTypes from 'prop-types'; import ImmutablePropTypes from 'react-immutable-proptypes'; const mapStateToProps = state => ({ - animate: state.getIn(['preferences', 'auto_play_gif']), + animate: state.getIn(['settings', 'autoPlayGif']), }); export default @connect(mapStateToProps) diff --git a/app/gabsocial/components/avatar_overlay.js b/app/gabsocial/components/avatar_overlay.js index 0d4069598..7c93cad1a 100644 --- a/app/gabsocial/components/avatar_overlay.js +++ b/app/gabsocial/components/avatar_overlay.js @@ -4,7 +4,7 @@ import PropTypes from 'prop-types'; import ImmutablePropTypes from 'react-immutable-proptypes'; const mapStateToProps = state => ({ - animate: state.getIn(['preferences', 'auto_play_gif']), + animate: state.getIn(['settings', 'autoPlayGif']), }); export default @connect(mapStateToProps) diff --git a/app/gabsocial/components/media_gallery.js b/app/gabsocial/components/media_gallery.js index 059abb7a8..8d9662d38 100644 --- a/app/gabsocial/components/media_gallery.js +++ b/app/gabsocial/components/media_gallery.js @@ -16,7 +16,7 @@ const messages = defineMessages({ }); const mapStateToProps = state => ({ - autoPlayGif: state.getIn(['preferences', 'auto_play_gif']), + autoPlayGif: state.getIn(['settings', 'autoPlayGif']), }); @connect(mapStateToProps) diff --git a/app/gabsocial/containers/gabsocial.js b/app/gabsocial/containers/gabsocial.js index a6c1fb5a0..77f4509fe 100644 --- a/app/gabsocial/containers/gabsocial.js +++ b/app/gabsocial/containers/gabsocial.js @@ -41,7 +41,7 @@ const mapStateToProps = (state) => { return { showIntroduction, me, - theme: state.getIn(['preferences', 'theme']), + theme: state.getIn(['settings', 'theme']), }; }; diff --git a/app/gabsocial/features/account/components/header.js b/app/gabsocial/features/account/components/header.js index 9b8dd7a4e..fc9c71e66 100644 --- a/app/gabsocial/features/account/components/header.js +++ b/app/gabsocial/features/account/components/header.js @@ -50,7 +50,7 @@ const messages = defineMessages({ const mapStateToProps = state => { return { me: state.get('me'), - autoPlayGif: state.getIn(['preferences', 'auto_play_gif']), + autoPlayGif: state.getIn(['settings', 'autoPlayGif']), }; }; diff --git a/app/gabsocial/features/account_gallery/components/media_item.js b/app/gabsocial/features/account_gallery/components/media_item.js index c4259f1e7..eda1ebe33 100644 --- a/app/gabsocial/features/account_gallery/components/media_item.js +++ b/app/gabsocial/features/account_gallery/components/media_item.js @@ -10,7 +10,7 @@ import { decode } from 'blurhash'; import { isIOS } from 'gabsocial/is_mobile'; const mapStateToProps = state => ({ - autoPlayGif: state.getIn(['preferences', 'auto_play_gif']), + autoPlayGif: state.getIn(['settings', 'autoPlayGif']), }); export default @connect(mapStateToProps) diff --git a/app/gabsocial/features/preferences/index.js b/app/gabsocial/features/preferences/index.js index c7c9c3539..023c6a898 100644 --- a/app/gabsocial/features/preferences/index.js +++ b/app/gabsocial/features/preferences/index.js @@ -4,7 +4,7 @@ import { defineMessages, injectIntl } from 'react-intl'; import ImmutablePureComponent from 'react-immutable-pure-component'; import PropTypes from 'prop-types'; import ImmutablePropTypes from 'react-immutable-proptypes'; -import { changePreference } from 'gabsocial/actions/preferences'; +import { changeSetting } from 'gabsocial/actions/settings'; import Column from '../ui/components/column'; const messages = defineMessages({ @@ -50,7 +50,7 @@ class Preferences extends ImmutablePureComponent { onThemeChange = e => { const { dispatch } = this.props; - dispatch(changePreference(['theme'], e.target.value)); + dispatch(changeSetting(['theme'], e.target.value)); } render() { diff --git a/app/gabsocial/features/ui/components/user_panel.js b/app/gabsocial/features/ui/components/user_panel.js index d18e1f9a9..407952386 100644 --- a/app/gabsocial/features/ui/components/user_panel.js +++ b/app/gabsocial/features/ui/components/user_panel.js @@ -90,7 +90,7 @@ const mapStateToProps = state => { return { account: getAccount(state, me), - autoPlayGif: state.getIn(['preferences', 'auto_play_gif']), + autoPlayGif: state.getIn(['settings', 'autoPlayGif']), }; }; diff --git a/app/gabsocial/reducers/__tests__/preferences-test.js b/app/gabsocial/reducers/__tests__/preferences-test.js deleted file mode 100644 index 36df57c4b..000000000 --- a/app/gabsocial/reducers/__tests__/preferences-test.js +++ /dev/null @@ -1,29 +0,0 @@ -import { mastoPrefsToMap } from '../preferences'; -import { Map as ImmutableMap } from 'immutable'; - -describe('mastoPrefToMap', () => { - const prefs = { - 'posting:default:visibility': 'public', - 'posting:default:sensitive': false, - 'posting:default:language': null, - 'reading:expand:media': 'default', - 'reading:expand:spoilers': false, - }; - it('returns a map', () => { - expect(mastoPrefsToMap(prefs)).toEqual(ImmutableMap({ - posting: ImmutableMap({ - default: ImmutableMap({ - visibility: 'public', - sensitive: false, - language: null, - }), - }), - reading: ImmutableMap({ - expand: ImmutableMap({ - media: 'default', - spoilers: false, - }), - }), - })); - }); -}); diff --git a/app/gabsocial/reducers/index.js b/app/gabsocial/reducers/index.js index b96d12e3f..c91f2f0cb 100644 --- a/app/gabsocial/reducers/index.js +++ b/app/gabsocial/reducers/index.js @@ -42,7 +42,6 @@ import soapbox from './soapbox'; import instance from './instance'; import me from './me'; import auth from './auth'; -import preferences from './preferences'; const reducers = { dropdown_menu, @@ -88,7 +87,6 @@ const reducers = { instance, me, auth, - preferences, }; export default combineReducers(reducers); diff --git a/app/gabsocial/reducers/preferences.js b/app/gabsocial/reducers/preferences.js deleted file mode 100644 index 9ce957b25..000000000 --- a/app/gabsocial/reducers/preferences.js +++ /dev/null @@ -1,55 +0,0 @@ -import { - MASTO_PREFS_FETCH_SUCCESS, - PREFERENCE_CHANGE, - PREFERENCE_SAVE, - FE_NAME, -} from 'gabsocial/actions/preferences'; -import { ME_FETCH_SUCCESS } from 'gabsocial/actions/me'; - -import { Map as ImmutableMap, fromJS } from 'immutable'; - -const initialState = ImmutableMap({ - saved: true, - posting: ImmutableMap({ - default: ImmutableMap({ - visibility: 'public', - sensitive: false, - language: null, - }), - }), - reading: ImmutableMap({ - expand: ImmutableMap({ - media: 'default', - spoilers: false, - }), - }), - auto_play_gif: false, - theme: 'lime', -}); - -export function mastoPrefsToMap(prefs) { - let map = ImmutableMap(); - for (const [key, value] of Object.entries(prefs)) { - map = map.setIn(key.split(':'), value); - } - return map; -} - -export default function preferences(state = initialState, action) { - switch(action.type) { - case MASTO_PREFS_FETCH_SUCCESS: - return state.merge(mastoPrefsToMap(action.prefs)); - case ME_FETCH_SUCCESS: - const me = fromJS(action.me); - const fePrefs = me.getIn(['pleroma', 'settings_store', FE_NAME]); - return state.merge(fePrefs); - case PREFERENCE_CHANGE: - return state - .setIn(action.path, action.value) - .set('saved', false); - case PREFERENCE_SAVE: - return state.set('saved', true); - default: - return state; - } -} diff --git a/app/gabsocial/reducers/settings.js b/app/gabsocial/reducers/settings.js index c9ccf17a0..24dab6301 100644 --- a/app/gabsocial/reducers/settings.js +++ b/app/gabsocial/reducers/settings.js @@ -1,17 +1,25 @@ -import { SETTING_CHANGE, SETTING_SAVE } from '../actions/settings'; +import { SETTING_CHANGE, SETTING_SAVE, FE_NAME } from '../actions/settings'; import { NOTIFICATIONS_FILTER_SET } from '../actions/notifications'; import { STORE_HYDRATE } from '../actions/store'; import { EMOJI_USE } from '../actions/emojis'; import { LIST_DELETE_SUCCESS, LIST_FETCH_FAIL } from '../actions/lists'; +import { ME_FETCH_SUCCESS } from 'gabsocial/actions/me'; import { Map as ImmutableMap, fromJS } from 'immutable'; import uuid from '../uuid'; const initialState = ImmutableMap({ saved: true, - onboarded: false, skinTone: 1, + reduceMotion: false, + autoPlayGif: false, + displayMedia: true, + expandSpoilers: false, + unfollowModal: false, + boostModal: false, + deleteModal: true, + theme: 'lime', home: ImmutableMap({ shows: ImmutableMap({ @@ -101,6 +109,10 @@ export default function settings(state = initialState, action) { switch(action.type) { case STORE_HYDRATE: return hydrate(state, action.state.get('settings')); + case ME_FETCH_SUCCESS: + const me = fromJS(action.me); + const fePrefs = me.getIn(['pleroma', 'settings_store', FE_NAME]); + return state.merge(fePrefs); case NOTIFICATIONS_FILTER_SET: case SETTING_CHANGE: return state