forked from NyaaPantsu/nyaa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.go
222 lines (198 loc) · 7.45 KB
/
find.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
package torrents
import (
"errors"
"net/http"
"strconv"
"strings"
"time"
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/models"
"github.com/NyaaPantsu/nyaa/utils/cache"
"github.com/NyaaPantsu/nyaa/utils/search/structs"
)
/* Function to interact with Models
*
* Get the torrents with where clause
*
*/
// FindByID : get a torrent with its id
func FindByID(id uint) (*models.Torrent, error) {
torrent := &models.Torrent{ID: id}
var err error
if found, ok := cache.C.Get(torrent.Identifier()); ok {
return found.(*models.Torrent), nil
}
tmp := models.ORM.Where("torrent_id = ?", id).Preload("Scrape").Preload("Uploader").Preload("Comments")
if id > config.Get().Models.LastOldTorrentID {
tmp = tmp.Preload("FileList")
}
if id <= config.Get().Models.LastOldTorrentID && !config.IsSukebei() {
// only preload old comments if they could actually exist
tmp = tmp.Preload("OldComments")
}
err = tmp.Error
if err != nil {
return torrent, err
}
if tmp.Find(torrent).RecordNotFound() {
err = errors.New("Article is not found")
return torrent, err
}
torrent.ParseLanguages()
// GORM relly likes not doing its job correctly
// (or maybe I'm just retarded)
torrent.OldUploader = ""
if torrent.ID <= config.Get().Models.LastOldTorrentID && torrent.UploaderID == 0 {
var tmp models.UserUploadsOld
if !models.ORM.Where("torrent_id = ?", torrent.ID).Find(&tmp).RecordNotFound() {
torrent.OldUploader = tmp.Username
}
}
for i := range torrent.Comments {
torrent.Comments[i].User = new(models.User)
models.ORM.Where("user_id = ?", torrent.Comments[i].UserID).Find(torrent.Comments[i].User)
}
cache.C.Set(torrent.Identifier(), torrent, 5*time.Minute)
return torrent, nil
}
// FindRawByID : Get torrent with id without user or comments
// won't fetch user or comments
func FindRawByID(id uint) (torrent models.Torrent, err error) {
err = nil
if models.ORM.Where("torrent_id = ?", id).Find(&torrent).RecordNotFound() {
err = errors.New("Torrent is not found")
}
torrent.ParseLanguages()
return
}
// FindUnscopeByID : Get torrent with ID deleted or not
func FindUnscopeByID(id uint) (torrent models.Torrent, err error) {
err = nil
if models.ORM.Unscoped().Where("torrent_id = ?", id).Preload("Uploader").Find(&torrent).RecordNotFound() {
err = errors.New("Torrent is not found")
}
torrent.ParseLanguages()
return
}
// FindRawByHash : Get torrent with id without user or comments
// won't fetch user or comments
func FindRawByHash(hash string) (torrent models.Torrent, err error) {
err = nil
if models.ORM.Where("torrent_hash = ?", hash).Find(&torrent).RecordNotFound() {
err = errors.New("Torrent is not found")
}
torrent.ParseLanguages()
return
}
// FindOrderByNoCount : Get torrents based on search without counting and user
func FindOrderByNoCount(parameters *structs.WhereParams, orderBy string, limit int, offset int) (torrents []models.Torrent, err error) {
torrents, _, err = findOrderBy(parameters, orderBy, limit, offset, false, false, false)
return
}
// FindOrderBy : Get torrents based on search without user
func FindOrderBy(parameters *structs.WhereParams, orderBy string, limit int, offset int) (torrents []models.Torrent, count int, err error) {
torrents, count, err = findOrderBy(parameters, orderBy, limit, offset, true, false, false)
return
}
// FindWithUserOrderBy : Get torrents based on search with user
func FindWithUserOrderBy(parameters *structs.WhereParams, orderBy string, limit int, offset int) (torrents []models.Torrent, count int, err error) {
torrents, count, err = findOrderBy(parameters, orderBy, limit, offset, true, true, false)
return
}
func findOrderBy(parameters *structs.WhereParams, orderBy string, limit int, offset int, countAll bool, withUser bool, deleted bool) (
torrents []models.Torrent, count int, err error,
) {
var conditionArray []string
var params []interface{}
if parameters != nil { // if there is where parameters
if len(parameters.Conditions) > 0 {
conditionArray = append(conditionArray, parameters.Conditions)
}
params = parameters.Params
}
if !deleted {
conditionArray = append(conditionArray, "deleted_at IS NULL")
} else {
conditionArray = append(conditionArray, "deleted_at NOT NULL")
}
conditions := strings.Join(conditionArray, " AND ")
/*if found, ok := cache.C.Get(fmt.Sprintf("%v", parameters)); ok {
torrentCache := found.(*structs.TorrentCache)
torrents = torrentCache.Torrents
count = torrentCache.Count
return
}*/
if countAll {
err = models.ORM.Unscoped().Model(&torrents).Where(conditions, params...).Count(&count).Error
if err != nil {
return
}
}
// build custom db query for performance reasons
dbQuery := "SELECT * FROM " + config.Get().Models.TorrentsTableName
if conditions != "" {
dbQuery = dbQuery + " WHERE " + conditions
}
if orderBy == "" { // default OrderBy
orderBy = "torrent_id DESC"
}
dbQuery = dbQuery + " ORDER BY " + orderBy
if limit != 0 || offset != 0 { // if limits provided
dbQuery = dbQuery + " LIMIT " + strconv.Itoa(limit) + " OFFSET " + strconv.Itoa(offset)
}
dbQ := models.ORM.Preload("Scrape")
if withUser {
dbQ = dbQ.Preload("Uploader")
}
if countAll {
dbQ = dbQ.Preload("Comments")
}
err = dbQ.Preload("FileList").Raw(dbQuery, params...).Find(&torrents).Error
// cache.C.Set(fmt.Sprintf("%v", parameters), &structs.TorrentCache{torrents, count}, 5*time.Minute) // Cache shouldn't be done here but in search util
return
}
// Find obtain a list of torrents matching 'parameters' from the
// database. The list will be of length 'limit' and in default order.
// GetTorrents returns the first records found. Later records may be retrieved
// by providing a positive 'offset'
func Find(parameters structs.WhereParams, limit int, offset int) ([]models.Torrent, int, error) {
return FindOrderBy(¶meters, "", limit, offset)
}
// FindDB : Get Torrents with where parameters but no limit and order by default (get all the torrents corresponding in the db)
func FindDB(parameters structs.WhereParams) ([]models.Torrent, int, error) {
return FindOrderBy(¶meters, "", 0, 0)
}
// FindAllOrderBy : Get all torrents ordered by parameters
func FindAllOrderBy(orderBy string, limit int, offset int) ([]models.Torrent, int, error) {
return FindOrderBy(nil, orderBy, limit, offset)
}
// FindAll : Get all torrents without order
func FindAll(limit int, offset int) ([]models.Torrent, int, error) {
return FindOrderBy(nil, "", limit, offset)
}
// GetAllInDB : Get all torrents
func GetAllInDB() ([]models.Torrent, int, error) {
return FindOrderBy(nil, "", 0, 0)
}
// ToggleBlock ; Lock/Unlock a torrent based on id
func ToggleBlock(id uint) (models.Torrent, int, error) {
var torrent models.Torrent
if models.ORM.Unscoped().Model(&torrent).First(&torrent, id).RecordNotFound() {
return torrent, http.StatusNotFound, errors.New("Torrent is not found")
}
torrent.ParseLanguages()
if torrent.Status == models.TorrentStatusBlocked {
torrent.Status = models.TorrentStatusNormal
} else {
torrent.Status = models.TorrentStatusBlocked
}
if models.ORM.Unscoped().Model(&torrent).UpdateColumn(&torrent).Error != nil {
return torrent, http.StatusInternalServerError, errors.New("Torrent was not updated")
}
return torrent, http.StatusOK, nil
}
// FindDeleted : Gets deleted torrents based on search params
func FindDeleted(parameters *structs.WhereParams, orderBy string, limit int, offset int) (torrents []models.Torrent, count int, err error) {
torrents, count, err = findOrderBy(parameters, orderBy, limit, offset, true, true, true)
return
}