-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathforce-chatter.js
282 lines (262 loc) · 9.52 KB
/
force-chatter.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* Copyright 2014 Atsushi Kojo.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
module.exports = function (RED) {
"use strict";
var jsforce = require("jsforce");
var request = require("request");
var fs = require("fs");
function ForceChatterInNode(n) {
RED.nodes.createNode(this, n);
this.force = n.force;
this.operation = n.operation;
this.group = n.group;
this.query = n.query;
this.to = n.to;
this.mention = n.mention;
this.forceConfig = RED.nodes.getNode(this.force);
if (this.forceConfig) {
var node = this;
node.on("input", function (msg) {
this.sendMsg = function (err, result) {
if (err) {
node.error(err.toString());
node.status({ fill: "red", shape: "ring", text: "failed" });
} else {
node.status({});
}
msg.payload = result;
node.send(msg);
};
this.forceConfig.login(function (conn, err) {
if (err) {
node.sendMsg(err);
return;
}
switch (node.operation) {
case "get_feed":
conn.chatter.resource("/feeds/news/me/feed-elements").retrieve(node.sendMsg);
break;
case "get_group":
var url = "/feeds/record/" + node.group + "/feed-elements";
conn.chatter.resource(url).retrieve(node.sendMsg);
break;
case "search_feed":
conn.chatter.resource("/feed-elements", { q: node.query }).retrieve(node.sendMsg);
break;
case "post_feed":
var feedItem = {
body: {
messageSegments: []
},
feedElementType: "FeedItem",
subjectId: node.to || msg.topic || "me"
};
var mentions = node.mention.split(",");
for (var i = 0; i < mentions.length; i++) {
if (mentions[i]) {
feedItem.body.messageSegments.push({
type: "Mention",
id: mentions[i]
});
feedItem.body.messageSegments.push({
type: "Text",
text: "\n"
});
}
}
feedItem.body.messageSegments.push({
type: "Text",
text: msg.payload
});
if (msg.filename && msg.binaryBuffer) {
//register photo
feedItem.capabilities = {
content:{
description: msg.payload,
title: msg.filename
}
};
var options = {
method: "POST",
url: `${conn.instanceUrl}/services/data/v${conn.version}/chatter/feed-elements`,
headers: {
Authorization: `Bearer ${conn.accessToken}`,
},
formData: {
feedElementFileUpload: {
value: msg.binaryBuffer,
options: {
filename: msg.filename,
contentType: "application/octet-stream",
},
},
json: {
value: JSON.stringify(feedItem),
options: {
contentType: "application/json; charset=UTF-8",
},
},
feedElementType: "FeedItem",
subjectId: node.to || msg.topic || "me",
},
};
request(options, function (error, response) {
if (error) throw new Error(error);
msg.payload = JSON.parse(response.body);
node.send(msg);
});
} else {
conn.chatter.resource("/feed-elements").create(feedItem, node.sendMsg);
}
break;
}
}, msg);
});
} else {
this.error("missing force configuration");
}
}
RED.nodes.registerType("force-chatter in", ForceChatterInNode);
RED.httpAdmin.get("/force-chatter/get-groups", function (req, res) {
var forceCredentials = RED.nodes.getCredentials(req.query.credentials);
var forceNode = RED.nodes.getNode(req.query.id);
var configNode = RED.nodes.getNode(req.query.credentials);
var forceConfig = null;
if (forceNode && forceNode.forceConfig) {
forceConfig = forceNode.forceConfig;
} else if (configNode) {
forceConfig = configNode;
} else {
forceConfig = req.query;
if (forceCredentials) forceConfig.credentials = forceCredentials;
forceConfig.login = ForceNodeLogin;
}
if (!req.query.id || !req.query.credentials || !forceConfig) {
return res.send('{"error": "Missing force credentials"}');
}
var credentials = {
password: forceConfig.credentials.password,
clientid: forceConfig.credentials.clientid,
clientsecret: forceConfig.credentials.clientsecret,
accessToken: forceConfig.credentials.accessToken,
refreshToken: forceConfig.credentials.refreshToken,
instanceUrl: forceConfig.credentials.instanceUrl
};
RED.nodes.addCredentials(req.query.credentials, credentials);
forceConfig.login(function (conn) {
conn.chatter.resource("/users/me/groups").retrieve(
function (err, result) {
if (err) {
return res.send('{"error": "error:' + err.toString() + '"}');
}
res.send(result);
});
}, {});
});
RED.httpAdmin.get("/force-chatter/get-mentions", function (req, res) {
var forceCredentials = RED.nodes.getCredentials(req.query.credentials);
var forceNode = RED.nodes.getNode(req.query.id);
var configNode = RED.nodes.getNode(req.query.credentials);
var forceConfig = null;
var splitUser = res.socket.parser.incoming._parsedUrl.path.split(
"&userPage="
);
var userPage = splitUser[1];
var splitGroup = res.socket.parser.incoming._parsedUrl.path.split(
"&groupPage="
);
var groupPage = splitGroup[1];
if (forceNode && forceNode.forceConfig) {
forceConfig = forceNode.forceConfig;
} else if (configNode) {
forceConfig = configNode;
} else {
forceConfig = req.query;
if (forceCredentials) forceConfig.credentials = forceCredentials;
forceConfig.login = ForceNodeLogin;
}
if (!req.query.id || !req.query.credentials || !forceConfig) {
return res.send('{"error": "Missing force credentials"}');
}
var credentials = {
password: forceConfig.credentials.password,
clientid: forceConfig.credentials.clientid,
clientsecret: forceConfig.credentials.clientsecret,
accessToken: forceConfig.credentials.accessToken,
refreshToken: forceConfig.credentials.refreshToken,
instanceUrl: forceConfig.credentials.instanceUrl
};
RED.nodes.addCredentials(req.query.credentials, credentials);
forceConfig.login(function (conn, err, n) {
var resData = { success: true };
conn.chatter.resource("/users?page=" + userPage + "&pageSize=50").retrieve(
function (err, result) {
if (err) {
return res.send('{"error": "error:' + err.toString() + '"}');
}
resData.users = result;
conn.chatter.resource("/groups?page=" + groupPage + "&pageSize=50").retrieve(
function (err, result) {
if (err) {
return res.send('{"error": "error:' + err.toString() + '"}');
}
resData.groups = result;
res.send(resData);
});
});
}, {});
});
function ForceNodeLogin(callback, msg) {
var accessToken = msg.accessToken || this.credentials.accessToken;
var instanceUrl = msg.instanceUrl || this.credentials.instanceUrl;
if (this.logintype == "oauth") {
var error;
if (!this.credentials.accessToken || !this.credentials.instanceUrl) {
error = JSON.parse('["' + "No Authenticate specified" + '"]');
}
var conn = new jsforce.Connection({
oauth2: {
clientId: this.credentials.clientid,
clientSecret: this.credentials.clientsecret,
redirectUri: null
}
});
conn.initialize({
accessToken: accessToken,
refreshToken: this.credentials.refreshToken,
instanceUrl: instanceUrl
});
callback(conn, error);
} else if (this.logintype == "Username-Password") {
var conn = new jsforce.Connection({
loginUrl: this.loginurl
});
var error;
conn.login(this.username, this.password, function (err, userInfo) {
if (err) {
error = err;
}
callback(conn, error);
});
} else if (this.logintype == "Signed-Request") {
var conn = new jsforce.Connection({
accessToken: accessToken,
instanceUrl: instanceUrl
});
callback(conn, error);
}
}
};