feat(calendarUtil): simplify event property copying

This commit is contained in:
xeruf 2024-11-25 13:32:47 +01:00
parent 2bd5c967af
commit 1706b9cad3
5 changed files with 24 additions and 41 deletions

View File

@ -112,12 +112,12 @@ export function createCalendarComponent(name) {
}
// Add events to the calendar component
export function addEventsToCalendar(calendarComponent, results, overrideFlag = false) {
export function addEventsToCalendar(calendarComponent, calendars, overrideFlag = false) {
let defaultTimeZone = null; // To store the first found X-WR-TIMEZONE
results.forEach((result) => {
calendars.forEach((calendar) => {
try {
const parsed = ICAL.parse(result.data);
const parsed = ICAL.parse(calendar.data);
const component = new ICAL.Component(parsed);
// Extract METHOD from the parsed data (if available)
@ -153,53 +153,36 @@ export function addEventsToCalendar(calendarComponent, results, overrideFlag = f
// Process VEVENT components
component.getAllSubcomponents('vevent').forEach((event) => {
const vevent = new ICAL.Event(event);
const newEvent = new ICAL.Component('vevent');
const newEvent = new ICAL.Event(newEvent);
// 1. Add DTSTART
processDateTimeProperty(event, 'dtstart', newEvent);
// 2. Add DTEND
processDateTimeProperty(event, 'dtend', newEvent);
newEvent.startDate = vevent.startDate
newEvent.endDate = vevent.endDate
// 3. Copy DTSTAMP
const dtstamp = event.getFirstPropertyValue('dtstamp');
if (dtstamp) newEvent.updatePropertyWithValue('dtstamp', dtstamp);
if (dtstamp) newEvent.component.updatePropertyWithValue('dtstamp', dtstamp);
// 4. Copy UID
newEvent.updatePropertyWithValue('uid', vevent.uid);
newEvent.uid = vevent.uid;
// 5. Add LOCATION (conditionally included)
if (!overrideFlag && vevent.location) {
newEvent.updatePropertyWithValue('location', vevent.location);
} else if (overrideFlag && vevent.location) {
// Modify SUMMARY if override is set
const modifiedSummary = `${vevent.summary.trim()} (Location omitted)`;
newEvent.updatePropertyWithValue('summary', modifiedSummary);
if (overrideFlag) {
newEvent.summary = 'Busy'
} else {
newEvent.updatePropertyWithValue('summary', vevent.summary.trim());
newEvent.summary = vevent.summary;
if (vevent.location) newEvent.location = vevent.location;
}
// 6. Copy Recurrence Rules (RRULE) and Recurrence ID
const rrule = event.getFirstPropertyValue('rrule');
if (rrule) newEvent.updatePropertyWithValue('rrule', rrule);
if (rrule) newEvent.component.updatePropertyWithValue('rrule', rrule);
const recurrenceId = event.getFirstPropertyValue('recurrence-id');
if (recurrenceId) newEvent.updatePropertyWithValue('recurrence-id', recurrenceId);
// 7. Copy SUMMARY
newEvent.updatePropertyWithValue('summary', vevent.summary.trim());
// 8. Add SEQUENCE (if available or default to 0)
const sequence = event.getFirstPropertyValue('sequence') || 0;
newEvent.updatePropertyWithValue('sequence', sequence);
// Add the VEVENT to the calendar
calendarComponent.addSubcomponent(newEvent);
calendarComponent.addSubcomponent(newEvent.component);
});
console.log(`Processed VEVENT components for calendar: ${result.name}`);
} catch (error) {
console.error('Error processing calendar data:', error.message);
console.log(`Processed VEVENT components for calendar: ${calendar.name}`);
} catch (error) {
console.error(`Error processing calendar:`, calendar, error);
}
});
}

View File

@ -47,9 +47,9 @@ async function refreshCalendarData(calendarName) {
// Read the JSON file to get the source URL and other details
const { calendars } = JSON.parse(fs.readFileSync(jsonFilePath, 'utf-8'));
const results = await Promise.all(calendars.map(fetchCalendarData));
const calendarResults = await Promise.all(calendars.map(fetchCalendarData));
const calendarComponent = createCalendarComponent(calendarName);
addEventsToCalendar(calendarComponent, results);
addEventsToCalendar(calendarComponent, calendarResults);
saveCalendarFile(`${calendarName}.ics`, calendarComponent.toString());
console.log('Calendar data refreshed and saved.');

View File

@ -1,11 +1,13 @@
BEGIN:VCALENDAR
NAME:US Holidays
PRODID:-//CalMerge//Calendar Merger 1.0//EN
VERSION:2.0
CALSCALE:GREGORIAN
BEGIN:VEVENT
UID:20231225T000000-001@example.com
DTSTAMP:20231225T000000Z
DTSTART;VALUE=DATE:20231225
DTEND;VALUE=DATE:20231226
DTSTAMP:20231225T000000Z
UID:20231225T000000-001@example.com
SUMMARY:Christmas Day
END:VEVENT
END:VCALENDAR

View File

@ -11,6 +11,5 @@ DTEND:20241003T200000Z
DTSTAMP:20241119T115316Z
UID:6tbrvsitniuu72li7kk15gou2b@google.com
SUMMARY:progodessey
SEQUENCE:0
END:VEVENT
END:VCALENDAR

View File

@ -25,9 +25,8 @@ DTSTART;TZID=Europe/Berlin:20241120T211500
DTEND;TZID=Europe/Berlin:20241120T215000
DTSTAMP:20241113T212909Z
UID:5f4ad965-16a8-48eb-8233-78bf93a8b35e
SUMMARY:JR Weekly Check-In
LOCATION:FaceTime
RRULE:FREQ=WEEKLY;BYDAY=WE
SUMMARY:JR Weekly Check-In
SEQUENCE:0
END:VEVENT
END:VCALENDAR