diff --git a/keyserver/src/responders/redux-state-responders.js b/keyserver/src/responders/redux-state-responders.js --- a/keyserver/src/responders/redux-state-responders.js +++ b/keyserver/src/responders/redux-state-responders.js @@ -14,6 +14,7 @@ parsePendingThreadID, createPendingThread, } from 'lib/shared/thread-utils.js'; +import { canUseDatabaseOnWeb } from 'lib/shared/web-database.js'; import { entryStoreValidator } from 'lib/types/entry-types.js'; import { defaultCalendarFilters } from 'lib/types/filter-types.js'; import { inviteLinksStoreValidator } from 'lib/types/link-types.js'; @@ -89,7 +90,9 @@ viewer: Viewer, request: InitialReduxStateRequest, ): Promise { - const { urlInfo } = request; + const { urlInfo, excludedData } = request; + const useDatabase = canUseDatabaseOnWeb(viewer.userID); + const hasNotAcknowledgedPoliciesPromise = hasAnyNotAcknowledgedPolicies( viewer.id, baseLegalPolicies, @@ -153,6 +156,9 @@ })(); const threadStorePromise = (async () => { + if (excludedData.threadStore && useDatabase) { + return { threadInfos: {} }; + } const [{ threadInfos }, hasNotAcknowledgedPolicies] = await Promise.all([ threadInfoPromise, hasNotAcknowledgedPoliciesPromise, 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 @@ -6,7 +6,11 @@ import type { Persistor } from 'redux-persist/es/types'; import { setClientDBStoreActionType } from 'lib/actions/client-db-store-actions.js'; +import type { ThreadStoreOperation } from 'lib/ops/thread-store-ops.js'; +import { canUseDatabaseOnWeb } from 'lib/shared/web-database.js'; +import type { RawThreadInfo } from 'lib/types/thread-types.js'; import { convertIDToNewSchema } from 'lib/utils/migration-utils.js'; +import { entries } from 'lib/utils/objects.js'; import { infoFromURL } from 'lib/utils/url-utils.js'; import { ashoatKeyserverID } from 'lib/utils/validation-utils.js'; @@ -15,7 +19,10 @@ useGetInitialReduxState, } from './action-types.js'; import { useSelector } from './redux-utils.js'; -import { getClientStore } from '../database/utils/store.js'; +import { + getClientStore, + processDBStoreOperations, +} from '../database/utils/store.js'; import Loading from '../loading.react.js'; type Props = { @@ -49,23 +56,56 @@ thread: convertIDToNewSchema(urlInfo.thread, ashoatKeyserverID), }; } - const clientDBStore = await getClientStore(); const payload = await callGetInitialReduxState({ urlInfo, - excludedData: { threadStore: false }, + excludedData: { threadStore: !!clientDBStore.threadStore }, }); const currentLoggedInUserID = payload.currentUserInfo?.anonymous ? undefined : payload.currentUserInfo?.id; + const useDatabase = canUseDatabaseOnWeb(currentLoggedInUserID); + + if (!currentLoggedInUserID || !useDatabase) { + dispatch({ type: setInitialReduxState, payload }); + return; + } - if (currentLoggedInUserID) { + if (clientDBStore.threadStore) { + // If there is data in the DB, populate the store dispatch({ type: setClientDBStoreActionType, payload: clientDBStore, }); + const { threadStore, ...rest } = payload; + dispatch({ type: setInitialReduxState, payload: rest }); + return; + } else { + // When there is no data in the DB, it's necessary to migrate data + // from the keyserver payload to the DB + const { + threadStore: { threadInfos }, + } = payload; + + const threadStoreOperations: ThreadStoreOperation[] = entries( + threadInfos, + ).map(([id, threadInfo]: [string, RawThreadInfo]) => ({ + type: 'replace', + payload: { + id, + threadInfo, + }, + })); + + await processDBStoreOperations({ + threadStoreOperations, + draftStoreOperations: [], + messageStoreOperations: [], + reportStoreOperations: [], + userStoreOperations: [], + }); } dispatch({ type: setInitialReduxState, payload }); } catch (err) {