Editable content rating for every community moderator

This commit is contained in:
LittleSheep 2025-04-05 12:06:11 +08:00
parent 1de0a86074
commit 26a267b111
2 changed files with 33 additions and 0 deletions

View File

@ -154,6 +154,38 @@ func updateAttachmentMeta(c *fiber.Ctx) error {
}
}
func updateAttachmentRating(c *fiber.Ctx) error {
id, _ := c.ParamsInt("id", 0)
user := c.Locals("nex_user").(*sec.UserInfo)
var data struct {
ContentRating int `json:"content_rating" validate:"required,min=3,max=21"`
QualityRating int `json:"quality_rating" validate:"min=0,max=5"`
}
if err := exts.BindAndValidate(c, &data); err != nil {
return err
}
attachment, err := services.GetAttachmentByID(uint(id))
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
} else if attachment.AccountID != user.ID {
if err = sec.EnsureGrantedPerm(c, "OverrideAttachmentRating", true); err != nil {
return err
}
}
attachment.ContentRating = data.ContentRating
attachment.QualityRating = data.QualityRating
if attachment, err = services.UpdateAttachment(attachment); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.JSON(attachment)
}
}
func deleteAttachment(c *fiber.Ctx) error {
id, _ := c.ParamsInt("id", 0)
user := c.Locals("nex_user").(*sec.UserInfo)

View File

@ -39,6 +39,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
attachments.Post("/", sec.ValidatorMiddleware, createAttachmentDirectly)
attachments.Post("/referenced", sec.ValidatorMiddleware, createAttachmentWithURL)
attachments.Put("/:id", sec.ValidatorMiddleware, updateAttachmentMeta)
attachments.Put("/:id/rating", sec.ValidatorMiddleware, updateAttachmentRating)
attachments.Delete("/:id", sec.ValidatorMiddleware, deleteAttachment)
}