diff --git a/keyserver/src/shared/state-sync/current-user-state-sync-spec.js b/keyserver/src/shared/state-sync/current-user-state-sync-spec.js --- a/keyserver/src/shared/state-sync/current-user-state-sync-spec.js +++ b/keyserver/src/shared/state-sync/current-user-state-sync-spec.js @@ -12,7 +12,7 @@ export const currentUserStateSyncSpec: StateSyncSpec< OldCurrentUserInfo | CurrentUserInfo, > = Object.freeze({ - fetchAll(viewer: Viewer) { + fetch(viewer: Viewer) { return fetchCurrentUserInfo(viewer); }, }); diff --git a/keyserver/src/shared/state-sync/entries-state-sync-spec.js b/keyserver/src/shared/state-sync/entries-state-sync-spec.js --- a/keyserver/src/shared/state-sync/entries-state-sync-spec.js +++ b/keyserver/src/shared/state-sync/entries-state-sync-spec.js @@ -4,12 +4,22 @@ import type { CalendarQuery, RawEntryInfos } from 'lib/types/entry-types.js'; import type { StateSyncSpec } from './state-sync-spec.js'; -import { fetchEntryInfos } from '../../fetchers/entry-fetchers.js'; +import { + fetchEntryInfos, + fetchEntryInfosByID, +} from '../../fetchers/entry-fetchers.js'; import type { Viewer } from '../../session/viewer.js'; export const entriesStateSyncSpec: StateSyncSpec = Object.freeze( { - async fetchAll(viewer: Viewer, query: $ReadOnlyArray) { + async fetch( + viewer: Viewer, + query: $ReadOnlyArray, + ids?: $ReadOnlySet, + ) { + if (ids) { + return fetchEntryInfosByID(viewer, ids); + } const entriesResult = await fetchEntryInfos(viewer, query); return serverEntryInfosObject(entriesResult.rawEntryInfos); }, diff --git a/keyserver/src/shared/state-sync/state-sync-spec.js b/keyserver/src/shared/state-sync/state-sync-spec.js --- a/keyserver/src/shared/state-sync/state-sync-spec.js +++ b/keyserver/src/shared/state-sync/state-sync-spec.js @@ -5,8 +5,9 @@ import type { Viewer } from '../../session/viewer.js'; export type StateSyncSpec = { - +fetchAll: ( + +fetch: ( viewer: Viewer, calendarQuery: $ReadOnlyArray, + ids?: $ReadOnlySet, ) => Promise, }; diff --git a/keyserver/src/shared/state-sync/threads-state-sync-spec.js b/keyserver/src/shared/state-sync/threads-state-sync-spec.js --- a/keyserver/src/shared/state-sync/threads-state-sync-spec.js +++ b/keyserver/src/shared/state-sync/threads-state-sync-spec.js @@ -1,5 +1,7 @@ // @flow +import type { CalendarQuery } from 'lib/types/entry-types.js'; + import type { StateSyncSpec } from './state-sync-spec.js'; import { fetchThreadInfos, @@ -9,7 +11,16 @@ export const threadsStateSyncSpec: StateSyncSpec = Object.freeze({ - fetchAll(viewer: Viewer) { + fetch( + viewer: Viewer, + query: $ReadOnlyArray, + ids?: $ReadOnlySet, + ) { + if (ids) { + return fetchThreadInfos(viewer, { + threadIDs: ids, + }); + } return fetchThreadInfos(viewer); }, }); diff --git a/keyserver/src/shared/state-sync/users-state-sync-spec.js b/keyserver/src/shared/state-sync/users-state-sync-spec.js --- a/keyserver/src/shared/state-sync/users-state-sync-spec.js +++ b/keyserver/src/shared/state-sync/users-state-sync-spec.js @@ -1,5 +1,6 @@ // @flow +import type { CalendarQuery } from 'lib/types/entry-types.js'; import type { UserInfos } from 'lib/types/user-types.js'; import type { StateSyncSpec } from './state-sync-spec.js'; @@ -7,7 +8,15 @@ import type { Viewer } from '../../session/viewer.js'; export const usersStateSyncSpec: StateSyncSpec = Object.freeze({ - fetchAll(viewer: Viewer) { + fetch( + viewer: Viewer, + query: $ReadOnlyArray, + ids?: $ReadOnlySet, + ) { + if (ids) { + return fetchKnownUserInfos(viewer, [...ids]); + } + return fetchKnownUserInfos(viewer); }, }); diff --git a/keyserver/src/socket/session-utils.js b/keyserver/src/socket/session-utils.js --- a/keyserver/src/socket/session-utils.js +++ b/keyserver/src/socket/session-utils.js @@ -4,7 +4,6 @@ import t from 'tcomb'; import type { TUnion } from 'tcomb'; -import { serverEntryInfo } from 'lib/shared/entry-utils.js'; import type { UpdateActivityResult } from 'lib/types/activity-types.js'; import type { IdentityKeysBlob } from 'lib/types/crypto-types.js'; import { isDeviceType } from 'lib/types/device-types.js'; @@ -38,17 +37,8 @@ import { createOlmSession } from '../creators/olm-session-creator.js'; import { saveOneTimeKeys } from '../creators/one-time-keys-creator.js'; import createReport from '../creators/report-creator.js'; -import { - fetchEntryInfos, - fetchEntryInfosByID, - fetchEntriesForSession, -} from '../fetchers/entry-fetchers.js'; +import { fetchEntriesForSession } from '../fetchers/entry-fetchers.js'; import { checkIfSessionHasEnoughOneTimeKeys } from '../fetchers/key-fetchers.js'; -import { fetchThreadInfos } from '../fetchers/thread-fetchers.js'; -import { - fetchCurrentUserInfo, - fetchKnownUserInfos, -} from '../fetchers/user-fetchers.js'; import { activityUpdatesInputValidator } from '../responders/activity-responders.js'; import { handleAsyncPromise } from '../responders/handlers.js'; import { @@ -395,10 +385,10 @@ return { sessionUpdate: { lastValidated: Date.now() } }; } else if (status.status === 'state_check') { const promises = { - threadsResult: serverStateSyncSpecs.threads.fetchAll(viewer, query), - entriesResult: serverStateSyncSpecs.entries.fetchAll(viewer, query), - currentUserInfo: serverStateSyncSpecs.currentUser.fetchAll(viewer, query), - userInfosResult: serverStateSyncSpecs.users.fetchAll(viewer, query), + threadsResult: serverStateSyncSpecs.threads.fetch(viewer, query), + entriesResult: serverStateSyncSpecs.entries.fetch(viewer, query), + currentUserInfo: serverStateSyncSpecs.currentUser.fetch(viewer, query), + userInfosResult: serverStateSyncSpecs.users.fetch(viewer, query), }; const fetchedData = await promiseAll(promises); const hashesToCheck = { @@ -446,24 +436,43 @@ const fetchPromises = {}; if (fetchAllThreads) { - fetchPromises.threadsResult = fetchThreadInfos(viewer); + fetchPromises.threadsResult = serverStateSyncSpecs.threads.fetch( + viewer, + query, + ); } else if (threadIDsToFetch.size > 0) { - fetchPromises.threadsResult = fetchThreadInfos(viewer, { - threadIDs: threadIDsToFetch, - }); + fetchPromises.threadsResult = serverStateSyncSpecs.threads.fetch( + viewer, + query, + threadIDsToFetch, + ); } if (fetchAllEntries) { - fetchPromises.entriesResult = fetchEntryInfos(viewer, [calendarQuery]); + fetchPromises.entryInfos = serverStateSyncSpecs.entries.fetch( + viewer, + query, + ); } else if (entryIDsToFetch.size > 0) { - fetchPromises.entryInfos = fetchEntryInfosByID(viewer, entryIDsToFetch); + fetchPromises.entryInfos = serverStateSyncSpecs.entries.fetch( + viewer, + query, + entryIDsToFetch, + ); } if (fetchAllUserInfos) { - fetchPromises.userInfos = fetchKnownUserInfos(viewer); + fetchPromises.userInfos = serverStateSyncSpecs.users.fetch(viewer, query); } else if (userIDsToFetch.size > 0) { - fetchPromises.userInfos = fetchKnownUserInfos(viewer, [...userIDsToFetch]); + fetchPromises.userInfos = serverStateSyncSpecs.users.fetch( + viewer, + query, + userIDsToFetch, + ); } if (fetchUserInfo) { - fetchPromises.currentUserInfo = fetchCurrentUserInfo(viewer); + fetchPromises.currentUserInfo = serverStateSyncSpecs.currentUser.fetch( + viewer, + query, + ); } const fetchedData = await promiseAll(fetchPromises); @@ -482,12 +491,10 @@ } else if (key === 'entryInfos') { // Instead of returning all entryInfos, we want to narrow down and figure // out which entryInfos don't match first - const { rawEntryInfos } = fetchedData.entriesResult; - for (const rawEntryInfo of rawEntryInfos) { - const entryInfo = serverEntryInfo(rawEntryInfo); + const { entryInfos } = fetchedData; + for (const entryID in entryInfos) { + const entryInfo = entryInfos[entryID]; invariant(entryInfo, 'should be set'); - const { id: entryID } = entryInfo; - invariant(entryID, 'should be set'); hashesToCheck[`entryInfo|${entryID}`] = hash(entryInfo); } failUnmentioned.entryInfos = true; @@ -518,14 +525,7 @@ stateChanges.rawThreadInfos.push(threadInfo); } else if (key.startsWith('entryInfo|')) { const [, entryID] = key.split('|'); - let entryInfo; - if (fetchedData.entriesResult) { - entryInfo = fetchedData.entriesResult.rawEntryInfos.find( - candidate => candidate.id === entryID, - ); - } else { - entryInfo = fetchedData.entryInfos[entryID]; - } + const entryInfo = fetchedData.entryInfos[entryID]; if (!entryInfo) { if (!stateChanges.deleteEntryIDs) { stateChanges.deleteEntryIDs = [];