Caldav/refactor: Mutiple Fixes and General code improvement (#8031)
* code cleanup and fixes for unsupported objects and fetch requests * improves sanitizer function * adds comment to the utility function sanitizeCalendarObjects * adds ICAL.parse in try catch-- fixes code breaking for all objects due to one bad ICAL body * adds a function that ensures expand=false fallback for unsupporting calendars * --WIP * improves code to now support multi-event calendar ics parsing * Handle failed calls to not break all selected calendars * removes debug remnants * removes further unnecessary commentspull/8234/head^2
parent
91f381bce9
commit
a8368aac79
|
@ -4,7 +4,7 @@ import type { Prisma } from "@prisma/client";
|
|||
import ICAL from "ical.js";
|
||||
import type { Attendee, DateArray, DurationObject, Person } from "ics";
|
||||
import { createEvent } from "ics";
|
||||
import type { DAVAccount, DAVCalendar } from "tsdav";
|
||||
import type { DAVAccount, DAVCalendar, DAVObject } from "tsdav";
|
||||
import {
|
||||
createAccount,
|
||||
createCalendarObject,
|
||||
|
@ -37,6 +37,13 @@ const DEFAULT_CALENDAR_TYPE = "caldav";
|
|||
|
||||
const CALENDSO_ENCRYPTION_KEY = process.env.CALENDSO_ENCRYPTION_KEY || "";
|
||||
|
||||
type FetchObjectsWithOptionalExpandOptionsType = {
|
||||
selectedCalendars: IntegrationCalendar[];
|
||||
startISOString: string;
|
||||
dateTo: string;
|
||||
headers?: Record<string, string>;
|
||||
};
|
||||
|
||||
function hasFileExtension(url: string): boolean {
|
||||
// Get the last portion of the URL (after the last '/')
|
||||
const fileName = url.substring(url.lastIndexOf("/") + 1);
|
||||
|
@ -44,9 +51,9 @@ function hasFileExtension(url: string): boolean {
|
|||
return fileName.includes(".") && !fileName.substring(fileName.lastIndexOf(".")).includes("/");
|
||||
}
|
||||
|
||||
function getFileExtension(url: string): string | null {
|
||||
function getFileExtension(url: string): string {
|
||||
// Return null if the URL does not have a file extension
|
||||
if (!hasFileExtension(url)) return null;
|
||||
if (!hasFileExtension(url)) return "ics";
|
||||
// Get the last portion of the URL (after the last '/')
|
||||
const fileName = url.substring(url.lastIndexOf("/") + 1);
|
||||
// Extract the file extension
|
||||
|
@ -283,10 +290,6 @@ export default abstract class BaseCalendarService implements Calendar {
|
|||
isValidFormat = (url: string): boolean => {
|
||||
const allowedExtensions = ["eml", "ics"];
|
||||
const urlExtension = getFileExtension(url);
|
||||
if (!urlExtension) {
|
||||
console.error("Invalid request, calendar object extension missing");
|
||||
return false;
|
||||
}
|
||||
if (!allowedExtensions.includes(urlExtension)) {
|
||||
console.error(`Unsupported calendar object format: ${urlExtension}`);
|
||||
return false;
|
||||
|
@ -300,36 +303,27 @@ export default abstract class BaseCalendarService implements Calendar {
|
|||
selectedCalendars: IntegrationCalendar[]
|
||||
): Promise<EventBusyDate[]> {
|
||||
const startISOString = new Date(dateFrom).toISOString();
|
||||
const objects = (
|
||||
await Promise.all(
|
||||
selectedCalendars
|
||||
.filter((sc) => ["caldav_calendar", "apple_calendar"].includes(sc.integration ?? ""))
|
||||
.map((sc) =>
|
||||
fetchCalendarObjects({
|
||||
urlFilter: (url: string) => this.isValidFormat(url),
|
||||
calendar: {
|
||||
url: sc.externalId,
|
||||
},
|
||||
|
||||
const objects = await this.fetchObjectsWithOptionalExpand({
|
||||
selectedCalendars,
|
||||
startISOString,
|
||||
dateTo,
|
||||
headers: this.headers,
|
||||
expand: true,
|
||||
timeRange: {
|
||||
start: startISOString,
|
||||
end: new Date(dateTo).toISOString(),
|
||||
},
|
||||
})
|
||||
)
|
||||
)
|
||||
).flat();
|
||||
|
||||
});
|
||||
const events: { start: string; end: string }[] = [];
|
||||
|
||||
objects.forEach((object) => {
|
||||
if (object.data == null || JSON.stringify(object.data) == "{}") return;
|
||||
|
||||
let vcalendar: ICAL.Component;
|
||||
try {
|
||||
const jcalData = ICAL.parse(sanitizeCalendarObject(object));
|
||||
const vcalendar = new ICAL.Component(jcalData);
|
||||
const vevent = vcalendar.getFirstSubcomponent("vevent");
|
||||
|
||||
vcalendar = new ICAL.Component(jcalData);
|
||||
} catch (e) {
|
||||
console.error("Error parsing calendar object: ", e);
|
||||
return;
|
||||
}
|
||||
// const vevent = vcalendar.getFirstSubcomponent("vevent");
|
||||
const vevents = vcalendar.getAllSubcomponents("vevent");
|
||||
vevents.forEach((vevent) => {
|
||||
// if event status is free or transparent, return
|
||||
if (vevent?.getFirstPropertyValue("transp") === "TRANSPARENT") return;
|
||||
|
||||
|
@ -341,12 +335,15 @@ export default abstract class BaseCalendarService implements Calendar {
|
|||
const tzid: string | undefined = vevent?.getFirstPropertyValue("tzid") || isUTC ? "UTC" : timezone;
|
||||
// In case of icalendar, when only tzid is available without vtimezone, we need to add vtimezone explicitly to take care of timezone diff
|
||||
if (!vcalendar.getFirstSubcomponent("vtimezone") && tzid) {
|
||||
// this.registerTimezone(tzid);
|
||||
|
||||
const timezoneComp = new ICAL.Component("vtimezone");
|
||||
timezoneComp.addPropertyWithValue("tzid", tzid);
|
||||
const standard = new ICAL.Component("standard");
|
||||
// get timezone offset
|
||||
const tzoffsetfrom = buildUtcOffset(dayjs(event.startDate.toJSDate()).tz(tzid, true).utcOffset());
|
||||
const tzoffsetto = buildUtcOffset(dayjs(event.endDate.toJSDate()).tz(tzid, true).utcOffset());
|
||||
|
||||
// set timezone offset
|
||||
standard.addPropertyWithValue("tzoffsetfrom", tzoffsetfrom);
|
||||
standard.addPropertyWithValue("tzoffsetto", tzoffsetto);
|
||||
|
@ -423,12 +420,12 @@ export default abstract class BaseCalendarService implements Calendar {
|
|||
event.startDate = event.startDate.convertToZone(zone);
|
||||
event.endDate = event.endDate.convertToZone(zone);
|
||||
}
|
||||
|
||||
return events.push({
|
||||
start: dayjs(event.startDate.toJSDate()).toISOString(),
|
||||
end: dayjs(event.endDate.toJSDate()).toISOString(),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
return Promise.resolve(events);
|
||||
}
|
||||
|
@ -466,6 +463,81 @@ export default abstract class BaseCalendarService implements Calendar {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The fetchObjectsWithOptionalExpand function is responsible for fetching calendar objects
|
||||
* from an array of selectedCalendars. It attempts to fetch objects with the expand option
|
||||
* alone such that it works if a calendar supports it. If any calendar object has an undefined 'data' property
|
||||
* and etag isn't undefined, the function makes a new request without the expand option to retrieve the data.
|
||||
* The result is a flattened array of calendar objects with the structure { url: ..., etag: ..., data: ...}.
|
||||
*
|
||||
* @param {Object} options - The options object containing the following properties:
|
||||
* @param {IntegrationCalendar[]} options.selectedCalendars - An array of IntegrationCalendar objects to fetch data from.
|
||||
* @param {string} options.startISOString - The start date of the date range to fetch events from, in ISO 8601 format.
|
||||
* @param {string} options.dateTo - The end date of the date range to fetch events from.
|
||||
* @param {Object} options.headers - Headers to be included in the API requests.
|
||||
* @returns {Promise<Array>} - A promise that resolves to a flattened array of calendar objects with the structure { url: ..., etag: ..., data: ...}.
|
||||
*/
|
||||
|
||||
async fetchObjectsWithOptionalExpand({
|
||||
selectedCalendars,
|
||||
startISOString,
|
||||
dateTo,
|
||||
headers,
|
||||
}: FetchObjectsWithOptionalExpandOptionsType): Promise<DAVObject[]> {
|
||||
const filteredCalendars = selectedCalendars.filter((sc) => sc.externalId);
|
||||
const fetchPromises = filteredCalendars.map(async (sc) => {
|
||||
const response = await fetchCalendarObjects({
|
||||
urlFilter: (url) => this.isValidFormat(url),
|
||||
calendar: {
|
||||
url: sc.externalId,
|
||||
},
|
||||
headers,
|
||||
expand: true,
|
||||
timeRange: {
|
||||
start: startISOString,
|
||||
end: new Date(dateTo).toISOString(),
|
||||
},
|
||||
});
|
||||
|
||||
const processedResponse = await Promise.all(
|
||||
response.map(async (calendarObject) => {
|
||||
const calendarObjectHasEtag = calendarObject.etag !== undefined;
|
||||
const calendarObjectDataUndefined = calendarObject.data === undefined;
|
||||
if (calendarObjectDataUndefined && calendarObjectHasEtag) {
|
||||
const responseWithoutExpand = await fetchCalendarObjects({
|
||||
urlFilter: (url) => this.isValidFormat(url),
|
||||
calendar: {
|
||||
url: sc.externalId,
|
||||
},
|
||||
headers,
|
||||
expand: false,
|
||||
timeRange: {
|
||||
start: startISOString,
|
||||
end: new Date(dateTo).toISOString(),
|
||||
},
|
||||
});
|
||||
|
||||
return responseWithoutExpand.find(
|
||||
(obj) => obj.url === calendarObject.url && obj.etag === calendarObject.etag
|
||||
);
|
||||
}
|
||||
return calendarObject;
|
||||
})
|
||||
);
|
||||
return processedResponse;
|
||||
});
|
||||
const resolvedPromises = await Promise.allSettled(fetchPromises);
|
||||
const fulfilledPromises = resolvedPromises.filter(
|
||||
(promise): promise is PromiseFulfilledResult<(DAVObject | undefined)[]> =>
|
||||
promise.status === "fulfilled"
|
||||
);
|
||||
const flatResult = fulfilledPromises
|
||||
.map((promise) => promise.value)
|
||||
.flat()
|
||||
.filter((obj) => obj !== null);
|
||||
return flatResult as DAVObject[];
|
||||
}
|
||||
|
||||
private async getEvents(
|
||||
calId: string,
|
||||
dateFrom: string | null,
|
||||
|
@ -533,7 +605,6 @@ export default abstract class BaseCalendarService implements Calendar {
|
|||
timezone: calendarTimezone,
|
||||
};
|
||||
});
|
||||
|
||||
return events;
|
||||
} catch (reason) {
|
||||
console.error(reason);
|
||||
|
|
|
@ -1,11 +1,30 @@
|
|||
import type { DAVObject } from "tsdav";
|
||||
|
||||
/**
|
||||
* sanitizeCalendarObject is a utility function that cleans up and normalizes
|
||||
* the iCalendar data from a DAVObject before parsing it using the ICAL.js library.
|
||||
* It addresses issues with incorrect line endings, line folding, and unwanted line
|
||||
* breaks before colons, semicolons, and equal signs.
|
||||
*
|
||||
* @param {DAVObject} obj - The DAVObject containing the iCalendar data to be sanitized.
|
||||
* @returns {string} The sanitized iCalendar data.
|
||||
*
|
||||
* NOTE: This function is a workaround for handling improperly formatted iCalendar
|
||||
* data. It is recommended to use a well-formed iCalendar data source to avoid
|
||||
* the need for these manual corrections. This function may not cover all edge cases
|
||||
* and might still cause issues with specific inputs.
|
||||
*/
|
||||
|
||||
export const sanitizeCalendarObject = (obj: DAVObject) => {
|
||||
return obj.data
|
||||
.replaceAll("\r\n", "\r")
|
||||
.replaceAll("\r", "\r\n")
|
||||
.replaceAll(/(; \r\n|;\r\n|\r\n;|\r\n ;)/gm, ";")
|
||||
.replaceAll(/(= \r\n|=\r\n|\r\n=|\r\n =)/gm, "=");
|
||||
.replaceAll("\r\n", "\n")
|
||||
.replaceAll("\r", "\n")
|
||||
.replaceAll("\n ", "")
|
||||
.replaceAll("\n\t", "")
|
||||
.replaceAll("\n", "\r\n")
|
||||
.replaceAll(/(:[ \t]*\r\n[ \t]*:)/gm, ":")
|
||||
.replaceAll(/(;[ \t]*\r\n[ \t]*;)/gm, ";")
|
||||
.replaceAll(/(=[ \t]*\r\n[ \t]*=)/gm, "=");
|
||||
};
|
||||
|
||||
export default sanitizeCalendarObject;
|
||||
|
|
Loading…
Reference in New Issue