-
Notifications
You must be signed in to change notification settings - Fork 7
/
mod.go
288 lines (218 loc) · 7.31 KB
/
mod.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
package postgres
import (
"context"
"fmt"
"math"
"strings"
"time"
"github.com/satisfactorymodding/smr-api/generated"
"github.com/satisfactorymodding/smr-api/models"
"github.com/satisfactorymodding/smr-api/util"
"github.com/patrickmn/go-cache"
"gorm.io/gorm"
)
func GetModByID(ctx context.Context, modID string) *Mod {
cacheKey := "GetModById_" + modID
if mod, ok := dbCache.Get(cacheKey); ok {
return mod.(*Mod)
}
var mod Mod
DBCtx(ctx).Find(&mod, "id = ?", modID)
if mod.ID == "" {
return nil
}
dbCache.Set(cacheKey, &mod, cache.DefaultExpiration)
return &mod
}
func GetModByReference(ctx context.Context, modReference string) *Mod {
cacheKey := "GetModByReference_" + modReference
if mod, ok := dbCache.Get(cacheKey); ok {
return mod.(*Mod)
}
var mod Mod
DBCtx(ctx).Find(&mod, "mod_reference = ?", modReference)
if mod.ID == "" {
return nil
}
dbCache.Set(cacheKey, &mod, cache.DefaultExpiration)
return &mod
}
func GetModsByID(ctx context.Context, modIds []string) []Mod {
cacheKey := "GetModsById_" + strings.Join(modIds, ":")
if mods, ok := dbCache.Get(cacheKey); ok {
return mods.([]Mod)
}
var mods []Mod
DBCtx(ctx).Find(&mods, "id in (?)", modIds)
if len(modIds) != len(mods) {
return nil
}
dbCache.Set(cacheKey, mods, cache.DefaultExpiration)
return mods
}
func DeleteMod(ctx context.Context, modID string) {
DBCtx(ctx).Delete(Mod{}, "id = ?", modID)
DBCtx(ctx).Delete(Version{}, "mod_id = ?", modID)
DBCtx(ctx).Delete(UserMod{}, "mod_id = ?", modID)
}
func GetModCount(ctx context.Context, search string, unapproved bool) int64 {
cacheLey := "GetModCount_" + search + "_" + fmt.Sprint(unapproved)
if count, ok := dbCache.Get(cacheLey); ok {
return count.(int64)
}
var modCount int64
query := DBCtx(ctx).Model(Mod{}).Where("approved = ? AND denied = ?", !unapproved, false)
if search != "" {
query = query.Where("to_tsvector(name) @@ to_tsquery(?)", strings.Replace(search, " ", " & ", -1))
}
query.Count(&modCount)
dbCache.Set(cacheLey, modCount, cache.DefaultExpiration)
return modCount
}
func GetModCountNew(ctx context.Context, filter *models.ModFilter, unapproved bool) int64 {
hash, err := filter.Hash()
cacheKey := ""
if err == nil {
cacheKey = "GetModCountNew_" + hash + "_" + fmt.Sprint(unapproved)
if count, ok := dbCache.Get(cacheKey); ok {
return count.(int64)
}
}
var modCount int64
NewModQuery(ctx, filter, unapproved, true).Count(&modCount)
if cacheKey != "" {
dbCache.Set(cacheKey, modCount, cache.DefaultExpiration)
}
return modCount
}
func IncrementModViews(ctx context.Context, mod *Mod) {
DBCtx(ctx).Model(mod).Update("views", mod.Views+1)
}
func GetMods(ctx context.Context, limit int, offset int, orderBy string, order string, search string, unapproved bool) []Mod {
cacheKey := "GetMods_" + fmt.Sprint(limit) + "_" + fmt.Sprint(offset) + "_" + orderBy + "_" + order + "_" + search + "_" + fmt.Sprint(unapproved)
if mods, ok := dbCache.Get(cacheKey); ok {
return mods.([]Mod)
}
var mods []Mod
query := DBCtx(ctx).Limit(limit).Offset(offset)
if orderBy == "last_version_date" {
query = query.Order("case when last_version_date is null then 1 else 0 end, last_version_date")
} else {
query = query.Order(orderBy + " " + order)
}
query = query.Where("approved = ? AND denied = ?", !unapproved, false)
if search != "" {
query = query.Where("to_tsvector(name) @@ to_tsquery(?)", strings.Replace(search, " ", " & ", -1))
}
query.Find(&mods)
dbCache.Set(cacheKey, mods, cache.DefaultExpiration)
return mods
}
func GetModsNew(ctx context.Context, filter *models.ModFilter, unapproved bool) []Mod {
hash, err := filter.Hash()
cacheKey := ""
if err == nil {
cacheKey = "GetModsNew_" + hash + "_" + fmt.Sprint(unapproved)
if mods, ok := dbCache.Get(cacheKey); ok {
return mods.([]Mod)
}
}
var mods []Mod
NewModQuery(ctx, filter, unapproved, false).Find(&mods)
if cacheKey != "" {
dbCache.Set(cacheKey, mods, cache.DefaultExpiration)
}
return mods
}
func CreateMod(ctx context.Context, mod *Mod) (*Mod, error) {
// Allow only new 4 mods per 24h
mod.ID = util.GenerateUniqueID()
var mods []Mod
DBCtx(ctx).Order("created_at asc").Find(&mods, "creator_id = ? AND created_at > ?", mod.CreatorID, time.Now().Add(time.Hour*24*-1))
currentAvailable := float64(4)
lastModTime := time.Now()
for _, mod := range mods {
currentAvailable--
if mod.CreatedAt.After(lastModTime) {
diff := mod.CreatedAt.Sub(lastModTime)
currentAvailable = math.Min(4, currentAvailable+diff.Hours()/6)
}
lastModTime = mod.CreatedAt
}
if currentAvailable < 1 {
timeToWait := time.Until(lastModTime.Add(time.Hour * 6)).Minutes()
return nil, fmt.Errorf("please wait %.0f minutes to post another mod", timeToWait)
}
DBCtx(ctx).Create(&mod)
DBCtx(ctx).Create(&UserMod{
Role: "creator",
ModID: mod.ID,
UserID: mod.CreatorID,
})
return mod, nil
}
func NewModQuery(ctx context.Context, filter *models.ModFilter, unapproved bool, count bool) *gorm.DB {
query := DBCtx(ctx)
if count {
query = query.Model(Mod{})
}
query = query.Where("approved = ? AND denied = ?", !unapproved, false)
if filter != nil {
if filter.Search != nil && *filter.Search != "" {
cleanSearch := strings.Replace(strings.TrimSpace(*filter.Search), " ", " & ", -1)
sub := DBCtx(ctx).Table("mods")
sub = sub.Select("id, (similarity(name, ?) * 2 + similarity(short_description, ?) + similarity(full_description, ?) * 0.5) as s", cleanSearch, cleanSearch, cleanSearch)
query = query.Joins("INNER JOIN (?) AS t1 on t1.id = mods.id", sub)
query = query.Where("t1.s > 0.2")
if !count && *filter.OrderBy == generated.ModFieldsSearch {
query = query.Order("t1.s DESC")
}
}
if !count {
query = query.Limit(*filter.Limit).
Offset(*filter.Offset)
if *filter.OrderBy != generated.ModFieldsSearch {
if string(*filter.OrderBy) == "last_version_date" {
query = query.Order("case when last_version_date is null then 1 else 0 end, last_version_date " + string(*filter.Order))
} else {
query = query.Order(string(*filter.OrderBy) + " " + string(*filter.Order))
}
}
}
if filter.Hidden == nil || !(*filter.Hidden) {
query = query.Where("hidden = false")
}
if filter.Ids != nil && len(filter.Ids) > 0 {
query = query.Where("id in (?)", filter.Ids)
} else if filter.References != nil && len(filter.References) > 0 {
query = query.Where("mod_reference in (?)", filter.References)
}
if filter.Fields != nil && len(filter.Fields) > 0 {
query = query.Select(filter.Fields)
}
}
return query.Debug()
}
func GetModByIDOrReference(ctx context.Context, modIDOrReference string) *Mod {
cacheKey := "GetModByIDOrReference_" + modIDOrReference
if mod, ok := dbCache.Get(cacheKey); ok {
return mod.(*Mod)
}
var mod Mod
DBCtx(ctx).Find(&mod, "mod_reference = ? OR id = ?", modIDOrReference, modIDOrReference)
if mod.ID == "" {
return nil
}
dbCache.Set(cacheKey, &mod, cache.DefaultExpiration)
return &mod
}
func GetModsByIDOrReference(ctx context.Context, modIDOrReferences []string) []Mod {
cacheKey := "GetModsByIDOrReference_" + strings.Join(modIDOrReferences, "-")
if mod, ok := dbCache.Get(cacheKey); ok {
return mod.([]Mod)
}
var mods []Mod
DBCtx(ctx).Find(&mods, "mod_reference IN ? OR id IN ?", modIDOrReferences, modIDOrReferences)
dbCache.Set(cacheKey, mods, cache.DefaultExpiration)
return mods
}