forked from lucianocosta/node-red-contrib-rocketchat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rocketchat-server.js
46 lines (39 loc) · 1.02 KB
/
rocketchat-server.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
const api = require('./rocketchat');
module.exports = function(RED) {
'use strict';
function RocketChatServer(n) {
RED.nodes.createNode(this, n);
this.name = n.name;
this.host = n.host;
this.user = n.user;
this.token = n.token;
}
RED.nodes.registerType('rocketchat-server', RocketChatServer, {
credentials: {
password: { type: 'password' }
}
});
RED.httpAdmin.get('/rocketchat-server/:id/spotlight/:search', RED.auth.needsPermission('rocketchat-server.read'), async function(
req,
res
) {
const { id, search } = req.params;
const node = RED.nodes.getNode(id);
if (node != null) {
try {
const { host, user, token } = node;
const apiInstance = api({ host, user, token });
const { users, rooms, success } = await apiInstance.spotlight(search);
if (success) {
return res.json({ users, rooms });
}
res.json([]);
} catch (err) {
res.sendStatus(500);
node.error(`Rocket.Chat Server Failed: ${err.toString()}`);
}
} else {
res.sendStatus(404);
}
});
};