-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
181 lines (170 loc) · 5.08 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
"use strict";
/* Magic Mirror
* Module: MMM-Todoist-Multi
*
* By Amos Glenn
*
* MIT Licensed.
*/
const NodeHelper = require("node_helper");
const fetch = require("node-fetch");
const { URLSearchParams } = require("url");
const Log = require("logger");
const TasksFetcher = require("./tasksfetcher.js");
module.exports = NodeHelper.create({
start: function () {
console.log("Starting node helper for: " + this.name);
this.fetchers = [];
},
socketNotificationReceived: function (notification, payload) {
if (notification === "ADD_TODOIST_FETCHER") {
//create a fetcher too collect Todoist tasks for a module and accessToken
this.createTodoistFetcher(
payload.url,
payload.reloadInterval,
payload.accessToken,
payload.resourceTypes,
payload.identifier
);
} /* else if (notification === "FETCH_TODOIST") {
this.fetchTodoistData(
payload.apiBase,
payload.apiVersion,
payload.todoistEndpoint,
payload.accessToken,
payload.todoistResourceType
);
} */
if (notification === "FETCH_TASKS") {
if (
typeof this.fetchers[payload.identifier + payload.accessToken] ===
"undefined"
) {
Log.error(
"Task fetching error. No fetcher exists for module: " +
payload.identifier +
" / accessToken: " +
payload.accessToken
);
this.sendSocketNotification("TASK_ERROR", {
error_type: "TASK_FETCHER_MISSING"
});
return;
}
this.fetchers[payload.identifier + payload.accessToken].startFetch();
}
},
/**
* Create a fetcher for a new combination of module is and Todoist Access Token
* Otherwise use the existing fetcher
*
* @param {string} url The url of the Todoist API to use when fetching
* @param {number} reloadInterval Time in ms to wait until fetching tasks again
* @param {string} accessToken API token from the Todoist Web app, at Todoist Settings -> Integrations -> API token
* @param {string[]} resourceTypes Array of strings indicating which Todoist resource types to fetch
* @param {string} identifier ID of the module
*
**/
createTodoistFetcher: function (
url,
reloadInterval,
accessToken,
resourceTypes,
identifier
) {
let fetcher;
if (typeof this.fetchers[identifier + accessToken] === "undefined") {
Log.log(
"Creating new taskFetcher for module: " +
identifier +
" and accessToken: " +
accessToken
);
fetcher = new TasksFetcher(
url,
accessToken,
resourceTypes,
reloadInterval
);
fetcher.onReceive((fetcher) => {
this.broadcastTasks(fetcher, identifier);
});
fetcher.onError((fetcher, error) => {
Log.error(
"Task fetcher error. Could not collect tasks: ",
fetcher.accessCode,
error
);
let error_type = NodeHelper.checkFetchError(error);
this.sendSocketNotification("FETCH_ERROR", {
id: identifier,
error_type
});
});
this.fetchers[identifier + accessToken] = fetcher;
} else {
Log.log(
"Using existing task fetcher for module: " +
identifier +
" with accessToken: " +
accessToken
);
fetcher = this.fetchers[identifier + accessToken];
fetcher.broadcastTasks();
}
fetcher.startFetch();
},
broadcastTasks: function (fetcher, identifier) {
this.sendSocketNotification("TODOIST_TASKS", {
id: identifier,
accessToken: fetcher.accessToken(),
tasks: fetcher.tasks()
});
}
/*
fetchTodoistData: async function (
apiBase,
apiVersion,
apiEndpoint,
accessCode,
resourceTypes
) {
let todoistUrl = apiBase + "/" + apiVersion + "/" + apiEndpoint + "/";
const fetchParams = new URLSearchParams({
sync_token: "*",
resource_types: resourceTypes
});
let fetchOptions = {
method: "POST",
body: fetchParams,
cache: "no-cache",
headers: {
"content-type": "application/x-www-form-urlencoded",
Authorization: "Bearer " + accessCode
}
};
//TODO: check to see if adding html to each task is important (since the synch function did, not sure why)
// items.forEach((item) => {item.contentHtml = markdown.makeHtml(item.content);
// previous sync function also added the access token to config, not sure why checking that is important
fetch(todoistUrl, fetchOptions)
.then(this.checkStatus)
.then((response) => response.json())
.then((json) => {
this.sendSocketNotification("TASKS", json);
})
.catch((err) =>
this.sendSocketNotification("FETCH_ERROR", {
error: "Error fetching Todoist data: " + err
})
);
},
checkStatus: function (response) {
if (response.ok) {
return response;
} else {
this.sendSocketNotification("FETCH_ERROR", {
error: "ERROR! response status is '" + response.statusText + "'"
});
}
} */
});