-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnode_helper.js
executable file
·57 lines (50 loc) · 1.64 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
/* Magic Mirror
* Node Helper: MMM-NextCloud-Tasks
*
* By Jan Ryklikas
* MIT Licensed.
*/
var NodeHelper = require("node_helper");
const { AuthType, createClient } = require("webdav");
const ical = require("node-ical");
const { transformData } = require("./transformer");
const { fetchList, parseList } = require("./webDavHelper");
module.exports = NodeHelper.create({
socketNotificationReceived: function(notification, payload) {
let self = this;
const moduleId = payload.id;
if (notification === "MMM-NextCloud-Tasks-UPDATE") {
self.getData(moduleId, payload.config, (payload) => {
self.sendData(moduleId, payload);
});
}
},
getData: async function(moduleId, config, callback) {
let self = this;
try {
const icsList = await fetchList(config);
const rawList = parseList(icsList);
const nestedList = transformData(rawList);
callback(nestedList);
} catch (error) {
console.error("WebDav", error);
if(error.status === 401) {
self.sendError(moduleId, "WebDav: Unauthorized!");
} else if(error.status === 404) {
self.sendError(moduleId, "WebDav: URL Not Found!");
} else {
self.sendError(moduleId, "WebDav: Unknown error!");
self.sendLog(moduleId, ["WebDav: Unknown error: ", error]);
}
}
},
sendData: function(moduleId, payload) {
this.sendSocketNotification("MMM-NextCloud-Tasks-Helper-TODOS#" + moduleId, payload);
},
sendLog: function(moduleId, payload) {
this.sendSocketNotification("MMM-NextCloud-Tasks-Helper-LOG#" + moduleId, payload);
},
sendError: function(moduleId, payload) {
this.sendSocketNotification("MMM-NextCloud-Tasks-Helper-ERROR#" + moduleId, payload);
}
});