diff --git a/lib/ops/dm-operations-store-ops.js b/lib/ops/dm-operations-store-ops.js index c56ddae79..caf3dff45 100644 --- a/lib/ops/dm-operations-store-ops.js +++ b/lib/ops/dm-operations-store-ops.js @@ -1,83 +1,94 @@ // @flow import type { DMOperation } from '../types/dm-ops.js'; type Operation = { +id: string, +type: string, +operation: DMOperation, }; export type ReplaceDMOperationOperation = { +type: 'replace_dm_operation', +payload: Operation, }; export type RemoveDMOperationsOperation = { +type: 'remove_dm_operations', +payload: { +ids: $ReadOnlyArray<string>, }, }; export type RemoveAllDMOperationsOperation = { +type: 'remove_all_dm_operations', }; export type DMOperationStoreOperation = | ReplaceDMOperationOperation | RemoveDMOperationsOperation | RemoveAllDMOperationsOperation; export type ClientDBDMOperation = { +id: string, +type: string, +operation: string, }; export type ClientDBReplaceDMOperationOperation = { +type: 'replace_dm_operation', +payload: ClientDBDMOperation, }; export type ClientDBDMOperationStoreOperation = | ClientDBReplaceDMOperationOperation | RemoveDMOperationsOperation | RemoveAllDMOperationsOperation; function convertDMOperationIntoClientDBDMOperation({ id, type, operation, }: Operation): ClientDBDMOperation { return { id, type, operation: JSON.stringify(operation), }; } function convertDMOperationOpsToClientDBOps( ops: ?$ReadOnlyArray<DMOperationStoreOperation>, ): $ReadOnlyArray<ClientDBDMOperationStoreOperation> { if (!ops) { return []; } return ops.map(operation => { if ( operation.type === 'remove_dm_operations' || operation.type === 'remove_all_dm_operations' ) { return operation; } return { type: 'replace_dm_operation', payload: convertDMOperationIntoClientDBDMOperation(operation.payload), }; }); } +function convertClientDBDMOperationToDMOperation( + operation: ClientDBDMOperation, +): Operation { + return { + id: operation.id, + type: operation.type, + operation: JSON.parse(operation.operation), + }; +} + export { convertDMOperationIntoClientDBDMOperation, convertDMOperationOpsToClientDBOps, + convertClientDBDMOperationToDMOperation, }; diff --git a/lib/shared/unshim-utils.js b/lib/shared/unshim-utils.js index f4bdbc2d3..70da3ef08 100644 --- a/lib/shared/unshim-utils.js +++ b/lib/shared/unshim-utils.js @@ -1,58 +1,112 @@ // @flow import _mapValues from 'lodash/fp/mapValues.js'; import { messageSpecs } from './messages/message-specs.js'; +import { + type ClientDBDMOperation, + convertClientDBDMOperationToDMOperation, +} from '../ops/dm-operations-store-ops.js'; +import { type DMOperationType } from '../types/dm-ops.js'; import { type MessageType, messageTypes } from '../types/message-types-enum.js'; import { type MessageStore, type RawMessageInfo, } from '../types/message-types.js'; +import type { BaseNavInfo } from '../types/nav-types.js'; +import type { BaseAppState } from '../types/redux-types.js'; +import { getConfig } from '../utils/config.js'; +import type { MigrationResult } from '../utils/migration-utils.js'; function unshimFunc( messageInfo: RawMessageInfo, unshimTypes: Set<MessageType>, ): RawMessageInfo { if (messageInfo.type !== messageTypes.UNSUPPORTED) { return messageInfo; } if (!unshimTypes.has(messageInfo.unsupportedMessageInfo.type)) { return messageInfo; } const unwrapped = messageInfo.unsupportedMessageInfo; const { unshimMessageInfo } = messageSpecs[unwrapped.type]; const unshimmed = unshimMessageInfo?.(unwrapped, messageInfo); return unshimmed ?? unwrapped; } function DEPRECATED_unshimMessageStore( messageStore: MessageStore, unshimTypes: $ReadOnlyArray<MessageType>, ): MessageStore { const set = new Set(unshimTypes); const messages = _mapValues((messageInfo: RawMessageInfo) => unshimFunc(messageInfo, set), )(messageStore.messages); return { ...messageStore, messages }; } const localUnshimTypes = new Set([ messageTypes.IMAGES, messageTypes.LEGACY_UPDATE_RELATIONSHIP, messageTypes.CREATE_SIDEBAR, messageTypes.SIDEBAR_SOURCE, messageTypes.MULTIMEDIA, messageTypes.REACTION, messageTypes.TOGGLE_PIN, messageTypes.EDIT_MESSAGE, messageTypes.UPDATE_RELATIONSHIP, ]); function unshimMessageInfos( messageInfos: $ReadOnlyArray<RawMessageInfo>, ): RawMessageInfo[] { return messageInfos.map((messageInfo: RawMessageInfo) => unshimFunc(messageInfo, localUnshimTypes), ); } -export { DEPRECATED_unshimMessageStore, unshimMessageInfos, unshimFunc }; +async function unshimDMOperations<N: BaseNavInfo, T: BaseAppState<N>>( + state: T, + type: DMOperationType, + handleMigrationFailure: T => T, +): Promise<MigrationResult<T>> { + const { fetchDMOperationsByType } = getConfig().sqliteAPI; + try { + const operations: Array<ClientDBDMOperation> = + await fetchDMOperationsByType(type); + if (operations.length === 0) { + return { + state, + ops: {}, + }; + } + + const shimmedOperations = operations + .map(convertClientDBDMOperationToDMOperation) + .map(op => ({ + id: op.id, + operation: op.operation, + })); + + return { + state: { + ...state, + queuedDMOperations: { + ...state.queuedDMOperations, + shimmedOperations, + }, + }, + ops: {}, + }; + } catch (e) { + console.log(e); + const newState = handleMigrationFailure(state); + return { state: newState, ops: {} }; + } +} + +export { + DEPRECATED_unshimMessageStore, + unshimMessageInfos, + unshimFunc, + unshimDMOperations, +};