✨ Bots aka. automated accounts
This commit is contained in:
90
pkg/internal/server/api/bots_api.go
Normal file
90
pkg/internal/server/api/bots_api.go
Normal file
@ -0,0 +1,90 @@
|
||||
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)
|
||||
|
||||
var bots []models.Account
|
||||
if err := database.C.Where("automated_id = ?", user.AutomatedID).Find(&bots).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(bots)
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
id, _ := c.ParamsInt("id", 0)
|
||||
|
||||
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)
|
||||
}
|
@ -101,6 +101,13 @@ func MapAPIs(app *fiber.App, baseURL string) {
|
||||
{
|
||||
developers.Post("/notify", notifyUser)
|
||||
|
||||
bots := developers.Group("/bots").Name("Bots")
|
||||
{
|
||||
bots.Get("/", listBots)
|
||||
bots.Post("/", createBot)
|
||||
bots.Delete("/:id", deleteBot)
|
||||
}
|
||||
|
||||
keys := developers.Group("/keys").Name("Keys")
|
||||
{
|
||||
keys.Get("/", listBotKeys)
|
||||
|
@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"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"
|
||||
@ -8,32 +9,36 @@ import (
|
||||
)
|
||||
|
||||
func notifyUser(c *fiber.Ctx) error {
|
||||
if err := exts.EnsureGrantedPerm(c, "DevNotifyUser", true); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
var data struct {
|
||||
ClientID string `json:"client_id" validate:"required"`
|
||||
ClientSecret string `json:"client_secret" validate:"required"`
|
||||
Topic string `json:"type" validate:"required"`
|
||||
Title string `json:"subject" validate:"required,max=1024"`
|
||||
Subtitle *string `json:"subtitle" validate:"max=1024"`
|
||||
Body string `json:"content" validate:"required,max=4096"`
|
||||
Metadata map[string]any `json:"metadata"`
|
||||
Avatar *string `json:"avatar"`
|
||||
Picture *string `json:"picture"`
|
||||
IsForcePush bool `json:"is_force_push"`
|
||||
IsRealtime bool `json:"is_realtime"`
|
||||
UserID uint `json:"user_id" validate:"required"`
|
||||
ClientID string `json:"client_id" validate:"required"`
|
||||
Topic string `json:"type" validate:"required"`
|
||||
Title string `json:"subject" validate:"required,max=1024"`
|
||||
Subtitle *string `json:"subtitle" validate:"max=1024"`
|
||||
Body string `json:"content" validate:"required,max=4096"`
|
||||
Metadata map[string]any `json:"metadata"`
|
||||
Avatar *string `json:"avatar"`
|
||||
Picture *string `json:"picture"`
|
||||
IsForcePush bool `json:"is_force_push"`
|
||||
IsRealtime bool `json:"is_realtime"`
|
||||
UserID uint `json:"user_id" validate:"required"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
client, err := services.GetThirdClientWithSecret(data.ClientID, data.ClientSecret)
|
||||
client, err := services.GetThirdClientWithUser(data.ClientID, user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusForbidden, err.Error())
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to get client: %v", err))
|
||||
}
|
||||
|
||||
var user models.Account
|
||||
if user, err = services.GetAccount(data.UserID); err != nil {
|
||||
var target models.Account
|
||||
if target, err = services.GetAccount(data.UserID); err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
@ -47,7 +52,7 @@ func notifyUser(c *fiber.Ctx) error {
|
||||
Picture: data.Picture,
|
||||
IsRealtime: data.IsRealtime,
|
||||
IsForcePush: data.IsForcePush,
|
||||
AccountID: user.ID,
|
||||
AccountID: target.ID,
|
||||
SenderID: &client.ID,
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user