-
Notifications
You must be signed in to change notification settings - Fork 9
/
announcement.js
291 lines (267 loc) · 8.78 KB
/
announcement.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
import _ from 'lodash'
import {
transcript,
getInfoForUser,
initBot,
airPatch,
userRecord as uR,
airFind,
airGet,
} from '../utils'
// \`-\`-._
// \` )`. `-.__ ,
// '' , . _ _,-._;'_,-`__,-' ,/
// : `. ` , _' :- '--'._ ' `------._,-;'
// `- ,`- ' `--..__,,---' credit: http://ascii.co.uk/art/dragon
// [email protected]: caution, here there be dragons
const sendAnnouncements = (bot, message) => {
bot.replyPrivateDelayed(message, transcript('announcement.starting'))
return uR(message.user)
.then(userRecord =>
userRecord
.patch({ announcement: { primed: true } })
.then(() => sendAnnouncementRecursive(bot, message))
.catch(err => {
throw err
})
)
.catch(err => {
throw err
})
}
// [email protected]: I know your first impulse will be to refactor this mess.
// before you re-write this, have a full understanding of the design decisions
// made here.
// - Announcements are sent in human time, not milliseconds. Damage control on an accidental announcement is much harder when 10 announcements are batched every 100ms
// - No (batching|caching) the club list. If an admin needs to change the clubs being addressed after the announcement is fired, they can make immediate changes
// - Admins know what's happening on a per-channel basis by following along in AirTable
// - Admins have to manually add their own announcement message so no one accidentally messes with an in-progress announcement
// - Admins have to manually check which channels are being messaged to ensure responsible use
// - Announcements will stop sending if anything goes wrong until the admin manually restarts
const sendAnnouncementRecursive = (bot, message) =>
new Promise((resolve, reject) =>
setTimeout(
() =>
Promise.all([
uR(message.user),
airFind('Clubs', 'AND({Announcement Queued}, {Slack Channel ID})'),
])
.then(values => {
const [userRecord, club] = values
if (!_.get(userRecord, 'fields.announcement.primed')) {
reject(
new Error(
'Primer was set to false! Not firing the announcement'
)
)
return
}
if (!club) {
bot.replyPrivateDelayed(
message,
transcript('announcement.finished')
)
resolve()
}
bot.sendEphemeral(
{
channel: message.channel,
user: message.user,
text: transcript('announcement.progress.start', {
channel: club.fields['Slack Channel ID'],
recordID: club.fields['ID'],
}),
},
err => {
if (err) console.error(err)
}
)
initBot().say(
{
text: userRecord.fields.announcement.message,
channel: club.fields['Slack Channel ID'],
},
(err, res) => {
if (err) {
reject(err)
return
}
bot.sendEphemeral(
{
channel: message.channel,
user: message.user,
text: transcript('announcement.progress.complete', {
channel: club.fields['Slack Channel ID'],
}),
},
err => {
if (err) console.error(err)
}
)
return airPatch('Clubs', club.id, {
'Announcement Queued': false,
})
.then(() => resolve(sendAnnouncementRecursive(bot, message)))
.catch(err => reject(err))
}
)
})
.catch(err => reject(err)),
2000
)
)
const getAnnFromSlack = content =>
new Promise((resolve, reject) => {
const slackUrlRegex = /^https?:\/\/hackclub.slack.com\/archives\/([a-zA-Z0-9]+)\/p([0-9]+)/
const [match, channel, timestamp] = content.match(slackUrlRegex)
const oldest = Number.parseFloat(timestamp / 1000000).toFixed(6) // Slack requires floating zeroes to the 6th decimal place
// How to handle different types of Slack links:
// Channel ID starts with 'C'? use channel.history
// https://hackclub.slack.com/archives/C1C3K2RQV/p1569610292034800
// Channel ID starts with 'G'? use conversations.history
// https://hackclub.slack.com/archives/GADJZHQJD/p1569558290000500
// Channel ID starts with 'D'? use im.history
// https://hackclub.slack.com/archives/DM4F8ES8P/p1569703631000200
switch (channel[0]) {
case 'G':
initBot(true).api.conversations.history(
{
channel,
oldest,
inclusive: 1,
limit: 1,
},
(err, res) => {
if (err) reject(err)
const message = res.messages[0]
if (!message || message.ts != oldest) {
reject(new Error(transcript('announcement.notExactSlackMatch')))
}
resolve(message.text)
}
)
break
case 'C':
initBot(true).api.channels.history(
{
channel,
oldest,
inclusive: 1,
count: 1,
},
(err, res) => {
if (err) reject(err)
const message = res.messages[0]
if (!message || message.ts != oldest) {
reject(new Error(transcript('announcement.notExactSlackMatch')))
}
resolve(message.text)
}
)
break
case 'D':
initBot(true).api.im.history(
{
channel,
oldest,
inclusive: 1,
count: 1,
},
(err, res) => {
if (err) reject(err)
const message = res.messages[0]
if (!message || message.ts != oldest) {
reject(new Error(transcript('announcement.notExactSlackMatch')))
}
resolve(message.text)
}
)
break
default:
resolve(content)
break
}
})
const sendStatus = (bot, message) =>
Promise.all([
uR(message.user),
airGet('Clubs', 'AND({Announcement Queued}, {Slack Channel ID})'),
])
.then(values => {
const [userRecord, clubs] = values
const announcementData = JSON.stringify(
{
...userRecord.fields.announcement,
channels: clubs
.map(
club =>
`<#${club.fields['Slack Channel ID']}> (AirTable record ${club.id})`
)
.join(', '),
},
null,
2 // https://stackoverflow.com/a/7220510
)
bot.replyPrivateDelayed(
message,
transcript('announcement.status', { announcementData })
)
})
.catch(err => {
throw err
})
const interactionAnnouncement = (bot, message) => {
const { text, user } = message
const verb = text.split(' ')[0]
const content = text.replace(verb, '').trim()
if (verb == 'help') {
bot.replyPrivateDelayed(message, transcript('announcement.help'))
} else if (!'record address status send stop'.split(' ').includes(verb)) {
bot.replyPrivateDelayed(
message,
transcript('announcement.unrecognizedCommand')
)
}
getInfoForUser(user)
.then(({ slackUser, userRecord }) => {
if (!slackUser.is_admin) {
throw new Error('This command can only be run by Slack Owner accounts')
}
if (verb == 'stop') {
return userRecord
.patch({ announcement: { primed: false } })
.then(() => sendStatus(bot, message))
.catch(err => {
throw err
})
} else if (verb == 'record') {
return getAnnFromSlack(content)
.then(message =>
userRecord.patch({ announcement: { message } }).catch(err => {
throw err
})
)
.then(() => sendStatus(bot, message))
.catch(err => {
throw err
})
} else if (verb == 'address') {
bot.replyPrivateDelayed(message, transcript('announcement.address'))
return
} else if (verb == 'status') {
return sendStatus(bot, message).catch(err => {
throw err
})
} else if (verb == 'send') {
console.log('got Send command')
return sendAnnouncements(bot, message).catch(err => {
throw err
})
}
})
.catch(err => {
console.error(err)
bot.replyPrivateDelayed(message, transcript('errors.general', { err }))
})
}
export default interactionAnnouncement