2024-08-24 15:49:19 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
|
|
|
|
"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"
|
|
|
|
"github.com/samber/lo"
|
|
|
|
"gorm.io/datatypes"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func listBots(c *fiber.Ctx) error {
|
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
|
2024-08-25 09:16:22 +00:00
|
|
|
tx := database.C.Where("automated_id = ?", user.ID)
|
2024-08-25 09:03:06 +00:00
|
|
|
|
|
|
|
countTx := tx
|
|
|
|
var count int64
|
|
|
|
if err := countTx.Model(&models.Account{}).Count(&count).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-08-24 15:49:19 +00:00
|
|
|
var bots []models.Account
|
2024-08-25 09:03:06 +00:00
|
|
|
if err := tx.Find(&bots).Error; err != nil {
|
2024-08-24 15:49:19 +00:00
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-08-25 09:03:06 +00:00
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"count": count,
|
|
|
|
"data": bots,
|
|
|
|
})
|
2024-08-24 15:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createBot(c *fiber.Ctx) error {
|
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
|
|
|
|
cnt, _ := services.GetBotCount(user)
|
|
|
|
if err := exts.EnsureGrantedPerm(c, "CreateBots", cnt+1); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Name string `json:"name" validate:"required,lowercase,alphanum,min=4,max=16"`
|
|
|
|
Nick string `json:"nick" validate:"required"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
data.Name = strings.TrimSpace(data.Name)
|
|
|
|
data.Nick = strings.TrimSpace(data.Nick)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !services.ValidateAccountName(data.Nick, 4, 24) {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "invalid bot nick, length requires 4 to 24")
|
|
|
|
}
|
|
|
|
|
|
|
|
bot, err := services.NewBot(user, models.Account{
|
|
|
|
Name: data.Name,
|
|
|
|
Nick: data.Nick,
|
|
|
|
Description: data.Description,
|
|
|
|
ConfirmedAt: lo.ToPtr(time.Now()),
|
|
|
|
PermNodes: datatypes.JSONMap{},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
|
|
|
return c.JSON(bot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteBot(c *fiber.Ctx) error {
|
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
|
2024-08-25 12:51:58 +00:00
|
|
|
id, _ := c.ParamsInt("botId", 0)
|
2024-08-24 15:49:19 +00:00
|
|
|
|
|
|
|
var bot models.Account
|
|
|
|
if err := database.C.Where("id = ? AND automated_id = ?", id, user.ID).First(&bot).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := services.DeleteAccount(bot.ID); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(bot)
|
|
|
|
}
|