-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreload-slack-data.js
More file actions
230 lines (199 loc) · 7.87 KB
/
reload-slack-data.js
File metadata and controls
230 lines (199 loc) · 7.87 KB
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
const fs = require('fs');
const path = require('path');
const request = require('request');
const async = require('async');
const secrets = require('./secrets');
class SlackDataLoader {
constructor(outputDir, secrets) {
this.baseUrl = 'https://slack.com/api';
this.outputDir = outputDir;
this.secrets = secrets;
}
getUsers(done) {
request({
baseUrl: this.baseUrl,
uri: 'users.list',
method: 'POST',
form: {
token: this.secrets.accessToken
},
json: true
}, (err, res, body) => {
if (err) return done(err);
if (body.ok !== true) return done(new Error('users.list request failed'));
let users = body.members.filter(user => !user.deleted && !user.is_bot);
users = users.map(user => {
let { id, name, real_name } = user;
return { id, name, real_name };
});
return done(null, users);
});
}
getChannels(done) {
request({
baseUrl: this.baseUrl,
uri: 'channels.list',
method: 'POST',
form: {
token: this.secrets.accessToken,
exclude_members: true
},
json: true
}, (err, res, body) => {
if (err) return done(err);
if (body.ok !== true) return done(new Error('chanels.list request failed'));
let channels = body.channels.map(channel => {
return {
id: channel.id,
name: channel.name
};
});
return done(null, channels);
});
}
getPrivateChannels(done) {
request({
baseUrl: this.baseUrl,
uri: 'groups.list',
method: 'POST',
form: {
token: this.secrets.accessToken
},
json: true
}, (err, res, body) => {
if (err) return done(err);
if (body.ok !== true) return done(new Error('groups.list request failed'));
let channels = body.groups.map(channel => {
return {
id: channel.id,
name: channel.name
};
});
return done(null, channels);
});
}
getChannelHistory(channelId, isPrivate, channelHistoryLimit, done) {
let uri = isPrivate ? 'groups.history' : 'channels.history';
const maxEntriesPerPage = 1000; // 1-1000
let fetchLimit = maxEntriesPerPage;
if (channelHistoryLimit < maxEntriesPerPage) fetchLimit = channelHistoryLimit;
let latest = null;
const results = [];
async.doUntil(next => {
request({
baseUrl: this.baseUrl,
uri: uri,
method: 'POST',
form: {
token: this.secrets.accessToken,
channel: channelId,
count: fetchLimit,
latest: latest
},
json: true
}, (err, res, body) => {
if (err) return next(err);
if (body.ok !== true) return next(new Error(`channels.history request failed for channel ${channelId}`));
console.log(`channels.history request fetched ${body.messages.length} messages for channel ${channelId}`);
results.push(body);
return next(null, body);
});
}, body => {
channelHistoryLimit -= fetchLimit;
if (channelHistoryLimit < maxEntriesPerPage) fetchLimit = channelHistoryLimit;
// get last record timestamp to be used as starting point into next request
if (body.messages.length > 0)
latest = body.messages[body.messages.length - 1].ts;
// continue fetching if there are more entries in channel and limit is not reached
return !body.has_more || fetchLimit === 0;
}, err => {
// Note: 2nd cb param seems to be the last returned result, not array of all. Used custom results holder.
if (err) return done(err);
let messagesByUser = {};
results.forEach(body => {
// filter text messages only
let messages = body.messages.filter(message => message.type === 'message' && message.subtype !== 'file_share');
// leave text only, sort by user
messages.forEach(message => {
if (!Array.isArray(messagesByUser[message.user]))
messagesByUser[message.user] = [];
messagesByUser[message.user].push(message.text);
});
});
let history = [];
Object.keys(messagesByUser).forEach(userId => {
history.push({user: userId, messages: messagesByUser[userId]});
});
return done(null, history);
});
}
makeChannelsFile(channelsLimit, channelHistoryLimit, done) {
let output = [];
this.getChannels((err, channels) => {
if (err) return done(err);
channels = channels.slice(0, channelsLimit);
async.eachSeries(channels, (channel, next) => {
console.log(`Fetching history for channel ${channel.name}`);
this.getChannelHistory(channel.id, false, channelHistoryLimit, (err, history) => {
if (err) return next(err);
output.push({channel: channel.id, history: history});
return next();
});
}, err => {
if (err) return done(err);
// write into file
fs.writeFile(path.join(this.outputDir, 'channels.json'), JSON.stringify(output), err => {
if (err) return done(err);
console.log('makeChannelsFile - channels.json done');
return done();
});
});
});
}
// TODO: reuse from makeChannelsFile?
makePrivateChannelsFile(channelsLimit, channelHistoryLimit, done) {
let output = [];
this.getPrivateChannels((err, channels) => {
if (err) return done(err);
channels = channels.slice(0, channelsLimit);
async.eachSeries(channels, (channel, next) => {
console.log(`Fetching history for private channel ${channel.name}`);
this.getChannelHistory(channel.id, true, channelHistoryLimit, (err, history) => {
if (err) return next(err);
output.push({channel: channel.id, history: history});
return next();
});
}, err => {
if (err) return done(err);
// write into file
fs.writeFile(path.join(this.outputDir, 'privateChannels.json'), JSON.stringify(output), err => {
if (err) return done(err);
console.log('makePrivateChannelsFile - privateChannels.json done');
return done();
});
});
});
}
makeUsersFile(done) {
this.getUsers((err, users) => {
if (err) return done(err);
fs.writeFile(path.join(this.outputDir, 'users.json'), JSON.stringify(users), err => {
if (err) return done(err);
console.log('makeUsersFile - users.json done');
return done();
});
});
}
}
const slack = new SlackDataLoader('slack-data', secrets.slack);
async.series([
next => { slack.makeUsersFile(next); },
next => { slack.makeChannelsFile(999, 10000, next); },
next => { slack.makePrivateChannelsFile(999, 10000, next); }
], err => {
if (err) {
console.error(err);
} else {
console.log('DONE');
}
});