forked from recursecenter/pairing-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
304 lines (247 loc) · 7.8 KB
/
database.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
package main
import (
"context"
"fmt"
"log"
"math/rand"
"strconv"
"strings"
"time"
"cloud.google.com/go/firestore"
"google.golang.org/api/iterator"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// A Schedule determines whether to pair a Recurser on each day.
// The only valid keys are all-lowercase day names (e.g., "monday").
type Schedule map[string]bool
func defaultSchedule() map[string]bool {
return map[string]bool{
"monday": true,
"tuesday": true,
"wednesday": true,
"thursday": true,
"friday": true,
"saturday": false,
"sunday": false,
}
}
func newSchedule(days []string) map[string]bool {
schedule := emptySchedule()
for _, day := range days {
schedule[day] = true
}
return schedule
}
func emptySchedule() map[string]bool {
return map[string]bool{
"monday": false,
"tuesday": false,
"wednesday": false,
"thursday": false,
"friday": false,
"saturday": false,
"sunday": false,
}
}
type Recurser struct {
ID int64 `firestore:"id"`
Name string `firestore:"name"`
Email string `firestore:"email"`
IsSkippingTomorrow bool `firestore:"isSkippingTomorrow"`
Schedule map[string]bool `firestore:"schedule"`
CurrentlyAtRC bool `firestore:"currentlyAtRC"`
// IsSubscribed really means "already had an entry in the database".
// It is not written to or read from the Firestore document.
IsSubscribed bool `firestore:"-"`
}
// FirestoreRecurserDB manages Pairing Bot subscribers ("Recursers").
type FirestoreRecurserDB struct {
client *firestore.Client
}
func (f *FirestoreRecurserDB) GetByUserID(ctx context.Context, userID int64, userEmail, userName string) (*Recurser, error) {
docID := strconv.FormatInt(userID, 10)
doc, err := f.client.Collection("recursers").Doc(docID).Get(ctx)
// A missing document still returns a non-nil doc with its NotFound error.
// Any other error is a real error.
if err != nil && status.Code(err) != codes.NotFound {
return nil, err
}
if !doc.Exists() {
// If we don't have a record, that just means they're not subscribed.
return &Recurser{
ID: userID,
Name: userName,
Email: userEmail,
Schedule: defaultSchedule(),
}, nil
}
var recurser Recurser
if err := doc.DataTo(&recurser); err != nil {
return nil, fmt.Errorf("parse document %q: %w", doc.Ref.Path, err)
}
// This field isn't stored in the DB, so populate it now.
recurser.IsSubscribed = true
// Prefer the Zulip values for these fields over our cached ones.
recurser.Name = userName
recurser.Email = userEmail
return &recurser, nil
}
func (f *FirestoreRecurserDB) GetAllUsers(ctx context.Context) ([]Recurser, error) {
iter := f.client.Collection("recursers").Documents(ctx)
return fetchAll[Recurser](iter)
}
func (f *FirestoreRecurserDB) Set(ctx context.Context, _ int64, recurser *Recurser) error {
docID := strconv.FormatInt(recurser.ID, 10)
// Merging isn't supported when using struct data, but we never do partial
// writes in the first place. So this will completely overwrite an existing
// document.
_, err := f.client.Collection("recursers").Doc(docID).Set(ctx, recurser)
return err
}
func (f *FirestoreRecurserDB) Delete(ctx context.Context, userID int64) error {
docID := strconv.FormatInt(userID, 10)
_, err := f.client.Collection("recursers").Doc(docID).Delete(ctx)
return err
}
func (f *FirestoreRecurserDB) ListPairingTomorrow(ctx context.Context) ([]Recurser, error) {
// this gets the time from system time, which is UTC
// on app engine (and most other places). This works
// fine for us in NYC, but might not if pairing bot
// were ever running in another time zone
today := strings.ToLower(time.Now().Weekday().String())
iter := f.client.
Collection("recursers").
Where("isSkippingTomorrow", "==", false).
Where("schedule."+today, "==", true).
Documents(ctx)
return fetchAll[Recurser](iter)
}
func (f *FirestoreRecurserDB) ListSkippingTomorrow(ctx context.Context) ([]Recurser, error) {
iter := f.client.
Collection("recursers").
Where("isSkippingTomorrow", "==", true).
Documents(ctx)
return fetchAll[Recurser](iter)
}
func (f *FirestoreRecurserDB) UnsetSkippingTomorrow(ctx context.Context, recurser *Recurser) error {
recurser.IsSkippingTomorrow = false
return f.Set(ctx, recurser.ID, recurser)
}
// FirestoreAPIAuthDB manages auth tokens stored in Firestore.
type FirestoreAPIAuthDB struct {
client *firestore.Client
}
func (f *FirestoreAPIAuthDB) GetToken(ctx context.Context, path string) (string, error) {
doc, err := f.client.Doc(path).Get(ctx)
if err != nil {
return "", err
}
var token struct {
Value string `firestore:"value"`
}
if err := doc.DataTo(&token); err != nil {
return "", err
}
return token.Value, nil
}
type Pairing struct {
Value int `firestore:"value"`
Timestamp int64 `firestore:"timestamp"`
}
type PairingsDB interface {
SetNumPairings(ctx context.Context, pairing Pairing) error
GetTotalPairingsDuringLastWeek(ctx context.Context) (int, error)
}
// implements RecurserDB
type FirestorePairingsDB struct {
client *firestore.Client
}
func (f *FirestorePairingsDB) SetNumPairings(ctx context.Context, pairing Pairing) error {
timestampAsString := strconv.FormatInt(pairing.Timestamp, 10)
_, err := f.client.Collection("pairings").Doc(timestampAsString).Set(ctx, pairing)
return err
}
func (f *FirestorePairingsDB) GetTotalPairingsDuringLastWeek(ctx context.Context) (int, error) {
totalPairings := 0
timestampSevenDaysAgo := time.Now().Add(-7 * 24 * time.Hour).Unix()
iter := f.client.Collection("pairings").Where("timestamp", ">", timestampSevenDaysAgo).Documents(ctx)
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return 0, err
}
var pairing Pairing
if err = doc.DataTo(&pairing); err != nil {
log.Printf("Skipping %q: %s", doc.Ref.Path, err)
continue
}
log.Println("The timestamp is: ", pairing.Timestamp)
totalPairings += pairing.Value
}
return totalPairings, nil
}
type Review struct {
Content string `firestore:"content"`
Email string `firestore:"email"`
Timestamp int64 `firestore:"timestamp"`
}
type ReviewDB interface {
GetAll(ctx context.Context) ([]Review, error)
GetLastN(ctx context.Context, n int) ([]Review, error)
GetRandom(ctx context.Context) (Review, error)
Insert(ctx context.Context, review Review) error
}
// implements ReviewDB
type FirestoreReviewDB struct {
client *firestore.Client
}
func (f *FirestoreReviewDB) GetAll(ctx context.Context) ([]Review, error) {
iter := f.client.Collection("reviews").Documents(ctx)
return fetchAll[Review](iter)
}
func (f *FirestoreReviewDB) GetLastN(ctx context.Context, n int) ([]Review, error) {
iter := f.client.
Collection("reviews").
OrderBy("timestamp", firestore.Desc).
Limit(n).
Documents(ctx)
return fetchAll[Review](iter)
}
func (f *FirestoreReviewDB) GetRandom(ctx context.Context) (Review, error) {
allReviews, err := f.GetAll(ctx)
if err != nil {
return Review{}, err
}
return allReviews[rand.Intn(len(allReviews))], nil
}
func (f *FirestoreReviewDB) Insert(ctx context.Context, review Review) error {
_, _, err := f.client.Collection("reviews").Add(ctx, review)
return err
}
// fetchAll converts all documents in iter to values of type T. Documents that
// cannot be converted will be skipped.
//
// If the iterator yields an error instead of a document, this returns the
// first such error and stops.
func fetchAll[T any](iter *firestore.DocumentIterator) ([]T, error) {
var all []T
defer iter.Stop()
for {
doc, err := iter.Next()
if err == iterator.Done {
return all, nil
} else if err != nil {
return nil, err
}
var item T
if err := doc.DataTo(&item); err != nil {
log.Printf("Skipping %q: %s", doc.Ref.Path, err)
continue
}
all = append(all, item)
}
}