diff --git a/src/boot/after_store.js b/src/boot/after_store.js index d16852bf8..a345e2ae5 100644 --- a/src/boot/after_store.js +++ b/src/boot/after_store.js @@ -42,7 +42,6 @@ import VBodyScrollLock from 'src/directives/body_scroll_lock' import { INSTANCE_DEFAULT_CONFIG_DEFINITIONS, INSTANCE_IDENTITY_DEFAULT_DEFINITIONS, - INSTANCE_IDENTIY_EXTERNAL, } from 'src/modules/default_config_state.js' let staticInitialResults = null @@ -171,14 +170,12 @@ const setSettings = async ({ apiConfig, staticConfig, store }) => { config = Object.assign({}, staticConfig, apiConfig) } - Object.keys(INSTANCE_IDENTITY_DEFAULT_DEFINITIONS).forEach((source) => { - if (source === 'name') return - if (INSTANCE_IDENTIY_EXTERNAL.has(source)) return + Object.keys(INSTANCE_IDENTITY_DEFAULT_DEFINITIONS).forEach((source) => useInstanceStore().set({ value: config[source], path: `instanceIdentity.${source}`, - }) - }) + }), + ) Object.keys(INSTANCE_DEFAULT_CONFIG_DEFINITIONS).forEach((source) => useInstanceStore().set({ @@ -280,7 +277,7 @@ const getNodeInfo = async ({ store }) => { const metadata = data.metadata const features = metadata.features useInstanceStore().set({ - path: 'instanceIdentity.name', + path: 'name', value: metadata.nodeName, }) useInstanceStore().set({ @@ -530,7 +527,6 @@ const afterStoreSetup = async ({ pinia, store, storageError, i18n }) => { useInterfaceStore().setLayoutWidth(windowWidth()) useInterfaceStore().setLayoutHeight(windowHeight()) - window.syncConfig = useSyncConfigStore() window.mergedConfig = useMergedConfigStore() window.localConfig = useLocalConfigStore() diff --git a/src/components/settings_modal/settings_modal.js b/src/components/settings_modal/settings_modal.js index 5c157e068..4ca2857b2 100644 --- a/src/components/settings_modal/settings_modal.js +++ b/src/components/settings_modal/settings_modal.js @@ -13,12 +13,7 @@ import { useLocalConfigStore } from 'src/stores/local_config.js' import { useMergedConfigStore } from 'src/stores/merged_config.js' import { useSyncConfigStore } from 'src/stores/sync_config.js' -import { - LOCAL_ONLY_KEYS, - ROOT_CONFIG, - ROOT_CONFIG_DEFINITIONS, - validateSetting, -} from 'src/modules/default_config_state.js' +import { LOCAL_ONLY_KEYS } from 'src/modules/default_config_state.js' import { newExporter, newImporter, @@ -143,52 +138,37 @@ const SettingsModal = { }) } }, - onImport(input) { - if (!input) return - const { _pleroma_settings_version, ...data } = input - - Object.entries(data).forEach(([path, value]) => { - const definition = ROOT_CONFIG_DEFINITIONS[path] - - const finalValue = validateSetting({ - path, - value, - definition, - throwError: false, - defaultState: ROOT_CONFIG, - }) - - if (finalValue === undefined) return - - if (LOCAL_ONLY_KEYS.has(path)) { - useLocalConfigStore().set({ path, value: finalValue }) - } else { - if (path.startsWith('muteFilters')) { - Object.keys( - useMergedConfigStore().mergedConfig.muteFilters, - ).forEach((key) => { - useSyncConfigStore().unsetPreference({ - path: `simple.${path}.${key}`, - }) - }) - - Object.entries(value).forEach(([key, filter]) => { - useSyncConfigStore().setPreference({ - path: `simple.${path}.${key}`, - value: filter, - }) - }) + onImport(data) { + if (data) { + Object.entries(data).forEach(([path, value]) => { + if (LOCAL_ONLY_KEYS.has(path)) { + useLocalConfigStore().set({ path, value }) } else { - if (finalValue !== undefined) { + if (path.startsWith('muteFilters')) { + Object.keys( + useMergedConfigStore().mergedConfig.muteFilters, + ).forEach((key) => { + useSyncConfigStore().unsetPreference({ + path: `simple.${path}.${key}`, + }) + }) + + Object.entries(value).forEach(([key, filter]) => { + useSyncConfigStore().setPreference({ + path: `simple.${path}.${key}`, + value: filter, + }) + }) + } else { useSyncConfigStore().setPreference({ path: `simple.${path}`, - value: finalValue, + value, }) } } - } - }) - useSyncConfigStore().pushSyncConfig() + }) + useSyncConfigStore().pushSyncConfig() + } }, restore() { this.dataImporter.importData() @@ -204,17 +184,10 @@ const SettingsModal = { let sample = config if (!theme) { const ignoreList = new Set([ - 'theme', 'customTheme', 'customThemeSource', 'colors', - 'style', - 'styleCustomData', - 'palette', - 'paletteCustomData', - 'themeChecksum', ]) - sample = Object.fromEntries( Object.entries(sample).filter( ([key, value]) => !ignoreList.has(key) && value !== undefined, diff --git a/src/modules/default_config_state.js b/src/modules/default_config_state.js index ff43e4dee..b5958adb6 100644 --- a/src/modules/default_config_state.js +++ b/src/modules/default_config_state.js @@ -112,19 +112,10 @@ export const INSTANCE_IDENTITY_DEFAULT_DEFINITIONS = { type: 'string', required: false, }, - name: { - description: 'Instance Name', - type: 'string', - required: false, - }, } export const INSTANCE_IDENTITY_DEFAULT = convertDefinitions( INSTANCE_IDENTITY_DEFAULT_DEFINITIONS, ) -export const INSTANCE_IDENTIY_EXTERNAL = new Set([ - 'tos', - 'instanceSpecificPanelContent', -]) /// This object contains setting entries that makes sense /// at the user level. The defaults can also be overriden by @@ -531,18 +522,6 @@ export const INSTANCE_DEFAULT_CONFIG_DEFINITIONS = { type: 'string', required: false, }, - theme3hacks: { - description: 'Theme 3 hacks (need separation)', - type: 'object', - required: false, - default: {}, - }, - highlights: { - description: 'User highlights', - type: 'object', - required: false, - default: {}, - }, } export const INSTANCE_DEFAULT_CONFIG = convertDefinitions( INSTANCE_DEFAULT_CONFIG_DEFINITIONS, @@ -684,11 +663,6 @@ export const SYNC_DEFAULT_CONFIG_DEFINITIONS = { description: 'Collapse navigation panel to header only', default: false, }, - muteFilters: { - description: 'Object containing mute filters', - type: 'object', - default: {}, - } } export const SYNC_DEFAULT_CONFIG = convertDefinitions( SYNC_DEFAULT_CONFIG_DEFINITIONS, @@ -757,27 +731,14 @@ export const ROOT_CONFIG_DEFINITIONS = { export const validateSetting = ({ value, - path: fullPath, + path, definition, throwError, defaultState, - validateObjects = true, }) => { - const path = fullPath.replace(/^simple./, '') - if (validateObjects && definition.type === 'object' && path.split('.').length <= 1) { - console.error(`attempt to set object ${fullPath} instead of its children. ignoring.`) - return undefined - } - - if (path.includes('muteFilters')) { - console.log('##', path, value, definition) - console.log(value) - console.log(path) - console.log('====') - } - if (value === undefined) return undefined // only null is allowed as missing value - if (get(defaultState, path.split('.')[0]) === undefined) { - const string = `Unknown option ${fullPath}, value: ${value}` + if (value === undefined) return // only null is allowed as missing value + if (get(defaultState, path) === undefined) { + const string = `Unknown instance option ${path}, value: ${value}` if (throwError) { throw new Error(string) diff --git a/src/stores/instance.js b/src/stores/instance.js index 54b3cf43c..2923f3ad8 100644 --- a/src/stores/instance.js +++ b/src/stores/instance.js @@ -201,7 +201,6 @@ export const useInstanceStore = defineStore('instance', { definition, throwError: true, defaultState: DEFAULT_STATE, - validateObjects: false, }) set(this, path, finalValue) diff --git a/src/stores/merged_config.js b/src/stores/merged_config.js index 334db6c4f..c4f27082c 100644 --- a/src/stores/merged_config.js +++ b/src/stores/merged_config.js @@ -5,10 +5,18 @@ import { useLocalConfigStore } from 'src/stores/local_config.js' import { useSyncConfigStore } from 'src/stores/sync_config.js' import { + INSTANCE_DEFAULT_CONFIG, + LOCAL_DEFAULT_CONFIG, LOCAL_ONLY_KEYS, - ROOT_CONFIG, + THEME_CONFIG, } from 'src/modules/default_config_state.js' +const ROOT_CONFIG = { + ...INSTANCE_DEFAULT_CONFIG, + ...LOCAL_DEFAULT_CONFIG, + ...THEME_CONFIG, +} + export const useMergedConfigStore = defineStore('merged_config', { getters: { mergedConfig: () => { @@ -42,15 +50,16 @@ export const useMergedConfigStore = defineStore('merged_config', { return result }, mergedConfigWithoutDefaults: () => { + const instancePrefs = useInstanceStore().prefsStorage const tempPrefs = useLocalConfigStore().tempStorage const localPrefs = useLocalConfigStore().prefsStorage const syncPrefs = useSyncConfigStore().prefsStorage const getValue = (k) => - tempPrefs[k] ?? localPrefs[k] ?? syncPrefs.simple[k] + tempPrefs[k] ?? localPrefs[k] ?? syncPrefs.simple[k] ?? instancePrefs[k] const result = Object.fromEntries( - Object.keys(ROOT_CONFIG).map((k) => [k, getValue(k)]), + Object.keys(ROOT_CONFIG).map(([k, value]) => [k, getValue(k)]), ) return result }, diff --git a/src/stores/sync_config.js b/src/stores/sync_config.js index bad0a1515..7a5b1f4e1 100644 --- a/src/stores/sync_config.js +++ b/src/stores/sync_config.js @@ -295,14 +295,12 @@ export const _mergePrefs = (recent, stale) => { const entry = path.split('.')[1] if (operation === 'unset') return ROOT_CONFIG[entry] !== undefined - if (operation !== 'set') return true - const definition = path.startsWith('simple.muteFilters') ? { default: {} } : ROOT_CONFIG_DEFINITIONS[entry] const finalValue = validateSetting({ - path, + path: entry, value: args[0], definition, throwError: false, @@ -509,14 +507,12 @@ export const useSyncConfigStore = defineStore('sync_config', { ) } - if (path.startsWith('collections.')) return value - const definition = path.startsWith('simple.muteFilters') ? { default: {} } : ROOT_CONFIG_DEFINITIONS[path.split('.')[1]] const finalValue = validateSetting({ - path, + path: path.split('.')[1], value, definition, throwError: false, @@ -777,19 +773,21 @@ export const useSyncConfigStore = defineStore('sync_config', { afterLoad(state) { console.debug('Validating persisted state of SyncConfig') const newState = { ...state } - const newEntries = Object.entries(ROOT_CONFIG).map( + const newEntries = Object.entries(newState.prefsStorage.simple).map( ([path, value]) => { + if (path === 'muteFilters') { + return value + } const definition = ROOT_CONFIG_DEFINITIONS[path] const finalValue = validateSetting({ path, - value: newState.prefsStorage.simple[path], + value, definition, throwError: false, - validateObjects: false, defaultState: ROOT_CONFIG, }) - return finalValue === undefined ? definition.default : [path, finalValue] + return finalValue === undefined ? undefined : [path, finalValue] }, ) newState.prefsStorage.simple = Object.fromEntries( diff --git a/test/unit/specs/stores/sync_config.spec.js b/test/unit/specs/stores/sync_config.spec.js index 83324f62b..9a582a193 100644 --- a/test/unit/specs/stores/sync_config.spec.js +++ b/test/unit/specs/stores/sync_config.spec.js @@ -241,9 +241,7 @@ describe('The SyncConfig store', () => { store.setPreference({ path: 'simple.fontInput.family', value: 'test' }) store.unsetPreference({ path: 'simple.fontInput.family' }) store.updateCache(store, { username: 'test' }) - expect(store.prefsStorage.simple.fontInput).to.not.have.property( - 'family', - ) + expect(store.prefsStorage.simple.fontInput).to.not.have.property('family') expect(store.prefsStorage._journal.length).to.eql(1) }) @@ -397,12 +395,7 @@ describe('The SyncConfig store', () => { { simple: { theme: '1', style: '0', hideISP: true }, _journal: [ - { - path: 'simple.style', - operation: 'set', - args: ['0'], - timestamp: 2, - }, + { path: 'simple.style', operation: 'set', args: ['0'], timestamp: 2 }, { path: 'simple.hideISP', operation: 'set', @@ -415,42 +408,17 @@ describe('The SyncConfig store', () => { { simple: { theme: '1', style: '1', hideISP: false }, _journal: [ - { - path: 'simple.theme', - operation: 'set', - args: ['1'], - timestamp: 1, - }, - { - path: 'simple.style', - operation: 'set', - args: ['1'], - timestamp: 3, - }, + { path: 'simple.theme', operation: 'set', args: ['1'], timestamp: 1 }, + { path: 'simple.style', operation: 'set', args: ['1'], timestamp: 3 }, ], }, ), ).to.eql({ simple: { theme: '1', style: '1', hideISP: true }, _journal: [ - { - path: 'simple.theme', - operation: 'set', - args: ['1'], - timestamp: 1, - }, - { - path: 'simple.style', - operation: 'set', - args: ['1'], - timestamp: 3, - }, - { - path: 'simple.hideISP', - operation: 'set', - args: [true], - timestamp: 4, - }, + { path: 'simple.theme', operation: 'set', args: ['1'], timestamp: 1 }, + { path: 'simple.style', operation: 'set', args: ['1'], timestamp: 3 }, + { path: 'simple.hideISP', operation: 'set', args: [true], timestamp: 4 }, ], }) }) @@ -462,12 +430,7 @@ describe('The SyncConfig store', () => { { simple: { theme: '1', style: '0', hideISP: false }, _journal: [ - { - path: 'simple.style', - operation: 'set', - args: ['0'], - timestamp: 2, - }, + { path: 'simple.style', operation: 'set', args: ['0'], timestamp: 2 }, { path: 'simple.hideISP', operation: 'set', @@ -480,42 +443,18 @@ describe('The SyncConfig store', () => { { simple: { theme: '0', style: '0', hideISP: true }, _journal: [ - { - path: 'simple.theme', - operation: 'set', - args: ['0'], - timestamp: 1, - }, - { - path: 'simple.style', - operation: 'set', - args: ['0'], - timestamp: 3, - }, + { path: 'simple.theme', operation: 'set', args: ['0'], timestamp: 1 }, + { path: 'simple.style', operation: 'set', args: ['0'], timestamp: 3 }, ], }, ), ).to.eql({ + simple: { a: 0, b: 0, c: false }, simple: { theme: '0', style: '0', hideISP: false }, _journal: [ - { - path: 'simple.theme', - operation: 'set', - args: ['0'], - timestamp: 1, - }, - { - path: 'simple.style', - operation: 'set', - args: ['0'], - timestamp: 3, - }, - { - path: 'simple.hideISP', - operation: 'set', - args: [false], - timestamp: 4, - }, + { path: 'simple.theme', operation: 'set', args: ['0'], timestamp: 1 }, + { path: 'simple.style', operation: 'set', args: ['0'], timestamp: 3 }, + { path: 'simple.hideISP', operation: 'set', args: [false], timestamp: 4 }, ], }) }) @@ -551,12 +490,7 @@ describe('The SyncConfig store', () => { ).to.eql({ simple: { theme: 'bar' }, _journal: [ - { - path: 'simple.theme', - operation: 'set', - args: ['bar'], - timestamp: 4, - }, + { path: 'simple.theme', operation: 'set', args: ['bar'], timestamp: 4 }, ], }) })