Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use ical lib in view ics #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions plugins/view-ics/ical.es5.min.cjs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions plugins/view-ics/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function Init() : void
{
// $this->UseLangs(true);
$this->addJs('message.js');
$this->addJs('ical.es5.min.cjs');
$this->addJs('windowsZones.js');
}
}
64 changes: 19 additions & 45 deletions plugins/view-ics/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@
<details data-bind="if: viewICS, visible: viewICS">
<summary data-icon="📅" data-bind="text: viewICS().SUMMARY"></summary>
<table><tbody style="white-space:pre">
<tr data-bind="visible: viewICS().ORGANIZER"><td>Organizer</td><td data-bind="text: viewICS().ORGANIZER"></td></tr>
<tr><td>Start</td><td data-bind="text: viewICS().DTSTART"></td></tr>
<tr><td>End</td><td data-bind="text: viewICS().DTEND"></td></tr>
<tr data-bind="visible: viewICS().ORGANIZER"><td>Organizer: </td><td data-bind="text: viewICS().ORGANIZER"></td></tr>
<tr><td>Start: </td><td data-bind="text: viewICS().DTSTART"></td></tr>
<tr><td>End: </td><td data-bind="text: viewICS().DTEND"></td></tr>
<tr><td>Location</td><td data-bind="text: viewICS().TRANSP"></td></tr>
<!-- <tr><td>Transparency</td><td data-bind="text: viewICS().TRANSP"></td></tr>-->
<tr data-bind="foreach: viewICS().ATTENDEE">
<td></td><td data-bind="text: $data.replace(/;/g,';\\n')"></td>
Expand Down Expand Up @@ -88,55 +89,28 @@
});
// ICS attachment
// let ics = msg.attachments.find(attachment => 'application/ics' == attachment.mimeType);

let ics = msg.attachments.find(attachment => 'text/calendar' == attachment.mimeType);
if (ics && ics.download) {

// fetch it and parse the VEVENT
rl.fetch(ics.linkDownload())
.then(response => (response.status < 400) ? response.text() : Promise.reject(new Error({ response })))
.then(text => {
let VEVENT,
VALARM,
multiple = ['ATTACH','ATTENDEE','CATEGORIES','COMMENT','CONTACT','EXDATE',
'EXRULE','RSTATUS','RELATED','RESOURCES','RDATE','RRULE'],
lines = text.split(/\r?\n/),
i = lines.length;
while (i--) {
let line = lines[i];
if (VEVENT) {
while (line.startsWith(' ') && i--) {
line = lines[i] + line.slice(1);
}
if (line.startsWith('END:VALARM')) {
VALARM = {};
continue;
} else if (line.startsWith('BEGIN:VALARM')) {
VEVENT.VALARM || (VEVENT.VALARM = []);
VEVENT.VALARM.push(VALARM);
VALARM = null;
continue;
} else if (line.startsWith('BEGIN:VEVENT')) {
break;
}
line = line.match(/^([^:;]+)[:;](.+)$/);
if (line) {
if (VALARM) {
VALARM[line[1]] = line[2];
} else if (multiple.includes(line[1]) || 'X-' == line[1].slice(0,2)) {
VEVENT[line[1]] || (VEVENT[line[1]] = []);
VEVENT[line[1]].push(line[2]);
} else {
if ('DTSTART' === line[1] || 'DTEND' === line[1]) {
line[2] = parseDate(line[2]);
}
VEVENT[line[1]] = line[2];
}
}
} else if (line.startsWith('END:VEVENT')) {
VEVENT = {};
}
console.log(ICAL.parse(text));
Copy link
Collaborator

@avinash-0007 avinash-0007 Sep 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove console.log if not needed

let jcalData = ICAL.parse(text)
var comp = new ICAL.Component(jcalData);
var vevent = comp.getFirstSubcomponent("vevent");
var event = new ICAL.Event(vevent);
let VEVENT = {};
VEVENT.SUMMARY = event.summary
VEVENT.DTSTART = parseDate(vevent.getFirstPropertyValue("dtstart"));
VEVENT.DTEND = parseDate(vevent.getFirstPropertyValue("dtend"));
VEVENT.ATTENDEE = []
for(let attendee of event.attendees){
VEVENT.ATTENDEE.push(attendee.getFirstParameter("cn"));
}
// METHOD:REPLY || METHOD:REQUEST
// console.dir({VEVENT:VEVENT});

if (VEVENT) {
VEVENT.rawText = text;
VEVENT.isCancelled = () => VEVENT.STATUS?.includes('CANCELLED');
Expand Down
Loading