-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathSlackGhost.js
221 lines (184 loc) · 6.62 KB
/
SlackGhost.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
"use strict";
var url = require('url');
var https = require('https');
var rp = require('request-promise');
const slackdown = require('Slackdown');
const substitutions = require("./substitutions");
const log = require("matrix-appservice-bridge").Logging.get("SlackGhost");
// How long in milliseconds to cache user info lookups.
var USER_CACHE_TIMEOUT = 10 * 60 * 1000; // 10 minutes
function SlackGhost(opts) {
this._main = opts.main;
this._user_id = opts.user_id;
this._display_name = opts.display_name;
this._avatar_url = opts.avatar_url;
this._intent = opts.intent;
this._atime = null; // last activity time in epoch seconds
}
SlackGhost.fromEntry = function(main, entry, intent) {
return new SlackGhost({
main: main,
user_id: entry.id,
display_name: entry.display_name,
avatar_url: entry.avatar_url,
intent: intent,
});
};
SlackGhost.prototype.toEntry = function() {
var entry = {
id: this._user_id,
display_name: this._display_name,
avatar_url: this._avatar_url,
};
return entry;
};
SlackGhost.prototype.getIntent = function() {
return this._intent;
};
SlackGhost.prototype.update = function(message, room) {
log.info("Updating user information for " + message.user_id);
return Promise.all([
this.updateDisplayname(message, room).catch((e) => {
log.error("Failed to update ghost displayname:", e);
}),
this.updateAvatar(message, room).catch((e) => {
log.error("Failed to update ghost avatar:", e);
}),
]);
};
SlackGhost.prototype.updateDisplayname = function(message, room) {
var display_name = message.user_name;
var getDisplayName;
if (!display_name) {
getDisplayName = this.lookupUserInfo(message.user_id, room.getAccessToken())
.then(user => {
if (user && user.profile) {
return user.profile.display_name || user.profile.real_name;
}
});
} else {
getDisplayName = Promise.resolve(display_name);
}
return getDisplayName.then(display_name => {
if (!display_name || this._display_name === display_name) return Promise.resolve();
return this.getIntent().setDisplayName(display_name).then(() => {
this._display_name = display_name;
return this._main.putUserToStore(this);
});
})
};
SlackGhost.prototype.lookupAvatarUrl = function(user_id, token) {
return this.lookupUserInfo(user_id, token).then((user) => {
if (!user || !user.profile) return;
var profile = user.profile;
// Pick the original image if we can, otherwise pick the largest image
// that is defined
var avatar_url = profile.image_original ||
profile.image_1024 || profile.image_512 || profile.image_192 ||
profile.image_72 || profile.image_48;
return avatar_url;
});
};
SlackGhost.prototype.lookupUserInfo = function(user_id, token) {
if (this._user_info_cache) return Promise.resolve(this._user_info_cache);
if (this._loading_user) return this._loading_user;
if (!token) return Promise.resolve();
this._main.incRemoteCallCounter("users.info");
this._loading_user = rp({
uri: 'https://slack.com/api/users.info',
qs: {
token: token,
user: user_id,
},
json: true,
}).then((response) => {
if (!response.user || !response.user.profile) {
log.error("Failed to get user profile", response);
return;
};
this._user_info_cache = response.user;
setTimeout(() => { this._user_info_cache = null }, USER_CACHE_TIMEOUT);
delete this._loading_user;
return response.user;
});
return this._loading_user;
};
SlackGhost.prototype.updateAvatar = function(message, room) {
var token = room.getAccessToken();
if (!token) return Promise.resolve();
return this.lookupAvatarUrl(message.user_id, token).then((avatar_url) => {
if (this._avatar_url === avatar_url) return;
var shortname = avatar_url.match(/\/([^\/]+)$/)[1];
return rp({
uri: avatar_url,
resolveWithFullResponse: true,
encoding: null,
}).then((response) => {
return this.uploadContent({
_content: response.body,
title: shortname,
mimetype: response.headers["content-type"],
});
}).then((content_uri) => {
this.getIntent().setAvatarUrl(content_uri);
}).then(() => {
this._avatar_url = avatar_url;
this._main.putUserToStore(this);
});
});
};
SlackGhost.prototype.sendText = function(room_id, text) {
// TODO: Slack's markdown is their own thing that isn't really markdown,
// but the only parser we have for it is slackdown. However, Matrix expects
// a variant of markdown that is in the realm of sanity. Currently text
// will be slack's markdown until we've got a slack -> markdown parser.
const content = {
body: text,
msgtype: "m.text",
formatted_body: Slackdown.parse(text),
format: "org.matrix.custom.html"
};
return this.getIntent().sendMessage(room_id, content).then(() => {
this._main.incCounter("sent_messages", {side: "matrix"});
});
};
SlackGhost.prototype.sendMessage = function(room_id, msg) {
return this.getIntent().sendMessage(room_id, msg).then(() => {
this._main.incCounter("sent_messages", {side: "matrix"});
});
};
SlackGhost.prototype.uploadContentFromURI = function(file, uri, token) {
return rp({
uri: uri,
headers: {
Authorization: `Bearer ${token}`,
},
encoding: null, // Because we expect a binary
}).then((buffer) => {
file._content = buffer;
return this.uploadContent(file);
}).then((contentUri) => {
return contentUri;
}).catch((reason) => {
log.error("Failed to upload content:\n%s", reason);
throw reason;
});
};
SlackGhost.prototype.uploadContent = function(file) {
return this.getIntent().getClient().uploadContent({
stream: new Buffer(file._content, "binary"),
name: file.title,
type: file.mimetype,
}).then((response) => {
var content_uri = JSON.parse(response).content_uri;
log.debug("Media uploaded to " + content_uri);
return content_uri;
});
};
SlackGhost.prototype.getATime = function() {
return this._atime;
};
SlackGhost.prototype.bumpATime = function() {
this._atime = Date.now() / 1000;
};
module.exports = SlackGhost;