/
util.go
111 lines (99 loc) · 2.53 KB
/
util.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
package templates
import (
"html"
"strings"
"time"
"meguca/auth"
"meguca/common"
)
func posClasses(pos auth.Positions) string {
var classes []string
// Any next moderation level can do anything that previous can.
// Add them all for simpler handling in CSS.
for level := pos.CurBoard; level >= auth.Moderator; level-- {
classes = append(classes, "pos_"+level.String())
}
for level := pos.AnyBoard; level >= auth.Moderator; level-- {
classes = append(classes, "anypos_"+level.String())
}
if pos.AnyBoard > auth.NotLoggedIn {
classes = append(classes, "user")
}
if pos.IsPowerUser() {
classes = append(classes, "user_power")
}
return strings.Join(classes, " ")
}
// Extract reverse links to linked posts on a page
func extractBacklinks(cap int, threads ...common.Thread) common.Backlinks {
bls := make(common.Backlinks, cap)
register := func(p *common.Post, op uint64) {
for _, l := range p.Links {
m, ok := bls[l[0]]
if !ok {
m = make(map[uint64]uint64, 4)
bls[l[0]] = m
}
m[p.ID] = op
}
}
for _, t := range threads {
register(t.Post, t.ID)
for _, p := range t.Posts {
register(p, t.ID)
}
}
return bls
}
// CalculateOmit returns the omitted post and image counts for a thread
func CalculateOmit(t common.Thread) (int, int) {
// There might still be posts missing due to deletions even in complete
// thread queries. Ensure we are actually retrieving an abbreviated thread
// before calculating.
if !t.Abbrev {
return 0, 0
}
// omit := int(t.PostCtr) - (len(t.Posts) + 1)
// imgOmit := 0
// if omit != 0 {
// imgOmit = int(t.ImageCtr) - len(t.Files)
// for _, p := range t.Posts {
// imgOmit -= len(p.Files)
// }
// }
// return omit, imgOmit
omit := int((t.PostCtr) - 1)
imgOmit := 0
if omit != 0 {
imgOmit = int(t.ImageCtr) - len(t.Files)
}
return omit, imgOmit
}
func bold(s string) string {
s = html.EscapeString(s)
b := make([]byte, 3, len(s)+7)
copy(b, "<b>")
b = append(b, s...)
b = append(b, "</b>"...)
return string(b)
}
// Manually correct time zone, because it gets stored wrong in the database
// somehow.
func correctTimeZone(t time.Time) time.Time {
t = t.Round(time.Second)
return time.Date(
t.Year(),
t.Month(),
t.Day(),
t.Hour(),
t.Minute(),
t.Second(),
0,
time.Local,
).UTC()
}
// https://stackoverflow.com/a/38608022
type sortableUInt64 []uint64
func (a sortableUInt64) Len() int { return len(a) }
func (a sortableUInt64) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a sortableUInt64) Less(i, j int) bool { return a[i] < a[j] }