-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
62 lines (52 loc) · 2.18 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
var NodeHelper = require('node_helper');
var axios = require('axios');
var notifications = [];
module.exports = NodeHelper.create({
start: function () {
console.log('MMM-GitHub-Notifications helper started...');
},
getNotifications: function (config) {
var headers = {
headers: {
Accept: 'application/vnd.github+json',
Authorization: 'Bearer ' + config.authToken
}
}
axios
.get('https://api.github.com/notifications?participating=true&per_page=' + Math.min(config.maxNotifications, 20), headers)
.then((response) => {
notifications = [];
response.data.forEach(element => {
notifications.push({
id: element.id,
title: element.subject.title,
type: element.subject.type,
reason: element.reason,
repository: element.repository.name,
date: element.updated_at
});
});
this.sendSocketNotification('MMM-GitHub-Notifications_RESULT', { data: notifications });
})
.catch((exc) => console.error('[MMM-GitHub-Notifications] Error occurred : ' + exc));
},
markNotificationAsRead: function (config, notificationId) {
var headers = {
headers: {
Accept: 'application/vnd.github+json',
Authorization: 'Bearer ' + config.authToken
}
}
axios
.patch('https://api.github.com/notifications/threads/' + notificationId, null, headers)
.then((response) => this.getNotifications(config))
.catch((exc) => console.error('[MMM-GitHub-Notifications] Error occurred : ' + exc));
},
socketNotificationReceived: function (notification, payload) {
if (notification === 'MMM-GitHub-Notifications_GET') {
this.getNotifications(payload.config);
} else if (notification === 'MMM-GitHub-Notifications_READ') {
this.markNotificationAsRead(payload.config, payload.notificationId);
}
}
});