This repository has been archived by the owner on Oct 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bans.go
89 lines (84 loc) · 2.08 KB
/
bans.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
package main
import (
"log"
"net/http"
"time"
"github.com/jackc/pgx/v4"
)
func bansHandler(w http.ResponseWriter, r *http.Request) {
type viewBan struct {
ID int
Identity *int
IdentityName *string
IdentityKey *string
Account *int
AccountName *string
Reason string
IssuedAt string
ExpiresAt string
IsBanned bool
Forbids string
}
ret := []viewBan{}
var (
banid int
whenbanned time.Time
whenexpires *time.Time
reason string
ident *int
identName *string
identKey *string
acc *int
accName *string
forbidsChatting bool
forbidsPlaying bool
forbidsJoining bool
)
_, err := dbpool.QueryFunc(r.Context(),
`select
bans.id, accounts.id, accounts.display_name, identities.id, identities.name, coalesce(encode(identities.pkey, 'hex'), identities.hash),
time_issued, time_expires, reason, forbids_chatting, forbids_playing, forbids_joining
from bans
left join identities on bans.identity = identities.id
left join accounts on bans.account = accounts.id
order by bans.id desc;`, []any{},
[]any{&banid, &acc, &accName, &ident, &identName, &identKey,
&whenbanned, &whenexpires, &reason, &forbidsChatting, &forbidsPlaying, &forbidsJoining},
func(_ pgx.QueryFuncRow) error {
v := viewBan{
ID: banid,
Identity: ident,
IdentityName: identName,
IdentityKey: identKey,
Account: acc,
AccountName: accName,
Reason: reason,
IssuedAt: whenbanned.Format(time.DateTime),
}
if whenexpires == nil {
v.ExpiresAt = "Never"
} else {
expiresAt := *whenexpires
v.ExpiresAt = expiresAt.Format(time.DateTime)
v.IsBanned = time.Now().Before(expiresAt)
}
if forbidsChatting {
v.Forbids += "chatting"
}
if forbidsPlaying {
v.Forbids += " playing"
}
if forbidsJoining {
v.Forbids += " joining"
}
ret = append(ret, v)
return nil
})
if err != nil {
log.Println(err)
return
}
basicLayoutLookupRespond("bans", w, r, map[string]any{
"Bans": ret,
})
}