forked from funsocietyirc/MMM-Wunderlist-Enhanced
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnode_helper.js
More file actions
130 lines (109 loc) · 3.41 KB
/
node_helper.js
File metadata and controls
130 lines (109 loc) · 3.41 KB
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
"use strict";
/* Magic Mirror
* Module: MMM-Wunderlist
*
* By Paul-Vincent Roll http://paulvincentroll.com
* MIT Licensed.
*/
const NodeHelper = require("node_helper");
const Fetcher = require("./fetcher.js");
var anydo = require('anydo-api');
var moment = require('moment');
module.exports = NodeHelper.create({
start: function() {
this.config = [];
this.fetchers = {};
this.started = false;
},
getLists: function(callback) {
this.anydo.http.lists.all().done(function(lists) {
callback(lists);
}).fail(function(resp, code) {
console.error('there was a Wunderlist problem', code);
});
},
getUsers: function(callback) {
this.anydo.http.users.all().done(function(users) {
var ret = {};
users.forEach(function(user) {
ret[user.id] = user.name[0]
});
callback(ret);
}).fail(function(resp, code) {
console.error('there was a Wunderlist problem', code);
})
},
createFetcher: function(listID, list, reloadInterval) {
var fetcher;
if (typeof this.fetchers[listID] === "undefined") {
var self = this;
console.log("Create new todo fetcher for list: " + list + " - Interval: " + reloadInterval);
fetcher = new Fetcher(listID, list, reloadInterval, this.config.accessToken, this.config.clientID, this.config.showAssignee);
fetcher.onReceive(function(fetcher) {
self.broadcastTodos();
});
fetcher.onError(function(fetcher, error) {
self.sendSocketNotification("FETCH_ERROR", {
url: fetcher.id(),
error: error
});
});
this.fetchers[listID] = {
"name": list,
"instance": fetcher
};
} else {
console.log("Use exsisting todo fetcher for list: " + list);
fetcher = this.fetchers[listID].instance;
fetcher.setReloadInterval(reloadInterval);
fetcher.broadcastItems();
}
fetcher.startFetch();
},
broadcastTodos: function() {
var todos = {};
for (var f in this.fetchers) {
todos[this.fetchers[f].name] = this.fetchers[f].instance.items();
}
this.sendSocketNotification("TASKS", todos);
},
// Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
const self = this;
switch (notification) {
case 'CONFIG':
// We are already configured
if (this.started)
break;
// Grab Initial payload
this.config = payload;
// Create wunderlist API and fetch lists
this.anydo = new WunderlistSDK({accessToken: self.config.accessToken, clientID: self.config.clientID});
// Wait Until initialized
this.anydo.initialized.done(function() {
// Get Initial List
self.getLists(function(data) {
self.lists = data;
self.sendSocketNotification("STARTED");
});
self.started = true;
});
break;
case 'addLists':
self.lists.forEach(function(currentValue, key) {
if (self.config.lists.indexOf(currentValue.title) >= 0) {
self.createFetcher(currentValue.id, currentValue.title, self.config.interval * 1000);
}
});
break;
case 'CONNECTED':
self.broadcastTodos();
break;
case 'getUsers':
self.getUsers(function(data) {
self.sendSocketNotification('USERS', data)
});
break;
}
}
});