-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpagination.go
174 lines (154 loc) · 5.17 KB
/
pagination.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
package models
import (
"net/http"
"strconv"
"gorm.io/gorm"
)
// pagination support for the Mastodon API.
func PaginateActors(r *http.Request) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
q := r.URL.Query()
limit, _ := strconv.Atoi(q.Get("limit"))
switch {
case limit > 40:
limit = 80
case limit <= 0:
limit = 20
}
db = db.Limit(limit)
// so there's a trick with min_id. If you pass a min_id, we need to find `limit` statuses that are above the min_id.
// We can do this by sorting by the latest and counting back until we either hit limit, or hit the min_id, but that
// creates a problem that if there are more that `limit` statuses between the min_id and the latest, we'll see the latest
// `limit`, not the _earliest_ `limit`.
//
// Mastodon seems to handle this by outsourcing the problem to redis, 'natch. We can't do that, so when min_id is passed,
// we'll sort ascending. This will work, but it creates the problem that all clients *probably* expect actors to be sorted
// in descending order, which again Mastodon does for free. The simplest way to handle this is to sort the actors in descending
// order, during rendering.
// mostly based on https://github.com/mastodon/mastodon/blob/main/app/models/concerns/paginable.rb#L3
maxID := q.Get("max_id")
minID := q.Get("min_id")
sinceID := q.Get("since_id")
switch minID {
case "":
// no min_id provided, so we'll sort descending
db = db.Order("actors.object_id desc")
if maxID != "" {
db = db.Where("actors.object_id < ?", maxID)
}
if sinceID != "" {
db = db.Where("actors.object_id > ?", sinceID)
}
default:
// min_id provided, so we'll sort ascending
// since_id is ignored
db = db.Order("actors.object_id asc")
db = db.Where("actors.object_id > ?", minID)
if maxID != "" {
db = db.Where("actors.object_id < ?", maxID)
}
}
return db
}
}
func PaginateConversation(r *http.Request) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
q := r.URL.Query()
limit, _ := strconv.Atoi(q.Get("limit"))
switch {
case limit > 40:
limit = 40
case limit <= 0:
limit = 20
}
db = db.Limit(limit)
sinceID, _ := strconv.Atoi(r.URL.Query().Get("since_id"))
if sinceID > 0 {
db = db.Where("statuses.object_id > ?", sinceID)
}
minID, _ := strconv.Atoi(r.URL.Query().Get("min_id"))
if minID > 0 {
db = db.Where("statuses.object_id > ?", minID)
}
maxID, _ := strconv.Atoi(r.URL.Query().Get("max_id"))
if maxID > 0 {
db = db.Where("statuses.object_id < ?", maxID)
}
return db
}
}
func PaginateRelationship(r *http.Request) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
q := r.URL.Query()
limit, _ := strconv.Atoi(q.Get("limit"))
switch {
case limit > 40:
limit = 40
case limit <= 0:
limit = 20
}
db = db.Limit(limit)
sinceID, _ := strconv.Atoi(r.URL.Query().Get("since_id"))
if sinceID > 0 {
db = db.Where("relationships.target_id > ?", sinceID)
}
minID, _ := strconv.Atoi(r.URL.Query().Get("min_id"))
if minID > 0 {
db = db.Where("relationships.target_id > ?", minID)
}
maxID, _ := strconv.Atoi(r.URL.Query().Get("max_id"))
if maxID > 0 {
db = db.Where("relationships.target_id < ?", maxID)
}
return db.Order("relationships.target_id desc")
}
}
func PreloadRelationshipTarget(db *gorm.DB) *gorm.DB {
return db.Preload("Target").Preload("Target.Object")
}
func PaginateStatuses(r *http.Request) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
q := r.URL.Query()
limit, _ := strconv.Atoi(q.Get("limit"))
switch {
case limit > 40:
limit = 40
case limit <= 0:
limit = 20
}
db = db.Limit(limit)
// so there's a trick with min_id. If you pass a min_id, we need to find `limit` statuses that are above the min_id.
// We can do this by sorting by the latest and counting back until we either hit limit, or hit the min_id, but that
// creates a problem that if there are more that `limit` statuses between the min_id and the latest, we'll see the latest
// `limit`, not the _earliest_ `limit`.
//
// Mastodon seems to handle this by outsourcing the problem to redis, 'natch. We can't do that, so when min_id is passed,
// we'll sort ascending. This will wor, but it creates the problem that all clients *probably* expect statuses to be sorted
// in descending order, which again Mastodon does for free. The simplest way to handle this is to sort the statuses in descending
// order, during rendering.
// mostly based on https://github.com/mastodon/mastodon/blob/main/app/models/feed.rb#L22
maxID := q.Get("max_id")
minID := q.Get("min_id")
sinceID := q.Get("since_id")
switch minID {
case "":
// no min_id provided, so we'll sort descending
db = db.Order("statuses.object_id desc")
if maxID != "" {
db = db.Where("statuses.object_id < ?", maxID)
}
if sinceID != "" {
db = db.Where("statuses.object_id > ?", sinceID)
}
default:
// min_id provided, so we'll sort ascending
// since_id is ignored
db = db.Order("statuses.object_id asc")
db = db.Where("statuses.object_id > ?", minID)
if maxID != "" {
db = db.Where("statuses.object_id < ?", maxID)
}
}
return db
}
}