From d79d32d9b1f0a3c4601ddbca020cc860a837c37c Mon Sep 17 00:00:00 2001 From: vednoc Date: Mon, 16 Oct 2023 18:26:29 +0200 Subject: [PATCH] refactor(style): improve code consistency --- handlers/style/ban.go | 20 +++---- handlers/style/bulkban.go | 121 ++++++++++++++------------------------ models/style.go | 10 ++++ 3 files changed, 63 insertions(+), 88 deletions(-) diff --git a/handlers/style/ban.go b/handlers/style/ban.go index 0c84ab5a..b9459004 100644 --- a/handlers/style/ban.go +++ b/handlers/style/ban.go @@ -46,13 +46,12 @@ func BanGet(c *fiber.Ctx) error { }) } -func BanStyle(style models.APIStyle, u *models.APIUser, user *storage.User, i int, id string, c *fiber.Ctx) (models.Log, error) { - - event := models.Log{ +func BanStyle(style *models.Style, u *models.APIUser, user *storage.User, i int, id string, c *fiber.Ctx) (*models.Log, error) { + event := &models.Log{ UserID: u.ID, Username: u.Username, Kind: models.LogRemoveStyle, - TargetUserName: style.Username, + TargetUserName: style.User.Username, TargetData: style.Name, Reason: strings.TrimSpace(c.FormValue("reason")), Message: strings.TrimSpace(c.FormValue("message")), @@ -66,7 +65,6 @@ func BanStyle(style models.APIStyle, u *models.APIUser, user *storage.User, i in StyleID: int(style.ID), } - // INSERT INTO `logs` err := database.Conn.Transaction(func(tx *gorm.DB) error { if err := storage.DeleteUserstyle(tx, i); err != nil { return err @@ -77,7 +75,7 @@ func BanStyle(style models.APIStyle, u *models.APIUser, user *storage.User, i in if err := storage.DeleteSearchData(tx, i); err != nil { return err } - if err := models.CreateLog(tx, &event); err != nil { + if err := models.CreateLog(tx, event); err != nil { return err } if err := models.CreateNotification(tx, ¬ification); err != nil { @@ -115,7 +113,7 @@ func BanPost(c *fiber.Ctx) error { } id := c.Params("id") - style, err := models.GetStyleByID(id) + style, err := models.TempGetStyleByID(i) if err != nil { c.Status(fiber.StatusNotFound) return c.Render("err", fiber.Map{ @@ -133,7 +131,7 @@ func BanPost(c *fiber.Ctx) error { }) } - event, err := BanStyle(*style, u, user, i, id, c) + event, err := BanStyle(style, u, user, i, id, c) if err != nil { log.Database.Printf("Failed to remove %d: %s\n", i, err) return c.Render("err", fiber.Map{ @@ -147,12 +145,12 @@ func BanPost(c *fiber.Ctx) error { return c.Redirect("/modlog", fiber.StatusSeeOther) } -func sendRemovalEmail(user *storage.User, style *models.APIStyle, entry models.Log) { +func sendRemovalEmail(user *storage.User, style *models.Style, event *models.Log) { args := fiber.Map{ "User": user, "Style": style, - "Log": entry, - "Link": config.BaseURL + "/modlog#id-" + strconv.Itoa(int(entry.ID)), + "Log": event, + "Link": config.BaseURL + "/modlog#id-" + strconv.Itoa(int(event.ID)), } title := "Your style has been removed" diff --git a/handlers/style/bulkban.go b/handlers/style/bulkban.go index cb5cf507..bb5b8591 100644 --- a/handlers/style/bulkban.go +++ b/handlers/style/bulkban.go @@ -16,127 +16,94 @@ import ( func BulkBanGet(c *fiber.Ctx) error { u, _ := jwt.User(c) + c.Locals("User", u) - // Check if logged-in user has permissions. if !u.IsModOrAdmin() { - c.Status(fiber.StatusUnauthorized) - return c.Render("err", fiber.Map{ - "Title": "You are not authorized to perform this action", - "User": u, - }) + c.Locals("Title", "You are not authorized to perform this action") + return c.Status(fiber.StatusUnauthorized).Render("err", fiber.Map{}) } - userid, err := c.ParamsInt("userid") - if err != nil || userid < 1 { - return c.Status(fiber.StatusBadRequest).Render("err", fiber.Map{ - "User": u, - "Title": "Invalid user ID", - }) + id, err := c.ParamsInt("userid") + if err != nil || id < 1 { + c.Locals("Title", "Invalid user ID") + return c.Status(fiber.StatusBadRequest).Render("err", fiber.Map{}) } - _, err = storage.FindUser(uint(userid)) - if err != nil { - c.Status(fiber.StatusInternalServerError) - return c.Status(fiber.StatusBadRequest).Render("err", fiber.Map{ - "User": u, - "Title": "Could not find such user", - }) + if _, err = storage.FindUser(uint(id)); err != nil { + c.Locals("Title", "Could not find such user") + return c.Status(fiber.StatusNotFound).Render("err", fiber.Map{}) } - return c.Render("style/bulkban", fiber.Map{ - "Title": "Perform a bulk ban", - "User": u, - "UserID": userid, - }) + c.Locals("UserID", id) + c.Locals("Title", "Perform a bulk userstyle removal") + + return c.Render("style/bulkban", fiber.Map{}) } func BulkBanPost(c *fiber.Ctx) error { u, _ := jwt.User(c) + c.Locals("User", u) - // Check if logged-in user has permissions. if !u.IsModOrAdmin() { - c.Status(fiber.StatusUnauthorized) - return c.Render("err", fiber.Map{ - "Title": "You are not authorized to perform this action", - "User": u, - }) + c.Locals("Title", "You are not authorized to perform this action") + return c.Status(fiber.StatusUnauthorized).Render("err", fiber.Map{}) } - userid, err := c.ParamsInt("userid") - if err != nil || userid < 1 { - return c.Status(fiber.StatusBadRequest).Render("err", fiber.Map{ - "User": u, - "Title": "Invalid user ID", - }) + uid, err := c.ParamsInt("userid") + if err != nil || uid < 1 { + c.Locals("Title", "Invalid user ID") + return c.Status(fiber.StatusBadRequest).Render("err", fiber.Map{}) } - user, err := storage.FindUser(uint(userid)) + user, err := storage.FindUser(uint(uid)) if err != nil { - c.Status(fiber.StatusInternalServerError) - return c.Status(fiber.StatusBadRequest).Render("err", fiber.Map{ - "User": u, - "Title": "Could not find such user", - }) + c.Locals("Title", "Could not find such user") + return c.Status(fiber.StatusNotFound).Render("err", fiber.Map{}) } - styles := []models.APIStyle{} - ids := strings.Split(c.FormValue("ids"), ",") + var styles []*models.Style // Process all IDs for problems not to have any errors in between of removal - for _, element := range ids { - id := strings.TrimSpace(element) - - style, err := models.GetStyleByID(id) + for _, val := range strings.Split(c.FormValue("ids"), ",") { + val := strings.TrimSpace(val) + id, err := strconv.Atoi(val) if err != nil { - c.Status(fiber.StatusNotFound) - return c.Render("err", fiber.Map{ - "Title": "Operation failed", - "ErrTitle": "Style " + id + " was not found", - "User": u, - }) + c.Locals("Title", "Operation failed") + c.Locals("ErrTitle", val+" is not a valid number") + return c.Status(fiber.StatusBadRequest).Render("err", fiber.Map{}) } - if int(style.UserID) != userid { - c.Status(fiber.StatusNotFound) - return c.Render("err", fiber.Map{ - "Title": "Operation failed", - "ErrTitle": "User " + strconv.Itoa(int(style.UserID)) + " is not the author of style " + id, - "User": u, - }) - } - styles = append(styles, *style) - _, err = strconv.Atoi(id) + style, err := models.GetStyleFromAuthor(id, uid) if err != nil { - c.Status(fiber.StatusNotFound) - return c.Render("err", fiber.Map{ - "Title": "Operation failed", - "ErrTitle": id + " is not a string", - "User": u, - }) + c.Locals("Title", "Operation failed") + c.Locals("ErrTitle", "User isn't the author of style with ID "+val) + return c.Status(fiber.StatusNotFound).Render("err", fiber.Map{}) } + + styles = append(styles, &style) } - // lastevent is used to link to the newest event in the modlog + // lastEvent is used to link to the newest event in the modlog // so the user will be presented with all of them on the screen. - var lastevent models.Log + var lastEvent *models.Log for index, style := range styles { event, _ := BanStyle(style, u, user, int(style.ID), strconv.Itoa(int(style.ID)), c) if index == len(styles)-1 { - lastevent = event + lastEvent = event } } - go sendBulkRemovalEmail(user, styles, lastevent) + go sendBulkRemovalEmail(user, styles, lastEvent) return c.Redirect("/modlog", fiber.StatusSeeOther) } -func sendBulkRemovalEmail(user *storage.User, styles []models.APIStyle, firstentry models.Log) { +func sendBulkRemovalEmail(user *storage.User, styles []*models.Style, event *models.Log) { args := fiber.Map{ "User": user, "Styles": styles, - "Log": firstentry, - "Link": config.BaseURL + "/modlog#id-" + strconv.Itoa(int(firstentry.ID)), + "Log": event, + "Link": config.BaseURL + "/modlog#id-" + strconv.Itoa(int(event.ID)), } title := strconv.Itoa(len(styles)) + " of your style have been removed" diff --git a/models/style.go b/models/style.go index bd47a52a..4581bebd 100644 --- a/models/style.go +++ b/models/style.go @@ -14,6 +14,7 @@ import ( "gorm.io/gorm" "userstyles.world/modules/config" + "userstyles.world/modules/database" "userstyles.world/modules/errors" "userstyles.world/modules/util" ) @@ -238,6 +239,15 @@ func AbleToReview(uid, sid uint) (string, bool) { return "", true } +func TempGetStyleByID(id int) (s *Style, err error) { + err = database.Conn. + Select("styles.*, u.username"). + Joins("JOIN users u ON u.id = styles.user_id"). + First(&s, "styles.id = ?", id). + Error + return s, err +} + // GetStyleFromAuthor tries to fetch a userstyle made by logged in user. func GetStyleFromAuthor(id, uid int) (Style, error) { var s Style