diff --git a/keyserver/src/creators/update-creator.js b/keyserver/src/creators/update-creator.js --- a/keyserver/src/creators/update-creator.js +++ b/keyserver/src/creators/update-creator.js @@ -386,31 +386,28 @@ rawUpdateInfos: $ReadOnlyArray, viewerInfo: ViewerInfo, ): Promise { - const { viewer } = viewerInfo; - - const threadIDsNeedingFetch = new Set(); - const entryIDsNeedingFetch = new Set(); - let currentUserNeedsFetch = false; - const threadIDsNeedingDetailedFetch = new Set(); // entries and messages - for (const rawUpdateInfo of rawUpdateInfos) { - if ( - !viewerInfo.threadInfos && - (rawUpdateInfo.type === updateTypes.UPDATE_THREAD || - rawUpdateInfo.type === updateTypes.JOIN_THREAD) - ) { - threadIDsNeedingFetch.add(rawUpdateInfo.threadID); - } - if (rawUpdateInfo.type === updateTypes.JOIN_THREAD) { - threadIDsNeedingDetailedFetch.add(rawUpdateInfo.threadID); - } else if (rawUpdateInfo.type === updateTypes.UPDATE_ENTRY) { - entryIDsNeedingFetch.add(rawUpdateInfo.entryID); - } else if (rawUpdateInfo.type === updateTypes.UPDATE_CURRENT_USER) { - currentUserNeedsFetch = true; - } - } + const entitiesToFetch = rawUpdateInfos + .map(info => updateSpecs[info.type].entitiesToFetch?.(info)) + .filter(Boolean); + const currentUserNeedsFetch = entitiesToFetch.some( + ({ currentUser }) => currentUser, + ); + const threadIDsNeedingFetch = viewerInfo.threadInfos + ? new Set() + : new Set(entitiesToFetch.map(({ threadID }) => threadID).filter(Boolean)); + const entryIDsNeedingFetch = new Set( + entitiesToFetch.map(({ entryID }) => entryID).filter(Boolean), + ); + // entries and messages + const threadIDsNeedingDetailedFetch = new Set( + entitiesToFetch + .map(({ detailedThreadID }) => detailedThreadID) + .filter(Boolean), + ); const promises = {}; + const { viewer } = viewerInfo; if (!viewerInfo.threadInfos && threadIDsNeedingFetch.size > 0) { promises.threadResult = fetchThreadInfos(viewer, { threadIDs: threadIDsNeedingFetch, diff --git a/lib/shared/updates/join-thread-spec.js b/lib/shared/updates/join-thread-spec.js --- a/lib/shared/updates/join-thread-spec.js +++ b/lib/shared/updates/join-thread-spec.js @@ -102,4 +102,10 @@ const { threadID } = data; return JSON.stringify({ threadID }); }, + entitiesToFetch(update: ThreadJoinRawUpdateInfo) { + return { + threadID: update.threadID, + detailedThreadID: update.threadID, + }; + }, }); diff --git a/lib/shared/updates/update-current-user-spec.js b/lib/shared/updates/update-current-user-spec.js --- a/lib/shared/updates/update-current-user-spec.js +++ b/lib/shared/updates/update-current-user-spec.js @@ -33,4 +33,9 @@ // user column contains all the info we need to construct the UpdateInfo return null; }, + entitiesToFetch() { + return { + currentUser: true, + }; + }, }); diff --git a/lib/shared/updates/update-entry-spec.js b/lib/shared/updates/update-entry-spec.js --- a/lib/shared/updates/update-entry-spec.js +++ b/lib/shared/updates/update-entry-spec.js @@ -40,4 +40,9 @@ const { entryID } = data; return JSON.stringify({ entryID }); }, + entitiesToFetch(update: EntryRawUpdateInfo) { + return { + entryID: update.entryID, + }; + }, }); diff --git a/lib/shared/updates/update-spec.js b/lib/shared/updates/update-spec.js --- a/lib/shared/updates/update-spec.js +++ b/lib/shared/updates/update-spec.js @@ -46,4 +46,10 @@ ) => void, +rawUpdateInfoFromRow: (row: Object) => RawInfo, +updateContentForServerDB: (data: Data) => ?string, + +entitiesToFetch?: (update: RawInfo) => { + +threadID?: string, + +detailedThreadID?: string, + +entryID?: string, + +currentUser?: boolean, + }, }; diff --git a/lib/shared/updates/update-thread-spec.js b/lib/shared/updates/update-thread-spec.js --- a/lib/shared/updates/update-thread-spec.js +++ b/lib/shared/updates/update-thread-spec.js @@ -60,4 +60,9 @@ updateContentForServerDB(data: ThreadUpdateData) { return JSON.stringify({ threadID: data.threadID }); }, + entitiesToFetch(update: ThreadRawUpdateInfo) { + return { + threadID: update.threadID, + }; + }, });