-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtasks.go
More file actions
65 lines (57 loc) · 1.54 KB
/
tasks.go
File metadata and controls
65 lines (57 loc) · 1.54 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
package main
import (
"fmt"
"log"
"net/http"
"time"
)
func handleNag(w http.ResponseWriter, r *http.Request) {
// If we never said otherwise, say ok.
defer func() {
w.Write([]byte("ok"))
}()
ctx := r.Context()
if nextGN, err := getNextGamenight(ctx); err != nil {
log.Printf("Couldn't find next gamenight: %v", err)
} else if nextGN != nil {
now := time.Now().In(tz())
sun := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, tz()).AddDate(0, 0, int(7-now.Weekday()))
if nextGN.When().Before(sun) {
log.Printf("No need to nag, gn is scheduled for %s", nextGN.When())
return
}
}
if err := r.ParseForm(); err != nil {
log.Printf("Error parsing form: %v", err)
http.Error(w, "Error parsing form", http.StatusBadRequest)
return
}
seq := r.FormValue("email")
var err error
switch seq {
case "first":
err = email(ctx, firstNag, nil)
case "second":
err = email(ctx, secondNag, nil)
default:
err = fmt.Errorf("Bad nag call: %q", seq)
}
if err != nil {
log.Printf("Error in nag task: %v", err)
http.Error(w, "Error processing task", http.StatusInternalServerError)
}
}
func handleTaskSchedule(w http.ResponseWriter, r *http.Request) {
// If we never said otherwise, say ok.
defer func() {
w.Write([]byte("ok"))
}()
// Check auth.
ctx := r.Context()
user, _ := getUserSession(ctx, r)
admin := user != nil && user.Superuser
if _, err := maybeSchedule(ctx, w, admin); err != nil {
log.Printf("Error scheduling gamenight: %v", err)
http.Error(w, "Error processing task", http.StatusInternalServerError)
}
}