-
Notifications
You must be signed in to change notification settings - Fork 78
/
main.js
executable file
·294 lines (251 loc) · 8.75 KB
/
main.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
283
284
285
286
287
288
289
290
291
292
293
294
const notifier = require('node-notifier');
const path = require('path');
const ui = require('./userInterface.js');
const slack = require('./slackClient.js');
const components = ui.init(); // ui components
let users;
let currentUser;
let channels;
let currentChannelId;
const UNKNOWN_USER_NAME = 'Unknown User';
// This is a hack to make the message list scroll to the bottom whenever a message is sent.
// Multiline messages would otherwise only scroll one line per message leaving part of the message
// cut off. This assumes that messages will be less than 50 lines high in the chat window.
const SCROLL_PER_MESSAGE = 50;
// generates ids for messages
const getNextId = (() => {
let id = 0;
return () => {
id += 1;
return id;
};
})();
// handles the reply to say that a message was successfully sent
function handleSentConfirmation(message) {
// for some reason getLines gives an object with int keys
const lines = components.chatWindow.getLines();
const keys = Object.keys(lines);
let line;
let i;
for (i = keys.length - 1; i >= 0; i -= 1) {
line = lines[keys[i]].split('(pending - ');
if (parseInt(line.pop()[0], 10) === message.reply_to) {
components.chatWindow.deleteLine(parseInt(keys[i], 10));
if (message.ok) {
components.chatWindow.insertLine(i, line.join(''));
} else {
components.chatWindow.insertLine(i, `${line.join('')} (FAILED)`);
}
break;
}
}
components.chatWindow.scroll(SCROLL_PER_MESSAGE);
components.screen.render();
}
// formats channel and user mentions readably
function formatMessageMentions(text) {
if (text === null || typeof text === 'undefined') {
return '';
}
let formattedText = text;
// find user mentions
const userMentions = text.match(/<@U[a-zA-Z0-9]+>/g);
if (userMentions !== null) {
userMentions
.map(match => match.substr(2, match.length - 3))
.forEach((userId) => {
let username;
let modifier;
if (userId === currentUser.id) {
username = currentUser.name;
modifier = 'yellow-fg';
} else {
const user = users.find(potentialUser => potentialUser.id === userId);
username = typeof user === 'undefined' ? UNKNOWN_USER_NAME : user.name;
modifier = 'underline';
}
formattedText = text.replace(
new RegExp(`<@${userId}>`, 'g'),
`{${modifier}}@${username}{/${modifier}}`
);
});
}
// find special words
return formattedText.replace(
/<!channel>/g,
'{yellow-fg}@channel{/yellow-fg}'
);
}
function handleNewMessage(message) {
let username;
if (message.user === currentUser.id) {
username = currentUser.name;
} else {
const author = users.find(user => message.user === user.id);
username = (author && author.name) || UNKNOWN_USER_NAME;
notifier.notify({
icon: path.join(__dirname, 'Slack_Mark_Black_Web.png'),
message: `${username}: ${message.text}`,
sound: true,
title: 'Terminal Slack',
});
}
if (message.channel !== currentChannelId ||
typeof message.text === 'undefined') {
return;
}
components.chatWindow.insertBottom(
`{bold}${username}{/bold}: ${formatMessageMentions(message.text)}`
);
components.chatWindow.scroll(SCROLL_PER_MESSAGE);
components.screen.render();
}
slack.init((data, ws) => {
currentUser = data.self;
// don't update focus until ws is connected
// focus on the channel list
components.channelList.select(0);
components.channelList.focus();
// re render screen
components.screen.render();
ws.on('message', (message /* , flags */) => {
const parsedMessage = JSON.parse(message);
if ('reply_to' in parsedMessage) {
handleSentConfirmation(parsedMessage);
} else if (parsedMessage.type === 'message') {
handleNewMessage(parsedMessage);
}
});
// initialize these event handlers here as they allow functionality
// that relies on websockets
// event handler when message is submitted
components.messageInput.on('submit', (text) => {
if (!text || !text.length) {
components.messageInput.focus();
return;
}
const id = getNextId();
components.messageInput.clearValue();
components.messageInput.focus();
components.chatWindow.scrollTo(components.chatWindow.getLines().length * SCROLL_PER_MESSAGE);
components.chatWindow.insertBottom(
`{bold}${currentUser.name}{/bold}: ${text} (pending - ${id})`
);
components.chatWindow.scroll(SCROLL_PER_MESSAGE);
components.screen.render();
ws.send(JSON.stringify({
id,
type: 'message',
channel: currentChannelId,
text,
}));
});
// set the user list to the users returned from slack
// called here to check against currentUser
slack.getUsers((error, response, userData) => {
if (error || response.statusCode !== 200) {
console.log( // eslint-disable-line no-console
'Error: ', error, response || response.statusCode
);
return;
}
const parsedUserData = JSON.parse(userData);
users = parsedUserData.members.filter(user => !user.deleted && user.id !== currentUser.id);
components.userList.setItems(users.map(slackUser => slackUser.name));
components.screen.render();
});
});
// set the channel list
components.channelList.setItems(['Connecting to Slack...']);
components.screen.render();
// set the channel list to the channels returned from slack
slack.getChannels((error, response, data) => {
if (error || response.statusCode !== 200) {
console.log( // eslint-disable-line no-console
'Error: ', error, response && response.statusCode
);
return;
}
const channelData = JSON.parse(data);
channels = channelData.channels.filter(channel => !channel.is_archived);
components.channelList.setItems(
channels.map(slackChannel => slackChannel.name)
);
components.screen.render();
});
// event handler when user selects a channel
function updateMessages(data, markFn) {
components.chatWindow.deleteTop(); // remove loading message
// filter and map the messages before displaying them
data.messages
.filter(item => !item.hidden)
.filter(item => item.type === 'message')
// Some messages related to message threading don't have text. This feature
// isn't supported by terminal-slack right now so we filter them out
.filter(item => typeof item.text !== 'undefined')
.map((message) => {
const len = users.length;
let username;
let i;
// get the author
if (message.user === currentUser.id) {
username = currentUser.name;
} else {
for (i = 0; i < len; i += 1) {
if (message.user === users[i].id) {
username = users[i].name;
break;
}
}
}
return { text: message.text, username: username || UNKNOWN_USER_NAME };
})
.forEach((message) => {
// add messages to window
components.chatWindow.unshiftLine(
`{bold}${message.username}{/bold}: ${formatMessageMentions(message.text)}`
);
});
// mark the most recently read message
if (data.messages.length) {
markFn(currentChannelId, data.messages[0].ts);
}
// reset messageInput and give focus
components.messageInput.clearValue();
components.chatWindow.scrollTo(components.chatWindow.getLines().length * SCROLL_PER_MESSAGE);
components.messageInput.focus();
components.screen.render();
}
components.userList.on('select', (data) => {
const username = data.content;
// a channel was selected
components.mainWindowTitle.setContent(`{bold}${username}{/bold}`);
components.chatWindow.setContent('Getting messages...');
components.screen.render();
// get user's id
const userId = users.find(potentialUser => potentialUser.name === username).id;
slack.openIm(userId, (error, response, imData) => {
const parsedImData = JSON.parse(imData);
currentChannelId = parsedImData.channel.id;
// load im history
slack.getImHistory(currentChannelId, (histError, histResponse, imHistoryData) => {
updateMessages(JSON.parse(imHistoryData), slack.markIm);
});
});
});
components.channelList.on('select', (data) => {
const channelName = data.content;
// a channel was selected
components.mainWindowTitle.setContent(`{bold}${channelName}{/bold}`);
components.chatWindow.setContent('Getting messages...');
components.screen.render();
// join the selected channel
slack.joinChannel(channelName, (error, response, channelData) => {
const parsedChannelData = JSON.parse(channelData);
currentChannelId = parsedChannelData.channel.id;
// get the previous messages of the channel and display them
slack.getChannelHistory(currentChannelId, (histError, histResponse, channelHistoryData) => {
updateMessages(JSON.parse(channelHistoryData), slack.markChannel);
});
});
});