forked from lucianocosta/node-red-contrib-rocketchat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rocketchat-create.js
61 lines (52 loc) · 1.84 KB
/
rocketchat-create.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
const api = require('./rocketchat');
module.exports = function(RED) {
'use strict';
function RocketChatCreate(config) {
RED.nodes.createNode(this, config);
const node = this;
node.server = RED.nodes.getNode(config.server);
node.on('input', async function(msg) {
const { host, user, token } = node.server;
const { roomType, roomName, roomNameType, users, usersType, readOnly } = config;
const apiInstance = api({ host, user, token });
const name = RED.util.evaluateNodeProperty(roomName, roomNameType, this, msg);
const configUsers = RED.util.evaluateNodeProperty(users, usersType, this, msg);
const members = Array.isArray(configUsers) ? configUsers : [configUsers];
const processResponse = (success, payload, errors) => {
if (success) {
node.send({
...msg,
payload
});
node.status({});
} else {
node.error(RED._('rocketchat-create.errors.error-processing', errors));
node.status({
fill: 'red',
shape: 'ring',
text: RED._('rocketchat-create.errors.error-processing', errors)
});
}
};
node.status({ fill: 'blue', shape: 'dot', text: 'rocketchat-create.label.sending' });
try {
if (roomType === 'channel') {
const { success, channel, errors } = await apiInstance.createChannel({ name, members, readOnly });
processResponse(success, channel, errors);
}
if (roomType === 'group') {
const { success, group, errors } = await apiInstance.createGroup({ name, members, readOnly });
processResponse(success, group, errors);
}
} catch (error) {
node.error(RED._('rocketchat-create.errors.error-processing', error));
node.status({
fill: 'red',
shape: 'ring',
text: RED._('rocketchat-create.errors.error-processing', error)
});
}
});
}
RED.nodes.registerType('rocketchat-create', RocketChatCreate);
};