/
fetch_collection.go
138 lines (111 loc) · 3.03 KB
/
fetch_collection.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
package fetch
import (
"github.com/itchio/butler/butlerd"
"github.com/itchio/butler/butlerd/messages"
"github.com/itchio/butler/database/hades"
"github.com/itchio/butler/database/models"
itchio "github.com/itchio/go-itchio"
"github.com/pkg/errors"
)
func FetchCollection(rc *butlerd.RequestContext, params *butlerd.FetchCollectionParams) (*butlerd.FetchCollectionResult, error) {
consumer := rc.Consumer
if params.CollectionID == 0 {
return nil, errors.New("collectionId must be non-zero")
}
_, client := rc.ProfileClient(params.ProfileID)
sendDBCollection := func() error {
collection := models.CollectionByID(rc.DB(), params.CollectionID)
if collection == nil {
return nil
}
models.CollectionExt(collection).PreloadCollectionGames(rc.DB())
err := messages.FetchCollectionYield.Notify(rc, &butlerd.FetchCollectionYieldNotification{Collection: collection})
if err != nil {
return errors.WithStack(err)
}
return nil
}
err := sendDBCollection()
if err != nil {
return nil, errors.WithStack(err)
}
consumer.Debugf("Querying API...")
collRes, err := client.GetCollection(&itchio.GetCollectionParams{
CollectionID: params.CollectionID,
})
if err != nil {
return nil, errors.WithStack(err)
}
collection := collRes.Collection
collection.Games = nil
collection.CollectionGames = nil
c := HadesContext(rc)
err = c.Save(rc.DB(), &hades.SaveParams{
Record: collRes.Collection,
})
if err != nil {
return nil, errors.WithStack(err)
}
// after collection metadata update
err = sendDBCollection()
if err != nil {
return nil, errors.WithStack(err)
}
var offset int64
for page := int64(1); ; page++ {
consumer.Infof("Fetching page %d", page)
gamesRes, err := client.GetCollectionGames(&itchio.GetCollectionGamesParams{
CollectionID: params.CollectionID,
Page: page,
})
if err != nil {
return nil, errors.WithStack(err)
}
numPageGames := int64(len(gamesRes.Games))
if numPageGames == 0 {
break
}
for i, game := range gamesRes.Games {
collection.CollectionGames = append(collection.CollectionGames, &itchio.CollectionGame{
Position: offset + int64(i),
Game: game,
})
}
err = c.Save(rc.DB(), &hades.SaveParams{
Record: collection,
Assocs: []string{"CollectionGames"},
PartialJoins: []string{"CollectionGames"},
})
if err != nil {
return nil, errors.WithStack(err)
}
offset += numPageGames
if offset >= collection.GamesCount {
// already fetched all or more?!
break
}
if numPageGames < gamesRes.PerPage {
// that probably means there's no more pages
break
}
// after each page of games fetched
err = sendDBCollection()
if err != nil {
return nil, errors.WithStack(err)
}
}
err = c.Save(rc.DB(), &hades.SaveParams{
Record: collection,
Assocs: []string{"CollectionGames"},
})
if err != nil {
return nil, errors.WithStack(err)
}
// after all pages are fetched
err = sendDBCollection()
if err != nil {
return nil, errors.WithStack(err)
}
res := &butlerd.FetchCollectionResult{}
return res, nil
}