From 664f3eb9e0ad637a008973397310a111bebdc2dc Mon Sep 17 00:00:00 2001 From: Gusted Date: Fri, 9 Jul 2021 01:16:14 +0200 Subject: [PATCH] feat(modlog): create modlog prototype --- handlers/core/modlog.go | 34 ++++++++++++++ handlers/server.go | 1 + models/log.go | 55 +++++++++++++++++++++++ modules/database/init/init.go | 20 +++++++++ modules/errors/errors.go | 9 ++++ views/core/modlog.html | 83 +++++++++++++++++++++++++++++++++++ 6 files changed, 202 insertions(+) create mode 100644 handlers/core/modlog.go create mode 100644 models/log.go create mode 100644 views/core/modlog.html diff --git a/handlers/core/modlog.go b/handlers/core/modlog.go new file mode 100644 index 00000000..189a8aeb --- /dev/null +++ b/handlers/core/modlog.go @@ -0,0 +1,34 @@ +package core + +import ( + "github.com/gofiber/fiber/v2" + "userstyles.world/handlers/jwt" + "userstyles.world/models" +) + +// GetModLog renders the modlog view. +// It will pass trough the relevant information from the database. +func GetModLog(c *fiber.Ctx) error { + u, _ := jwt.User(c) + + bannedUsers, err := models.GetLogOfKind(models.LogBanUser) + if err != nil { + return c.Render("err", fiber.Map{ + "Title": "Internal Server error", + "User": u, + }) + } + + removedStyles, err := models.GetLogOfKind(models.LogRemoveStyle) + if err != nil { + return c.Render("err", fiber.Map{ + "Title": "Internal Server error", + "User": u, + }) + } + + return c.Render("core/modlog", fiber.Map{ + "BannedUsers": bannedUsers, + "RemovedStyles": removedStyles, + }) +} diff --git a/handlers/server.go b/handlers/server.go index 42e89215..40b6170b 100644 --- a/handlers/server.go +++ b/handlers/server.go @@ -68,6 +68,7 @@ func Initialize() { app.Get("/user/:name", user.Profile) app.Get("~:name", user.Profile) app.Get("/docs/:document", core.GetDocs) + app.Get("/modlog", core.GetModLog) app.Get("/logout", jwtware.Protected, user.Logout) app.Get("/account", jwtware.Protected, user.Account) diff --git a/models/log.go b/models/log.go new file mode 100644 index 00000000..a00e60df --- /dev/null +++ b/models/log.go @@ -0,0 +1,55 @@ +package models + +import ( + "gorm.io/gorm" + "userstyles.world/modules/database" + "userstyles.world/modules/errors" +) + +type LogKind = uint8 + +const ( + LogBanUser LogKind = iota + 1 + LogRemoveStyle +) + +// Log struct has all the relavant information for a log entry +type Log struct { + gorm.Model + Username string + UserID uint + Reason string + Kind LogKind + TargetData string + TargetUserName string +} + +// AddLog adds a new log to the database. +func AddLog(log Log) (err error) { + err = database.Conn. + Debug(). + Model(Log{}). + Create(log). + Error + if err != nil { + return errors.ErrFailedLogAddition + } + return nil +} + +// GetLogOfKind returns all the logs of the specified kind and +// select the correct user Author. +func GetLogOfKind(kind LogKind) (q *[]Log, err error) { + err = database.Conn. + Debug(). + Model(Log{}). + Select("logs.*, u.id, u.username"). + Joins("join users u on u.id = logs.user_id"). + Where("kind = ?", kind). + Find(&q). + Error + if err != nil { + return nil, errors.ErrFailedLogRetrieval + } + return q, nil +} diff --git a/modules/database/init/init.go b/modules/database/init/init.go index c8aa838f..9d4263fc 100644 --- a/modules/database/init/init.go +++ b/modules/database/init/init.go @@ -25,6 +25,7 @@ var tables = []struct { {"stats", &models.Stats{}}, {"oauths", &models.OAuth{}}, {"histories", &models.History{}}, + {"logs", &models.Log{}}, } func connect() (*gorm.DB, error) { @@ -200,6 +201,22 @@ func seed() { }, } + Logs := []models.Log{ + { + UserID: 1, + Reason: "I like to abuse powers.", + Kind: models.LogBanUser, + TargetUserName: "gusted", + }, + { + UserID: 1, + Reason: "My style is superior", + Kind: models.LogRemoveStyle, + TargetUserName: "gusted", + TargetData: "Black-Discord", + }, + } + if config.DB_RANDOM_DATA != "false" { amount, _ := strconv.Atoi(config.DB_RANDOM_DATA) s, u := generateData(amount) @@ -216,4 +233,7 @@ func seed() { for i := range OAuths { database.Conn.Create(&OAuths[i]) } + for i := range Logs { + database.Conn.Create(&Logs[i]) + } } diff --git a/modules/errors/errors.go b/modules/errors/errors.go index 926494c9..f6f7d575 100644 --- a/modules/errors/errors.go +++ b/modules/errors/errors.go @@ -77,6 +77,15 @@ var ( // ErrNoStyleStats errors that the given style doesn't have any styles yet. ErrNoStyleStats = errors.New("style doesn't have stats yet") + + // ErrFailedLogRetrieval errors that it couldn't retrieve all the logs. + ErrFailedLogRetrieval = errors.New("failed to find all logs") + + // ErrFailedLogAddition errors that it couldn't add the log. + ErrFailedLogAddition = errors.New("failed to add the log") + + // ErrOnlyRemovedStyle errors that this function only allows to remove style kind. + ErrOnlyRemovedStyle = errors.New("only remove style kind is allowed") ) // UnexpectedSigningMethod errors that a unexpected jwt signing method was used. diff --git a/views/core/modlog.html b/views/core/modlog.html new file mode 100644 index 00000000..83f8fa71 --- /dev/null +++ b/views/core/modlog.html @@ -0,0 +1,83 @@ +
+ + +
+

Read-Only Mod log

+

This is a transpancy modlog that is read-only and list all of the mod actions.

+

All names are displayed as their Username and not their Displayname.

+
+ +
+

Banned users

+

{{ len .BannedUsers }} banned users in total.

+ + {{ if ge (len .BannedUsers) 1 }} + + + + + + + + + {{ range .BannedUsers }} + + + + + + + {{ end }} + +
ModeratorTime of EntryBanned userReason
{{ .Username }}{{ .CreatedAt | Date }}{{ .TargetUserName }}{{ .Reason }}
+ {{ else }} +

No banned users found.

+ {{ end }} +
+ +
+

Removed styles

+

{{ len .RemovedStyles }} removed styles in total.

+ + {{ if ge (len .RemovedStyles) 1 }} + + + + + + + + + + {{ range .RemovedStyles }} + + + + + + + + {{ end }} + +
ModeratorTime of EntryRemoved StyleOwner of removed styleReason
{{ .Username }}{{ .CreatedAt | Date }}{{ .TargetData }}{{ .TargetUserName }}{{ .Reason }}
+ {{ else }} +

No removed styles found.

+ {{ end }} +
+