-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
134 lines (116 loc) · 4.35 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
const cheerio = require("cheerio");
const Log = require("logger");
const NodeHelper = require("node_helper");
module.exports = NodeHelper.create({
async socketNotificationReceived (notification, payload) {
switch (notification) {
case "MMM-FORUM-LOGIN":
this.config = payload;
await this.loginAndFetchData();
break;
}
},
async loginAndFetchData () {
Log.debug(`[${this.name}] Trying to log in and retrieve session cookie.`);
try {
const loginPageResponse = await fetch(`${this.config.baseUrl}login`);
const loginPageHtml = await loginPageResponse.text();
const cheerioInstance = cheerio.load(loginPageHtml);
const csrfToken = cheerioInstance("input[name=\"_csrf\"]").val();
const loginResponse = await fetch(`${this.config.baseUrl}login`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Cookie: loginPageResponse.headers.get("set-cookie")
},
body: JSON.stringify({
username: this.config.username,
password: this.config.password,
_csrf: csrfToken
})
});
this.loginSetCookieHeader = loginResponse.headers.get("set-cookie");
} catch (error) {
Log.error(`[${this.name}] Error while logging in and retrieving session cookie: ${error}`);
this.sendSocketNotification("MMM-FORUM_ERROR");
return;
}
if (this.loginSetCookieHeader) {
Log.debug(`[${this.name}] Successfully logged in and retrieved session cookie.`);
// Initial fetch
await this.fetchData();
// Set interval for subsequent fetches
setInterval(() => this.fetchData(), this.config.apiRequestInterval);
} else {
Log.error(`[${this.name}] Error while getting session cookie.`);
this.sendSocketNotification("MMM-FORUM_ERROR");
}
},
async fetchData () {
if (this.config.maxUnreadTopics > 0) {
await this.getUnreadTopics();
}
if (this.config.maxUnreadNotifications > 0) {
await this.getUnreadNotifications();
}
if (this.config.maxUnreadMessages > 0) {
await this.getUnreadMessages();
}
},
async getUnreadTopics () {
Log.debug(`[${this.name}] Fetching unread topics.`);
const apiResponse = await fetch(`${this.config.baseUrl}api/unread`, {
method: "GET",
headers: {
Cookie: this.loginSetCookieHeader
}
});
const data = await apiResponse.json();
if (!data || !data.topics) {
Log.error(`[${this.name}] Error while fetching unread topics: ${apiResponse.statusText}`);
this.sendSocketNotification("MMM-FORUM_ERROR");
} else {
Log.debug(`[${this.name}] Successfully fetched unread topics.`);
this.sendSocketNotification("MMM-FORUM_UNREAD_TOPICS", data.topics);
}
},
async getUnreadNotifications () {
Log.debug(`[${this.name}] Fetching unread notifications.`);
const apiResponse = await fetch(`${this.config.baseUrl}api/notifications`, {
method: "GET",
headers: {
Cookie: this.loginSetCookieHeader
}
});
const data = await apiResponse.json();
if (!data || !data.notifications) {
Log.error(`[${this.name}] Error while fetching unread notifications: ${apiResponse.statusText}`);
this.sendSocketNotification("MMM-FORUM_ERROR");
} else {
Log.debug(`[${this.name}] Successfully fetched unread notifications.`);
this.sendSocketNotification("MMM-FORUM_UNREAD_NOTIFICATIONS", data.notifications);
}
},
async getUnreadMessages () {
Log.debug(`[${this.name}] Fetching unread messages.`);
try {
const apiResponse = await fetch(`${this.config.baseUrl}api/user/${this.config.username.toLowerCase()}/chats`, {
method: "GET",
headers: {
Cookie: this.loginSetCookieHeader
}
});
const data = await apiResponse.json();
if (!data || !data.rooms) {
Log.error(`[${this.name}] Error while fetching unread messages: ${apiResponse.statusText}`);
this.sendSocketNotification("MMM-FORUM_ERROR");
} else {
Log.debug(`[${this.name}] Successfully fetched unread messages.`);
this.sendSocketNotification("MMM-FORUM_UNREAD_MESSAGES", data.rooms);
}
} catch (error) {
Log.error(`[${this.name}] Error while fetching unread messages: ${error}`);
this.sendSocketNotification("MMM-FORUM_ERROR");
}
}
});