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

Display ICS preview using an ICS library #1745

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 20 additions & 0 deletions plugins/ics-viewer/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2022 SnappyMail

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5 changes: 5 additions & 0 deletions plugins/ics-viewer/ical.es5.min.cjs

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions plugins/ics-viewer/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

class ICSViewerPlugin extends \RainLoop\Plugins\AbstractPlugin
{
const
NAME = 'ICS Viewer',
VERSION = '1',
RELEASE = '2024-09-17',
CATEGORY = 'Messages',
DESCRIPTION = 'Display ICS attachment using ical lib, or JSON-LD details, based on viewICS',
REQUIRED = '2.34.0';

public function Init() : void
{
// $this->UseLangs(true);
$this->addJs('message.js');
$this->addJs('ical.es5.min.cjs');
$this->addJs('windowsZones.js');
}
}
136 changes: 136 additions & 0 deletions plugins/ics-viewer/message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
(rl => {
const templateId = 'MailMessageView';

addEventListener('rl-view-model.create', e => {
if (templateId === e.detail.viewModelTemplateID) {

const
template = document.getElementById(templateId),
view = e.detail,
attachmentsPlace = template.content.querySelector('.attachmentsPlace'),
dateRegEx = /(TZID=(?<tz>[^:]+):)?(?<year>[0-9]{4})(?<month>[0-9]{2})(?<day>[0-9]{2})T(?<hour>[0-9]{2})(?<minute>[0-9]{2})(?<second>[0-9]{2})(?<utc>Z?)/,
parseDate = str => {
let parts = dateRegEx.exec(str)?.groups,
options = {dateStyle: 'long', timeStyle: 'short'},
date = (parts ? new Date(
parseInt(parts.year, 10),
parseInt(parts.month, 10) - 1,
parseInt(parts.day, 10),
parseInt(parts.hour, 10),
parseInt(parts.minute, 10),
parseInt(parts.second, 10)
) : new Date(str));
parts?.tz && (options.timeZone = windowsVTIMEZONEs[parts.tz] || parts.tz);
try {
return date.format(options);
} catch (e) {
console.error(e);
if (options.timeZone) {
options.timeZone = undefined;
return date.format(options);
}
}
};

attachmentsPlace.after(Element.fromHTML(`
<details data-bind="if: ICSViewer, visible: ICSViewer">
<summary data-icon="📅" data-bind="text: ICSViewer().SUMMARY"></summary>
<table><tbody style="white-space:pre">
<tr data-bind="visible: ICSViewer().ORGANIZER_TXT"><td>Organizer: </td><td><a data-bind="text: ICSViewer().ORGANIZER_TXT, attr: { href: ICSViewer().ORGANIZER_MAIL }"></a></td></tr>
<tr><td>Start: </td><td data-bind="text: ICSViewer().DTSTART"></td></tr>
<tr><td>End: </td><td data-bind="text: ICSViewer().DTEND"></td></tr>
<tr data-bind="visible: ICSViewer().LOCATION"><td>Location: </td><td data-bind="text: ICSViewer().LOCATION"></td></tr>
<!-- <tr><td>Transparency</td><td data-bind="text: ICSViewer().TRANSP"></td></tr>-->
<tr><td>Attendees: </td><td data-bind="foreach: ICSViewer().ATTENDEE"><span data-bind="text: $data.replace(/;/g,';\\n')"></span> </td>

</tbody></table>
</details>`));

view.ICSViewer = ko.observable(null);

view.saveICS = () => {
let VEVENT = view.VEVENT();
if (VEVENT) {
if (rl.nextcloud && VEVENT.rawText) {
rl.nextcloud.selectCalendar()
.then(href => href && rl.nextcloud.calendarPut(href, VEVENT));
} else {
// TODO
}
}
}

/**
* TODO
*/
view.message.subscribe(msg => {
view.ICSViewer(null);
if (msg) {
// JSON-LD after parsing HTML
// See http://schema.org/
msg.linkedData.subscribe(data => {
if (!view.ICSViewer()) {
data.forEach(item => {
if (item["ical:summary"]) {
let VEVENT = {
SUMMARY: item["ical:summary"],
DTSTART: parseDate(item["ical:dtstart"]),
// DTEND: parseDate(item["ical:dtend"]),
// TRANSP: item["ical:transp"],
// LOCATION: item["ical:location"],
ATTENDEE: []
}
view.ICSViewer(VEVENT);
return;
}
});
}
});
// 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 jcalData = ICAL.parse(text)
var comp = new ICAL.Component(jcalData);
var vevent = comp.getFirstSubcomponent("vevent");
var event = new ICAL.Event(vevent);
let VEVENT = {};
if(event.organizer.startsWith("mailto:")){
VEVENT.ORGANIZER_TXT=event.organizer.substr(7)
VEVENT.ORGANIZER_MAIL = event.organizer
} else
VEVENT.ORGANIZER_TXT=event.organizer
VEVENT.SUMMARY = event.summary;
VEVENT.DTSTART = parseDate(vevent.getFirstPropertyValue("dtstart"));
VEVENT.DTEND = parseDate(vevent.getFirstPropertyValue("dtend"));
VEVENT.LOCATION = event.location;
VEVENT.ATTENDEE = []
for(let attendee of event.attendees){
VEVENT.ATTENDEE.push(attendee.getFirstParameter("cn"));
}

if (VEVENT) {
VEVENT.rawText = text;
VEVENT.isCancelled = () => VEVENT.STATUS?.includes('CANCELLED');
VEVENT.isConfirmed = () => VEVENT.STATUS?.includes('CONFIRMED');
VEVENT.shouldReply = () => VEVENT.METHOD?.includes('REPLY');
console.dir({
isCancelled: VEVENT.isCancelled(),
shouldReply: VEVENT.shouldReply()
});
view.ICSViewer(VEVENT);
}
});
}
}
});
}
});

})(window.rl);
44 changes: 44 additions & 0 deletions plugins/ics-viewer/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

/**
* .SML-@type where @type is the value of the JSON-LD "@type":
* See http://schema.org/
*/

.SML-FlightReservation {
}

.SML-Airline {
}

.SML-Airport {
}

.SML-Flight {
}

.SML-FoodEstablishmentReservation {
}

.SML-FoodEstablishment {
}

.SML-ParcelDelivery {
}

.SML-Order {
}

.SML-Organization {
}

.SML-Product {
}

.SML-PostalAddress {
}

.SML-Person {
}

.SML-PromotionCard {
}
Loading