-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
412 lines (329 loc) · 9.92 KB
/
utils.go
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package main
import (
"fmt"
"runtime/debug"
"sort"
"strconv"
"strings"
"time"
"github.com/UniversityRadioYork/myradio-go"
)
// Team numbers come from MyRadio
type Team int
// Officer numbers come from MyRadio
type Officer int
// BookingType relates particular constants to their values in the DB
type BookingType string
// TrainingStatus numbers come from MyRadio
type TrainingStatus int
const (
TeamManagement Team = 1
TeamEngineering = 7
TeamComputing = 8
OfficerTrainingCoordinator Officer = 107
TrainingStudioTrained TrainingStatus = 1
TrainingPodcastTrained = 28
TrainingTrainer = 3
TypeMeeting BookingType = "Meeting"
TypeOther = "Other"
TypeEngineering = "Engineering"
TypeTraining = "Training"
TypeRecording = "Recording"
TypeTrainingAutoAddedFromMyRadio = "ONLY_FOR_USE_IN_MYRADIO_TRAINING_SYNC"
)
// typeOrdering defines the order booking types will be sorted into for
// the user's selector
var typeOrdering map[BookingType]int = map[BookingType]int{
TypeTraining: 1,
TypeRecording: 2,
TypeEngineering: 3,
TypeMeeting: 4,
TypeOther: 5,
}
// cacheInvalidationTime determines how long we'll keep things in the
// cache before checking MyRadio to see if it needs updating
const cacheInvalidationTime = time.Duration(-2*24) * time.Hour
type myRadioNameCacheObject struct {
Name string
CacheTime time.Time
}
var myRadioNameCache map[int]myRadioNameCacheObject = make(map[int]myRadioNameCacheObject)
// getNameOfUser will take a user ID and return their name, looked up from the cache,
// or from MyRadio if it is not known in the cache, or is too old
func getNameOfUser(id int) string {
if cacheObject, ok := myRadioNameCache[id]; ok {
if !cacheObject.CacheTime.Before(time.Now().Add(cacheInvalidationTime)) {
return cacheObject.Name
}
}
name, err := myrSession.GetUserName(id)
if err != nil {
// TODO
panic(err)
}
myRadioNameCache[id] = myRadioNameCacheObject{
Name: name,
CacheTime: time.Now(),
}
return name
}
type myRadioOfficershipCacheObject struct {
Officerships []myradio.Officership
CacheTime time.Time
}
var myRadioOfficershipsCache map[int]myRadioOfficershipCacheObject = make(map[int]myRadioOfficershipCacheObject)
// getOfficerships will return officerships for a user, either from the cache or MyRadio
func getOfficerships(userID int) ([]myradio.Officership, error) {
if cacheObject, ok := myRadioOfficershipsCache[userID]; ok {
if !cacheObject.CacheTime.Before(time.Now().Add(cacheInvalidationTime)) {
return cacheObject.Officerships, nil
}
}
officerships, err := myrSession.GetUserOfficerships(userID)
if err != nil {
return nil, err
}
myRadioOfficershipsCache[userID] = myRadioOfficershipCacheObject{
Officerships: officerships,
CacheTime: time.Now(),
}
return officerships, nil
}
type myRadioTrainingsCacheObject struct {
Trainings []myradio.Training
CacheTime time.Time
}
var myRadioTrainingsCache map[int]myRadioTrainingsCacheObject = make(map[int]myRadioTrainingsCacheObject)
// getTrainings will return the user's trainings, either from cache or MyRadio
func getTrainings(userID int) ([]myradio.Training, error) {
if cacheObject, ok := myRadioTrainingsCache[userID]; ok {
if !cacheObject.CacheTime.Before(time.Now().Add(cacheInvalidationTime)) {
return cacheObject.Trainings, nil
}
}
trainings, err := myrSession.GetUserTraining(userID)
if err != nil {
return nil, err
}
myRadioTrainingsCache[userID] = myRadioTrainingsCacheObject{
Trainings: trainings,
CacheTime: time.Now(),
}
return trainings, nil
}
// isManagement will return if a user is on management
// this gives them permissions, such as deleting all events
func isManagement(userID int) bool {
officerships, err := getOfficerships(userID)
if err != nil {
// TODO
panic(err)
}
for _, officership := range officerships {
if officership.TillDateRaw != "" {
continue
}
if officership.Officer.Team.TeamID == uint(TeamManagement) {
// Management
return true
}
}
return false
}
// isComputing returns if a user has computing officer permissions,
// for working with cache-related endpoints, such as flushing
func isComputing(userID int) bool {
officerships, err := getOfficerships(userID)
if err != nil {
// TODO
panic(err)
}
for _, officership := range officerships {
if officership.TillDateRaw != "" {
continue
}
if officership.Officer.Team.TeamID == TeamComputing {
return true
}
}
return false
}
func isTrainingCoordinator(userID int) bool {
officerships, err := getOfficerships(userID)
if err != nil {
// TODO
panic(err)
}
for _, officership := range officerships {
if officership.TillDateRaw != "" {
continue
}
if officership.Officer.OfficerID == int(OfficerTrainingCoordinator) {
return true
}
}
return false
}
// hasPermissionToDelete works out if a user can delete a particular event,
// such as the TC being able to delete all training events
func hasPermissionToDelete(userID int, eventID int) bool {
var event Event
db.QueryRow("SELECT * FROM events WHERE event_id = $1", eventID).Scan(
&event.ID, &event.Type, &event.Title, &event.User, &event.StartTime, &event.EndTime)
// you can delete your own
if userID == event.User {
return true
}
// management can delete all
if isManagement(userID) {
return true
}
// training
if event.Type == TypeTraining && isTrainingCoordinator(userID) {
return true
}
// computing
if event.Type == TypeEngineering && isComputing(userID) {
return true
}
// engineering
officerships, err := getOfficerships(userID)
if err != nil {
// TODO
panic(err)
}
for _, officership := range officerships {
if officership.TillDateRaw != "" {
continue
}
if event.Type == TypeEngineering && officership.Officer.Team.TeamID == TeamEngineering {
// Engineering Type Events
return true
}
}
return false
}
// canClaimEventForStation determins if a user can remove a personal name
// from an event, and turn it into a station-wide event
func canClaimEventForStation(userID int, eventID int) bool {
if !isManagement(userID) {
return false
}
var event Event
db.QueryRow("SELECT * FROM events WHERE event_id = $1", eventID).Scan(
&event.ID, &event.Type, &event.Title, &event.User, &event.StartTime, &event.EndTime)
if event.Type != TypeOther {
return false
}
return strings.HasSuffix(event.Title, fmt.Sprintf("- %s", getNameOfUser(event.User)))
}
func isTrainer(userID int) bool {
trainings, err := getTrainings(userID)
if err != nil {
// TODO
panic(err)
}
for _, training := range trainings {
if training.StatusID == TrainingTrainer {
return true
}
}
return false
}
// bookingUserCanCreate returns an ordered list for the types
// of booking a user can create, based on their officerships and
// trainings
func bookingsUserCanCreate(userID int) []BookingType {
bookingTypes := []BookingType{TypeOther}
// If Studio Trained -> Recording
// If Trainer -> Training
trainings, err := getTrainings(userID)
if err != nil {
// TODO
panic(err)
}
for _, training := range trainings {
if training.StatusID == int(TrainingStudioTrained) || training.StatusID == TrainingPodcastTrained {
bookingTypes = append(bookingTypes, TypeRecording)
continue
}
}
if isTrainer(userID) {
bookingTypes = append(bookingTypes, TypeTraining)
}
// If Committee -> Meeting
// If Tech -> Engineering
officerships, err := getOfficerships(userID)
if err != nil {
// TODO
panic(err)
}
for _, officership := range officerships {
if officership.TillDateRaw != "" {
continue
}
bookingTypes = append(bookingTypes, TypeMeeting)
if officership.Officer.Team.TeamID == TeamEngineering || officership.Officer.Team.TeamID == TeamComputing {
bookingTypes = append(bookingTypes, TypeEngineering)
}
}
sort.SliceStable(bookingTypes, func(i, j int) bool {
return typeOrdering[bookingTypes[i]] < typeOrdering[bookingTypes[j]]
})
return bookingTypes
}
// getBuildCommit allows us to see the Git commit in the app,
// as a way of having version numbers
func getBuildCommit() string {
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
return setting.Value[:7]
}
}
}
return ""
}
var weekNamesCache map[string]string = make(map[string]string)
var weekNameCacheSetTime time.Time = time.Now()
// updateWeekNamesCache will cache week names from MyRadio.
// it will need to format them in the way the calendar displays dates
// NOTE: this uses a weird dash character
// NOTE: it uses three letter abbreviations for months, except September, which is Sept
func updateWeekNamesCache() {
terms, err := myrSession.GetAllTerms()
if err != nil {
panic(err)
}
for _, term := range terms {
for weekNo, weekName := range term.WeekNames {
weekMonday := term.StartTime().Add(time.Duration(weekNo*7*24*60) * time.Minute)
weekSunday := weekMonday.Add(6 * 24 * 60 * time.Minute)
weekString := strconv.Itoa(weekMonday.Day())
if weekMonday.Month() != weekSunday.Month() {
weekString = weekString + " " + weekMonday.Month().String()[:3]
if weekMonday.Month() == time.September {
weekString = weekString + "t"
}
}
weekString = weekString + " – " + strconv.Itoa(weekSunday.Day()) + " " + weekSunday.Month().String()[:3]
if weekSunday.Month() == time.September {
weekString = weekString + "t"
}
weekString = weekString + " " + strconv.Itoa(weekMonday.Year())
weekNamesCache[weekString] = weekName
}
}
weekNameCacheSetTime = time.Now()
}
// getWeekNames will return the week names, and possibly update the cache if
// it is old
func getWeekNames() map[string]string {
if len(weekNamesCache) == 0 {
updateWeekNamesCache()
}
if weekNameCacheSetTime.Before(time.Now().Add(cacheInvalidationTime)) {
go updateWeekNamesCache()
}
return weekNamesCache
}