2023-03-22 10:43:53 +00:00
|
|
|
import { set, get, cloneDeep } from 'lodash'
|
2023-03-12 22:09:47 +00:00
|
|
|
|
|
|
|
export const defaultState = {
|
|
|
|
needsReboot: null,
|
|
|
|
config: null,
|
2023-03-19 19:27:07 +00:00
|
|
|
modifiedPaths: null,
|
2023-03-22 10:43:53 +00:00
|
|
|
descriptions: null,
|
|
|
|
draft: null
|
2023-03-12 22:09:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const newUserFlags = {
|
|
|
|
...defaultState.flagStorage
|
|
|
|
}
|
|
|
|
|
2023-03-14 19:50:43 +00:00
|
|
|
const adminSettingsStorage = {
|
2023-03-12 22:09:47 +00:00
|
|
|
state: {
|
|
|
|
...cloneDeep(defaultState)
|
|
|
|
},
|
|
|
|
mutations: {
|
|
|
|
updateAdminSettings (state, { config, modifiedPaths }) {
|
|
|
|
state.config = config
|
|
|
|
state.modifiedPaths = modifiedPaths
|
2023-03-19 19:27:07 +00:00
|
|
|
},
|
|
|
|
updateAdminDescriptions (state, { descriptions }) {
|
|
|
|
state.descriptions = descriptions
|
2023-03-22 10:43:53 +00:00
|
|
|
},
|
|
|
|
updateAdminDraft (state, { path, value }) {
|
|
|
|
const [group, key, subkey] = path
|
|
|
|
const parent = [group, key, subkey]
|
|
|
|
|
|
|
|
set(state.draft, path, value)
|
|
|
|
|
|
|
|
// force-updating grouped draft to trigger refresh of group settings
|
|
|
|
if (path.length > parent.length) {
|
|
|
|
set(state.draft, parent, cloneDeep(get(state.draft, parent)))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
resetAdminDraft (state) {
|
|
|
|
state.draft = cloneDeep(state.config)
|
2023-03-12 22:09:47 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
setInstanceAdminSettings ({ state, commit, dispatch }, { backendDbConfig }) {
|
2023-03-16 21:18:55 +00:00
|
|
|
const config = state.config || {}
|
|
|
|
const modifiedPaths = state.modifiedPaths || new Set()
|
2023-03-12 22:09:47 +00:00
|
|
|
backendDbConfig.configs.forEach(c => {
|
2023-03-20 21:36:47 +00:00
|
|
|
const path = [c.group, c.key]
|
2023-03-12 22:09:47 +00:00
|
|
|
if (c.db) {
|
2023-03-22 10:43:53 +00:00
|
|
|
// Path elements can contain dot, therefore we use ' -> ' as a separator instead
|
|
|
|
// Using strings for modified paths for easier searching
|
|
|
|
c.db.forEach(x => modifiedPaths.add([...path, x].join(' -> ')))
|
2023-03-12 22:09:47 +00:00
|
|
|
}
|
|
|
|
const convert = (value) => {
|
|
|
|
if (Array.isArray(value) && value.length > 0 && value[0].tuple) {
|
|
|
|
return value.reduce((acc, c) => {
|
|
|
|
return { ...acc, [c.tuple[0]]: convert(c.tuple[1]) }
|
|
|
|
}, {})
|
|
|
|
} else {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set(config, path, convert(c.value))
|
|
|
|
})
|
2023-03-20 21:36:47 +00:00
|
|
|
console.log(config[':pleroma'])
|
2023-03-12 22:09:47 +00:00
|
|
|
commit('updateAdminSettings', { config, modifiedPaths })
|
2023-03-22 10:43:53 +00:00
|
|
|
commit('resetAdminDraft')
|
2023-03-16 21:18:55 +00:00
|
|
|
},
|
2023-03-19 19:27:07 +00:00
|
|
|
setInstanceAdminDescriptions ({ state, commit, dispatch }, { backendDescriptions }) {
|
|
|
|
const convert = ({ children, description, label, key = '<ROOT>', group, suggestions }, path, acc) => {
|
2023-03-20 21:36:47 +00:00
|
|
|
const newPath = group ? [group, key] : [key]
|
2023-03-19 19:27:07 +00:00
|
|
|
const obj = { description, label, suggestions }
|
|
|
|
if (Array.isArray(children)) {
|
|
|
|
children.forEach(c => {
|
2023-03-20 21:36:47 +00:00
|
|
|
convert(c, newPath, obj)
|
2023-03-19 19:27:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
set(acc, newPath, obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
const descriptions = {}
|
|
|
|
backendDescriptions.forEach(d => convert(d, '', descriptions))
|
2023-03-20 21:36:47 +00:00
|
|
|
console.log(descriptions[':pleroma']['Pleroma.Captcha'])
|
2023-03-19 19:27:07 +00:00
|
|
|
commit('updateAdminDescriptions', { descriptions })
|
|
|
|
},
|
2023-03-16 21:18:55 +00:00
|
|
|
pushAdminSetting ({ rootState, state, commit, dispatch }, { path, value }) {
|
2023-03-20 21:36:47 +00:00
|
|
|
const [group, key, ...rest] = Array.isArray(path) ? path : path.split(/\./g)
|
2023-03-16 21:18:55 +00:00
|
|
|
const clone = {} // not actually cloning the entire thing to avoid excessive writes
|
2023-03-20 21:36:47 +00:00
|
|
|
set(clone, rest, value)
|
2023-03-16 21:18:55 +00:00
|
|
|
|
|
|
|
// TODO cleanup paths in modifiedPaths
|
|
|
|
const convert = (value) => {
|
|
|
|
if (typeof value !== 'object') {
|
|
|
|
return value
|
|
|
|
} else if (Array.isArray(value)) {
|
|
|
|
return value.map(convert)
|
|
|
|
} else {
|
|
|
|
return Object.entries(value).map(([k, v]) => ({ tuple: [k, v] }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rootState.api.backendInteractor.pushInstanceDBConfig({
|
|
|
|
payload: {
|
|
|
|
configs: [{
|
|
|
|
group,
|
|
|
|
key,
|
|
|
|
value: convert(clone)
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(() => rootState.api.backendInteractor.fetchInstanceDBConfig())
|
|
|
|
.then(backendDbConfig => dispatch('setInstanceAdminSettings', { backendDbConfig }))
|
|
|
|
},
|
|
|
|
resetAdminSetting ({ rootState, state, commit, dispatch }, { path }) {
|
|
|
|
const [group, key, subkey] = path.split(/\./g)
|
|
|
|
|
|
|
|
state.modifiedPaths.delete(path)
|
|
|
|
|
|
|
|
return rootState.api.backendInteractor.pushInstanceDBConfig({
|
|
|
|
payload: {
|
|
|
|
configs: [{
|
|
|
|
group,
|
|
|
|
key,
|
|
|
|
delete: true,
|
|
|
|
subkeys: [subkey]
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(() => rootState.api.backendInteractor.fetchInstanceDBConfig())
|
|
|
|
.then(backendDbConfig => dispatch('setInstanceAdminSettings', { backendDbConfig }))
|
2023-03-12 22:09:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-14 19:50:43 +00:00
|
|
|
export default adminSettingsStorage
|