-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathBaseSlackHandler.js
172 lines (158 loc) · 5.74 KB
/
BaseSlackHandler.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
"use strict";
const rp = require('request-promise');
const Promise = require('bluebird');
const promiseWhile = require("./promiseWhile");
const getSlackFileUrl = require("./substitutions").getSlackFileUrl;
/**
* @constructor
* @param {Main} main the toplevel bridge instance through which to
* communicate with matrix.
*/
function BaseSlackHandler(main) {
this._main = main;
}
BaseSlackHandler.prototype.replaceChannelIdsWithNames = function (message, token) {
var main = this._main;
// match all channelIds
var testForName = message.text.match(/<#(\w+)\|?\w*?>/g);
var iteration = 0;
var matches = 0;
if (testForName && testForName.length) {
matches = testForName.length;
}
return promiseWhile(function () {
// Do this until there are no more channel ID matches
return iteration < matches;
}, function () {
// foreach channelId, pull out the ID
// (if this is an emote msg, the format is <#ID|name>, but in normal msgs it's just <#ID>
var id = testForName[iteration].match(/<#(\w+)\|?\w*?>/)[1];
var channelsInfoApiParams = {
uri: 'https://slack.com/api/channels.info',
qs: {
token: token,
channel: id
},
json: true
};
main.incRemoteCallCounter("channels.info");
return rp(channelsInfoApiParams).then((response) => {
if (response && response.channel && response.channel.name) {
console.log("channels.info: " + id + " mapped to " + response.channel.name);
message.text = message.text.replace(/<#(\w+)\|?\w*?>/, "#" + response.channel.name);
}
else {
console.log("channels.info returned no result for " + id);
}
iteration++;
}).catch((err) => {
console.log("Caught error " + err);
});
}).then(() => {
// Notice we can chain it because it's a Promise,
// this will run after completion of the promiseWhile Promise!
return message;
});
};
BaseSlackHandler.prototype.replaceUserIdsWithNames = function (message, token) {
var main = this._main;
// match all userIds
var testForName = message.text.match(/<@(\w+)\|?\w*?>/g);
var iteration = 0;
var matches = 0;
if (testForName && testForName.length) {
matches = testForName.length;
}
return promiseWhile(() => {
// Condition for stopping
return iteration < matches;
}, function () {
// foreach userId, pull out the ID
// (if this is an emote msg, the format is <@ID|nick>, but in normal msgs it's just <@ID>
var id = testForName[iteration].match(/<@(\w+)\|?\w*?>/)[1];
var channelsInfoApiParams = {
uri: 'https://slack.com/api/users.info',
qs: {
token: token,
user: id
},
json: true
};
main.incRemoteCallCounter("users.info");
return rp(channelsInfoApiParams).then((response) => {
if (response && response.user && response.user.profile) {
var name = response.user.profile.display_name || response.user.profile.real_name;
console.log("users.info: " + id + " mapped to " + name);
message.text = message.text.replace(/<@(\w+)\|?\w*?>/, name);
}
else {
console.log("users.info returned no result for " + id);
}
iteration++;
}).catch((err) => {
console.log("Caught error " + err);
});
}).then(() => {
// Notice we can chain it because it's a Promise,
// this will run after completion of the promiseWhile Promise!
return message;
});
};
// Return true if we ought to fetch the content of the given file object
BaseSlackHandler.prototype.shouldFetchContent = function (file) {
if (!file) return false;
if (file.mimetype && file.mimetype.indexOf("image/") === 0) return true;
return false;
}
/**
* Enables public sharing on the given file object. then fetches its content.
*
* @param {Object} file A slack 'message.file' data object
* @param {string} token A slack API token that has 'files:write:user' scope
* @return {Promise<Object>} A Promise of the updated slack file data object
*/
BaseSlackHandler.prototype.enablePublicSharing = function (file, token) {
if (file.public_url_shared) return Promise.resolve(file);
this._main.incRemoteCallCounter("files.sharedPublicURL");
return rp({
method: 'POST',
form: {
file: file.id,
token: token,
},
uri: "https://slack.com/api/files.sharedPublicURL",
json: true
}).then((response) => {
if (!response || !response.file || !response.file.permalink_public) {
console.log("Could not find sharedPublichURL: " + JSON.stringify(response));
return undefined;
}
return response.file;
});
}
/**
* Fetchs the file at a given url.
*
* @param {Object} file A slack 'message.file' data object
* @return {Promise<string>} A Promise of file contents
*/
BaseSlackHandler.prototype.fetchFileContent = function (file, token) {
if (!file) return Promise.resolve();
// if (file.is_public) {
var url = getSlackFileUrl(file);
if (!url) url = file.permalink_public;
// } else {
// url = file.url_private
// }
return rp({
uri: url,
resolveWithFullResponse: true,
encoding: null
}).then((response) => {
var content = response.body;
console.log("Successfully fetched file " + file.id +
" content (" + content.length + " bytes)");
return content;
});
};
module.exports = BaseSlackHandler;