diff --git a/lib/utils/action-utils.js b/lib/utils/action-utils.js --- a/lib/utils/action-utils.js +++ b/lib/utils/action-utils.js @@ -22,6 +22,7 @@ } from '../types/account-types.js'; import type { PlatformDetails } from '../types/device-types.js'; import type { Endpoint, SocketAPIHandler } from '../types/endpoints.js'; +import type { CalendarQuery } from '../types/entry-types.js'; import type { LoadingOptions, LoadingInfo } from '../types/loading-types.js'; import type { ActionPayload, @@ -476,6 +477,52 @@ return results; } +function sortCalendarQueryPerKeyserver( + calendarQuery: CalendarQuery, + keyserverIDs: $ReadOnlyArray, +): { + +[keyserverID: string]: CalendarQuery, +} { + const { startDate, endDate, filters } = calendarQuery; + const results = {}; + + for (const keyserverID of keyserverIDs) { + results[keyserverID] = { + startDate, + endDate, + filters: [], + }; + } + + for (const filter of filters) { + if (filter.type === 'not_deleted') { + for (const keyserverID in results) { + results[keyserverID].filters.push({ type: 'not_deleted' }); + } + } else if (filter.type === 'threads') { + for (const threadID of filter.threadIDs) { + const keyserverID = extractKeyserverIDFromID(threadID); + if (results[keyserverID] === undefined) { + continue; + } + let threadFilter = results[keyserverID].filters.find( + flt => flt.type === 'threads', + ); + if (!threadFilter) { + threadFilter = { type: 'threads', threadIDs: [] }; + results[keyserverID].filters.push(threadFilter); + } + + threadFilter.threadIDs.push(threadID); + } + } else { + console.warn('unhandled filter in sortCalendarQueryPerKeyserver'); + } + } + + return results; +} + export { useDispatchActionPromise, setNewSessionActionType, @@ -486,4 +533,5 @@ bindCookieAndUtilsIntoCallServerEndpoint, extractKeyserverIDFromID, sortThreadIDsPerKeyserver, + sortCalendarQueryPerKeyserver, }; diff --git a/lib/utils/action-utils.test.js b/lib/utils/action-utils.test.js --- a/lib/utils/action-utils.test.js +++ b/lib/utils/action-utils.test.js @@ -1,6 +1,10 @@ // @flow -import { extractKeyserverIDFromID } from './action-utils.js'; +import { + extractKeyserverIDFromID, + sortCalendarQueryPerKeyserver, +} from './action-utils.js'; +import type { CalendarQuery } from '../types/entry-types'; describe('extractKeyserverIDFromID', () => { it('should return for |', () => { @@ -9,3 +13,71 @@ expect(extractKeyserverIDFromID(id)).toBe(keyserverID); }); }); + +describe('sortCalendarQueryPerKeyserver', () => { + it('should split the calendar query into multiple queries, one for every \ + keyserver, that have all the properties of the original one, \ + but only the thread ids that the keyserver should get', () => { + const query: CalendarQuery = { + startDate: '1463588881886', + endDate: '1463588889886', + filters: [ + { type: 'not_deleted' }, + { + type: 'threads', + threadIDs: ['256|1', '256|2', '100|100', '100|101'], + }, + ], + }; + const queriesPerKeyserver = { + ['256']: { + startDate: '1463588881886', + endDate: '1463588889886', + filters: [ + { type: 'not_deleted' }, + { + type: 'threads', + threadIDs: ['256|1', '256|2'], + }, + ], + }, + ['100']: { + startDate: '1463588881886', + endDate: '1463588889886', + filters: [ + { type: 'not_deleted' }, + { + type: 'threads', + threadIDs: ['100|100', '100|101'], + }, + ], + }, + }; + expect(sortCalendarQueryPerKeyserver(query, ['100', '256'])).toEqual( + queriesPerKeyserver, + ); + }); + + it('should create calendar query for every keyserver in the second argument', () => { + const query: CalendarQuery = { + startDate: '1463588881886', + endDate: '1463588889886', + filters: [{ type: 'not_deleted' }], + }; + const queriesPerKeyserver = { + ['256']: { + startDate: '1463588881886', + endDate: '1463588889886', + filters: [{ type: 'not_deleted' }], + }, + ['100']: { + startDate: '1463588881886', + endDate: '1463588889886', + filters: [{ type: 'not_deleted' }], + }, + }; + expect(sortCalendarQueryPerKeyserver(query, ['100', '256'])).toEqual( + queriesPerKeyserver, + ); + }); +});