diff --git a/lib/utils/keyserver-store-utils.js b/lib/utils/keyserver-store-utils.js new file mode 100644 --- /dev/null +++ b/lib/utils/keyserver-store-utils.js @@ -0,0 +1,20 @@ +// @flow + +import type { KeyserverStore } from '../types/keyserver-types.js'; + +function removeCookiesFromKeyserverStore( + oldStore: KeyserverStore, +): KeyserverStore { + const keyserverInfos = { + ...oldStore.keyserverInfos, + }; + for (const key in keyserverInfos) { + keyserverInfos[key] = { ...keyserverInfos[key], cookie: null }; + } + return { + ...oldStore, + keyserverInfos, + }; +} + +export { removeCookiesFromKeyserverStore }; diff --git a/native/redux/client-db-utils.js b/native/redux/client-db-utils.js --- a/native/redux/client-db-utils.js +++ b/native/redux/client-db-utils.js @@ -13,6 +13,7 @@ LegacyRawThreadInfo, MixedRawThreadInfos, } from 'lib/types/thread-types.js'; +import { removeCookiesFromKeyserverStore } from 'lib/utils/keyserver-store-utils.js'; import { translateClientDBMessageInfoToRawMessageInfo, translateRawMessageInfoToClientDBMessageInfo, @@ -47,14 +48,9 @@ commCoreModule.processThreadStoreOperationsSync(operations); } catch (exception) { console.log(exception); - const keyserverInfos = { ...state.keyserverStore.keyserverInfos }; - for (const key in keyserverInfos) { - keyserverInfos[key] = { ...keyserverInfos[key], cookie: null }; - } - const keyserverStore = { ...state.keyserverStore, keyserverInfos }; return { ...state, - keyserverStore, + keyserverStore: removeCookiesFromKeyserverStore(state.keyserverStore), }; } diff --git a/native/redux/persist.js b/native/redux/persist.js --- a/native/redux/persist.js +++ b/native/redux/persist.js @@ -73,6 +73,7 @@ LegacyRawThreadInfo, MixedRawThreadInfos, } from 'lib/types/thread-types.js'; +import { removeCookiesFromKeyserverStore } from 'lib/utils/keyserver-store-utils.js'; import { translateClientDBMessageInfoToRawMessageInfo, translateRawMessageInfoToClientDBMessageInfo, @@ -83,6 +84,7 @@ convertThreadStoreThreadInfosToNewIDSchema, } from 'lib/utils/migration-utils.js'; import { defaultNotifPermissionAlertInfo } from 'lib/utils/push-alerts.js'; +import { resetUserSpecificStateOnIdentityActions } from 'lib/utils/reducers-utils.js'; import { deprecatedConvertClientDBThreadInfoToRawThreadInfo, convertRawThreadInfoToClientDBThreadInfo, @@ -108,6 +110,28 @@ import { isTaskCancelledError } from '../utils/error-handling.js'; import { defaultURLPrefix } from '../utils/url-utils.js'; +function handleReduxMigrationFailure(oldState: AppState): AppState { + const stateAfterReset = resetUserSpecificStateOnIdentityActions( + oldState, + defaultState, + [ + 'storeLoaded', + 'customServer', + 'nextLocalID', + 'deviceCameraInfo', + 'frozen', + 'keyserverStore', + '_persist', + ], + ); + return { + ...stateAfterReset, + keyserverStore: removeCookiesFromKeyserverStore( + stateAfterReset.keyserverStore, + ), + }; +} + const migrations = { [1]: (state: AppState) => ({ ...state, @@ -829,7 +853,7 @@ if (isTaskCancelledError(exception)) { return state; } - return { ...state, cookie: null }; + return handleReduxMigrationFailure(state); } const { inconsistencyReports, ...newUserStore } = state.userStore; @@ -934,7 +958,7 @@ if (isTaskCancelledError(exception)) { return state; } - return { ...state, cookie: null }; + return handleReduxMigrationFailure(state); } return state; }, @@ -977,7 +1001,7 @@ commCoreModule.processThreadStoreOperationsSync(operations); } catch (exception) { console.log(exception); - return { ...state, cookie: null }; + return handleReduxMigrationFailure(state); } return state; diff --git a/web/redux/persist.js b/web/redux/persist.js --- a/web/redux/persist.js +++ b/web/redux/persist.js @@ -16,13 +16,16 @@ import { defaultGlobalThemeInfo } from 'lib/types/theme-types.js'; import { parseCookies } from 'lib/utils/cookie-utils.js'; import { isDev } from 'lib/utils/dev-utils.js'; +import { removeCookiesFromKeyserverStore } from 'lib/utils/keyserver-store-utils.js'; import { generateIDSchemaMigrationOpsForDrafts, convertDraftStoreToNewIDSchema, } from 'lib/utils/migration-utils.js'; +import { resetUserSpecificStateOnIdentityActions } from 'lib/utils/reducers-utils.js'; import { ashoatKeyserverID } from 'lib/utils/validation-utils.js'; import commReduxStorageEngine from './comm-redux-storage-engine.js'; +import { defaultWebState } from './default-state.js'; import type { AppState } from './redux-setup.js'; import { getDatabaseModule } from '../database/database-module-provider.js'; import { isSQLiteSupported } from '../database/utils/db-utils.js'; @@ -30,6 +33,21 @@ declare var keyserverURL: string; +// eslint-disable-next-line no-unused-vars +function handleReduxMigrationFailure(oldState: AppState): AppState { + const stateAfterReset = resetUserSpecificStateOnIdentityActions( + oldState, + defaultWebState, + ['keyserverStore', '_persist', 'customServer'], + ); + return { + ...stateAfterReset, + keyserverStore: removeCookiesFromKeyserverStore( + stateAfterReset.keyserverStore, + ), + }; +} + const migrations = { [1]: async (state: any) => { const {