-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathSlackEventHandler.js
213 lines (184 loc) · 7.47 KB
/
SlackEventHandler.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
"use strict";
const BaseSlackHandler = require('./BaseSlackHandler');
const Promise = require('bluebird');
const util = require("util");
const UnknownEvent = function () {
};
const UnknownChannel = function (channel) {
this.channel = channel;
};
/**
* @constructor
* @param {Main} main the toplevel bridge instance through which to
* communicate with matrix.
*/
function SlackEventHandler(main) {
this._main = main;
}
util.inherits(SlackEventHandler, BaseSlackHandler);
/**
* Handles a slack event request.
*
* @param {Object} params HTTP body of the event request, as a JSON-parsed dictionary.
* @param {string} params.team_id The unique identifier for the workspace/team where this event occurred.
* @param {Object} params.event Slack event object
* @param {string} params.event.type Slack event type
* @param {string} params.type type of callback we are receiving. typically event_callback
* or url_verification.
*/
SlackEventHandler.prototype.handle = function (params, response) {
try {
console.log("Received slack event:", params);
var main = this._main;
var endTimer = main.startTimer("remote_request_seconds");
// respond to event url challenges
if (params.type === 'url_verification') {
response.writeHead(200, {"Content-Type": "application/json"});
response.write(JSON.stringify({challenge: params.challenge}));
response.end();
return;
}
var result;
switch (params.event.type) {
case 'message':
result = this.handleMessageEvent(params);
break;
case 'channel_rename':
result = this.handleChannelRenameEvent(params);
break;
case 'team_domain_change':
result = this.handleDomainChangeEvent(params);
break;
case 'file_comment_added':
result = Promise.resolve();
break;
default:
result = Promise.reject(new UnknownEvent());
}
result.then(() => endTimer({outcome: "success"}))
.catch((e) => {
if (e instanceof UnknownChannel) {
console.log("Ignoring message from unrecognised slack channel id : %s (%s)",
e.channel, params.team_id);
main.incCounter("received_messages", {side: "remote"});
endTimer({outcome: "dropped"});
return;
} else if (e instanceof UnknownEvent) {
endTimer({outcome: "dropped"});
} else {
endTimer({outcome: "fail"});
}
console.log("Failed: ", e);
});
} catch (e) {
console.error("SlackEventHandler failed:", e);
}
// return 200 so slack doesn't keep sending the event
response.writeHead(200, {"Content-Type": "text/plain"});
response.end();
};
/**
* Attempts to handle the `team_domain_change` event.
*
* @param {Object} params The event request emitted.
* @param {Object} params.team_id The slack team_id for the event.
* @param {string} params.event.domain The new team domain.
*/
SlackEventHandler.prototype.handleDomainChangeEvent = function (params) {
this._main.getRoomsBySlackTeamId(params.team_id).forEach(room => {
room.updateSlackTeamDomain(params.event.domain);
if (room.isDirty()) {
this._main.putRoomToStore(room);
}
});
return Promise.resolve();
};
/**
* Attempts to handle the `channel_rename` event.
*
* @param {Object} params The event request emitted.
* @param {string} params.event.id The slack channel id
* @param {string} params.event.name The new name
*/
SlackEventHandler.prototype.handleChannelRenameEvent = function (params) {
//TODO test me. and do we even need this? doesn't appear to be used anymore
var room = this._main.getRoomBySlackChannelId(params.event.channel.id);
if (!room) throw new UnknownChannel(params.event.channel.id);
var channel_name = room.getSlackTeamDomain() + ".#" + params.name;
room.updateSlackChannelName(channel_name);
if (room.isDirty()) {
this._main.putRoomToStore(room);
}
return Promise.resolve();
};
/**
* Attempts to handle the `message` event.
*
* Sends a message to Matrix if it understands enough of the message to do so.
* Attempts to make the message as native-matrix feeling as it can.
*
* @param {Object} params The event request emitted.
* @param {string} params.event.user Slack user ID of user sending the message.
* @param {?string} params.event.text Text contents of the message, if a text message.
* @param {string} params.event.channel The slack channel id
* @param {string} params.event.ts The unique (per-channel) timestamp
*/
SlackEventHandler.prototype.handleMessageEvent = function (params) {
var room = this._main.getRoomBySlackChannelId(params.event.channel);
if (!room) throw new UnknownChannel(params.event.channel);
if (params.event.subtype === 'bot_message' &&
(!room.getSlackBotId() || params.event.bot_id === room.getSlackBotId())) {
return Promise.resolve();
}
// Only count received messages that aren't self-reflections
this._main.incCounter("received_messages", {side: "remote"});
var token = room.getAccessToken();
var msg = Object.assign({}, params.event, {
user_id: params.event.user || params.event.bot_id,
team_domain: room.getSlackTeamDomain() || room.getSlackTeamId(),
team_id: params.team_id,
channel_id: params.event.channel
});
var processMsg = msg.text || msg.subtype === 'message_deleted' || msg.files != undefined;
if (msg.subtype === 'file_comment') {
msg.user_id = msg.comment.user;
}
if (!token) {
// If we can't look up more details about the message
// (because we don't have a master token), but it has text,
// just send the message as text.
console.log("no slack token for " + room.getSlackTeamDomain() || room.getSlackChannelId());
return (processMsg) ? room.onSlackMessage(msg) : Promise.resolve();
}
if (!processMsg) {
// TODO(paul): When I started looking at this code there was no lookupAndSendMessage()
// I wonder if this code path never gets called...?
// lookupAndSendMessage(params.channel_id, params.timestamp, intent, roomID, token);
console.log("Did not understand event. " + JSON.stringify(msg));
return Promise.resolve();
}
var result;
if (msg.subtype === "file_share" && msg.file) {
// TODO check is_public when matrix supports authenticated media https://github.com/matrix-org/matrix-doc/issues/701
// we need a user token to be able to enablePublicSharing
if (room.getSlackUserToken()) {
result = this.enablePublicSharing(msg.file, room.getSlackUserToken())
.then((file) => {
if (file) {
msg.file = file;
}
return this.fetchFileContent(msg.file, token)
.then((content) => {
msg.file._content = content;
});
})
}
} else {
result = Promise.resolve();
}
return result.then(() => msg)
.then((msg) => this.replaceChannelIdsWithNames(msg, token))
.then((msg) => this.replaceUserIdsWithNames(msg, token))
.then((msg) => room.onSlackMessage(msg));
};
module.exports = SlackEventHandler;