-
Notifications
You must be signed in to change notification settings - Fork 0
/
refreshwantlists.go
142 lines (122 loc) · 3.78 KB
/
refreshwantlists.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
package background
import (
"context"
"fmt"
"log"
"sort"
"github.com/brotherlogic/discogs"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/brotherlogic/gramophile/proto"
)
func (b *BackgroundRunner) RefreshWantlists(ctx context.Context, di discogs.Discogs, auth string) error {
lists, err := b.db.GetWantlists(ctx, di.GetUserId())
if err != nil {
return fmt.Errorf("unable to get wantlists: %w", err)
}
for _, list := range lists {
err = b.processWantlist(ctx, di, list)
if err != nil {
return fmt.Errorf("Unable to process wantlist %v -> %w", list.GetName(), err)
}
}
return nil
}
func (b *BackgroundRunner) processWantlist(ctx context.Context, di discogs.Discogs, list *pb.Wantlist) error {
log.Printf("Processing %v", list.GetName())
records, err := b.db.LoadAllRecords(ctx, di.GetUserId())
if err != nil {
return fmt.Errorf("unable to load all records: %w", err)
}
log.Printf("Found records: %v", records)
changed := false
for _, entry := range list.GetEntries() {
// Hard sync from the want
want, err := b.db.GetWant(ctx, di.GetUserId(), entry.GetId())
if err != nil {
return err
}
entry.State = want.GetState()
if entry.GetState() == pb.WantState_WANTED {
log.Printf("STATE matches")
for _, r := range records {
if r.GetRelease().GetId() == entry.GetId() {
entry.State = pb.WantState_PURCHASED
changed = true
}
}
}
}
rchanged, err := b.refreshWantlist(ctx, di.GetUserId(), list)
if err != nil && status.Code(err) != codes.FailedPrecondition {
return fmt.Errorf("unable to refresh wantlist: %w", err)
}
if changed || rchanged {
log.Printf("List has changed: %v", list)
err := b.db.SaveWantlist(ctx, di.GetUserId(), list)
if err != nil {
return fmt.Errorf("unable to save wantlist: %w", err)
}
}
return nil
}
func (b *BackgroundRunner) refreshWantlist(ctx context.Context, userid int32, list *pb.Wantlist) (bool, error) {
switch list.GetType() {
case pb.WantlistType_ONE_BY_ONE:
return b.refreshOneByOneWantlist(ctx, userid, list)
default:
log.Printf("Failure to process want list because %v", list.GetType())
return false, status.Errorf(codes.FailedPrecondition, "%v is not currently processable (%v)", list.GetName(), list.GetType())
}
}
func (b *BackgroundRunner) refreshOneByOneWantlist(ctx context.Context, userid int32, list *pb.Wantlist) (bool, error) {
sort.SliceStable(list.GetEntries(), func(i, j int) bool {
return list.GetEntries()[i].GetIndex() < list.GetEntries()[j].GetIndex()
})
for _, entry := range list.GetEntries() {
log.Printf("Refreshing entry: %v", entry)
switch entry.GetState() {
case pb.WantState_WANTED:
if list.GetVisibility() == pb.WantlistVisibility_INVISIBLE {
return true, b.mergeWant(ctx, userid, &pb.Want{
Id: entry.GetId(),
State: pb.WantState_HIDDEN,
})
}
return false, nil
case pb.WantState_PURCHASED:
continue
case pb.WantState_PENDING, pb.WantState_RETIRED:
state := pb.WantState_WANTED
if list.GetVisibility() == pb.WantlistVisibility_INVISIBLE {
state = pb.WantState_HIDDEN
}
entry.State = state
log.Printf("ENTRY: %v", entry)
return true, b.mergeWant(ctx, userid, &pb.Want{
Id: entry.GetId(),
State: state,
})
}
}
return false, nil
}
func (b *BackgroundRunner) mergeWant(ctx context.Context, userid int32, want *pb.Want) error {
val, err := b.db.GetWant(ctx, userid, want.GetId())
if err != nil {
if status.Code(err) != codes.NotFound {
val = want
} else {
return err
}
}
if want.State != pb.WantState_HIDDEN {
val.State = want.State
}
if want.State == pb.WantState_HIDDEN {
if val.State == pb.WantState_PENDING || val.State == pb.WantState_WANTED {
val.State = want.State
}
}
return b.db.SaveWant(ctx, userid, val, "Updated from refresh wantlist")
}