diff --git a/lib/utils/migration-utils.js b/lib/utils/migration-utils.js --- a/lib/utils/migration-utils.js +++ b/lib/utils/migration-utils.js @@ -1,7 +1,9 @@ // @flow +import type { TranslatedThreadMessageInfos } from './message-ops-utils.js'; import { entries } from './objects.js'; import { keyserverPrefixID } from './validation-utils.js'; +import { convertRawThreadInfoToNewIDSchema } from '../_generated/migration-utils.js'; import { parsePendingThreadID, getPendingThreadID, @@ -11,6 +13,7 @@ ClientDBDraftStoreOperation, DraftStore, } from '../types/draft-types'; +import type { ThreadStoreThreadInfos } from '../types/thread-types.js'; const draftKeySuffix = '/message_composer'; @@ -57,8 +60,32 @@ return [{ type: 'remove_all' }, ...operations]; } +function convertMessageStoreThreadsToNewIDSchema( + messageStoreThreads: TranslatedThreadMessageInfos, +): TranslatedThreadMessageInfos { + return Object.fromEntries( + entries(messageStoreThreads).map(([id, translatedThreadMessageInfo]) => [ + `${keyserverPrefixID}|` + id, + translatedThreadMessageInfo, + ]), + ); +} + +function convertThreadStoreThreadInfosToNewIDSchema( + threadStoreThreadInfos: ThreadStoreThreadInfos, +): ThreadStoreThreadInfos { + return Object.fromEntries( + entries(threadStoreThreadInfos).map(([id, threadInfo]) => [ + `${keyserverPrefixID}|` + id, + convertRawThreadInfoToNewIDSchema(threadInfo), + ]), + ); +} + export { convertDraftKeyToNewIDSchema, convertDraftStoreToNewIDSchema, generateIDSchemaMigrationOpsForDrafts, + convertMessageStoreThreadsToNewIDSchema, + convertThreadStoreThreadInfosToNewIDSchema, }; diff --git a/native/redux/persist.js b/native/redux/persist.js --- a/native/redux/persist.js +++ b/native/redux/persist.js @@ -7,6 +7,14 @@ import { createTransform } from 'redux-persist'; import type { Transform } from 'redux-persist/es/types.js'; +import { + convertEntryStoreToNewIDSchema, + convertInviteLinksStoreToNewIDSchema, + convertMessageStoreToNewIDSchema, + convertRawMessageInfoToNewIDSchema, + convertCalendarFilterToNewIDSchema, + convertConnectionInfoToNewIDSchema, +} from 'lib/_generated/migration-utils.js'; import { type ReportStoreOperation, type ClientDBReportStoreOperation, @@ -46,6 +54,11 @@ translateClientDBMessageInfoToRawMessageInfo, translateRawMessageInfoToClientDBMessageInfo, } from 'lib/utils/message-ops-utils.js'; +import { + generateIDSchemaMigrationOpsForDrafts, + convertMessageStoreThreadsToNewIDSchema, + convertThreadStoreThreadInfosToNewIDSchema, +} from 'lib/utils/migration-utils.js'; import { defaultNotifPermissionAlertInfo } from 'lib/utils/push-alerts.js'; import { convertClientDBThreadInfoToRawThreadInfo, @@ -54,7 +67,12 @@ } from 'lib/utils/thread-ops-utils.js'; import { getUUID } from 'lib/utils/uuid.js'; -import { updateClientDBThreadStoreThreadInfos } from './client-db-utils.js'; +import { + updateClientDBThreadStoreThreadInfos, + createUpdateDBOpsForThreadStoreThreadInfos, + createUpdateDBOpsForMessageStoreMessages, + createUpdateDBOpsForMessageStoreThreads, +} from './client-db-utils.js'; import { migrateThreadStoreForEditThreadPermissions } from './edit-thread-permission-migration.js'; import { persistMigrationForManagePinsThreadPermission } from './manage-pins-permission-migration.js'; import type { AppState } from './state-types.js'; @@ -577,6 +595,56 @@ } return state; }, + [43]: async (state: AppState) => { + const { messages, drafts, threads, messageStoreThreads } = + await commCoreModule.getClientDBStore(); + + const messageStoreThreadsOperations = + createUpdateDBOpsForMessageStoreThreads( + messageStoreThreads, + convertMessageStoreThreadsToNewIDSchema, + ); + + const messageStoreMessagesOperations = + createUpdateDBOpsForMessageStoreMessages(messages, messageInfos => + messageInfos.map(convertRawMessageInfoToNewIDSchema), + ); + + const threadOperations = createUpdateDBOpsForThreadStoreThreadInfos( + threads, + convertThreadStoreThreadInfosToNewIDSchema, + ); + + const draftOperations = generateIDSchemaMigrationOpsForDrafts(drafts); + + try { + await Promise.all([ + commCoreModule.processMessageStoreOperations([ + ...messageStoreMessagesOperations, + ...messageStoreThreadsOperations, + ]), + commCoreModule.processThreadStoreOperations(threadOperations), + commCoreModule.processDraftStoreOperations(draftOperations), + ]); + } catch (exception) { + console.log(exception); + return { ...state, cookie: null }; + } + + return { + ...state, + entryStore: convertEntryStoreToNewIDSchema(state.entryStore), + messageStore: convertMessageStoreToNewIDSchema(state.messageStore), + calendarFilters: state.calendarFilters.map( + convertCalendarFilterToNewIDSchema, + ), + connection: convertConnectionInfoToNewIDSchema(state.connection), + watchedThreadIDs: state.watchedThreadIDs.map(id => '256|' + id), + inviteLinksStore: convertInviteLinksStoreToNewIDSchema( + state.inviteLinksStore, + ), + }; + }, }; // After migration 31, we'll no longer want to persist `messageStore.messages`