-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcursor.go
371 lines (339 loc) · 8.99 KB
/
cursor.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
363
364
365
366
367
368
369
370
371
package filters
import (
"net/url"
"sort"
"strconv"
vocab "github.com/go-ap/activitypub"
)
// PaginateCollection is a function that populates the received collection
func PaginateCollection(it vocab.Item, filters ...Check) vocab.Item {
if vocab.IsNil(it) || !it.IsCollection() {
return it
}
total := uint(0)
_ = vocab.OnCollectionIntf(it, func(col vocab.CollectionInterface) error {
total = col.Count()
return nil
})
col, prevIRI, nextIRI := CursorFromItem(it, filters...)
if vocab.IsNil(col) {
return it
}
if vocab.IsItemCollection(col) {
return col
}
maxItems := MaxCount(filters...)
if maxItems < 0 {
maxItems = MaxItems
}
partOfIRI := it.GetID()
firstIRI := partOfIRI
if u, err := it.GetLink().URL(); err == nil {
q := u.Query()
for k := range q {
if k == keyMaxItems || k == keyAfter || k == keyBefore {
q.Del(k)
}
}
u.RawQuery = q.Encode()
partOfIRI = vocab.IRI(u.String())
if !q.Has(keyMaxItems) {
q.Set(keyMaxItems, strconv.Itoa(maxItems))
}
u.RawQuery = q.Encode()
firstIRI = vocab.IRI(u.String())
}
switch col.GetType() {
case vocab.OrderedCollectionType:
_ = vocab.OnOrderedCollection(col, func(c *vocab.OrderedCollection) error {
c.TotalItems = total
c.First = firstIRI
return nil
})
case vocab.OrderedCollectionPageType:
_ = vocab.OnOrderedCollectionPage(col, func(c *vocab.OrderedCollectionPage) error {
c.TotalItems = total
c.PartOf = partOfIRI
c.First = firstIRI
if !nextIRI.GetLink().Equals(firstIRI, true) {
c.Next = nextIRI
}
if !prevIRI.GetLink().Equals(firstIRI, true) {
c.Prev = prevIRI
}
return nil
})
case vocab.CollectionType:
_ = vocab.OnCollection(col, func(c *vocab.Collection) error {
c.TotalItems = total
c.First = firstIRI
return nil
})
case vocab.CollectionPageType:
_ = vocab.OnCollectionPage(col, func(c *vocab.CollectionPage) error {
c.TotalItems = total
c.PartOf = partOfIRI
c.First = firstIRI
if !nextIRI.GetLink().Equals(firstIRI, true) {
c.Next = nextIRI
}
if !prevIRI.GetLink().Equals(firstIRI, true) {
c.Prev = prevIRI
}
return nil
})
}
return col
}
func getURL(i vocab.IRI, f url.Values) vocab.IRI {
if f == nil {
return i
}
_, hasAfter := f[keyAfter]
_, hasBefore := f[keyBefore]
if u, err := i.URL(); err == nil {
q := u.Query()
if hasAfter || hasBefore {
q.Del(keyAfter)
q.Del(keyBefore)
}
for k, v := range f {
q[k] = v
}
u.RawQuery = q.Encode()
i = vocab.IRI(u.String())
}
return i
}
func CursorFromItem(it vocab.Item, filters ...Check) (vocab.Item, vocab.Item, vocab.Item) {
typ := it.GetType()
if !vocab.CollectionTypes.Contains(typ) {
return it, nil, nil
}
var prev url.Values
var next url.Values
var prevIRI vocab.IRI
var nextIRI vocab.IRI
shouldBePage := len(PaginationChecks(filters...)) > 0
maxCount := MaxCount(filters...)
switch typ {
case vocab.OrderedCollectionPageType:
_ = vocab.OnOrderedCollectionPage(it, func(new *vocab.OrderedCollectionPage) error {
items := new.OrderedItems
if maxCount < 0 && len(items) > MaxItems {
filters = append(filters, WithMaxCount(MaxItems))
}
new.OrderedItems, prev, next = filterCollection(sortItemsByPublishedUpdated(items), filters...)
if len(prev) > 0 {
prevIRI = getURL(it.GetLink(), prev)
}
if len(next) > 0 {
nextIRI = getURL(it.GetLink(), next)
}
return nil
})
case vocab.CollectionPageType:
_ = vocab.OnCollectionPage(it, func(new *vocab.CollectionPage) error {
items := new.Items
if maxCount < 0 && len(items) > MaxItems {
filters = append(filters, WithMaxCount(MaxItems))
}
new.Items, prev, next = filterCollection(items, filters...)
if len(prev) > 0 {
prevIRI = getURL(it.GetLink(), prev)
}
if len(next) > 0 {
nextIRI = getURL(it.GetLink(), next)
}
return nil
})
case vocab.OrderedCollectionType:
if shouldBePage {
result := new(vocab.OrderedCollectionPage)
old, _ := it.(*vocab.OrderedCollection)
err := vocab.OnOrderedCollection(result, func(new *vocab.OrderedCollection) error {
_, err := vocab.CopyOrderedCollectionProperties(new, old)
new.Type = vocab.OrderedCollectionPageType
items := new.OrderedItems
if maxCount < 0 && len(items) > MaxItems {
filters = append(filters, WithMaxCount(MaxItems))
}
new.OrderedItems, prev, next = filterCollection(sortItemsByPublishedUpdated(items), filters...)
if len(prev) > 0 {
prevIRI = getURL(it.GetLink(), prev)
}
if len(next) > 0 {
nextIRI = getURL(it.GetLink(), next)
}
return err
})
if err == nil {
it = result
}
} else {
_ = vocab.OnOrderedCollection(it, func(new *vocab.OrderedCollection) error {
items := new.OrderedItems
if maxCount < 0 && len(items) > MaxItems {
filters = append(filters, WithMaxCount(MaxItems))
}
new.OrderedItems, prev, next = filterCollection(sortItemsByPublishedUpdated(items), filters...)
if len(next) > 0 {
new.First = getURL(it.GetLink(), next)
}
return nil
})
}
case vocab.CollectionType:
if shouldBePage {
result := new(vocab.CollectionPage)
old, _ := it.(*vocab.Collection)
err := vocab.OnCollection(result, func(new *vocab.Collection) error {
_, err := vocab.CopyCollectionProperties(new, old)
new.Type = vocab.CollectionPageType
items := new.Items
if maxCount < 0 && len(items) > MaxItems {
filters = append(filters, WithMaxCount(MaxItems))
}
new.Items, prev, next = filterCollection(items, filters...)
if len(prev) > 0 {
prevIRI = getURL(it.GetLink(), prev)
}
if len(next) > 0 {
nextIRI = getURL(it.GetLink(), next)
}
return err
})
if err == nil {
it = result
}
} else {
_ = vocab.OnCollection(it, func(new *vocab.Collection) error {
items := new.Items
if maxCount < 0 && len(items) > MaxItems {
filters = append(filters, WithMaxCount(MaxItems))
}
new.Items, prev, next = filterCollection(items, filters...)
if len(next) > 0 {
new.First = getURL(it.GetLink(), next)
}
return nil
})
}
case vocab.CollectionOfItems:
_ = vocab.OnItemCollection(it, func(col *vocab.ItemCollection) error {
items := *col
if maxCount < 0 && len(items) > MaxItems {
filters = append(filters, WithMaxCount(MaxItems))
}
it, prev, next = filterCollection(sortItemsByPublishedUpdated(items), filters...)
if len(prev) > 0 {
prevIRI = getURL(it.GetLink(), prev)
}
if len(next) > 0 {
nextIRI = getURL(it.GetLink(), next)
}
return nil
})
}
return it, prevIRI, nextIRI
}
func resetAfter(fns ...Check) {
for _, fn := range fns {
if af, ok := fn.(*afterCrit); ok {
af.check = false
}
}
}
func resetBefore(fns ...Check) {
for _, fn := range fns {
if bf, ok := fn.(*beforeCrit); ok {
bf.check = true
}
}
}
func resetCounter(fn Check) {
if mit, ok := fn.(*counter); ok {
mit.cnt = 0
}
}
func filterCollection(col vocab.ItemCollection, fns ...Check) (vocab.ItemCollection, url.Values, url.Values) {
if len(col) == 0 {
return col, nil, nil
}
pp := url.Values{}
np := url.Values{}
maxItems := MaxCount(fns...)
if maxItems < 0 {
maxItems = MaxItems
}
if maxItems == 0 {
// NOTE(marius): this is a shortcut. We're assuming that if the calling code wants max 0 items in the
// list, they're ok with circumventing the rest of filtering and receiving a hard 0 items collection.
return vocab.ItemCollection{}, nil, nil
}
resetCounter(MaxCountCheck(fns...))
resetAfter(fns...)
resetBefore(fns...)
var lastPage vocab.ItemCollection
var result vocab.ItemCollection
result = Checks(fns).runOnItems(col)
if len(result) == 0 {
return result, pp, np
}
onLastPage := len(AfterChecks(fns...)) > 0 && len(result) < maxItems
onFirstPage := len(AfterChecks(fns...)) == 0 && result.First().GetLink().Equals(col.First().GetLink(), true)
var firstPage vocab.ItemCollection
first := result.First()
if len(col) <= maxItems {
return result, pp, np
}
pp.Add(keyMaxItems, strconv.Itoa(maxItems))
np.Add(keyMaxItems, strconv.Itoa(maxItems))
for _, top := range firstPage {
if onFirstPage = first.GetLink().Equals(top.GetLink(), true); onFirstPage {
break
}
}
if !onFirstPage {
pp.Add(keyBefore, first.GetLink().String())
} else {
pp = nil
}
if len(result) >= 1 && len(col) > maxItems+1 {
last := result[len(result)-1]
for _, bottom := range lastPage {
if onLastPage = last.GetLink().Equals(bottom.GetLink(), true); onLastPage {
break
}
}
if !onLastPage {
np.Add(keyAfter, last.GetLink().String())
} else {
np = nil
}
}
return result, pp, np
}
func sortItemsByPublishedUpdated(col vocab.ItemCollection) vocab.ItemCollection {
sort.SliceStable(col, func(i, j int) bool {
return vocab.ItemOrderTimestamp(col[i], col[j])
})
return col
}
func isCounterFn(fn Check) bool {
_, ok := fn.(*counter)
return ok
}
func isCursorFn(fn Check) bool {
ok := false
switch fn.(type) {
case *afterCrit:
ok = true
case *beforeCrit:
ok = true
}
return ok
}
func isFilterFn(fn Check) bool {
return !(isCursorFn(fn) || isCounterFn(fn))
}