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))
},
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'])
},

View file

@ -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)

View file

@ -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')
}
})
}
}
}

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