Changeset View
Changeset View
Standalone View
Standalone View
web/redux/persist.js
// @flow | // @flow | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
import { getStoredState, purgeStoredState } from 'redux-persist'; | import { getStoredState, purgeStoredState } from 'redux-persist'; | ||||
import storage from 'redux-persist/es/storage/index.js'; | import storage from 'redux-persist/es/storage/index.js'; | ||||
import type { PersistConfig } from 'redux-persist/src/types.js'; | import type { PersistConfig } from 'redux-persist/src/types.js'; | ||||
import { | import { | ||||
communityStoreOpsHandlers, | |||||
type ClientDBCommunityStoreOperation, | |||||
type ReplaceCommunityOperation, | |||||
} from 'lib/ops/community-store-ops.js'; | |||||
import { | |||||
type ClientDBKeyserverStoreOperation, | type ClientDBKeyserverStoreOperation, | ||||
keyserverStoreOpsHandlers, | keyserverStoreOpsHandlers, | ||||
type ReplaceKeyserverOperation, | type ReplaceKeyserverOperation, | ||||
} from 'lib/ops/keyserver-store-ops.js'; | } from 'lib/ops/keyserver-store-ops.js'; | ||||
import { | import { | ||||
createAsyncMigrate, | createAsyncMigrate, | ||||
type StorageMigrationFunction, | type StorageMigrationFunction, | ||||
} from 'lib/shared/create-async-migrate.js'; | } from 'lib/shared/create-async-migrate.js'; | ||||
import { keyserverStoreTransform } from 'lib/shared/transforms/keyserver-store-transform.js'; | import { keyserverStoreTransform } from 'lib/shared/transforms/keyserver-store-transform.js'; | ||||
import type { CommunityInfo } from 'lib/types/community-types.js'; | |||||
import { defaultEnabledApps } from 'lib/types/enabled-apps.js'; | |||||
import type { KeyserverInfo } from 'lib/types/keyserver-types.js'; | import type { KeyserverInfo } from 'lib/types/keyserver-types.js'; | ||||
import { cookieTypes } from 'lib/types/session-types.js'; | import { cookieTypes } from 'lib/types/session-types.js'; | ||||
import { defaultConnectionInfo } from 'lib/types/socket-types.js'; | import { defaultConnectionInfo } from 'lib/types/socket-types.js'; | ||||
import { defaultGlobalThemeInfo } from 'lib/types/theme-types.js'; | import { defaultGlobalThemeInfo } from 'lib/types/theme-types.js'; | ||||
import { threadTypeIsCommunityRoot } from 'lib/types/thread-types-enum.js'; | |||||
import { parseCookies } from 'lib/utils/cookie-utils.js'; | import { parseCookies } from 'lib/utils/cookie-utils.js'; | ||||
import { isDev } from 'lib/utils/dev-utils.js'; | import { isDev } from 'lib/utils/dev-utils.js'; | ||||
import { wipeKeyserverStore } from 'lib/utils/keyserver-store-utils.js'; | import { wipeKeyserverStore } from 'lib/utils/keyserver-store-utils.js'; | ||||
import { | import { | ||||
generateIDSchemaMigrationOpsForDrafts, | generateIDSchemaMigrationOpsForDrafts, | ||||
convertDraftStoreToNewIDSchema, | convertDraftStoreToNewIDSchema, | ||||
} from 'lib/utils/migration-utils.js'; | } from 'lib/utils/migration-utils.js'; | ||||
import { entries } from 'lib/utils/objects.js'; | import { entries } from 'lib/utils/objects.js'; | ||||
Show All 13 Lines | |||||
const persistWhitelist = [ | const persistWhitelist = [ | ||||
'enabledApps', | 'enabledApps', | ||||
'cryptoStore', | 'cryptoStore', | ||||
'notifPermissionAlertInfo', | 'notifPermissionAlertInfo', | ||||
'commServicesAccessToken', | 'commServicesAccessToken', | ||||
'keyserverStore', | 'keyserverStore', | ||||
'globalThemeInfo', | 'globalThemeInfo', | ||||
'customServer', | 'customServer', | ||||
'communityStore', | |||||
kamil: we don't need that - `communityStore` should only be on SQLite, this whitelist is for `redux… | |||||
]; | ]; | ||||
function handleReduxMigrationFailure(oldState: AppState): AppState { | function handleReduxMigrationFailure(oldState: AppState): AppState { | ||||
const persistedNonUserSpecificFields = nonUserSpecificFieldsWeb.filter( | const persistedNonUserSpecificFields = nonUserSpecificFieldsWeb.filter( | ||||
field => persistWhitelist.includes(field) || field === '_persist', | field => persistWhitelist.includes(field) || field === '_persist', | ||||
); | ); | ||||
const stateAfterReset = resetUserSpecificState( | const stateAfterReset = resetUserSpecificState( | ||||
oldState, | oldState, | ||||
▲ Show 20 Lines • Show All 197 Lines • ▼ Show 20 Lines | try { | ||||
storeOperations: { keyserverStoreOperations }, | storeOperations: { keyserverStoreOperations }, | ||||
}); | }); | ||||
return state; | return state; | ||||
} catch (e) { | } catch (e) { | ||||
console.log(e); | console.log(e); | ||||
return handleReduxMigrationFailure(state); | return handleReduxMigrationFailure(state); | ||||
} | } | ||||
}, | }, | ||||
[12]: async (state: AppState) => { | |||||
kamilUnsubmitted Not Done Inline ActionsThis code looks okay but it's not enough, in D10826 there is some context on what the state of threads persists on the web. In short: kamil: This code looks okay but it's not enough, in D10826 there is some context on what the state of… | |||||
const databaseModule = await getDatabaseModule(); | |||||
const isDatabaseSupported = await databaseModule.isDatabaseSupported(); | |||||
if (!isDatabaseSupported) { | |||||
return state; | |||||
} | |||||
const clientDBStores = await databaseModule.schedule({ | |||||
type: workerRequestMessageTypes.GET_CLIENT_STORE, | |||||
}); | |||||
invariant(clientDBStores?.store, 'Stores should exist'); | |||||
const threadInfos = clientDBStores.store.threads; | |||||
const replaceOperations: $ReadOnlyArray<ReplaceCommunityOperation> = | |||||
threadInfos | |||||
.filter(threadInfo => threadTypeIsCommunityRoot(threadInfo.type)) | |||||
.map(threadInfo => { | |||||
const communityInfo: CommunityInfo = { | |||||
enabledApps: defaultEnabledApps, | |||||
}; | |||||
return { | |||||
type: 'replace_community', | |||||
payload: { | |||||
id: threadInfo.id, | |||||
communityInfo, | |||||
}, | |||||
}; | |||||
}); | |||||
const commmunityStoreOperations: $ReadOnlyArray<ClientDBCommunityStoreOperation> = | |||||
communityStoreOpsHandlers.convertOpsToClientDBOps([ | |||||
{ type: 'remove_all_communities' }, | |||||
...replaceOperations, | |||||
]); | |||||
try { | |||||
await databaseModule.schedule({ | |||||
type: workerRequestMessageTypes.PROCESS_STORE_OPERATIONS, | |||||
storeOperations: { | |||||
communityStoreOperations: commmunityStoreOperations, | |||||
}, | |||||
}); | |||||
return state; | |||||
} catch (e) { | |||||
console.log(e); | |||||
return handleReduxMigrationFailure(state); | |||||
} | |||||
}, | |||||
}; | }; | ||||
const rootKey = 'root'; | const rootKey = 'root'; | ||||
const migrateStorageToSQLite: StorageMigrationFunction = async debug => { | const migrateStorageToSQLite: StorageMigrationFunction = async debug => { | ||||
const databaseModule = await getDatabaseModule(); | const databaseModule = await getDatabaseModule(); | ||||
const isSupported = await databaseModule.isDatabaseSupported(); | const isSupported = await databaseModule.isDatabaseSupported(); | ||||
if (!isSupported) { | if (!isSupported) { | ||||
Show All 30 Lines | const persistConfig: PersistConfig = { | ||||
whitelist: isSQLiteSupported() | whitelist: isSQLiteSupported() | ||||
? persistWhitelist | ? persistWhitelist | ||||
: [...persistWhitelist, 'draftStore'], | : [...persistWhitelist, 'draftStore'], | ||||
migrate: (createAsyncMigrate( | migrate: (createAsyncMigrate( | ||||
migrations, | migrations, | ||||
{ debug: isDev }, | { debug: isDev }, | ||||
migrateStorageToSQLite, | migrateStorageToSQLite, | ||||
): any), | ): any), | ||||
version: 11, | version: 12, | ||||
transforms: [keyserverStoreTransform], | transforms: [keyserverStoreTransform], | ||||
}; | }; | ||||
export { persistConfig }; | export { persistConfig }; |
we don't need that - communityStore should only be on SQLite, this whitelist is for redux-persist
on the other hand, we should add communityStore to persistBlacklist on native