-
Notifications
You must be signed in to change notification settings - Fork 15
/
dispatch.go
215 lines (175 loc) · 5.63 KB
/
dispatch.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
package main
import (
"context"
"fmt"
"log"
"strconv"
"strings"
"time"
"github.com/recursecenter/pairing-bot/store"
)
func (pl *PairingLogic) dispatch(ctx context.Context, cmd string, cmdArgs []string, rec *store.Recurser) (string, error) {
// here's the actual actions. command input from
// the user input has already been sanitized, so we can
// trust that cmd and cmdArgs only have valid stuff in them
switch cmd {
case "schedule":
return pl.SetSchedule(ctx, rec, cmdArgs)
case "subscribe":
return pl.Subscribe(ctx, rec)
case "unsubscribe":
return pl.Unsubscribe(ctx, rec)
case "skip":
return pl.SkipTomorrow(ctx, rec)
case "unskip":
return pl.UnskipTomorrow(ctx, rec)
case "status":
return pl.Status(ctx, rec)
case "add-review":
content := cmdArgs[0]
return pl.AddReview(ctx, rec, content)
case "get-reviews":
numReviews := 5
if len(cmdArgs) > 0 {
numReviews, _ = strconv.Atoi(cmdArgs[0])
}
return pl.GetReviews(ctx, numReviews)
case "cookie":
return cookieClubMessage, nil
case "help":
return helpMessage, nil
case "version":
return pl.version, nil
case "thanks":
return youreWelcomeMessage, nil
default:
// this won't execute because all input has been sanitized
// by parseCmd() and all cases are handled explicitly above
return "", nil
}
}
func (pl *PairingLogic) SetSchedule(ctx context.Context, rec *store.Recurser, days []string) (string, error) {
if !rec.IsSubscribed {
return notSubscribedMessage, nil
}
rec.Schedule = store.NewSchedule(days)
if err := store.Recursers(pl.db).Set(ctx, rec.ID, rec); err != nil {
return writeErrorMessage, err
}
return "Awesome, your new schedule's been set! You can check it with `status`.", nil
}
func (pl *PairingLogic) Subscribe(ctx context.Context, rec *store.Recurser) (string, error) {
if rec.IsSubscribed {
return "You're already subscribed! Use `schedule` to set your schedule.", nil
}
atRC, err := pl.recurse.IsCurrentlyAtRC(ctx, rec.ID)
if err != nil {
log.Printf("Could not read currently-at-RC data from RC API: %s", err)
return readErrorMessage, err
}
rec.CurrentlyAtRC = atRC
if err = store.Recursers(pl.db).Set(ctx, rec.ID, rec); err != nil {
log.Printf("Could not update recurser in database: %s", err)
return writeErrorMessage, err
}
return subscribeMessage, nil
}
func (pl *PairingLogic) Unsubscribe(ctx context.Context, rec *store.Recurser) (string, error) {
if !rec.IsSubscribed {
return notSubscribedMessage, nil
}
if err := store.Recursers(pl.db).Delete(ctx, rec.ID); err != nil {
return writeErrorMessage, err
}
return unsubscribeMessage, nil
}
func (pl *PairingLogic) SkipTomorrow(ctx context.Context, rec *store.Recurser) (string, error) {
if !rec.IsSubscribed {
return notSubscribedMessage, nil
}
rec.IsSkippingTomorrow = true
if err := store.Recursers(pl.db).Set(ctx, rec.ID, rec); err != nil {
return writeErrorMessage, err
}
return `Tomorrow: cancelled. I feel you. **I will not match you** for pairing tomorrow <3`, nil
}
func (pl *PairingLogic) UnskipTomorrow(ctx context.Context, rec *store.Recurser) (string, error) {
if !rec.IsSubscribed {
return notSubscribedMessage, nil
}
rec.IsSkippingTomorrow = false
if err := store.Recursers(pl.db).Set(ctx, rec.ID, rec); err != nil {
return writeErrorMessage, err
}
return "Tomorrow: uncancelled! Heckin *yes*! **I will match you** for pairing tomorrow :)", nil
}
func (pl *PairingLogic) Status(ctx context.Context, rec *store.Recurser) (string, error) {
if !rec.IsSubscribed {
return notSubscribedMessage, nil
}
// this particular days list is for sorting and printing the
// schedule correctly, since it's stored in a map in all lowercase
var daysList = []string{
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
}
// get their current name
whoami := rec.Name
// get skip status and prepare to write a sentence with it
var skipStr string
if rec.IsSkippingTomorrow {
skipStr = " "
} else {
skipStr = " not "
}
// make a sorted list of their schedule
var schedule []string
for _, day := range daysList {
// this line is a little wild, sorry. it looks so weird because we
// have to do type assertion on both interface types
if rec.Schedule[strings.ToLower(day)] {
schedule = append(schedule, day)
}
}
// make a lil nice-lookin schedule string
var scheduleStr string
for i := range schedule[:len(schedule)-1] {
scheduleStr += schedule[i] + "s, "
}
if len(schedule) > 1 {
scheduleStr += "and " + schedule[len(schedule)-1] + "s"
} else if len(schedule) == 1 {
scheduleStr += schedule[0] + "s"
}
return fmt.Sprintf("* You're %v\n* You're scheduled for pairing on **%v**\n* **You're%vset to skip** pairing tomorrow", whoami, scheduleStr, skipStr), nil
}
func (pl *PairingLogic) AddReview(ctx context.Context, rec *store.Recurser, content string) (string, error) {
currentTimestamp := time.Now().Unix()
err := store.Reviews(pl.db).Insert(ctx, store.Review{
Content: content,
Timestamp: currentTimestamp,
Email: rec.Email,
})
if err != nil {
log.Println("Encountered an error when trying to save a review: ", err)
return writeErrorMessage, err
}
return "Thank you for sharing your review with pairing bot!", nil
}
func (pl *PairingLogic) GetReviews(ctx context.Context, numReviews int) (string, error) {
lastN, err := store.Reviews(pl.db).GetLastN(ctx, numReviews)
if err != nil {
log.Printf("Encountered an error when trying to fetch the last %v reviews: %v", numReviews, err)
return readErrorMessage, err
}
response := "Here are some reviews of pairing bot:\n"
for _, rev := range lastN {
response += "* \"" + rev.Content + "\"!\n"
}
return response, nil
}