✨ Abuse report system
This commit is contained in:
@ -30,6 +30,17 @@ func MapAPIs(app *fiber.App, baseURL string) {
|
||||
preferences.Put("/notifications", updateNotificationPreference)
|
||||
}
|
||||
|
||||
reports := api.Group("/reports").Name("Reports API")
|
||||
{
|
||||
abuse := reports.Group("/abuse").Name("Abuse Reports")
|
||||
{
|
||||
abuse.Get("/", listAbuseReports)
|
||||
abuse.Get("/:id", getAbuseReport)
|
||||
abuse.Put("/:id/status", updateAbuseReportStatus)
|
||||
abuse.Post("/", createAbuseReport)
|
||||
}
|
||||
}
|
||||
|
||||
api.Get("/users/lookup", lookupAccount)
|
||||
api.Get("/users/search", searchAccount)
|
||||
|
||||
|
77
pkg/internal/server/api/reports.go
Normal file
77
pkg/internal/server/api/reports.go
Normal file
@ -0,0 +1,77 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func listAbuseReports(c *fiber.Ctx) error {
|
||||
if err := exts.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
reports, err := services.ListAbuseReport(user)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(reports)
|
||||
}
|
||||
|
||||
func getAbuseReport(c *fiber.Ctx) error {
|
||||
id, _ := c.ParamsInt("id")
|
||||
report, err := services.GetAbuseReport(uint(id))
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(report)
|
||||
}
|
||||
|
||||
func updateAbuseReportStatus(c *fiber.Ctx) error {
|
||||
if err := exts.EnsureGrantedPerm(c, "DealAbuseReport", true); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var data struct {
|
||||
Status string `json:"status" validate:"required"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
id, _ := c.ParamsInt("id")
|
||||
|
||||
if err := services.UpdateAbuseReportStatus(uint(id), data.Status); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
}
|
||||
|
||||
func createAbuseReport(c *fiber.Ctx) error {
|
||||
if err := exts.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
var data struct {
|
||||
Resource string `json:"resource" validate:"required"`
|
||||
Reason string `json:"reason" validate:"required,min=16,max=4096"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
report, err := services.NewAbuseReport(data.Resource, data.Reason, user)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(report)
|
||||
}
|
Reference in New Issue
Block a user