-
Notifications
You must be signed in to change notification settings - Fork 13
/
cursor.go
150 lines (132 loc) · 2.91 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
package brutalinks
import (
"sort"
"time"
vocab "github.com/go-ap/activitypub"
)
type Cursor struct {
after Hash
before Hash
items RenderableList
total uint
}
var emptyCursor = Cursor{}
type colCursor struct {
filters *Filters
loaded int
items vocab.ItemCollection
}
type RenderableList []Renderable
func (r *RenderableList) Valid() bool {
return r != nil && len(*r) > 0
}
func (r RenderableList) Items() ItemCollection {
items := make(ItemCollection, 0)
for _, ren := range r {
if it, ok := ren.(*Item); ok {
items = append(items, *it)
}
}
return items
}
func (r RenderableList) Contains(ren Renderable) bool {
for _, rr := range r {
if rr.Type() == ren.Type() && rr.ID() == ren.ID() {
return true
}
}
return false
}
func (r RenderableList) Follows() FollowRequests {
follows := make(FollowRequests, 0)
for _, ren := range r {
if it, ok := ren.(*FollowRequest); ok {
follows = append(follows, *it)
}
}
return follows
}
func (r *RenderableList) Merge(other RenderableList) {
for k, it := range other {
(*r)[k] = it
}
}
func (r *RenderableList) Append(others ...Renderable) {
for _, o := range others {
*r = append(*r, o)
}
}
func ByDate(r RenderableList) []Renderable {
rl := make([]Renderable, 0)
for _, rr := range r {
rl = append(rl, rr)
}
sort.SliceStable(rl, func(i, j int) bool {
ri := rl[i]
rj := rl[j]
if ri.Type() == rj.Type() {
switch ri.Type() {
case CommentType:
ii, oki := ri.(*Item)
ij, okj := rj.(*Item)
subOrder := ii.SubmittedAt.After(ij.SubmittedAt)
subSame := ii.SubmittedAt.Sub(ij.SubmittedAt) == 0
updOrder := ii.UpdatedAt.After(ij.UpdatedAt)
return oki && okj && (subOrder || (subSame && updOrder))
}
}
return ri.Date().After(rj.Date())
})
return rl
}
func ByScore(r RenderableList) []Renderable {
rl := make([]Renderable, 0, len(r))
for _, rr := range r {
rl = append(rl, rr)
}
sort.SliceStable(rl, func(i, j int) bool {
var hi, hj float64
ri := rl[i]
ii, oki := ri.(*Item)
rj := rl[j]
ij, okj := rj.(*Item)
if Instance.Conf.VotingEnabled {
if oki {
hi = Hacker(int64(ii.Votes.Score()), time.Now().Sub(ii.SubmittedAt))
}
if okj {
hj = Hacker(int64(ij.Votes.Score()), time.Now().Sub(ij.SubmittedAt))
}
if oki && okj {
return hi > hj
}
}
return ri.Date().After(rj.Date())
})
return rl
}
func lastUpdatedInThread(it Renderable) time.Time {
maxDate := it.Date()
ob, ok := it.(*Item)
if !ok {
return maxDate
}
for _, ic := range *ob.Children() {
if threadLastUpdate := lastUpdatedInThread(ic); threadLastUpdate.After(maxDate) {
maxDate = threadLastUpdate
}
}
return maxDate
}
func ByRecentActivity(r RenderableList) []Renderable {
rl := make([]Renderable, 0, len(r))
for _, rr := range r {
rl = append(rl, rr)
}
sort.SliceStable(rl, func(i, j int) bool {
ri := rl[i]
rj := rl[j]
return lastUpdatedInThread(ri).After(lastUpdatedInThread(rj))
})
return rl
}