-
Notifications
You must be signed in to change notification settings - Fork 0
/
dao.go
682 lines (585 loc) · 16.4 KB
/
dao.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
package humandao
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"sync"
"time"
"cloud.google.com/go/firestore"
"github.com/go-chi/httplog"
"github.com/segmentio/ksuid"
"golang.org/x/sync/errgroup"
"google.golang.org/api/iterator"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var (
ErrHumanNotFound = errors.New("human not found")
ErrHumanAlreadyExists = errors.New("human already exists")
ErrInvalidOrderBy = errors.New("orderBy must be one of: created_at, views")
ErrInvalidGender = errors.New("invalid gender")
)
type ReactionCount map[string]int
type Human struct {
ID string `firestore:"-"`
Name string `firestore:"name"`
Path string `firestore:"urn_path"`
ReactionCount ReactionCount `firestore:"reaction_count"`
DOB string `firestore:"dob,omitempty"`
DOD string `firestore:"dod,omitempty"`
Tags []string `firestore:"tags,omitempty"`
Ethnicity []string `firestore:"ethnicity,omitempty"`
BirthLocation string `firestore:"birth_location,omitempty"`
Location []string `firestore:"location,omitempty"`
InfluencedBy []string `firestore:"influenced_by,omitempty"`
FeaturedImage string `firestore:"featured_image,omitempty"`
Draft bool `firestore:"draft"`
AIGenerated bool `firestore:"ai_generated,omitempty"`
Description string `firestore:"description,omitempty"`
CreatedAt time.Time `firestore:"created_at"`
CreatedBy string `firestore:"created_by,omitempty"`
UpdatedAt time.Time `firestore:"updated_at"`
PublishedBy string `firestore:"published_by,omitempty"`
PublishedAt time.Time `firestore:"published_at,omitempty"`
Affiliates []Affiliate `firestore:"affiliates,omitempty"`
Socials Socials `firestore:"socials,omitempty"`
Views int64 `firestore:"views,omitempty"`
Gender Gender `firestore:"gender,omitempty"`
}
func (h Human) CurrentAge(inputTime ...time.Time) (string, error) {
now := time.Now()
if len(inputTime) > 0 {
now = inputTime[0]
}
if h.DOB == "" {
return "", nil
}
born, err := parseDateString(h.DOB)
if err != nil {
return "", err
}
if h.DOD != "" {
died, err := parseDateString(h.DOD)
if err != nil {
return "", err
}
ageInYears, _, _, _, _, _ := diff(died, born)
return fmt.Sprintf("%v - %v (aged %v)", displayFormat(h.DOB), displayFormat(h.DOD), ageInYears), nil
}
ageInYears, _, _, _, _, _ := diff(now, born)
return fmt.Sprintf("%v (age %v years)", displayFormat(h.DOB), ageInYears), nil
}
func displayFormat(date string) string {
parts := strings.Split(date, "-")
var month time.Month
if len(parts) > 1 {
// we have a month
monthRaw, err := strconv.Atoi(parts[1])
if err != nil {
return date
}
month = time.Month(monthRaw)
if len(parts) == 2 {
return fmt.Sprintf("%v %v", month, parts[0])
}
return fmt.Sprintf("%v %v, %v", month, strings.TrimLeft(parts[2], "0"), parts[0])
}
return date
}
func parseDateString(date string) (time.Time, error) {
format := "2006-01-02"
if len(date) == 4 {
// only have the year
format = "2006"
} else if len(date) == 7 {
// only have the year and month
format = "2006-01"
}
res, err := time.Parse(format, date)
if err != nil {
return time.Time{}, err
}
return res, nil
}
func diff(a, b time.Time) (year, month, day, hour, min, sec int) {
if a.Location() != b.Location() {
b = b.In(a.Location())
}
if a.After(b) {
a, b = b, a
}
y1, M1, d1 := a.Date()
y2, M2, d2 := b.Date()
h1, m1, s1 := a.Clock()
h2, m2, s2 := b.Clock()
year = int(y2 - y1)
month = int(M2 - M1)
day = int(d2 - d1)
hour = int(h2 - h1)
min = int(m2 - m1)
sec = int(s2 - s1)
// Normalize negative values
if sec < 0 {
sec += 60
min--
}
if min < 0 {
min += 60
hour--
}
if hour < 0 {
hour += 24
day--
}
if day < 0 {
// days in month:
t := time.Date(y1, M1, 32, 0, 0, 0, 0, time.UTC)
day += 32 - t.Day()
month--
}
if month < 0 {
month += 12
year--
}
return
}
type Gender string
const (
GenderMale Gender = "male"
GenderFemale Gender = "female"
GenderNonBinary Gender = "nonbinary"
)
var ValidGenders = map[Gender]struct{}{
GenderMale: {},
GenderFemale: {},
GenderNonBinary: {},
}
type Socials struct {
IMDB string `firestore:"imdb,omitempty"`
Website string `firestore:"website,omitempty"`
X string `firestore:"x,omitempty"`
YouTube string `firestore:"youtube,omitempty"`
Instagram string `firestore:"instagram,omitempty"`
}
type Affiliate struct {
ID string `firestore:"id,omitempty"`
URL string `firestore:"url,omitempty"`
Image string `firestore:"image,omitempty"`
Name string `firestore:"name,omitempty"`
}
type Reaction struct {
ID string `firestore:"-"`
UserID string `firestore:"user_id,omitempty"`
HumanID string `firestore:"human_id,omitempty"`
ReactionKind ReactionKind `firestore:"reaction_kind,omitempty"`
CreatedAt time.Time `firestore:"created_at,omitempty"`
}
type HumanInput struct {
HumanID string
Path string
}
func (d *DAO) Human(ctx context.Context, input HumanInput) (human Human, err error) {
logger := httplog.LogEntry(ctx)
var doc *firestore.DocumentSnapshot
if input.HumanID != "" {
doc, err = d.client.Collection(d.humanCollection).Doc(input.HumanID).Get(ctx)
} else if input.Path != "" {
doc, err = d.client.Collection(d.humanCollection).Where("urn_path", "==", input.Path).
Documents(ctx).Next()
}
if err != nil {
if status.Code(err) == codes.NotFound || err == iterator.Done {
logger.Warn().Err(err).Interface("input", input).
Str("humanID", input.HumanID).
Str("path", input.Path).
Msg("human not found")
identifier := input.HumanID
if input.Path != "" {
identifier = input.Path
}
return Human{}, fmt.Errorf("%w: %v", ErrHumanNotFound, identifier)
}
logger.Err(err).Interface("input", input).Msg("unable to get human")
return Human{}, fmt.Errorf("unable to get human: %w", err)
}
human, err = convertHumanDoc(doc)
if err != nil {
return Human{}, fmt.Errorf("unable to convert human: %w", err)
}
human.ID = doc.Ref.ID
return human, nil
}
type HumansByIDInput struct {
HumanIDs []string
}
func (d *DAO) HumansByID(ctx context.Context, input HumansByIDInput) ([]Human, error) {
group, ctx := errgroup.WithContext(ctx)
group.SetLimit(10)
var mutex sync.Mutex
humans := make([]Human, 0, len(input.HumanIDs))
for _, id := range input.HumanIDs {
id := id
group.Go(func() error {
human, err := d.Human(ctx, HumanInput{HumanID: id})
if err != nil {
return err
}
mutex.Lock()
humans = append(humans, human)
mutex.Unlock()
return nil
})
}
if err := group.Wait(); err != nil {
return nil, err
}
// Preserve ordering
idToHuman := make(map[string]Human)
for _, human := range humans {
idToHuman[human.ID] = human
}
orderedHumans := make([]Human, 0, len(humans))
for _, id := range input.HumanIDs {
orderedHumans = append(orderedHumans, idToHuman[id])
}
return orderedHumans, nil
}
func (d *DAO) UpdateHuman(ctx context.Context, human Human) error {
for i, affiliate := range human.Affiliates {
if affiliate.ID == "" {
human.Affiliates[i].ID = ksuid.New().String()
}
}
human.UpdatedAt = time.Now()
_, err := d.client.Collection(d.humanCollection).
Doc(human.ID).
Set(ctx, human)
if err != nil {
return fmt.Errorf("unable to update human: %v (%v): %w", human.Name, human.ID, err)
}
return nil
}
type AddHumanInput struct {
HumanID string
Name string
DOB string
DOD string
Ethnicity []string
Description string
Location []string
Website string
Twitter string
Instagram string
IMDB string
Tags []string
Draft bool
CreatedBy string
Affiliates []Affiliate
Gender Gender
}
func (d *DAO) AddHuman(ctx context.Context, input AddHumanInput) (Human, error) {
path := strings.ToLower(strings.ReplaceAll(input.Name, " ", "-"))
if input.Name == "" {
return Human{}, fmt.Errorf("name must be provided")
}
_, err := d.Human(ctx, HumanInput{Path: path})
if err != nil {
if !errors.Is(err, ErrHumanNotFound) {
return Human{}, fmt.Errorf("error checking if human (%v) exists: %w", path, err)
}
}
if err == nil {
return Human{}, ErrHumanAlreadyExists
}
for i, affiliate := range input.Affiliates {
if affiliate.ID == "" {
input.Affiliates[i].ID = ksuid.New().String()
}
}
_, ok := ValidGenders[input.Gender]
if !ok {
return Human{}, ErrInvalidGender
}
now := time.Now().In(time.UTC)
human := Human{
Name: input.Name,
DOB: input.DOB,
DOD: input.DOD,
Ethnicity: input.Ethnicity,
Description: input.Description,
Location: input.Location,
Tags: input.Tags,
Draft: input.Draft,
CreatedAt: now,
CreatedBy: input.CreatedBy,
Path: path,
UpdatedAt: now,
Affiliates: input.Affiliates,
Socials: Socials{
Website: input.Website,
X: input.Twitter,
IMDB: input.IMDB,
Instagram: input.Instagram,
},
Gender: input.Gender,
}
if input.HumanID == "" {
input.HumanID = ksuid.New().String()
}
_, err = d.client.Collection(d.humanCollection).Doc(input.HumanID).Create(ctx, human)
if err != nil {
return Human{}, fmt.Errorf("unable to create human: %w", err)
}
human.ID = input.HumanID
return human, nil
}
var ErrUnauthorized = errors.New("user is not authorized to perform this operation")
type ReactionKind string
var (
ReactionKindLove ReactionKind = "love"
ReactionKindFire ReactionKind = "fire"
ReactionKindJoy ReactionKind = "joy"
ReactionKindFlower ReactionKind = "flower"
)
var validReactionKinds = map[ReactionKind]struct{}{
ReactionKindLove: {},
ReactionKindFire: {},
ReactionKindJoy: {},
ReactionKindFlower: {},
}
var AllReactionKinds = []ReactionKind{ReactionKindLove, ReactionKindFire, ReactionKindJoy}
func ToReactionKind(kind string) (ReactionKind, error) {
if _, ok := validReactionKinds[ReactionKind(kind)]; !ok {
return "", fmt.Errorf("invalid reaction kind")
}
return ReactionKind(kind), nil
}
type ReactInput struct {
UserID string
HumanID string
ReactionKind ReactionKind
}
func (d *DAO) React(ctx context.Context, input ReactInput) (Reaction, error) {
data := Reaction{
UserID: input.UserID,
HumanID: input.HumanID,
ReactionKind: input.ReactionKind,
CreatedAt: time.Now(),
}
err := d.client.RunTransaction(ctx, func(ctx context.Context, tx *firestore.Transaction) error {
reactionRef := d.client.Collection(d.reactionCollection).NewDoc()
if err := tx.Create(reactionRef, data); err != nil {
return fmt.Errorf("unable to create reaction: %w", err)
}
humanRef := d.client.Collection(d.humanCollection).Doc(input.HumanID)
if err := tx.Update(humanRef, []firestore.Update{
{
Path: fmt.Sprintf("reaction_count.%v", input.ReactionKind),
Value: firestore.Increment(1),
},
}); err != nil {
return fmt.Errorf("unable to update reaction count: %w", err)
}
data.ID = reactionRef.ID
return nil
})
if err != nil {
return Reaction{}, err
}
return data, nil
}
type ReactUndoInput struct {
UserID string
ReactionID string
}
func (d *DAO) ReactUndo(ctx context.Context, input ReactUndoInput) error {
doc := d.client.Collection(d.reactionCollection).Doc(input.ReactionID)
reactionRef, err := doc.Get(ctx)
if err != nil {
if status.Code(err) == codes.NotFound {
return nil
}
return fmt.Errorf("unable to find reaction by id: %v: %w", input.ReactionID, err)
}
reaction, err := convertReactionDoc(reactionRef)
if err != nil {
return err
}
if reaction.UserID != input.UserID {
return ErrUnauthorized
}
err = d.client.RunTransaction(ctx, func(ctx context.Context, tx *firestore.Transaction) error {
if err := tx.Delete(doc, firestore.Exists); err != nil {
return fmt.Errorf("error deleting reaction: %w", err)
}
humanRef := d.client.Collection(d.humanCollection).Doc(reaction.HumanID)
if err := tx.Update(humanRef, []firestore.Update{
{
Path: fmt.Sprintf("reaction_count.%v", reaction.ReactionKind),
Value: firestore.Increment(-1),
},
}); err != nil {
return fmt.Errorf("error decrementing reaction count: %w", err)
}
return nil
})
if err != nil {
return fmt.Errorf("error running transaction: %w", err)
}
return nil
}
type GetReactionsInput struct {
UserID string
}
func (d *DAO) GetReactions(ctx context.Context, input GetReactionsInput) ([]Reaction, error) {
docs, err := d.client.Collection(d.reactionCollection).
Where("user_id", "==", input.UserID).
OrderBy("created_at", firestore.Asc).
Documents(ctx).GetAll()
if err != nil {
return nil, fmt.Errorf("unable to get reactions for user %s: %w", input.UserID, err)
}
return convertReactionDocs(docs)
}
type OrderBy string
var (
OrderByCreatedAt OrderBy = "created_at"
OrderByViews OrderBy = "views"
)
type ListHumansInput struct {
Limit int
Offset int
OrderBy OrderBy
Direction firestore.Direction
IncludeDrafts bool
}
func (d *DAO) ListHumans(ctx context.Context, input ListHumansInput) ([]Human, error) {
allowedOrderBy := map[OrderBy]struct{}{
OrderByCreatedAt: {},
OrderByViews: {},
}
query := d.client.Collection(d.humanCollection).Query
if !input.IncludeDrafts {
query = query.Where("draft", "==", false)
}
if input.OrderBy == "" {
query = query.OrderBy(string(OrderByCreatedAt), firestore.Desc)
} else {
if _, ok := allowedOrderBy[input.OrderBy]; !ok {
return nil, ErrInvalidOrderBy
}
query = query.OrderBy(string(input.OrderBy), input.Direction)
}
docs, err := query.
Offset(input.Offset).
Limit(input.Limit).
Documents(ctx).
GetAll()
if err != nil {
return nil, fmt.Errorf("unable to get humans: %w", err)
}
return convertHumansDocs(docs)
}
type CreatedByInput struct {
CreatedBy string
Limit int
Offset int
}
func (d *DAO) CreatedBy(ctx context.Context, input CreatedByInput) ([]Human, error) {
docs, err := d.client.Collection(d.humanCollection).
Where("created_by", "==", input.CreatedBy).
OrderBy("created_at", firestore.Desc).
Offset(input.Offset).
Limit(input.Limit).
Documents(ctx).
GetAll()
if err != nil {
return nil, fmt.Errorf("unable to get humans: %w", err)
}
return convertHumansDocs(docs)
}
type UserDraftsInput struct {
Limit int
Offset int
UserID string
}
func (d *DAO) UserDrafts(ctx context.Context, input UserDraftsInput) ([]Human, error) {
docs, err := d.client.Collection(d.humanCollection).
Where("draft", "==", true).
Where("created_by", "==", input.UserID).
OrderBy("created_at", firestore.Desc).
Offset(input.Offset).
Limit(input.Limit).
Documents(ctx).
GetAll()
if err != nil {
return nil, fmt.Errorf("unable to get humans: %w", err)
}
return convertHumansDocs(docs)
}
type DraftsInput struct {
Limit int
Offset int
}
func (d *DAO) Drafts(ctx context.Context, input DraftsInput) ([]Human, error) {
docs, err := d.client.Collection(d.humanCollection).
Where("draft", "==", true).
OrderBy("created_at", firestore.Asc).
Offset(input.Offset).
Limit(input.Limit).
Documents(ctx).
GetAll()
if err != nil {
return nil, fmt.Errorf("unable to get humans: %w", err)
}
return convertHumansDocs(docs)
}
type PublishInput struct {
HumanID string
UserID string
}
func (d *DAO) Publish(ctx context.Context, input PublishInput) error {
now := time.Now()
_, err := d.client.Collection(d.humanCollection).
Doc(input.HumanID).
Update(ctx, []firestore.Update{
{Path: "draft", Value: false},
{Path: "published_by", Value: input.UserID},
{Path: "published_at", Value: now},
})
if err != nil {
if status.Code(err) == codes.NotFound {
return ErrHumanNotFound
}
return fmt.Errorf("unable to update human: %v: %w", input.HumanID, err)
}
return nil
}
type DeleteInput struct {
HumanID string
}
func (d *DAO) Delete(ctx context.Context, input DeleteInput) error {
_, err := d.client.Collection(d.humanCollection).
Doc(input.HumanID).
Delete(ctx)
if err != nil {
return fmt.Errorf("unable to delete human: %v: %w", input.HumanID, err)
}
return nil
}
type ViewInput struct {
HumanID string
}
func (d *DAO) View(ctx context.Context, input ViewInput) error {
_, err := d.client.Collection(d.humanCollection).
Doc(input.HumanID).
Update(ctx, []firestore.Update{
{Path: "views", Value: firestore.Increment(1)},
})
if err != nil {
return fmt.Errorf("unable to view human: %v: %w", input.HumanID, err)
}
return nil
}