diff --git a/lib/shared/redux/client-db-utils.js b/lib/shared/redux/client-db-utils.js --- a/lib/shared/redux/client-db-utils.js +++ b/lib/shared/redux/client-db-utils.js @@ -8,8 +8,16 @@ type ReplaceThreadOperation, type ThreadStoreOperation, } from '../../ops/thread-store-ops.js'; -import type { RawThreadInfos, ThreadStore } from '../../types/thread-types.js'; -import { entries } from '../../utils/objects.js'; +import type { + RawThreadInfos, + ThreadStore, + ClientDBThreadInfo, +} from '../../types/thread-types.js'; +import { entries, values } from '../../utils/objects.js'; +import { + convertClientDBThreadInfoToRawThreadInfo, + convertRawThreadInfoToClientDBThreadInfo, +} from '../../utils/thread-ops-utils.js'; function updateThreadStoreThreadInfos( threadStore: ThreadStore, @@ -54,4 +62,40 @@ }; } -export { updateThreadStoreThreadInfos }; +function createUpdateDBOpsForThreadStoreThreadInfos( + clientDBThreadInfos: $ReadOnlyArray, + migrationFunc: RawThreadInfos => RawThreadInfos, +): $ReadOnlyArray { + // 1. Translate `ClientDBThreadInfo`s to `RawThreadInfo`s. + const rawThreadInfos = clientDBThreadInfos.map( + convertClientDBThreadInfoToRawThreadInfo, + ); + + // 2. Convert `RawThreadInfo`s to a map of `threadID` => `threadInfo`. + const keyedRawThreadInfos = _keyBy('id')(rawThreadInfos); + + // 3. Apply `migrationFunc` to `ThreadInfo`s. + const updatedKeyedRawThreadInfos = migrationFunc(keyedRawThreadInfos); + + // 4. Convert the updated `RawThreadInfos` back into an array. + const updatedKeyedRawThreadInfosArray = values(updatedKeyedRawThreadInfos); + + // 5. Translate `RawThreadInfo`s back to `ClientDBThreadInfo`s. + const updatedClientDBThreadInfos = updatedKeyedRawThreadInfosArray.map( + convertRawThreadInfoToClientDBThreadInfo, + ); + + // 6. Construct `replace` `ClientDBThreadStoreOperation`s. + const replaceThreadOperations = updatedClientDBThreadInfos.map(thread => ({ + type: 'replace', + payload: thread, + })); + + // 7. Prepend `replaceThreadOperations` with `remove_all` op and return. + return [{ type: 'remove_all' }, ...replaceThreadOperations]; +} + +export { + updateThreadStoreThreadInfos, + createUpdateDBOpsForThreadStoreThreadInfos, +}; 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,18 +1,13 @@ // @flow -import _keyBy from 'lodash/fp/keyBy.js'; - import type { ClientDBMessageStoreOperation } from 'lib/ops/message-store-ops.js'; -import type { ClientDBThreadStoreOperation } from 'lib/ops/thread-store-ops.js'; +import { createUpdateDBOpsForThreadStoreThreadInfos } from 'lib/shared/redux/client-db-utils.js'; import type { RawMessageInfo, ClientDBMessageInfo, ClientDBThreadMessageInfo, } from 'lib/types/message-types.js'; -import type { - ClientDBThreadInfo, - RawThreadInfos, -} from 'lib/types/thread-types.js'; +import type { RawThreadInfos } from 'lib/types/thread-types.js'; import { translateClientDBMessageInfoToRawMessageInfo, translateRawMessageInfoToClientDBMessageInfo, @@ -20,11 +15,7 @@ translateThreadMessageInfoToClientDBThreadMessageInfo, type TranslatedThreadMessageInfos, } from 'lib/utils/message-ops-utils.js'; -import { entries, values } from 'lib/utils/objects.js'; -import { - convertClientDBThreadInfoToRawThreadInfo, - convertRawThreadInfoToClientDBThreadInfo, -} from 'lib/utils/thread-ops-utils.js'; +import { entries } from 'lib/utils/objects.js'; import type { AppState } from './state-types.js'; import { commCoreModule } from '../native-modules.js'; @@ -52,39 +43,6 @@ return state; } -function createUpdateDBOpsForThreadStoreThreadInfos( - clientDBThreadInfos: $ReadOnlyArray, - migrationFunc: RawThreadInfos => RawThreadInfos, -): $ReadOnlyArray { - // 1. Translate `ClientDBThreadInfo`s to `RawThreadInfo`s. - const rawThreadInfos = clientDBThreadInfos.map( - convertClientDBThreadInfoToRawThreadInfo, - ); - - // 2. Convert `RawThreadInfo`s to a map of `threadID` => `threadInfo`. - const keyedRawThreadInfos = _keyBy('id')(rawThreadInfos); - - // 3. Apply `migrationFunc` to `ThreadInfo`s. - const updatedKeyedRawThreadInfos = migrationFunc(keyedRawThreadInfos); - - // 4. Convert the updated `RawThreadInfos` back into an array. - const updatedKeyedRawThreadInfosArray = values(updatedKeyedRawThreadInfos); - - // 5. Translate `RawThreadInfo`s back to `ClientDBThreadInfo`s. - const updatedClientDBThreadInfos = updatedKeyedRawThreadInfosArray.map( - convertRawThreadInfoToClientDBThreadInfo, - ); - - // 6. Construct `replace` `ClientDBThreadStoreOperation`s. - const replaceThreadOperations = updatedClientDBThreadInfos.map(thread => ({ - type: 'replace', - payload: thread, - })); - - // 7. Prepend `replaceThreadOperations` with `remove_all` op and return. - return [{ type: 'remove_all' }, ...replaceThreadOperations]; -} - function createUpdateDBOpsForMessageStoreMessages( clientDBMessageInfos: $ReadOnlyArray, migrationFunc: (