forked from MMM-CalendarExt2/MMM-CalendarExt2
/
node_helper.js
277 lines (253 loc) · 8.55 KB
/
node_helper.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
const fs = require("fs")
const path = require("path")
const validUrl = require("valid-url")
const request = require("request")
const moment = require("moment-timezone")
const ICAL = require("ical.js")
const IcalExpander = require('ical-expander')
var NodeHelper = require('node_helper')
module.exports = NodeHelper.create({
start: function() {
this.config = {}
this.calendars = {}
this.calendarEvents = {}
},
stop: function() {
},
socketNotificationReceived: function (noti, payload) {
switch(noti) {
case "START":
this.work(payload)
break
}
},
work: function(config) {
this.config = config
this.calendars = this.config.calendars
if (this.config.locale) {
moment.locale(this.config.locale)
}
this.startScanCalendars()
},
startScanCalendars: function() {
for(i = 0; i < this.calendars.length; i++) {
this.scanCalendar(
this.calendars[i],
(calendar, icalData=null, error=null)=>{
this.parser(calendar, icalData, error)
}
)
}
},
scanCalendar: function(calendar, cb) {
console.log(`[CALEXT2] calendar:${calendar.name} >> Scanning start with interval:${calendar.scanInterval}`)
var opts = null
var nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1])
opts = {
"headers" : {
"User-Agent":
"Mozilla/5.0 (Node.js "
+ nodeVersion + ") MagicMirror/"
+ global.version
+ " (https://github.com/MichMich/MagicMirror/)",
},
"gzip": true
}
if (calendar.auth && Object.keys(calendar.auth).length > 0) {
if(calendar.auth.method === "bearer"){
opts.auth = {
bearer: calendar.auth.pass
}
} else {
opts.auth = {
user: calendar.auth.user,
pass: calendar.auth.pass
}
if(calendar.auth.method === "digest"){
opts.auth.sendImmediately = 0
}else{
opts.auth.sendImmediately = 1
}
}
}
var url = calendar.url
url = url.replace("webcal://", "http://")
request(url, opts, (e, r, data)=>{
if (e) {
cb(calendar, null, e)
} else {
cb(calendar, data, e)
}
setTimeout(()=>{
this.scanCalendar(calendar, cb)
}, calendar.scanInterval)
})
},
parser: function(calendar, iCalData=null, error=null) {
if (error) {
console.log(`[CALEXT2] calendar:${calendar.name} >> ${error.message}`)
return
}
if (!iCalData) {
console.log(`[CALEXT2] calendar:${calendar.name} >> No data to fetch`)
return
}
var icalExpander
try {
icalExpander = new IcalExpander({ics:iCalData, maxIterations: calendar.maxIterations})
} catch (e) {
console.log(`[CALEXT2] calendar:${calendar.name} >> ${e.message}`)
return
}
var events
try {
events = icalExpander.between(
moment().subtract(calendar.beforeDays, "days").startOf('day').toDate(),
moment().add(calendar.afterDays, "days").endOf('day').toDate()
)
} catch (e) {
console.log(`[CALEXT2] calendar:${calendar.name} >> ${e.message}`)
return
}
var wholeEvents = [].concat(events.events, events.occurrences)
var eventPool = []
for (i in wholeEvents) {
var item = wholeEvents[i]
var ri = (item.hasOwnProperty("item")) ? item.item : item
var ev = {}
ev.calendarId = calendar.uid
ev.location = ri.location
ev.description = ri.description
ev.title = ri.summary
ev.isRecurring = ri.isRecurring()
if (Array.isArray(calendar.replaceTitle) && calendar.replaceTitle.length > 0) {
for (let j = 0; j < calendar.replaceTitle.length; j++) {
var rt = calendar.replaceTitle[j]
var re = (rt[0] instanceof RegExp) ? rt[0] : new RegExp(rt[0], "g")
var rto = (rt[1]) ? rt[1] : ""
ev.title = ev.title.replace(re, rto)
}
}
var startDate, endDate
if (calendar.forceLocalTZ) {
var ts = item.startDate.toJSON()
ts.month -= 1
var te = item.endDate.toJSON()
te.month -= 1
startDate = moment(ts)
endDate = moment(te)
} else {
startDate = moment(item.startDate.toJSDate())
endDate = moment(item.endDate.toJSDate())
}
ev.startDate = startDate.format("X")
ev.endDate = endDate.format("X")
ev.startDateJ = startDate.toJSON()
ev.endDateJ = endDate.toJSON()
ev.duration = ri.duration.toSeconds()
ev.isMoment = (ev.duration == 0) ? true : false
ev.isPassed = (endDate.isBefore(moment())) ? true : false
if (ev.duration <= 86400) {
if (startDate.format("YYMMDD") == endDate.format("YYMMDD")) {
ev.isOneday = true
} else {
if (endDate.format("HHmmss") == "000000") {
ev.isOneday = true
}
}
}
ev.className = calendar.className
ev.icon = calendar.icon
var isFullday = (startDate.format('HHmmss') == "000000" && endDate.format('HHmmss') == "000000") ? true : false
ev.isFullday = isFullday
if (isFullday) {
}
// import the Microsoft property X-MICROSOFT-CDO-BUSYSTATUS, fall back to "BUSY" in case none was found
// possible values are 'FREE'|'TENTATIVE'|'BUSY'|'OOF' acording to
// https://docs.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxcical/cd68eae7-ed65-4dd3-8ea7-ad585c76c736
ev.ms_busystatus = ri.component.getFirstPropertyValue('x-microsoft-cdo-busystatus') || 'BUSY'
ev.uid = (ri.uid)
? calendar.uid + ":" + ev.startDate + ":" + ev.endDate + ":" + ri.uid
: calendar.uid + ":" + ev.startDate + ":" + ev.endDate + ":" + ev.title
ev.calendarName = calendar.name
if (calendar.filter) {
var f = JSON.parse(calendar.filter).filter
var filter = Function("return " + f.toString())
var r = filter(ev)
if (r(ev)) {
eventPool.push(ev)
} else {
// do nothing
}
} else {
eventPool.push(ev)
}
}
eventPool.slice(calendar.maxItems)
console.log(`[CALEXT2] calendar:${calendar.name} >> Scanned: ${wholeEvents.length}, Selected: ${eventPool.length}`)
this.mergeEvents(eventPool, calendar.uid)
},
mergeEvents: function(eventPool, calendarId) {
this.calendarEvents[calendarId] = eventPool
var events = []
for (i in Object.keys(this.calendarEvents)) {
if (this.calendarEvents.hasOwnProperty(i)) {
var cal = this.calendarEvents[i]
events = events.concat(cal)
}
}
// only run sorting and deduplication is the user actually wants it
if(Array.isArray(this.config.deduplicateEventsOn) && this.config.deduplicateEventsOn.length > 0){
// copied from https://stackoverflow.com/a/34853778
var spaceship = (val1, val2) => {
if ((val1 === null || val2 === null) || (typeof val1 != typeof val2)) {
return null;
}
if (typeof val1 === 'string') {
return (val1).localeCompare(val2)
}
else {
if (val1 > val2) { return 1 }
else if (val1 < val2) { return -1 }
return 0
}
}
var compare_them = (a,b) => {
for (let property of this.config.deduplicateEventsOn) {
var comparison_result = spaceship(
a[property], b[property]
)
// if the comparison has found an order change
// immediately return to not waste more cycles
if( comparison_result !== null && comparison_result !== 0 ){
return comparison_result
}
}
// if the order hasn't been changed, these two events must be identical
return 0
}
// first sort all events by the properties they should be deduplicated on
events.sort(compare_them)
// now do the actual deduplication
events = events.filter((event,eventIndex) => {
if( eventIndex === 0 ){
return true
}
// use the comparison again, but now events where the immediate
// predecessor is identical will be removed
var old_event = events[eventIndex-1]
if( compare_them(old_event, event) === 0 ){
// as most typically the duplicate event comes from another calendar merge the two calendarNames
old_event.calendarName += '|'+event.calendarName
// now we can exclude this event
return false
}
// finally keep all other events
return true
})
}
if (events.length > 0) {
this.sendSocketNotification("EVENTS_REFRESHED", events)
}
}
})