forked from jgauth/MMM-GoogleTasks
-
Notifications
You must be signed in to change notification settings - Fork 12
/
node_helper.js
91 lines (75 loc) · 2.96 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
var NodeHelper = require("node_helper");
const {google} = require('googleapis');
const fs = require('fs');
module.exports = NodeHelper.create({
start: function() {
console.log("Starting node helper for: " + this.name);
this.oAuth2Client;
this.service;
},
socketNotificationReceived: function(notification, payload) {
if (notification === "MODULE_READY") {
if(!this.service) {
this.authenticate();
} else {
// Check if tasks service is already running, avoids running authentication twice
console.log("TASKS SERVICE ALREADY RUNNING, DONT NEED TO AUTHENTICATE AGAIN")
this.sendSocketNotification("SERVICE_READY", {});
}
} else if (notification === "REQUEST_UPDATE") {
this.getList(payload);
}
},
authenticate: function() {
var self = this;
fs.readFile(self.path + '/credentials.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Google Tasks API.
authorize(JSON.parse(content), self.startTasksService);
});
function authorize(credentials, callback) {
const {client_secret, client_id, redirect_uris} = credentials.installed;
self.oAuth2Client = new google.auth.OAuth2(
client_id, client_secret, redirect_uris[0]);
// Check if we have previously stored a token.
fs.readFile(self.path + '/token.json', (err, token) => {
if (err) return console.log('Error loading token');
self.oAuth2Client.setCredentials(JSON.parse(token));
callback(self.oAuth2Client, self);
});
}
},
startTasksService: function(auth, self) {
self.service = google.tasks({version: 'v1', auth});
self.sendSocketNotification("SERVICE_READY", {});
},
getList: function(config) {
var self = this;
if(!self.service) {
console.log("Refresh required");
return;
}
self.service.tasks.list({
tasklist: config.listID,
maxResults: config.maxResults,
showCompleted: config.showCompleted,
showHidden: config.showHidden,
}, (err, res) => {
if (err) return console.error('The API returned an error: ' + err);
// Testing
/*
const tasksList = res.data.items;
console.log(tasksList);
if (tasksList) {
tasksList.forEach((task) => {
console.log(task);
});
} else {
console.log('No tasks found.');
}
*/
var payload = {id: config.listID, items: res.data.items};
self.sendSocketNotification("UPDATE_DATA", payload);
});
},
});