n8n-nodes-nextcloud-calendar
Version:
n8n Node für die Integration mit Nextcloud Calendar
219 lines • 9.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseICalEvent = exports.parseEventResults = void 0;
function parseICalDate(date) {
const year = parseInt(date.substr(0, 4));
const month = parseInt(date.substr(4, 2)) - 1;
const day = parseInt(date.substr(6, 2));
if (date.includes('T')) {
const hour = parseInt(date.substr(9, 2));
const minute = parseInt(date.substr(11, 2));
const second = parseInt(date.substr(13, 2));
return new Date(Date.UTC(year, month, day, hour, minute, second));
}
return new Date(Date.UTC(year, month, day));
}
function parseICS(icsData) {
const lines = icsData.split('\n').map(line => line.trim());
const event = {};
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.startsWith('BEGIN:VEVENT')) {
continue;
}
if (line.startsWith('END:VEVENT')) {
break;
}
const [key, ...values] = line.split(':');
const value = values.join(':');
switch (key) {
case 'UID':
event.uid = value;
break;
case 'SUMMARY':
event.summary = value;
break;
case 'DESCRIPTION':
event.description = value;
break;
case 'LOCATION':
event.location = value;
break;
case 'DTSTART':
event.start = parseICalDate(value);
break;
case 'DTEND':
event.end = parseICalDate(value);
break;
case 'CREATED':
event.created = parseICalDate(value);
break;
case 'LAST-MODIFIED':
event.lastmodified = parseICalDate(value);
break;
case 'STATUS':
event.status = value;
break;
case 'ATTENDEE':
if (!event.attendee) {
event.attendee = [];
}
const [params, email] = value.split('mailto:');
const attendee = {
val: `mailto:${email}`,
params: {}
};
if (params) {
params.split(';').forEach((param) => {
const [paramKey, paramValue] = param.split('=');
if (paramKey && paramValue) {
attendee.params[paramKey] = paramValue;
}
});
}
event.attendee.push(attendee);
break;
case 'ORGANIZER':
const [orgParams, orgEmail] = value.split('mailto:');
event.organizer = {
val: `mailto:${orgEmail}`,
params: {}
};
if (orgParams) {
if (event.organizer && event.organizer.params) {
orgParams.split(';').forEach((param) => {
const [paramKey, paramValue] = param.split('=');
if (paramKey && paramValue) {
if (event.organizer) {
event.organizer.params[paramKey] = paramValue;
}
}
});
}
}
break;
}
}
return { vevent: event };
}
function parseEventResults(events) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
const eventResults = [];
for (const event of events) {
const eventData = 'data' in event
? event.data
: (_b = (_a = event.props) === null || _a === void 0 ? void 0 : _a['calendar-data']) === null || _b === void 0 ? void 0 : _b._text;
if (!eventData) {
continue;
}
const parsedData = parseICS(eventData);
const eventInfo = parsedData.vevent;
const attendees = [];
if (eventInfo.attendee) {
const attendeeList = Array.isArray(eventInfo.attendee)
? eventInfo.attendee
: eventInfo.attendee
? [eventInfo.attendee]
: [];
for (const attendee of attendeeList) {
const email = (_d = (_c = attendee === null || attendee === void 0 ? void 0 : attendee.val) === null || _c === void 0 ? void 0 : _c.replace('mailto:', '')) !== null && _d !== void 0 ? _d : '';
const params = (attendee === null || attendee === void 0 ? void 0 : attendee.params) || {};
attendees.push({
email,
displayName: params.CN,
role: params.ROLE || 'REQ-PARTICIPANT',
rsvp: params.RSVP === 'TRUE',
status: params.PARTSTAT || 'NEEDS-ACTION',
});
}
}
let organizer = { email: '', displayName: '' };
if (eventInfo.organizer && eventInfo.organizer.val) {
organizer = {
email: eventInfo.organizer.val.replace('mailto:', ''),
displayName: (_f = (_e = eventInfo.organizer.params) === null || _e === void 0 ? void 0 : _e.CN) !== null && _f !== void 0 ? _f : '',
};
}
const url = 'url' in event
? event.url
: (_g = event.href) !== null && _g !== void 0 ? _g : '';
const etag = 'etag' in event
? event.etag
: (_k = (_j = (_h = event.props) === null || _h === void 0 ? void 0 : _h.getetag) === null || _j === void 0 ? void 0 : _j._text) !== null && _k !== void 0 ? _k : '';
eventResults.push({
url: url || '',
etag: etag || '',
uid: (_l = eventInfo.uid) !== null && _l !== void 0 ? _l : '',
title: (_m = eventInfo.summary) !== null && _m !== void 0 ? _m : '',
start: (_o = eventInfo.start) === null || _o === void 0 ? void 0 : _o.toISOString(),
end: (_p = eventInfo.end) === null || _p === void 0 ? void 0 : _p.toISOString(),
description: (_q = eventInfo.description) !== null && _q !== void 0 ? _q : '',
location: (_r = eventInfo.location) !== null && _r !== void 0 ? _r : '',
created: eventInfo.created,
lastModified: eventInfo.lastmodified,
status: eventInfo.status,
attendees,
organizer,
});
}
return eventResults.sort((a, b) => {
var _a, _b;
const startA = (_a = a === null || a === void 0 ? void 0 : a.start) !== null && _a !== void 0 ? _a : '';
const startB = (_b = b === null || b === void 0 ? void 0 : b.start) !== null && _b !== void 0 ? _b : '';
if (startA < startB) {
return -1;
}
else if (startA > startB) {
return 1;
}
else {
return 0;
}
});
}
exports.parseEventResults = parseEventResults;
function parseICalEvent(calendarObject) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
const icalData = parseICS(calendarObject.data);
const event = icalData.vevent;
const response = {
uid: (_a = event.uid) !== null && _a !== void 0 ? _a : '',
title: (_b = event.summary) !== null && _b !== void 0 ? _b : '',
start: (_c = event.start) === null || _c === void 0 ? void 0 : _c.toISOString(),
end: (_d = event.end) === null || _d === void 0 ? void 0 : _d.toISOString(),
description: (_e = event.description) !== null && _e !== void 0 ? _e : '',
location: (_f = event.location) !== null && _f !== void 0 ? _f : '',
url: (_g = calendarObject.url) !== null && _g !== void 0 ? _g : '',
etag: (_h = calendarObject.etag) !== null && _h !== void 0 ? _h : '',
created: event.created,
lastModified: event.lastmodified,
status: event.status,
organizer: event.organizer ? {
email: event.organizer.val.replace('mailto:', ''),
displayName: (_j = event.organizer.params) === null || _j === void 0 ? void 0 : _j.CN,
} : undefined,
attendees: !event.attendee
? undefined
: Array.isArray(event.attendee)
? event.attendee.map((a) => {
var _a, _b, _c, _d, _e, _f;
return ({
email: a.val.replace('mailto:', ''),
displayName: (_a = a.params) === null || _a === void 0 ? void 0 : _a.CN,
role: (_c = (_b = a.params) === null || _b === void 0 ? void 0 : _b.ROLE) !== null && _c !== void 0 ? _c : 'REQ-PARTICIPANT',
status: (_e = (_d = a.params) === null || _d === void 0 ? void 0 : _d.PARTSTAT) !== null && _e !== void 0 ? _e : 'NEEDS-ACTION',
rsvp: ((_f = a.params) === null || _f === void 0 ? void 0 : _f.RSVP) === 'TRUE',
});
})
: [{
email: event.attendee.val.replace('mailto:', ''),
displayName: (_k = event.attendee.params) === null || _k === void 0 ? void 0 : _k.CN,
role: (_m = (_l = event.attendee.params) === null || _l === void 0 ? void 0 : _l.ROLE) !== null && _m !== void 0 ? _m : 'REQ-PARTICIPANT',
status: (_p = (_o = event.attendee.params) === null || _o === void 0 ? void 0 : _o.PARTSTAT) !== null && _p !== void 0 ? _p : 'NEEDS-ACTION',
rsvp: ((_q = event.attendee.params) === null || _q === void 0 ? void 0 : _q.RSVP) === 'TRUE',
}],
};
return response;
}
exports.parseICalEvent = parseICalEvent;
//# sourceMappingURL=parser.js.map