load settings on boot

This commit is contained in:
FloatingGhost 2022-09-19 20:35:28 +01:00
parent d7c9cd7585
commit c82f73482e
4 changed files with 67 additions and 12 deletions

View file

@ -83,7 +83,7 @@ const StatusContent = {
return this.status.attachments.map(file => fileType.fileType(file.mimetype)) return this.status.attachments.map(file => fileType.fileType(file.mimetype))
}, },
translationLanguages () { 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']) ...mapGetters(['mergedConfig'])
}, },

View file

@ -265,12 +265,6 @@ const api = {
store.dispatch('setInstanceOption', { name: 'supportedTranslationLanguages', value: data }) store.dispatch('setInstanceOption', { name: 'supportedTranslationLanguages', value: data })
}) })
}, },
getSettingsProfile (store) {
store.state.backendInteractor.getSettingsProfile({ store })
.then((data) => {
console.log('LOADED', data)
})
},
// Pleroma websocket // Pleroma websocket
setWsToken (store, token) { setWsToken (store, token) {
store.commit('setWsToken', token) store.commit('setWsToken', token)

View file

@ -21,6 +21,8 @@ export const multiChoiceProperties = [
] ]
export const defaultState = { export const defaultState = {
profile: 'default',
profileVersion: 0,
expertLevel: 0, // used to track which settings to show and hide expertLevel: 0, // used to track which settings to show and hide
colors: {}, colors: {},
theme: undefined, theme: undefined,
@ -149,7 +151,6 @@ const config = {
mutations: { mutations: {
setOption (state, { name, value }) { setOption (state, { name, value }) {
state[name] = value state[name] = value
console.log('SETOPTION', JSON.stringify(state))
}, },
setHighlight (state, { user, color, type }) { setHighlight (state, { user, color, type }) {
const data = this.state.config.highlight[user] const data = this.state.config.highlight[user]
@ -161,8 +162,33 @@ const config = {
} }
}, },
actions: { actions: {
syncSettings: ({ dispatch }) => { syncSettings: (store) => {
dispatch('syncSettingsToServer') 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) { loadSettings ({ dispatch }, data) {
const knownKeys = new Set(Object.keys(defaultState)) const knownKeys = new Set(Object.keys(defaultState))
@ -184,7 +210,7 @@ const config = {
setOption ({ commit, dispatch }, { name, value, manual }) { setOption ({ commit, dispatch }, { name, value, manual }) {
commit('setOption', { name, value }) commit('setOption', { name, value })
if (manual === true) { if (manual === true) {
dispatch('syncSettingsToServer') dispatch('syncSettings')
} }
switch (name) { switch (name) {
case 'theme': case 'theme':
@ -203,6 +229,28 @@ const config = {
dispatch('setLayoutWidth', undefined) dispatch('setLayoutWidth', undefined)
break 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')
}
})
} }
} }
} }

View file

@ -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 = {} }) => { export const getMastodonSocketURI = ({ credentials, stream, args = {} }) => {
return Object.entries({ return Object.entries({
...(credentials ...(credentials
@ -1686,7 +1698,8 @@ const apiService = {
adminFetchAnnouncements, adminFetchAnnouncements,
translateStatus, translateStatus,
getSupportedTranslationlanguages, getSupportedTranslationlanguages,
getSettingsProfile getSettingsProfile,
saveSettingsProfile
} }
export default apiService export default apiService