diff --git a/lib/ops/report-store-ops.js b/lib/ops/report-store-ops.js --- a/lib/ops/report-store-ops.js +++ b/lib/ops/report-store-ops.js @@ -1,5 +1,6 @@ // @flow +import { type BaseStoreOperations } from './base-ops.js'; import type { ClientReportCreationRequest } from '../types/report-types.js'; export type ReplaceQueuedReportOperation = { @@ -33,31 +34,6 @@ export type ClientDBReport = { +id: string, +report: string }; -function processReportStoreOperations( - queuedReports: $ReadOnlyArray, - reportStoreOps: $ReadOnlyArray, -): $ReadOnlyArray { - if (reportStoreOps.length === 0) { - return queuedReports; - } - let processedReports = [...queuedReports]; - for (const operation of reportStoreOps) { - if (operation.type === 'replace_report') { - const filteredReports = processedReports.filter( - report => report.id !== operation.payload.report.id, - ); - processedReports = [...filteredReports, { ...operation.payload.report }]; - } else if (operation.type === 'remove_reports') { - processedReports = processedReports.filter( - report => !operation.payload.ids.includes(report.id), - ); - } else if (operation.type === 'remove_all_reports') { - processedReports = []; - } - } - return processedReports; -} - function convertReportsToReplaceReportOps( reports: $ReadOnlyArray, ): $ReadOnlyArray { @@ -76,36 +52,68 @@ }; } -function convertReportStoreOperationToClientDBReportStoreOperation( - reportStoreOperations: $ReadOnlyArray, -): $ReadOnlyArray { - return reportStoreOperations.map(operation => { - if ( - operation.type === 'remove_reports' || - operation.type === 'remove_all_reports' - ) { - return operation; +export const reportStoreOps: BaseStoreOperations< + $ReadOnlyArray, + ReportStoreOperation, + ClientDBReportStoreOperation, + $ReadOnlyArray, + ClientDBReport, +> = { + processStoreOperations( + queuedReports: $ReadOnlyArray, + ops: $ReadOnlyArray, + ): $ReadOnlyArray { + if (ops.length === 0) { + return queuedReports; } - return { - type: 'replace_report', - payload: { - id: operation.payload.report.id, - report: JSON.stringify(operation.payload.report), - }, - }; - }); -} + let processedReports = [...queuedReports]; + for (const operation of ops) { + if (operation.type === 'replace_report') { + const filteredReports = processedReports.filter( + report => report.id !== operation.payload.report.id, + ); + processedReports = [ + ...filteredReports, + { ...operation.payload.report }, + ]; + } else if (operation.type === 'remove_reports') { + processedReports = processedReports.filter( + report => !operation.payload.ids.includes(report.id), + ); + } else if (operation.type === 'remove_all_reports') { + processedReports = []; + } + } + return processedReports; + }, -function convertClientDBReportToClientReportCreationRequest( - reports: $ReadOnlyArray, -): $ReadOnlyArray { - return reports.map(reportRecord => JSON.parse(reportRecord.report)); -} + convertOpsToClientDBOps( + ops: $ReadOnlyArray, + ): $ReadOnlyArray { + return ops.map(operation => { + if ( + operation.type === 'remove_reports' || + operation.type === 'remove_all_reports' + ) { + return operation; + } + return { + type: 'replace_report', + payload: { + id: operation.payload.report.id, + report: JSON.stringify(operation.payload.report), + }, + }; + }); + }, + translateClientDBData( + data: $ReadOnlyArray, + ): $ReadOnlyArray { + return data.map(reportRecord => JSON.parse(reportRecord.report)); + }, +}; export { - processReportStoreOperations, convertReportsToReplaceReportOps, convertReportsToRemoveReportsOperation, - convertReportStoreOperationToClientDBReportStoreOperation, - convertClientDBReportToClientReportCreationRequest, }; diff --git a/lib/ops/report-store-ops.test.js b/lib/ops/report-store-ops.test.js --- a/lib/ops/report-store-ops.test.js +++ b/lib/ops/report-store-ops.test.js @@ -1,6 +1,6 @@ // @flow -import { processReportStoreOperations } from './report-store-ops.js'; +import { reportStoreOps } from './report-store-ops.js'; import { type ClientReportCreationRequest, reportTypes, @@ -51,18 +51,20 @@ ); }; +const { processStoreOperations: processReportStoreOperations } = reportStoreOps; + describe('processReportStoreOperations', () => { it('should return the original reports if no operations are processed', () => { - const reportStoreOps = []; + const reportOps = []; const processedReports = processReportStoreOperations( mockReports, - reportStoreOps, + reportOps, ); expect(processedReports).toEqual(mockReports); }); it('should replace the report with the given id', () => { - const reportStoreOps = [ + const reportOps = [ { type: 'replace_report', payload: { @@ -89,14 +91,14 @@ ]; const processedReports = processReportStoreOperations( mockReports, - reportStoreOps, + reportOps, ); expect(sortReports(processedReports)).toEqual(sortReports(expectedReports)); }); it('should handle an empty reports with replace operation', () => { - const reportStoreOps = [ + const reportOps = [ { type: 'replace_report', payload: { @@ -114,12 +116,12 @@ }, ]; - const processedReports = processReportStoreOperations([], reportStoreOps); + const processedReports = processReportStoreOperations([], reportOps); expect(processedReports).toEqual(expectedReports); }); it('should remove reports with given ids', () => { - const reportStoreOps = [ + const reportOps = [ { type: 'remove_reports', payload: { @@ -135,13 +137,13 @@ ]; const processedReports = processReportStoreOperations( mockReports, - reportStoreOps, + reportOps, ); expect(processedReports).toEqual(expectedReports); }); it('should handle empty reports with remove_reports operation', () => { - const reportStoreOps = [ + const reportOps = [ { type: 'remove_reports', payload: { @@ -150,31 +152,31 @@ }, ]; - const processedReports = processReportStoreOperations([], reportStoreOps); + const processedReports = processReportStoreOperations([], reportOps); expect(processedReports).toEqual([]); }); it('should remove all reports', () => { - const reportStoreOps = [ + const reportOps = [ { type: 'remove_all_reports', }, ]; const processedReports = processReportStoreOperations( mockReports, - reportStoreOps, + reportOps, ); expect(processedReports).toEqual([]); }); it('should handle empty reports with remove_all_reports operation', () => { - const reportStoreOps = [ + const reportOps = [ { type: 'remove_all_reports', }, ]; - const processedReports = processReportStoreOperations([], reportStoreOps); + const processedReports = processReportStoreOperations([], reportOps); expect(processedReports).toEqual([]); }); }); diff --git a/lib/reducers/report-store-reducer.js b/lib/reducers/report-store-reducer.js --- a/lib/reducers/report-store-reducer.js +++ b/lib/reducers/report-store-reducer.js @@ -16,7 +16,7 @@ import { convertReportsToRemoveReportsOperation, convertReportsToReplaceReportOps, - processReportStoreOperations, + reportStoreOps, } from '../ops/report-store-ops.js'; import { isStaff } from '../shared/staff-utils.js'; import type { BaseAction } from '../types/redux-types.js'; @@ -32,6 +32,8 @@ export const updateReportsEnabledActionType = 'UPDATE_REPORTS_ENABLED'; +const { processStoreOperations: processReportStoreOperations } = reportStoreOps; + export default function reduceReportStore( state: ReportStore, action: BaseAction, diff --git a/native/data/sqlite-data-handler.js b/native/data/sqlite-data-handler.js --- a/native/data/sqlite-data-handler.js +++ b/native/data/sqlite-data-handler.js @@ -6,7 +6,7 @@ import { setClientDBStoreActionType } from 'lib/actions/client-db-store-actions.js'; import { MediaCacheContext } from 'lib/components/media-cache-provider.react.js'; -import { convertClientDBReportToClientReportCreationRequest } from 'lib/ops/report-store-ops.js'; +import { reportStoreOps } from 'lib/ops/report-store-ops.js'; import { threadStoreOps } from 'lib/ops/thread-store-ops.js'; import { cookieSelector } from 'lib/selectors/keyserver-selectors.js'; import { isLoggedIn } from 'lib/selectors/user-selectors.js'; @@ -172,8 +172,7 @@ const { threads, messages, drafts, messageStoreThreads, reports } = await commCoreModule.getClientDBStore(); const threadInfosFromDB = threadStoreOps.translateClientDBData(threads); - const reportsFromDb = - convertClientDBReportToClientReportCreationRequest(reports); + const reportsFromDb = reportStoreOps.translateClientDBData(reports); dispatch({ type: setClientDBStoreActionType, diff --git a/native/redux/persist.js b/native/redux/persist.js --- a/native/redux/persist.js +++ b/native/redux/persist.js @@ -19,8 +19,8 @@ import { type ReportStoreOperation, type ClientDBReportStoreOperation, - convertReportStoreOperationToClientDBReportStoreOperation, convertReportsToReplaceReportOps, + reportStoreOps, } from 'lib/ops/report-store-ops.js'; import type { ClientDBThreadStoreOperation } from 'lib/ops/thread-store-ops'; import { threadStoreOps } from 'lib/ops/thread-store-ops.js'; @@ -580,9 +580,7 @@ ...convertReportsToReplaceReportOps(state.reportStore.queuedReports), ]; const dbOperations: $ReadOnlyArray = - convertReportStoreOperationToClientDBReportStoreOperation( - reportStoreOperations, - ); + reportStoreOps.convertOpsToClientDBOps(reportStoreOperations); try { commCoreModule.processReportStoreOperationsSync(dbOperations); diff --git a/native/redux/redux-utils.js b/native/redux/redux-utils.js --- a/native/redux/redux-utils.js +++ b/native/redux/redux-utils.js @@ -2,7 +2,7 @@ import { useSelector as reactReduxUseSelector } from 'react-redux'; -import { convertReportStoreOperationToClientDBReportStoreOperation } from 'lib/ops/report-store-ops.js'; +import { reportStoreOps } from 'lib/ops/report-store-ops.js'; import { threadStoreOps } from 'lib/ops/thread-store-ops.js'; import type { StoreOperations } from 'lib/types/store-ops-types.js'; import { convertMessageStoreOperationsToClientDBOperations } from 'lib/utils/message-ops-utils.js'; @@ -33,10 +33,9 @@ ); const convertedMessageStoreOperations = convertMessageStoreOperationsToClientDBOperations(messageStoreOperations); - const convertedReportStoreOperations = - convertReportStoreOperationToClientDBReportStoreOperation( - reportStoreOperations, - ); + const convertedReportStoreOperations = reportStoreOps.convertOpsToClientDBOps( + reportStoreOperations, + ); try { const promises = []; diff --git a/web/database/sqlite-data-handler.js b/web/database/sqlite-data-handler.js --- a/web/database/sqlite-data-handler.js +++ b/web/database/sqlite-data-handler.js @@ -5,7 +5,7 @@ import { useDispatch } from 'react-redux'; import { setClientDBStoreActionType } from 'lib/actions/client-db-store-actions.js'; -import { convertClientDBReportToClientReportCreationRequest } from 'lib/ops/report-store-ops.js'; +import { reportStoreOps } from 'lib/ops/report-store-ops.js'; import { databaseModule } from './database-module-provider.js'; import { SQLITE_ENCRYPTION_KEY } from './utils/constants.js'; @@ -95,9 +95,7 @@ if (!data?.store?.drafts && !data?.store?.reports) { return; } - const reports = convertClientDBReportToClientReportCreationRequest( - data.store.reports, - ); + const reports = reportStoreOps.translateClientDBData(data.store.reports); dispatch({ type: setClientDBStoreActionType, payload: { diff --git a/web/redux/redux-setup.js b/web/redux/redux-setup.js --- a/web/redux/redux-setup.js +++ b/web/redux/redux-setup.js @@ -7,7 +7,7 @@ logOutActionTypes, deleteAccountActionTypes, } from 'lib/actions/user-actions.js'; -import { convertReportStoreOperationToClientDBReportStoreOperation } from 'lib/ops/report-store-ops.js'; +import { reportStoreOps } from 'lib/ops/report-store-ops.js'; import baseReducer from 'lib/reducers/master-reducer.js'; import { mostRecentlyReadThreadSelector } from 'lib/selectors/thread-selectors.js'; import { isLoggedIn } from 'lib/selectors/user-selectors.js'; @@ -189,9 +189,7 @@ return; } const convertedReportStoreOperations = - convertReportStoreOperationToClientDBReportStoreOperation( - reportStoreOperations, - ); + reportStoreOps.convertOpsToClientDBOps(reportStoreOperations); await databaseModule.schedule({ type: workerRequestMessageTypes.PROCESS_STORE_OPERATIONS, storeOperations: {