pleroma-fe/src/modules/adminSettings.js

405 lines
13 KiB
JavaScript
Raw Normal View History

2026-01-06 16:23:17 +02:00
import { cloneDeep, differenceWith, flatten, get, isEqual, set } from 'lodash'
export const defaultState = {
frontends: [],
2023-03-27 22:57:50 +03:00
loaded: false,
needsReboot: null,
config: null,
2023-03-19 21:27:07 +02:00
modifiedPaths: null,
descriptions: null,
2023-03-27 22:57:50 +03:00
draft: null,
2026-01-06 16:22:52 +02:00
dbConfigEnabled: null,
}
export const newUserFlags = {
2026-01-06 16:22:52 +02:00
...defaultState.flagStorage,
}
2023-03-14 21:50:43 +02:00
const adminSettingsStorage = {
state: {
2026-01-06 16:22:52 +02:00
...cloneDeep(defaultState),
},
mutations: {
2026-01-06 16:22:52 +02:00
setInstanceAdminNoDbConfig(state) {
2023-03-27 22:57:50 +03:00
state.loaded = false
state.dbConfigEnabled = false
},
2026-01-06 16:22:52 +02:00
setAvailableFrontends(state, { frontends }) {
state.frontends = frontends.map((f) => {
f.installedRefs = f.installed_refs
if (f.name === 'pleroma-fe') {
f.refs = ['master', 'develop']
} else {
f.refs = [f.ref]
}
return f
})
},
2026-01-06 16:22:52 +02:00
updateAdminSettings(state, { config, modifiedPaths }) {
2023-03-27 22:57:50 +03:00
state.loaded = true
state.dbConfigEnabled = true
state.config = config
state.modifiedPaths = modifiedPaths
2023-03-19 21:27:07 +02:00
},
2026-01-06 16:22:52 +02:00
updateAdminDescriptions(state, { descriptions }) {
2023-03-19 21:27:07 +02:00
state.descriptions = descriptions
},
2026-01-06 16:22:52 +02: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)))
}
},
2026-01-06 16:22:52 +02:00
resetAdminDraft(state) {
state.draft = cloneDeep(state.config)
2026-01-06 16:22:52 +02:00
},
},
actions: {
async fetchAdminUsers(store, opts) {
const data = await store.rootState.api.backendInteractor.adminListUsers({
opts,
})
data.users.forEach((user) =>
store.dispatch('fetchUserIfMissing', user.id),
)
return data
2025-07-15 14:30:46 +02:00
},
adminAddUserToAdminGroup(store, user) {
store.rootState.api.backendInteractor
.adminAddUserToAdminGroup({ user })
.then((res) =>
store.commit('updateRight', {
user,
right: 'admin',
value: res.is_admin,
}),
)
2025-07-20 18:59:05 +02:00
},
adminRemoveUserFromAdminGroup(store, user) {
// prevent revokation of own rights
if (user.id !== store.rootState.users.currentUser.id) {
return store.rootState.api.backendInteractor
.adminRemoveUserFromAdminGroup({ user })
.then((res) =>
store.commit('updateRight', {
user,
right: 'admin',
value: res.is_admin,
}),
)
}
2025-07-20 18:59:05 +02:00
},
adminAddUserToModeratorGroup(store, user) {
return store.rootState.api.backendInteractor
.adminAddUserToModeratorGroup({ user })
.then((res) =>
store.commit('updateRight', {
user,
right: 'moderator',
value: res.is_moderator,
}),
)
2025-07-20 18:59:05 +02:00
},
adminRemoveUserFromModeratorGroup(store, user) {
// prevent revokation of own rights
if (user.id !== store.state.users.currentUser.id) {
return store.rootState.api.backendInteractor
.adminRemoveUserFromModeratorGroup({ user })
.then((res) =>
store.commit('updateRight', {
user,
right: 'moderator',
value: res.is_moderator,
}),
)
}
2025-07-20 18:59:05 +02:00
},
adminActivateUser(store, user) {
return store.rootState.api.backendInteractor
.activateUser({ user })
.then((res) => {
const deactivated = !res.is_active
store.commit('updateActivationStatus', { user, deactivated })
})
2025-07-20 18:59:05 +02:00
},
adminDeactivateUser(store, user) {
return store.rootState.api.backendInteractor
.deactivateUser({ user })
.then((res) => {
const deactivated = !res.is_active
store.commit('updateActivationStatus', { user, deactivated })
})
2025-07-20 18:59:05 +02:00
},
adminDeleteUser(store, user) {
return store.rootState.api.backendInteractor.deleteUser({ user })
2025-07-21 00:32:49 +02:00
},
adminConfirmUser(store, user) {
return store.rootState.api.backendInteractor
.adminConfirmUser({ user })
.then(() => store.dispatch('fetchUser', user.id))
2025-07-21 00:32:49 +02:00
},
adminResendConfirmationEmail(store, user) {
return store.rootState.api.backendInteractor.adminResendConfirmationEmail(
{ user },
)
2025-07-20 18:59:05 +02:00
},
adminApproveUser(store, user) {
return store.rootState.api.backendInteractor.adminApproveUser({ user })
},
2026-06-08 20:31:42 +03:00
adminListStatuses(store, { userId, opts }) {
return store.rootState.api.backendInteractor.adminListStatuses({
2026-06-08 20:31:42 +03:00
userId,
opts,
})
},
adminChangeStatusScope(store, { opts }) {
return store.rootState.api.backendInteractor.adminChangeStatusScope({
opts,
})
},
adminDisableMFA(store, user) {
2025-09-16 09:24:02 +02:00
return store.rootState.api.backendInteractor.adminDisableMFA({ user })
2025-09-06 15:12:32 +02:00
},
adminTagUser(store, { user, tag }) {
2025-09-15 13:37:08 +02:00
return store.rootState.api.backendInteractor.tagUser({ user, tag })
},
adminUntagUser(store, { user, tag }) {
2025-09-15 13:37:08 +02:00
return store.rootState.api.backendInteractor.untagUser({ user, tag })
},
2026-01-06 16:22:52 +02:00
loadFrontendsStuff({ rootState, commit }) {
rootState.api.backendInteractor
.fetchAvailableFrontends()
.then((frontends) => commit('setAvailableFrontends', { frontends }))
},
2026-01-06 16:22:52 +02:00
loadAdminStuff({ state, rootState, dispatch, commit }) {
rootState.api.backendInteractor
.fetchInstanceDBConfig()
.then((backendDbConfig) => {
2023-03-27 22:57:50 +03:00
if (backendDbConfig.error) {
if (backendDbConfig.error.status === 400) {
2026-01-06 16:22:52 +02:00
backendDbConfig.error.json().then((errorJson) => {
2023-03-27 22:57:50 +03:00
if (/configurable_from_database/.test(errorJson.error)) {
commit('setInstanceAdminNoDbConfig')
}
})
}
} else {
dispatch('setInstanceAdminSettings', { backendDbConfig })
}
})
if (state.descriptions === null) {
2026-01-06 16:22:52 +02:00
rootState.api.backendInteractor
.fetchInstanceConfigDescriptions()
.then((backendDescriptions) =>
dispatch('setInstanceAdminDescriptions', { backendDescriptions }),
)
2023-03-27 22:57:50 +03:00
}
},
2026-01-06 16:22:52 +02:00
setInstanceAdminSettings({ state, commit }, { backendDbConfig }) {
const config = state.config || {}
const modifiedPaths = new Set()
2025-12-09 13:21:46 +02:00
2026-01-06 16:22:52 +02:00
backendDbConfig.configs.forEach((c) => {
const path = [c.group, c.key]
if (c.db) {
// Path elements can contain dot, therefore we use ' -> ' as a separator instead
// Using strings for modified paths for easier searching
2026-01-06 16:22:52 +02:00
c.db.forEach((x) => modifiedPaths.add([...path, x].join(' -> ')))
}
2025-12-09 13:21:46 +02:00
// we need to preserve tuples on second level only, possibly third
// but it's not a case right now.
const convert = (value, preserveTuples, preserveTuplesLv2) => {
if (Array.isArray(value) && value.length > 0 && value[0].tuple) {
2025-12-09 13:21:46 +02:00
if (!preserveTuples) {
return value.reduce((acc, c) => {
if (c.tuple == null) {
return {
...acc,
[c]: c,
}
}
2026-01-06 16:22:52 +02:00
return {
...acc,
[c.tuple[0]]: convert(c.tuple[1], preserveTuplesLv2),
}
2025-12-09 13:21:46 +02:00
}, {})
} else {
2026-01-06 16:22:52 +02:00
return value.map((x) => x.tuple)
2025-12-09 13:21:46 +02:00
}
} else {
2025-12-09 13:21:46 +02:00
if (!preserveTuples) {
return value
} else {
return value.tuple
}
}
}
2025-12-09 13:21:46 +02:00
// for most stuff we want maps since those are more convenient
// however this doesn't allow for multiple values per same key
// so for those cases we want to preserve tuples as-is
// right now it's made exclusively for :pleroma.:rate_limit
// so it might not work properly elsewhere
2026-01-06 16:22:52 +02:00
const preserveTuples = path.find((x) => x === ':rate_limit')
2025-12-09 13:21:46 +02:00
set(config, path, convert(c.value, false, preserveTuples))
})
2025-12-08 13:50:08 +02:00
// patching http adapter config to be easier to handle
const adapter = config[':pleroma'][':http'][':adapter']
if (Array.isArray(adapter)) {
config[':pleroma'][':http'][':adapter'] = {
[':ssl_options']: {
2026-01-06 16:22:52 +02:00
[':versions']: [],
},
2025-12-08 13:50:08 +02:00
}
}
commit('updateAdminSettings', { config, modifiedPaths })
commit('resetAdminDraft')
},
2026-01-06 16:22:52 +02:00
setInstanceAdminDescriptions({ commit }, { backendDescriptions }) {
const convert = (
{ children, description, label, key = '<ROOT>', group, suggestions },
path,
acc,
) => {
const newPath = group ? [group, key] : [key]
2023-03-19 21:27:07 +02:00
const obj = { description, label, suggestions }
if (Array.isArray(children)) {
2026-01-06 16:22:52 +02:00
children.forEach((c) => {
convert(c, newPath, obj)
2023-03-19 21:27:07 +02:00
})
}
set(acc, newPath, obj)
}
const descriptions = {}
2025-12-08 17:09:07 +02:00
2026-01-06 16:22:52 +02:00
backendDescriptions.forEach((d) => convert(d, '', descriptions))
2023-03-19 21:27:07 +02:00
commit('updateAdminDescriptions', { descriptions })
},
// This action takes draft state, diffs it with live config state and then pushes
// only differences between the two. Difference detection only work up to subkey (third) level.
2026-01-06 16:22:52 +02:00
pushAdminDraft({ rootState, state, dispatch }) {
// 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] }))
}
}
// Getting all group-keys used in config
const allGroupKeys = flatten(
2026-01-06 16:22:52 +02:00
Object.entries(state.config).map(([group, lv1data]) =>
Object.keys(lv1data).map((key) => ({ group, key })),
),
)
// Only using group-keys where there are changes detected
const changedGroupKeys = allGroupKeys.filter(({ group, key }) => {
return !isEqual(state.config[group][key], state.draft[group][key])
})
// Here we take all changed group-keys and get all changed subkeys
const changed = changedGroupKeys.map(({ group, key }) => {
const config = state.config[group][key]
const draft = state.draft[group][key]
// We convert group-key value into entries arrays
const eConfig = Object.entries(config)
const eDraft = Object.entries(draft)
// Then those entries array we diff so only changed subkey entries remain
// We use the diffed array to reconstruct the object and then shove it into convert()
2026-01-06 16:22:52 +02:00
return {
group,
key,
value: convert(
Object.fromEntries(differenceWith(eDraft, eConfig, isEqual)),
),
}
})
2026-01-06 16:22:52 +02:00
rootState.api.backendInteractor
.pushInstanceDBConfig({
payload: {
configs: changed,
},
})
.then(() => rootState.api.backendInteractor.fetchInstanceDBConfig())
2026-01-06 16:22:52 +02:00
.then((backendDbConfig) =>
dispatch('setInstanceAdminSettings', { backendDbConfig }),
)
},
2026-01-06 16:22:52 +02:00
pushAdminSetting({ rootState, dispatch }, { path, value }) {
const [group, key, ...rest] = Array.isArray(path)
? path
: path.split(/\./g)
const clone = {} // not actually cloning the entire thing to avoid excessive writes
set(clone, rest, value)
// 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] }))
}
}
2026-01-06 16:22:52 +02:00
rootState.api.backendInteractor
.pushInstanceDBConfig({
payload: {
configs: [
{
group,
key,
value: convert(clone),
},
],
},
})
.then(() => rootState.api.backendInteractor.fetchInstanceDBConfig())
2026-01-06 16:22:52 +02:00
.then((backendDbConfig) =>
dispatch('setInstanceAdminSettings', { backendDbConfig }),
)
},
2026-01-06 16:22:52 +02:00
resetAdminSetting({ rootState, state, dispatch }, { path }) {
const [group, key, subkey] = Array.isArray(path)
? path
: path.split(/\./g)
state.modifiedPaths.delete(path)
2026-01-06 16:22:52 +02:00
return rootState.api.backendInteractor
.pushInstanceDBConfig({
payload: {
configs: [
{
group,
key,
delete: true,
subkeys: [subkey],
},
],
},
})
.then(() => rootState.api.backendInteractor.fetchInstanceDBConfig())
2026-01-06 16:22:52 +02:00
.then((backendDbConfig) =>
dispatch('setInstanceAdminSettings', { backendDbConfig }),
)
},
},
}
2023-03-14 21:50:43 +02:00
export default adminSettingsStorage