/
version.go
362 lines (280 loc) · 10 KB
/
version.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
package postgres
import (
"context"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/satisfactorymodding/smr-api/models"
"github.com/satisfactorymodding/smr-api/util"
"github.com/patrickmn/go-cache"
)
var semverCheck = regexp.MustCompile(`^(<=|<|>|>=|\^)?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$`)
func GetVersionsByID(ctx context.Context, versionIds []string) []Version {
cacheKey := "GetVersionsById_" + strings.Join(versionIds, ":")
if versions, ok := dbCache.Get(cacheKey); ok {
return versions.([]Version)
}
var versions []Version
DBCtx(ctx).Find(&versions, "id in (?)", versionIds)
if len(versionIds) != len(versions) {
return nil
}
dbCache.Set(cacheKey, versions, cache.DefaultExpiration)
return versions
}
func GetModLatestVersions(ctx context.Context, modID string, unapproved bool) *[]Version {
cacheKey := "GetModLatestVersions_" + modID + "_" + fmt.Sprint(unapproved)
if versions, ok := dbCache.Get(cacheKey); ok {
return versions.(*[]Version)
}
var versions []Version
DBCtx(ctx).Select("distinct on (mod_id, stability) *").
Where("mod_id = ?", modID).
Where("approved = ? AND denied = ?", !unapproved, false).
Order("mod_id, stability, created_at desc").
Find(&versions)
dbCache.Set(cacheKey, &versions, cache.DefaultExpiration)
return &versions
}
func GetModsLatestVersions(ctx context.Context, modIds []string, unapproved bool) *[]Version {
cacheKey := "GetModsLatestVersions_" + strings.Join(modIds, ":") + "_" + fmt.Sprint(unapproved)
if versions, ok := dbCache.Get(cacheKey); ok {
return versions.(*[]Version)
}
var versions []Version
DBCtx(ctx).Select("distinct on (mod_id, stability) *").
Where("mod_id in (?)", modIds).
Where("approved = ? AND denied = ?", !unapproved, false).
Order("mod_id, stability, created_at desc").
Find(&versions)
dbCache.Set(cacheKey, &versions, cache.DefaultExpiration)
return &versions
}
func GetModVersions(ctx context.Context, modID string, limit int, offset int, orderBy string, order string, unapproved bool) []Version {
cacheKey := "GetModVersions_" + modID + "_" + fmt.Sprint(limit) + "_" + fmt.Sprint(offset) + "_" + orderBy + "_" + order + "_" + fmt.Sprint(unapproved)
if versions, ok := dbCache.Get(cacheKey); ok {
return versions.([]Version)
}
var versions []Version
DBCtx(ctx).Limit(limit).Offset(offset).Order(orderBy+" "+order).Where("approved = ? AND denied = ?", !unapproved, false).Find(&versions, "mod_id = ?", modID)
dbCache.Set(cacheKey, versions, cache.DefaultExpiration)
return versions
}
func GetModVersionsNew(ctx context.Context, modID string, filter *models.VersionFilter, unapproved bool) []Version {
hash, err := filter.Hash()
cacheKey := ""
if err == nil {
cacheKey = "GetModVersionsNew_" + modID + "_" + hash + "_" + fmt.Sprint(unapproved)
if versions, ok := dbCache.Get(cacheKey); ok {
return versions.([]Version)
}
}
var versions []Version
query := DBCtx(ctx)
if filter != nil {
query = query.Limit(*filter.Limit).
Offset(*filter.Offset).
Order(string(*filter.OrderBy) + " " + string(*filter.Order))
}
query.Where("approved = ? AND denied = ?", !unapproved, false).Find(&versions, "mod_id = ?", modID)
if cacheKey != "" {
dbCache.Set(cacheKey, versions, cache.DefaultExpiration)
}
return versions
}
func GetModVersion(ctx context.Context, modID string, versionID string) *Version {
cacheKey := "GetModVersion_" + modID + "_" + versionID
if version, ok := dbCache.Get(cacheKey); ok {
return version.(*Version)
}
var version Version
DBCtx(ctx).First(&version, "mod_id = ? AND id = ?", modID, versionID)
if version.ID == "" {
return nil
}
dbCache.Set(cacheKey, &version, cache.DefaultExpiration)
return &version
}
func GetModVersionByName(ctx context.Context, modID string, versionName string) *Version {
cacheKey := "GetModVersionByName_" + modID + "_" + versionName
if version, ok := dbCache.Get(cacheKey); ok {
return version.(*Version)
}
var version Version
DBCtx(ctx).First(&version, "mod_id = ? AND version = ?", modID, versionName)
if version.ID == "" {
return nil
}
dbCache.Set(cacheKey, &version, cache.DefaultExpiration)
return &version
}
func CreateVersion(ctx context.Context, version *Version) error {
var versionCount int64
DBCtx(ctx).Model(Version{}).Where("mod_id = ? AND version = ?", version.ModID, version.Version).Count(&versionCount)
if versionCount > 0 {
return errors.New("this mod already has a version with this name")
}
// Allow only new 5 versions per 24h
var versions []Version
DBCtx(ctx).Order("created_at asc").Find(&versions, "mod_id = ? AND created_at > ?", version.ModID, time.Now().Add(time.Hour*24*-1))
if len(versions) >= 5 {
timeToWait := time.Until(versions[0].CreatedAt.Add(time.Hour * 24)).Minutes()
return fmt.Errorf("please wait %.0f minutes to post another version", timeToWait)
}
version.ID = util.GenerateUniqueID()
DBCtx(ctx).Create(&version)
return nil
}
func IncrementVersionDownloads(ctx context.Context, version *Version) {
DBCtx(ctx).Model(version).Update("downloads", version.Downloads+1)
}
func GetVersion(ctx context.Context, versionID string) *Version {
cacheKey := "GetVersion_" + versionID
if version, ok := dbCache.Get(cacheKey); ok {
return version.(*Version)
}
var version Version
DBCtx(ctx).First(&version, "id = ?", versionID)
if version.ID == "" {
return nil
}
dbCache.Set(cacheKey, &version, cache.DefaultExpiration)
return &version
}
func GetVersionsNew(ctx context.Context, filter *models.VersionFilter, unapproved bool) []Version {
hash, err := filter.Hash()
cacheKey := ""
if err == nil {
cacheKey = "GetVersionsNew_" + hash + "_" + fmt.Sprint(unapproved)
if versions, ok := dbCache.Get(cacheKey); ok {
return versions.([]Version)
}
}
var versions []Version
query := DBCtx(ctx).Where("approved = ? AND denied = ?", !unapproved, false)
if filter != nil {
query = query.Limit(*filter.Limit).
Offset(*filter.Offset).
Order(string(*filter.OrderBy) + " " + string(*filter.Order))
if filter.Search != nil && *filter.Search != "" {
query = query.Where("to_tsvector(version) @@ to_tsquery(?)", strings.Replace(*filter.Search, " ", " & ", -1))
}
if filter.Fields != nil && len(filter.Fields) > 0 {
query = query.Select(filter.Fields)
}
}
query.Find(&versions)
if cacheKey != "" {
dbCache.Set(cacheKey, versions, cache.DefaultExpiration)
}
return versions
}
func GetVersionCountNew(ctx context.Context, filter *models.VersionFilter, unapproved bool) int64 {
hash, err := filter.Hash()
cacheKey := ""
if err == nil {
cacheKey = "GetVersionCountNew_" + hash + "_" + fmt.Sprint(unapproved)
if versionCount, ok := dbCache.Get(cacheKey); ok {
return versionCount.(int64)
}
}
var versionCount int64
query := DBCtx(ctx).Model(Version{}).Where("approved = ? AND denied = ?", !unapproved, false)
if filter != nil {
if filter.Search != nil && *filter.Search != "" {
query = query.Where("to_tsvector(version) @@ to_tsquery(?)", strings.Replace(*filter.Search, " ", " & ", -1))
}
}
query.Count(&versionCount)
if cacheKey != "" {
dbCache.Set(cacheKey, versionCount, cache.DefaultExpiration)
}
return versionCount
}
func GetVersionDependencies(ctx context.Context, versionID string) []VersionDependency {
var versionDependencies []VersionDependency
DBCtx(ctx).Where("version_id = ?", versionID).Find(&versionDependencies)
return versionDependencies
}
func GetModVersionsConstraint(ctx context.Context, modID string, constraint string) []Version {
matches := semverCheck.FindAllStringSubmatch(constraint, -1)
if len(matches) == 0 {
return nil
}
major, err := strconv.Atoi(matches[0][2])
if err != nil {
return nil
}
minor, err := strconv.Atoi(matches[0][3])
if err != nil {
return nil
}
patch, err := strconv.Atoi(matches[0][4])
if err != nil {
return nil
}
query := DBCtx(ctx).Where("mod_id", modID)
/*
<=1.2.3
major < 1
major = 1, minor < 2
major = 1, minor = 2, patch <= 3
<1.2.3
major < 1
major = 1, minor < 2
major = 1, minor = 2, patch < 3
>1.2.3
major > 1
major = 1, minor > 2
major = 1, minor = 2, patch > 3
>=1.2.3
major > 1
major = 1, minor > 2
major = 1, minor = 2, patch >= 3
1.2.3
major = 1, minor = 2, patch = 3
^1.2.3 (>=1.2.3, <2.0.0)
major = 1, minor > 2
major = 1, minor = 2, patch >= 3
^0.2.3 (>=0.2.3, <0.3.0)
major = 0, minor = 2, patch >= 3
^0.0.3 (>=0.0.3, <0.0.4)
major = 0, minor = 0, patch = 3
*/
sign := matches[0][1]
switch sign {
case "<=":
query = query.Where(db.Or("version_major < ?", major).
Or("version_major = ? AND version_minor < ?", major, minor).
Or("version_major = ? AND version_minor = ? AND version_patch <= ?", major, minor, patch))
case "<":
query = query.Where(db.Or("version_major < ?", major).
Or("version_major = ? AND version_minor < ?", major, minor).
Or("version_major = ? AND version_minor = ? AND version_patch < ?", major, minor, patch))
case ">":
query = query.Where(db.Or("version_major > ?", major).
Or("version_major = ? AND version_minor > ?", major, minor).
Or("version_major = ? AND version_minor = ? AND version_patch > ?", major, minor, patch))
case ">=":
query = query.Where(db.Or("version_major > ?", major).
Or("version_major = ? AND version_minor > ?", major, minor).
Or("version_major = ? AND version_minor = ? AND version_patch >= ?", major, minor, patch))
case "^":
if major != 0 {
query = query.Where(db.Or("version_major = ? AND version_minor > ?", major, minor).
Or("version_major = ? AND version_minor = ? AND version_patch >= ?", major, minor, patch))
} else if minor != 0 {
query = query.Where("version_major = ? AND version_minor = ? AND version_patch >= ?", major, minor, patch)
} else {
query = query.Where("version_major = ? AND version_minor = ? AND version_patch = ?", major, minor, patch)
}
default:
query = query.Where("version_major = ? AND version_minor = ? AND version_patch = ?", major, minor, patch)
}
var versions []Version
query.Find(&versions)
return versions
}