11package api
22
33import (
4- "encoding/json"
5- "io"
64 "net/http"
7- "strconv"
85 "time"
96 "wwfc/database"
107 "wwfc/gpcm"
11- "wwfc/logging"
12-
13- "github.com/logrusorgru/aurora/v3"
148)
159
16- func HandleBan (w http.ResponseWriter , r * http.Request ) {
17- var user * database.User
18- var statusCode int
19- var err error
20-
21- if r .Method == http .MethodPost {
22- user , statusCode , err = handleBanImpl (r )
23- } else if r .Method == http .MethodOptions {
24- statusCode = http .StatusNoContent
25- w .Header ().Set ("Access-Control-Allow-Methods" , "POST" )
26- w .Header ().Set ("Access-Control-Allow-Headers" , "Content-Type" )
27- } else {
28- err = ErrPostOnly
29- statusCode = http .StatusMethodNotAllowed
30- w .Header ().Set ("Allow" , "POST" )
31- }
32-
33- w .Header ().Set ("Access-Control-Allow-Origin" , "*" )
34-
35- if user == nil {
36- user = & database.User {}
37- }
38-
39- var jsonData []byte
40-
41- if statusCode != http .StatusNoContent {
42- w .Header ().Set ("Content-Type" , "application/json" )
43- jsonData , _ = json .Marshal (UserActionResponse {* user , err == nil , resolveError (err )})
44- }
45-
46- w .Header ().Set ("Content-Length" , strconv .Itoa (len (jsonData )))
47- w .WriteHeader (statusCode )
48- w .Write (jsonData )
49- }
50-
51- type BanRequestSpec struct {
10+ type BanRequest struct {
5211 Secret string `json:"secret"`
5312 ProfileID uint32 `json:"pid"`
5413 Days uint64 `json:"days"`
@@ -60,24 +19,16 @@ type BanRequestSpec struct {
6019 Moderator string `json:"moderator"`
6120}
6221
63- func handleBanImpl (r * http.Request ) (* database.User , int , error ) {
64- // TODO: Actual authentication rather than a fixed secret
65-
66- body , err := io .ReadAll (r .Body )
67- if err != nil {
68- return nil , http .StatusBadRequest , ErrRequestBody
69- }
70-
71- var req BanRequestSpec
72- err = json .Unmarshal (body , & req )
73- if err != nil {
74- return nil , http .StatusBadRequest , err
75- }
76-
77- if apiSecret == "" || req .Secret != apiSecret {
78- return nil , http .StatusUnauthorized , ErrInvalidSecret
79- }
22+ var BanRoute = MakeRouteSpec [BanRequest , UserActionResponse ](
23+ true ,
24+ "/api/ban" ,
25+ func (req any , v bool , _ * http.Request ) (any , int , error ) {
26+ return handleUserAction (req .(BanRequest ), v , handleBanImpl )
27+ },
28+ http .MethodPost ,
29+ )
8030
31+ func handleBanImpl (req BanRequest , _ bool ) (* database.User , int , error ) {
8132 if req .ProfileID == 0 {
8233 return nil , http .StatusBadRequest , ErrPIDMissing
8334 }
@@ -98,8 +49,6 @@ func handleBanImpl(r *http.Request) (*database.User, int, error) {
9849
9950 length := time .Duration (minutes ) * time .Minute
10051
101- logging .Notice ("API:" + moderator , "Ban profile:" , aurora .Cyan (req .ProfileID ), "TOS:" , aurora .Cyan (req .Tos ), "Length:" , aurora .Cyan (length ), "Reason:" , aurora .BrightCyan (req .Reason ), "Reason (Hidden):" , aurora .BrightCyan (req .ReasonHidden ))
102-
10352 if ! database .BanUser (pool , ctx , req .ProfileID , req .Tos , length , req .Reason , req .ReasonHidden , moderator ) {
10453 return nil , http .StatusInternalServerError , ErrTransaction
10554 }
0 commit comments