From c82f73482ec63d2ccbe046da09d3a0958c3c71f4 Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Mon, 19 Sep 2022 20:35:28 +0100 Subject: [PATCH] load settings on boot --- src/components/status_body/status_body.js | 2 +- src/modules/api.js | 6 --- src/modules/config.js | 56 +++++++++++++++++++++-- src/services/api/api.service.js | 15 +++++- 4 files changed, 67 insertions(+), 12 deletions(-) diff --git a/src/components/status_body/status_body.js b/src/components/status_body/status_body.js index 9e380f28..19cec6c6 100644 --- a/src/components/status_body/status_body.js +++ b/src/components/status_body/status_body.js @@ -83,7 +83,7 @@ const StatusContent = { return this.status.attachments.map(file => fileType.fileType(file.mimetype)) }, translationLanguages () { - return (this.$store.getters.mergedConfig.supportedTranslationLanguages.source || []).map(lang => ({ key: lang.code, value: lang.code, label: lang.name })) + return (this.$store.state.instance.supportedTranslationLanguages.source || []).map(lang => ({ key: lang.code, value: lang.code, label: lang.name })) }, ...mapGetters(['mergedConfig']) }, diff --git a/src/modules/api.js b/src/modules/api.js index fb81d1b3..30060911 100644 --- a/src/modules/api.js +++ b/src/modules/api.js @@ -265,12 +265,6 @@ const api = { store.dispatch('setInstanceOption', { name: 'supportedTranslationLanguages', value: data }) }) }, - getSettingsProfile (store) { - store.state.backendInteractor.getSettingsProfile({ store }) - .then((data) => { - console.log('LOADED', data) - }) - }, // Pleroma websocket setWsToken (store, token) { store.commit('setWsToken', token) diff --git a/src/modules/config.js b/src/modules/config.js index 669767b5..ac24b99e 100644 --- a/src/modules/config.js +++ b/src/modules/config.js @@ -21,6 +21,8 @@ export const multiChoiceProperties = [ ] export const defaultState = { + profile: 'default', + profileVersion: 0, expertLevel: 0, // used to track which settings to show and hide colors: {}, theme: undefined, @@ -149,7 +151,6 @@ const config = { mutations: { setOption (state, { name, value }) { state[name] = value - console.log('SETOPTION', JSON.stringify(state)) }, setHighlight (state, { user, color, type }) { const data = this.state.config.highlight[user] @@ -161,8 +162,33 @@ const config = { } }, actions: { - syncSettings: ({ dispatch }) => { - dispatch('syncSettingsToServer') + syncSettings: (store) => { + store.commit('setOption', { name: 'profileVersion', value: store.state.profileVersion + 1 }) + const notice = { + level: 'warning', + messageKey: 'settingsProfile.synchronizing', + timeout: 5000 + } + store.dispatch('pushGlobalNotice', notice) + store.rootState.api.backendInteractor.saveSettingsProfile({ + settings: store.state, profileName: store.state.profile, version: store.state.profileVersion + }).then(() => { + store.dispatch('removeGlobalNotice', notice) + store.dispatch('pushGlobalNotice', { + level: 'success', + messageKey: 'settingsProfile.synchronized', + timeout: 2000 + }) + }).catch((err) => { + store.dispatch('removeGlobalNotice', notice) + store.dispatch('pushGlobalNotice', { + level: 'error', + messageKey: 'settingsProfile.synchronizationError', + messageArgs: { error: err.message }, + timeout: 5000 + }) + console.error(err) + }) }, loadSettings ({ dispatch }, data) { const knownKeys = new Set(Object.keys(defaultState)) @@ -184,7 +210,7 @@ const config = { setOption ({ commit, dispatch }, { name, value, manual }) { commit('setOption', { name, value }) if (manual === true) { - dispatch('syncSettingsToServer') + dispatch('syncSettings') } switch (name) { case 'theme': @@ -203,6 +229,28 @@ const config = { dispatch('setLayoutWidth', undefined) break } + }, + getSettingsProfile (store) { + const profile = store.state.profile + store.rootState.api.backendInteractor.getSettingsProfile({ store, profileName: profile }) + .then(({ settings, version }) => { + if (version > store.state.profileVersion) { + store.commit('setOption', { name: 'profileVersion', value: version }) + store.dispatch('loadSettings', settings) + } + }) + .catch((err) => { + console.error(`could not fetch profile ${profile}`, err) + if (err.statusCode === 404) { + // create profile + store.dispatch('pushGlobalNotice', { + level: 'warning', + messageKey: 'settingsProfile.creating', + timeout: 5000 + }) + store.dispatch('syncSettings') + } + }) } } } diff --git a/src/services/api/api.service.js b/src/services/api/api.service.js index 0ce7937f..319277e3 100644 --- a/src/services/api/api.service.js +++ b/src/services/api/api.service.js @@ -1459,6 +1459,18 @@ const getSettingsProfile = ({ profileName, credentials }) => { }) } +const saveSettingsProfile = ({ profileName, credentials, settings, version }) => { + return promisedRequest({ + url: AKKOMA_SETTING_PROFILE_URL(profileName), + method: 'PUT', + credentials, + payload: { + settings, + version + } + }) +} + export const getMastodonSocketURI = ({ credentials, stream, args = {} }) => { return Object.entries({ ...(credentials @@ -1686,7 +1698,8 @@ const apiService = { adminFetchAnnouncements, translateStatus, getSupportedTranslationlanguages, - getSettingsProfile + getSettingsProfile, + saveSettingsProfile } export default apiService