-
Notifications
You must be signed in to change notification settings - Fork 6
/
home.js
261 lines (236 loc) · 7.02 KB
/
home.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
const { getAllTimezones } = require('countries-and-timezones')
const db = require('./db')
const { publishView } = require('./slack')
const { statsBlocks } = require('./stats')
function optionTime (seconds) {
seconds = parseInt(seconds, 10)
// Seconds to 00:00 - 23:59 format.
const hour = (Math.floor(seconds / 3600)).toString()
const minute = (Math.floor(seconds / 60) % 60).toString()
const text = ('00' + hour).substr(hour.length) + ':' + ('00' + minute).substr(minute.length)
return {
text: {
type: 'plain_text',
text,
emoji: true
},
value: `${seconds}`
}
}
function optionTimezone (zone) {
return {
text: {
type: 'plain_text',
text: zone,
emoji: true
},
value: zone
}
}
function optionWeekdays (mask) {
const weekdays = [
{ text: 'Monday', value: '1' },
{ text: 'Tuesday', value: '2' },
{ text: 'Wednesday', value: '3' },
{ text: 'Thursday', value: '4' },
{ text: 'Friday', value: '5' },
{ text: 'Saturday', value: '6' },
{ text: 'Sunday', value: '7' }
]
const weekdayInMask = (weekday, mask) => ((1 << (6 - (weekday - 1))) & mask) !== 0
// Convert mask to array in a very lazy way
const found = weekdays.filter(w => weekdayInMask(parseInt(w.value), mask))
return found.map(w => ({
text: {
type: 'plain_text',
text: w.text,
emoji: true
},
value: w.value
}))
}
function timeZoneGroups () {
// Split time zones into several different option groups, since Slack can only have
// 100 options in every group. We can _almost_ divide by continent, since all continents
// have less than 100 zones, except America (-.-) so we divide it into two.
// Special case for America since it has > 100 elements. We divide it into A-M, N-Z.
// We also filter out a few deprecated timezones.
let timeZones = Object.keys(getAllTimezones({ deprecated: true })).sort()
// Also skip everything that doesn't contain a / since they seem to be deprecated anyway.
// Some continents contain only deprecated aliases. Skip them.
timeZones = timeZones.filter(z => z.split('/').length > 1)
const deprecated = ['Brazil/', 'Canada/', 'Chile/', 'Mexico/', 'US/', 'Etc/']
timeZones = timeZones.filter(z => !deprecated.some(d => z.startsWith(d)))
const groupedZones = {}
for (const zone of timeZones) {
let [continent] = zone.split('/', 1)
if (continent === 'America') {
// Special case, split into two different groups.
const firstChar = zone.split('/', 2)[1]
if (firstChar < 'K') {
continent = 'America (A-J)'
} else {
continent = 'America (K-Z)'
}
}
if (groupedZones[continent] === undefined) {
groupedZones[continent] = []
}
groupedZones[continent].push(zone)
}
return groupedZones
}
function renderHome (statsBlocks, isAdmin, initialSettings) {
const view = {
type: 'home',
blocks: statsBlocks
}
if (isAdmin) {
const timeZones = timeZoneGroups()
// Generates 00:00, 01, ..., 23:30 with 30 minutes increments.
const times = []
for (let seconds = 0; seconds < 86400; seconds += 1800) {
times.push(seconds)
}
// Current settings.
let {
channel: initialChannel,
timezone: initialTimezone,
intervalStart: initialStart,
intervalEnd: initialEnd,
weekdays: initialWeekdaysMask
} = initialSettings
if (initialWeekdaysMask === undefined && initialWeekdaysMask === null) {
initialWeekdaysMask = 0
}
const adminBlocks = [
{
type: 'divider'
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: '*Wildbutton configuration :female-construction-worker:*'
}
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: 'Here you can configure wildbutton. This can only be done by admins.'
}
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: ':globe_with_meridians: Select your time zone'
},
accessory: {
type: 'static_select',
action_id: 'admin_timezone',
placeholder: {
type: 'plain_text',
text: 'Select a time zone',
emoji: true
},
option_groups: Object.entries(timeZones).map(([continent, zones]) => ({
label: {
type: 'plain_text',
text: continent
},
options: zones.map(optionTimezone)
})),
...(initialTimezone && { initial_option: optionTimezone(initialTimezone) })
}
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: ':calendar: Select weekdays to show a button'
},
accessory: {
type: 'multi_static_select',
action_id: 'admin_weekdays',
placeholder: {
type: 'plain_text',
text: 'Select weekdays',
emoji: true
},
options: optionWeekdays(0b1111111), // All weekdays
initial_options: optionWeekdays(initialWeekdaysMask)
}
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: ':clock9: Select a start time'
},
accessory: {
type: 'static_select',
action_id: 'admin_starttime',
placeholder: {
type: 'plain_text',
text: 'Select a start time',
emoji: true
},
options: times.map(optionTime),
...(initialStart && { initial_option: optionTime(initialStart) })
}
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: ':clock4: Select an end time'
},
accessory: {
type: 'static_select',
action_id: 'admin_endtime',
placeholder: {
type: 'plain_text',
text: 'Select an end time',
emoji: true
},
options: times.map(optionTime),
...(initialEnd && { initial_option: optionTime(initialEnd) })
}
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: ':hash: Select the channel to post to'
},
accessory: {
type: 'channels_select',
action_id: 'admin_channel',
placeholder: {
type: 'plain_text',
text: 'Select a channel',
emoji: true
},
...(initialChannel && { initial_channel: initialChannel })
}
}
]
view.blocks.push(...adminBlocks)
}
return view
}
module.exports = {
async publishHome ({ instanceRef, user }) {
// Get current settings and stats.
const instance = await db.instance(instanceRef)
const stats = await statsBlocks(instanceRef, user)
// Check if user is admin.
const isAdmin = instance.authedUser.id === user
// The initialSettings object matches the one from the db.
const view = renderHome(stats, isAdmin, instance)
await publishView(instance, user, view)
},
timeZoneGroups
}