This repository has been archived by the owner on Jan 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.js
executable file
·73 lines (60 loc) · 1.8 KB
/
__main__.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const { ICalParser, VCalendar, VEvent } = require('cozy-ical')
const nodeFetch = require('node-fetch')
const uuid = require('uuid/v4')
async function parseIcsData(icsData) {
const parser = new ICalParser()
return new Promise((resolve, reject) => {
parser.parseString(icsData, (err, cal) => {
if (err) {
return reject(err)
}
resolve(cal)
})
})
}
async function fetch(url) {
return nodeFetch(url).then(res => res.text())
}
function decorateEventWithAlarm(event) {
const uid = event.getTextFieldValue('UID') || uuid()
const alarmFields = [
{k: 'BEGIN', v: 'VALARM'},
{k: 'TRIGGER', v: '-PT30M'},
{k: 'REPEAT', v: '1'},
{k: 'DURATION', v: 'PT15M'},
{k: 'DESCRIPTION', v: event.model.summary},
{k: 'ACTION', v: 'DISPLAY'},
{k: 'UID', v: uid},
{k: 'X-WR-ALARMUID', v: uid},
{k: 'END', v: 'VALARM'}
]
for (const { k, v } of alarmFields) {
event.addRawField(k, v)
}
return event
}
function buildCalendar(originalCalendar) {
const calendar = new VCalendar(originalCalendar.model)
for (const originalEvent of originalCalendar.subComponents) {
// ... doesn't work
const url = originalEvent.getTextFieldValue('URL') || ''
originalEvent.model.description = `${url}\n\n${originalEvent.model.description}`
const event = new VEvent(originalEvent.model)
const eventWithAlarm = decorateEventWithAlarm(event)
calendar.add(eventWithAlarm)
}
return calendar.toString()
}
/**
* https://tools.ietf.org/html/rfc5545
*
* @param {string} icsCalendarUrl
* @returns {buffer}
*/
module.exports = async (icsCalendarUrl = '') => {
const icsData = await fetch(icsCalendarUrl)
const calendar = await parseIcsData(icsData)
return new Buffer(buildCalendar(calendar), {
'Content-Type': 'text/calendar; charset=utf-8'
})
}