-
Notifications
You must be signed in to change notification settings - Fork 938
/
Copy pathtemplate_extensions.go
70 lines (57 loc) · 1.67 KB
/
template_extensions.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
package logs
import (
"context"
"emperror.dev/errors"
"github.com/jonas747/yagpdb/common/templates"
"time"
)
func init() {
templates.RegisterSetupFunc(func(ctx *templates.Context) {
ctx.ContextFuncs["pastUsernames"] = tmplUsernames(ctx)
ctx.ContextFuncs["pastNicknames"] = tmplNicknames(ctx)
})
}
type CCNameChange struct {
Name string
Time time.Time
}
func tmplUsernames(tmplCtx *templates.Context) interface{} {
return func(userIDi interface{}, offset int) (interface{}, error) {
if tmplCtx.IncreaseCheckCallCounter("pastUsernames", 2) {
return nil, errors.New("Max calls to pastUsernames (2) reached")
}
target := templates.ToInt64(userIDi)
result := make([]*CCNameChange, 0)
usernames, err := GetUsernames(context.Background(), target, 15, offset)
if err != nil {
return nil, err
}
for _, v := range usernames {
result = append(result, &CCNameChange{
Name: v.Username.String,
Time: v.CreatedAt.Time,
})
}
return result, nil
}
}
func tmplNicknames(tmplCtx *templates.Context) interface{} {
return func(userIDi interface{}, offset int) (interface{}, error) {
if tmplCtx.IncreaseCheckCallCounter("pastNicknames", 2) {
return nil, errors.New("Max calls to pastNicknames (2) reached")
}
target := templates.ToInt64(userIDi)
result := make([]*CCNameChange, 0)
nicknames, err := GetNicknames(context.Background(), target, tmplCtx.GS.ID, 15, offset)
if err != nil {
return nil, err
}
for _, v := range nicknames {
result = append(result, &CCNameChange{
Name: v.Nickname.String,
Time: v.CreatedAt.Time,
})
}
return result, nil
}
}