From e23a38e48648a8b90472d38d46405801563f0392 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Wed, 20 Oct 2021 12:15:56 +0200 Subject: Prevent duplicate CalDAV availability slots for recurring slots If there is the same time slot for more than one day, then we wrote two recurring AVAILABILITY components before. With this patch equal blocks are combined into one recurring component. This makes the generated rules more compact and easier to work with. Signed-off-by: Christoph Wurst --- apps/dav/src/service/CalendarService.js | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'apps/dav/src') diff --git a/apps/dav/src/service/CalendarService.js b/apps/dav/src/service/CalendarService.js index 3d3fbe34b07..c187d61b86e 100644 --- a/apps/dav/src/service/CalendarService.js +++ b/apps/dav/src/service/CalendarService.js @@ -100,15 +100,37 @@ export async function saveScheduleInboxAvailability(slots, timezoneId) { })))] const vavailabilityComp = new ICAL.Component('vavailability') - // TODO: deduplicate slots that occur on more than one day - all.map(slot => { + + // Deduplicate by start and end time + const deduplicated = all.reduce((acc, slot) => { + const key = [ + slot.start.getHours(), + slot.start.getMinutes(), + slot.end.getHours(), + slot.end.getMinutes(), + ].join('-') + + return { + ...acc, + [key]: [...(acc[key] ?? []), slot], + } + }, {}) + + // Create an AVAILABILITY component for every recurring slot + Object.keys(deduplicated).map(key => { + const slots = deduplicated[key] + const start = slots[0].start + const end = slots[0].end + // Combine days but make them also unique + const days = slots.map(slot => slot.day).filter((day, index, self) => self.indexOf(day) === index) + const availableComp = new ICAL.Component('available') // Define DTSTART and DTEND // TODO: tz? moment.tz(dateTime, timezone).toDate() - const startTimeProp = availableComp.addPropertyWithValue('dtstart', ICAL.Time.fromJSDate(slot.start, false)) + const startTimeProp = availableComp.addPropertyWithValue('dtstart', ICAL.Time.fromJSDate(start, false)) startTimeProp.setParameter('tzid', timezoneId) - const endTimeProp = availableComp.addPropertyWithValue('dtend', ICAL.Time.fromJSDate(slot.end, false)) + const endTimeProp = availableComp.addPropertyWithValue('dtend', ICAL.Time.fromJSDate(end, false)) endTimeProp.setParameter('tzid', timezoneId) // Add mandatory UID @@ -119,7 +141,7 @@ export async function saveScheduleInboxAvailability(slots, timezoneId) { // Define RRULE availableComp.addPropertyWithValue('rrule', { freq: 'WEEKLY', - byday: slot.day, + byday: days, }) return availableComp -- cgit v1.2.3