From 31308634565980b0194740feb4684d4bf27f8200 Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Sat, 9 Dec 2017 09:36:00 +0100 Subject: [PATCH] custom rap logs --- app/start.go | 3 ++ app/v1/manage_user.go | 17 --------- app/v1/rap.go | 87 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 17 deletions(-) create mode 100644 app/v1/rap.go diff --git a/app/start.go b/app/start.go index 851c998..cfd0a6e 100644 --- a/app/start.go +++ b/app/start.go @@ -116,6 +116,9 @@ func Start(conf common.Conf, dbO *sqlx.DB) *fhr.Router { r.POSTMethod("/api/v1/users/self/userpage", v1.UserSelfUserpagePOST, common.PrivilegeWrite) r.POSTMethod("/api/v1/beatmaps/rank_requests", v1.BeatmapRankRequestsSubmitPOST, common.PrivilegeWrite) + // Admin: RAP + r.POSTMethod("/api/v1/rap/log", v1.RAPLogPOST) + // Admin: beatmap r.POSTMethod("/api/v1/beatmaps/set_status", v1.BeatmapSetStatusPOST, common.PrivilegeBeatmap) r.Method("/api/v1/beatmaps/ranked_frozen_full", v1.BeatmapRankedFrozenFullGET, common.PrivilegeBeatmap) diff --git a/app/v1/manage_user.go b/app/v1/manage_user.go index 54a028f..48037be 100644 --- a/app/v1/manage_user.go +++ b/app/v1/manage_user.go @@ -174,23 +174,6 @@ func UserEditPOST(md common.MethodData) common.CodeMessager { return userPutsSingle(md, md.DB.QueryRowx(userFields+" WHERE users.id = ? LIMIT 1", data.ID)) } -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) - } -} - func appendToUserNotes(md common.MethodData, message string, user int) { message = "\n[" + time.Now().Format("2006-01-02 15:04:05") + "] API: " + message _, err := md.DB.Exec("UPDATE users SET notes = CONCAT(COALESCE(notes, ''), ?) WHERE id = ?", diff --git a/app/v1/rap.go b/app/v1/rap.go new file mode 100644 index 0000000..09ad885 --- /dev/null +++ b/app/v1/rap.go @@ -0,0 +1,87 @@ +package v1 + +import ( + "fmt" + "time" + + "zxq.co/ripple/rippleapi/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) + } +}