diff --git a/lib/utils/message-ops-utils.js b/lib/utils/message-ops-utils.js --- a/lib/utils/message-ops-utils.js +++ b/lib/utils/message-ops-utils.js @@ -249,12 +249,13 @@ ); } -type TranslatedThreadMessageInfos = { - [threadID: string]: { - startReached: boolean, - lastNavigatedTo: number, - lastPruned: number, - }, +type TranslatedThreadMessageInfo = { + +startReached: boolean, + +lastNavigatedTo: number, + +lastPruned: number, +}; +export type TranslatedThreadMessageInfos = { + +[threadID: string]: TranslatedThreadMessageInfo, }; function translateClientDBThreadMessageInfos( clientDBThreadMessageInfo: $ReadOnlyArray, @@ -273,7 +274,7 @@ function translateThreadMessageInfoToClientDBThreadMessageInfo( id: string, - threadMessageInfo: ThreadMessageInfo, + threadMessageInfo: ThreadMessageInfo | TranslatedThreadMessageInfo, ): ClientDBThreadMessageInfo { const startReached = threadMessageInfo.startReached ? 1 : 0; const lastNavigatedTo = threadMessageInfo.lastNavigatedTo ?? 0; @@ -353,4 +354,5 @@ translateClientDBMediaInfosToMedia, getPinnedContentFromClientDBMessageInfo, translateClientDBThreadMessageInfos, + translateThreadMessageInfoToClientDBThreadMessageInfo, }; 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 @@ -1,12 +1,25 @@ // @flow +import type { + ClientDBMessageStoreOperation, + RawMessageInfo, +} from 'lib/types/message-types.js'; import type { ClientDBThreadInfo, ClientDBThreadStoreOperation, RawThreadInfo, ThreadStoreThreadInfos, } from 'lib/types/thread-types.js'; -import { values } from 'lib/utils/objects.js'; +import { + translateClientDBMessageInfoToRawMessageInfo, + translateRawMessageInfoToClientDBMessageInfo, +} from 'lib/utils/message-ops-utils.js'; +import { + translateClientDBThreadMessageInfos, + translateThreadMessageInfoToClientDBThreadMessageInfo, + type TranslatedThreadMessageInfos, +} from 'lib/utils/message-ops-utils.js'; +import { values, entries } from 'lib/utils/objects.js'; import { convertClientDBThreadInfoToRawThreadInfo, convertRawThreadInfoToClientDBThreadInfo, @@ -70,4 +83,83 @@ return state; } -export { updateClientDBThreadStoreThreadInfos }; +function updateClientDBMessageStoreMessages( + state: AppState, + migrationFunc: ( + $ReadOnlyArray, + ) => $ReadOnlyArray, +): AppState { + const clientDBMessageInfos = commCoreModule.getAllMessagesSync(); + + const rawMessageInfos = clientDBMessageInfos.map( + translateClientDBMessageInfoToRawMessageInfo, + ); + + const convertedRawMessageInfos = migrationFunc(rawMessageInfos); + + const replaceMessagesOperations: $ReadOnlyArray = + convertedRawMessageInfos.map(messageInfo => ({ + type: 'replace', + payload: translateRawMessageInfoToClientDBMessageInfo(messageInfo), + })); + + const operations: $ReadOnlyArray = [ + { + type: 'remove_all', + }, + ...replaceMessagesOperations, + ]; + + try { + commCoreModule.processMessageStoreOperationsSync(operations); + } catch (exception) { + console.log(exception); + return { ...state, cookie: null }; + } + + return state; +} + +async function updateClientDBMessageStoreThreads( + state: AppState, + migrationFunc: TranslatedThreadMessageInfos => TranslatedThreadMessageInfos, +): Promise { + const { messageStoreThreads } = await commCoreModule.getClientDBStore(); + + const translatedMessageStoreThreads = + translateClientDBThreadMessageInfos(messageStoreThreads); + + const convertedTranslatedMessageStoreThreads = migrationFunc( + translatedMessageStoreThreads, + ); + + const operations: $ReadOnlyArray = [ + { + type: 'remove_all_threads', + }, + { + type: 'replace_threads', + payload: { + threads: entries(convertedTranslatedMessageStoreThreads).map( + ([id, thread]) => + translateThreadMessageInfoToClientDBThreadMessageInfo(id, thread), + ), + }, + }, + ]; + + try { + commCoreModule.processMessageStoreOperationsSync(operations); + } catch (exception) { + console.log(exception); + return { ...state, cookie: null }; + } + + return state; +} + +export { + updateClientDBThreadStoreThreadInfos, + updateClientDBMessageStoreMessages, + updateClientDBMessageStoreThreads, +};