This repository has been archived by the owner on Nov 20, 2020. It is now read-only.
forked from osuthailand/api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rap.go
87 lines (74 loc) · 1.72 KB
/
rap.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
package v1
import (
"fmt"
"time"
"github.com/osuthailand/api/common"
)
type rapLogData struct {
Through string `json:"through"`
Text string `json:"text"`
}
type rapLogMessage struct {
rapLogData
Author int `json:"author"`
CreatedAt time.Time `json:"created_at"`
}
type rapLogResponse struct {
common.ResponseBase
rapLogMessage
}
// RAPLogPOST creates a new entry in the RAP logs
func RAPLogPOST(md common.MethodData) common.CodeMessager {
if md.User.UserPrivileges&common.AdminPrivilegeAccessRAP == 0 {
return common.SimpleResponse(403, "Got lost, kiddo?")
}
var d rapLogData
if err := md.Unmarshal(&d); err != nil {
fmt.Println(err)
return ErrBadJSON
}
if d.Text == "" {
return ErrMissingField("text")
}
if d.Through == "" {
ua := string(md.Ctx.UserAgent())
if len(ua) > 20 {
ua = ua[:20] + "…"
}
d.Through = "API"
if ua != "" {
d.Through += " (" + ua + ")"
}
}
if len(d.Through) > 30 {
d.Through = d.Through[:30]
}
created := time.Now()
_, err := md.DB.Exec("INSERT INTO rap_logs(userid, text, datetime, through) VALUES (?, ?, ?, ?)",
md.User.UserID, d.Text, created.Unix(), d.Through)
if err != nil {
md.Err(err)
return Err500
}
var resp rapLogResponse
resp.rapLogData = d
resp.Author = md.User.UserID
resp.CreatedAt = created.Truncate(time.Second)
resp.Code = 200
return resp
}
func rapLog(md common.MethodData, message string) {
ua := string(md.Ctx.UserAgent())
if len(ua) > 20 {
ua = ua[:20] + "…"
}
through := "API"
if ua != "" {
through += " (" + ua + ")"
}
_, err := md.DB.Exec("INSERT INTO rap_logs(userid, text, datetime, through) VALUES (?, ?, ?, ?)",
md.User.UserID, message, time.Now().Unix(), through)
if err != nil {
md.Err(err)
}
}