Skip to content

Commit

Permalink
refactor(style): improve code consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
vednoc committed Oct 16, 2023
1 parent 0af5966 commit d79d32d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 88 deletions.
20 changes: 9 additions & 11 deletions handlers/style/ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand All @@ -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
Expand All @@ -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, &notification); err != nil {
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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"
Expand Down
121 changes: 44 additions & 77 deletions handlers/style/bulkban.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions models/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"gorm.io/gorm"

"userstyles.world/modules/config"
"userstyles.world/modules/database"
"userstyles.world/modules/errors"
"userstyles.world/modules/util"
)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d79d32d

Please sign in to comment.