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 @@ -255,12 +255,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, @@ -279,7 +280,7 @@ function translateThreadMessageInfoToClientDBThreadMessageInfo( id: string, - threadMessageInfo: ThreadMessageInfo, + threadMessageInfo: ThreadMessageInfo | TranslatedThreadMessageInfo, ): ClientDBThreadMessageInfo { const startReached = threadMessageInfo.startReached ? 1 : 0; const lastNavigatedTo = threadMessageInfo.lastNavigatedTo ?? 0; @@ -359,4 +360,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, + ClientDBMessageInfo, + ClientDBThreadMessageInfo, +} 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, + translateClientDBThreadMessageInfos, + translateThreadMessageInfoToClientDBThreadMessageInfo, + type TranslatedThreadMessageInfos, +} from 'lib/utils/message-ops-utils.js'; +import { values, entries } from 'lib/utils/objects.js'; import { convertClientDBThreadInfoToRawThreadInfo, convertRawThreadInfoToClientDBThreadInfo, @@ -19,36 +32,57 @@ state: AppState, migrationFunc: ThreadStoreThreadInfos => ThreadStoreThreadInfos, ): AppState { - // 1. Get threads from SQLite `threads` table. + // Get threads from SQLite `threads` table. const clientDBThreadInfos = commCoreModule.getAllThreadsSync(); - // 2. Translate `ClientDBThreadInfo`s to `RawThreadInfo`s. + const operations = createUpdateDBOpsForThreadStoreThreadInfos( + clientDBThreadInfos, + migrationFunc, + ); + + // Try processing `ClientDBThreadStoreOperation`s and log out if + // `processThreadStoreOperationsSync(...)` throws an exception. + try { + commCoreModule.processThreadStoreOperationsSync(operations); + } catch (exception) { + console.log(exception); + return { ...state, cookie: null }; + } + + return state; +} + +function createUpdateDBOpsForThreadStoreThreadInfos( + clientDBThreadInfos: $ReadOnlyArray, + migrationFunc: ThreadStoreThreadInfos => ThreadStoreThreadInfos, +): $ReadOnlyArray { + // Translate `ClientDBThreadInfo`s to `RawThreadInfo`s. const rawThreadInfos = clientDBThreadInfos.map( convertClientDBThreadInfoToRawThreadInfo, ); - // 3. Convert `rawThreadInfo`s to a map of `threadID` => `threadInfo`. + // Convert `rawThreadInfo`s to a map of `threadID` => `threadInfo`. const threadIDToThreadInfo = rawThreadInfos.reduce((acc, threadInfo) => { acc[threadInfo.id] = threadInfo; return acc; }, {}); - // 4. Apply `migrationFunc` to `threadInfo`s. + // Apply `migrationFunc` to `threadInfo`s. const updatedThreadIDToThreadInfo: ThreadStoreThreadInfos = migrationFunc(threadIDToThreadInfo); - // 5. Convert the updated `threadInfo`s back into an array. + // Convert the updated `threadInfo`s back into an array. const updatedRawThreadInfos: $ReadOnlyArray = values( updatedThreadIDToThreadInfo, ); - // 6. Translate `RawThreadInfo`s to `ClientDBThreadInfo`s. + // Translate `RawThreadInfo`s to `ClientDBThreadInfo`s. const convertedClientDBThreadInfos: $ReadOnlyArray = updatedRawThreadInfos.map(convertRawThreadInfoToClientDBThreadInfo); - // 7. Construct `ClientDBThreadStoreOperation`s to clear SQLite `threads` + // Construct `ClientDBThreadStoreOperation`s to clear SQLite `threads` // table and repopulate with `ClientDBThreadInfo`s. - const operations: $ReadOnlyArray = [ + return [ { type: 'remove_all', }, @@ -57,17 +91,64 @@ payload: thread, })), ]; +} - // 8. Try processing `ClientDBThreadStoreOperation`s and log out if - // `processThreadStoreOperationsSync(...)` throws an exception. - try { - commCoreModule.processThreadStoreOperationsSync(operations); - } catch (exception) { - console.log(exception); - return { ...state, cookie: null }; - } +function createUpdateDBOpsForMessageStoreMessages( + clientDBMessageInfos: $ReadOnlyArray, + migrationFunc: ( + $ReadOnlyArray, + ) => $ReadOnlyArray, +): $ReadOnlyArray { + const rawMessageInfos = clientDBMessageInfos.map( + translateClientDBMessageInfoToRawMessageInfo, + ); - return state; + const convertedRawMessageInfos = migrationFunc(rawMessageInfos); + + const replaceMessagesOperations: $ReadOnlyArray = + convertedRawMessageInfos.map(messageInfo => ({ + type: 'replace', + payload: translateRawMessageInfoToClientDBMessageInfo(messageInfo), + })); + + return [ + { + type: 'remove_all', + }, + ...replaceMessagesOperations, + ]; +} + +function createUpdateDBOpsForMessageStoreThreads( + messageStoreThreads: $ReadOnlyArray, + migrationFunc: TranslatedThreadMessageInfos => TranslatedThreadMessageInfos, +): $ReadOnlyArray { + const translatedMessageStoreThreads = + translateClientDBThreadMessageInfos(messageStoreThreads); + + const convertedTranslatedMessageStoreThreads = migrationFunc( + translatedMessageStoreThreads, + ); + + return [ + { + type: 'remove_all_threads', + }, + { + type: 'replace_threads', + payload: { + threads: entries(convertedTranslatedMessageStoreThreads).map( + ([id, thread]) => + translateThreadMessageInfoToClientDBThreadMessageInfo(id, thread), + ), + }, + }, + ]; } -export { updateClientDBThreadStoreThreadInfos }; +export { + updateClientDBThreadStoreThreadInfos, + createUpdateDBOpsForThreadStoreThreadInfos, + createUpdateDBOpsForMessageStoreMessages, + createUpdateDBOpsForMessageStoreThreads, +};