diff --git a/lib/types/redux-types.js b/lib/types/redux-types.js --- a/lib/types/redux-types.js +++ b/lib/types/redux-types.js @@ -22,7 +22,7 @@ GetVersionActionPayload, LastCommunicatedPlatformDetails, } from './device-types.js'; -import type { ClientDBDraftInfo, DraftStore } from './draft-types.js'; +import type { DraftStore } from './draft-types.js'; import type { EnabledApps, SupportedApps } from './enabled-apps.js'; import type { RawEntryInfo, @@ -68,9 +68,7 @@ NewMessagesPayload, MessageStorePrunePayload, LocallyComposedMessageInfo, - ClientDBMessageInfo, SimpleMessagesPayload, - ClientDBThreadMessageInfo, FetchPinnedMessagesResult, SearchMessagesResponse, } from './message-types.js'; @@ -88,7 +86,6 @@ ClearDeliveredReportsPayload, QueueReportsPayload, ReportStore, - ClientReportCreationRequest, } from './report-types.js'; import type { ProcessServerRequestAction, @@ -106,6 +103,7 @@ SetLateResponsePayload, UpdateDisconnectedBarPayload, } from './socket-types.js'; +import { type ClientStore } from './store-ops-types.js'; import type { SubscriptionUpdateResult } from './subscription-types.js'; import type { GlobalThemeInfo } from './theme-types.js'; import type { ThreadActivityStore } from './thread-activity-types.js'; @@ -120,7 +118,7 @@ RoleDeletionPayload, } from './thread-types.js'; import type { ClientUpdatesResultWithUserInfos } from './update-types.js'; -import type { CurrentUserInfo, UserInfos, UserStore } from './user-types.js'; +import type { CurrentUserInfo, UserStore } from './user-types.js'; import type { SetDeviceTokenActionPayload } from '../actions/device-actions.js'; import type { Shape } from '../types/core.js'; import type { NotifPermissionAlertInfo } from '../utils/push-alerts.js'; @@ -652,15 +650,7 @@ } | { +type: 'SET_CLIENT_DB_STORE', - +payload: { - +currentUserID: ?string, - +drafts: $ReadOnlyArray, - +messages: ?$ReadOnlyArray, - +threadStore: ?ThreadStore, - +messageStoreThreads: ?$ReadOnlyArray, - +reports: ?$ReadOnlyArray, - +users: ?UserInfos, - }, + +payload: ClientStore, } | { +type: 'UPDATE_ACTIVITY_STARTED', diff --git a/lib/types/store-ops-types.js b/lib/types/store-ops-types.js --- a/lib/types/store-ops-types.js +++ b/lib/types/store-ops-types.js @@ -9,7 +9,9 @@ ClientDBMessageInfo, ClientDBThreadMessageInfo, } from './message-types.js'; -import type { ClientDBThreadInfo } from './thread-types.js'; +import type { ClientReportCreationRequest } from './report-types.js'; +import type { ClientDBThreadInfo, ThreadStore } from './thread-types.js'; +import type { UserInfos } from './user-types.js'; import type { ClientDBMessageStoreOperation, MessageStoreOperation, @@ -51,3 +53,13 @@ +reports: $ReadOnlyArray, +users: $ReadOnlyArray, }; + +export type ClientStore = { + +currentUserID: ?string, + +drafts: $ReadOnlyArray, + +messages: ?$ReadOnlyArray, + +threadStore: ?ThreadStore, + +messageStoreThreads: ?$ReadOnlyArray, + +reports: ?$ReadOnlyArray, + +users: ?UserInfos, +}; 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 @@ -3,9 +3,6 @@ import * as React from 'react'; import { useDispatch } from 'react-redux'; -import { setClientDBStoreActionType } from 'lib/actions/client-db-store-actions.js'; -import { reportStoreOpsHandlers } from 'lib/ops/report-store-ops.js'; - import { getDatabaseModule } from './database-module-provider.js'; import { useSelector } from '../redux/redux-utils.js'; import { workerRequestMessageTypes } from '../types/worker-types.js'; @@ -59,33 +56,8 @@ return; } await handleSensitiveData(); - if (!currentLoggedInUserID) { - return; - } - const data = await databaseModule.schedule({ - type: workerRequestMessageTypes.GET_CLIENT_STORE, - }); - - if (!data?.store?.drafts && !data?.store?.reports) { - return; - } - const reports = reportStoreOpsHandlers.translateClientDBData( - data.store.reports, - ); - dispatch({ - type: setClientDBStoreActionType, - payload: { - drafts: data.store.drafts, - reports, - }, - }); })(); - }, [ - currentLoggedInUserID, - dispatch, - handleSensitiveData, - rehydrateConcluded, - ]); + }, [dispatch, handleSensitiveData, rehydrateConcluded]); return null; } diff --git a/web/database/utils/store.js b/web/database/utils/store.js new file mode 100644 --- /dev/null +++ b/web/database/utils/store.js @@ -0,0 +1,38 @@ +// @flow + +import { reportStoreOpsHandlers } from 'lib/ops/report-store-ops.js'; +import type { ClientStore } from 'lib/types/store-ops-types.js'; + +import { workerRequestMessageTypes } from '../../types/worker-types.js'; +import { getDatabaseModule } from '../database-module-provider.js'; + +async function getClientStore(): Promise { + const databaseModule = await getDatabaseModule(); + let result: ClientStore = { + currentUserID: null, + drafts: [], + messages: null, + threadStore: null, + messageStoreThreads: null, + reports: null, + users: null, + }; + const data = await databaseModule.schedule({ + type: workerRequestMessageTypes.GET_CLIENT_STORE, + }); + if (data?.store?.drafts) { + result = { + ...result, + drafts: data.store.drafts, + }; + } + if (data?.store?.reports) { + result = { + ...result, + reports: reportStoreOpsHandlers.translateClientDBData(data.store.reports), + }; + } + return result; +} + +export { getClientStore }; diff --git a/web/redux/initial-state-gate.js b/web/redux/initial-state-gate.js --- a/web/redux/initial-state-gate.js +++ b/web/redux/initial-state-gate.js @@ -5,6 +5,7 @@ import { PersistGate } from 'redux-persist/es/integration/react.js'; import type { Persistor } from 'redux-persist/es/types'; +import { setClientDBStoreActionType } from 'lib/actions/client-db-store-actions.js'; import { convertIDToNewSchema } from 'lib/utils/migration-utils.js'; import { infoFromURL } from 'lib/utils/url-utils.js'; import { ashoatKeyserverID } from 'lib/utils/validation-utils.js'; @@ -14,6 +15,7 @@ useGetInitialReduxState, } from './action-types.js'; import { useSelector } from './redux-utils.js'; +import { getClientStore } from '../database/utils/store.js'; import Loading from '../loading.react.js'; type Props = { @@ -47,10 +49,24 @@ thread: convertIDToNewSchema(urlInfo.thread, ashoatKeyserverID), }; } + + const clientDBStore = await getClientStore(); + const payload = await callGetInitialReduxState({ urlInfo, excludedData: { threadStore: false }, }); + + const currentLoggedInUserID = payload.currentUserInfo?.anonymous + ? undefined + : payload.currentUserInfo?.id; + + if (currentLoggedInUserID) { + dispatch({ + type: setClientDBStoreActionType, + payload: clientDBStore, + }); + } dispatch({ type: setInitialReduxState, payload }); } catch (err) { setInitError(err);