diff --git a/src/modules/config_declaration.js b/src/modules/config_declaration.js new file mode 100644 index 000000000..e3328d448 --- /dev/null +++ b/src/modules/config_declaration.js @@ -0,0 +1,42 @@ +export const CONFIG_MIGRATION = 1 +import { v4 as uuidv4 } from 'uuid'; + +// for future use +/* +const simpleDeclaration = { + store: 'server-side', + migrationFlag: 'configMigration', + migration(serverside, rootState) { + serverside.setPreference({ path: 'simple.' + field, value: rootState.config[oldField ?? field] }) + } +} +*/ + +export const declarations = [ + { + field: 'muteFilters', + store: 'server-side', + migrationFlag: 'configMigration', + migrationNum: 1, + description: 'Mute filters, wordfilter/regexp/etc', + valueType: 'complex', + migration (serverside, rootState) { + rootState.config.muteWords.forEach((word, order) => { + const uniqueId = uuidv4() + + serverside.setPreference({ + path: 'simple.muteFilters.' + uniqueId, + value: { + type: 'word', + value: word, + name: word, + enabled: true, + expires: null, + hide: false, + order + } + }) + }) + } + } +] diff --git a/src/modules/users.js b/src/modules/users.js index d9dae91ea..d5ef6cdf5 100644 --- a/src/modules/users.js +++ b/src/modules/users.js @@ -1,5 +1,4 @@ import { compact, map, each, mergeWith, last, concat, uniq, isArray } from 'lodash' -import { v4 as uuidv4 } from 'uuid'; import backendInteractorService from '../services/backend_interactor_service/backend_interactor_service.js' import { windowWidth, windowHeight } from '../services/window_utils/window_utils' @@ -9,7 +8,9 @@ import { registerPushNotifications, unregisterPushNotifications } from '../servi import { useInterfaceStore } from 'src/stores/interface.js' import { useOAuthStore } from 'src/stores/oauth.js' -import { useServerSideStorageStore, CONFIG_MIGRATION } from 'src/stores/serverSideStorage' +import { useServerSideStorageStore } from 'src/stores/serverSideStorage' + +import { declarations } from 'src/modules/config_declaration' // TODO: Unify with mergeOrAdd in statuses.js export const mergeOrAdd = (arr, obj, item) => { @@ -637,33 +638,17 @@ const users = { /**/ const { configMigration } = useServerSideStorageStore().flagStorage - - // Wordfilter migration - if (configMigration < 1) { - // Convert existing wordfilter into synced one - store.rootState.config.muteWords.forEach((word, order) => { - const uniqueId = uuidv4() - - useServerSideStorageStore().setPreference({ - path: 'simple.muteFilters.' + uniqueId, - value: { - type: 'word', - value: word, - name: word, - enabled: true, - expires: null, - hide: false, - order - } - }) + declarations + .filter(x => { + return x.store === 'server-side' && + (x.migrationNum ?? x.migrationNum > configMigration) + }) + .toSorted((a, b) => a.configMigration - b.configMigration) + .forEach(value => { + value.migration(useServerSideStorageStore(), store.rootState) + useServerSideStorageStore().setFlag({ flag: 'configMigration', value: value.migrationNum }) + useServerSideStorageStore().pushServerSideStorage() }) - } - - if (configMigration < CONFIG_MIGRATION) { - // Update the flag - useServerSideStorageStore().setFlag({ flag: 'configMigration', value: CONFIG_MIGRATION }) - useServerSideStorageStore().pushServerSideStorage() - } if (user.token) { dispatch('setWsToken', user.token) diff --git a/src/stores/serverSideStorage.js b/src/stores/serverSideStorage.js index b7c192f0c..9cfdac1a8 100644 --- a/src/stores/serverSideStorage.js +++ b/src/stores/serverSideStorage.js @@ -17,7 +17,6 @@ import { import { CURRENT_UPDATE_COUNTER } from 'src/components/update_notification/update_notification.js' export const VERSION = 1 -export const CONFIG_MIGRATION = 1 export const NEW_USER_DATE = new Date('2022-08-04') // date of writing this, basically export const COMMAND_TRIM_FLAGS = 1000