2024-09-17 06:50:05 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/http/exts"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/services"
|
2024-09-17 06:50:05 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
2024-10-13 04:36:51 +00:00
|
|
|
func getAuthPreference(c *fiber.Ctx) error {
|
2024-10-12 17:45:08 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-10-12 17:45:08 +00:00
|
|
|
|
2024-10-30 16:17:53 +00:00
|
|
|
cfg, err := services.GetAuthPreference(user)
|
2024-10-13 04:36:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-10-13 06:07:49 +00:00
|
|
|
return c.JSON(cfg.Config.Data())
|
2024-10-12 17:45:08 +00:00
|
|
|
}
|
|
|
|
|
2024-10-13 04:36:51 +00:00
|
|
|
func updateAuthPreference(c *fiber.Ctx) error {
|
2024-10-12 17:45:08 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-10-12 17:45:08 +00:00
|
|
|
|
|
|
|
var data models.AuthConfig
|
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-10-30 16:17:53 +00:00
|
|
|
cfg, err := services.UpdateAuthPreference(user, data)
|
2024-10-13 04:36:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
2024-10-14 14:23:51 +00:00
|
|
|
} else {
|
|
|
|
services.AddEvent(user.ID, "preferences.edit", "auth", c.IP(), c.Get(fiber.HeaderUserAgent))
|
2024-10-12 17:45:08 +00:00
|
|
|
}
|
|
|
|
|
2024-10-13 06:07:49 +00:00
|
|
|
return c.JSON(cfg.Config.Data())
|
2024-10-12 17:45:08 +00:00
|
|
|
}
|
|
|
|
|
2024-09-17 06:50:05 +00:00
|
|
|
func getNotificationPreference(c *fiber.Ctx) error {
|
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
notification, err := services.GetNotificationPreference(user)
|
2024-09-17 06:50:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(notification)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateNotificationPreference(c *fiber.Ctx) error {
|
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-09-17 06:50:05 +00:00
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Config map[string]bool `json:"config"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-10-30 16:17:53 +00:00
|
|
|
notification, err := services.UpdateNotificationPreference(user, data.Config)
|
2024-09-17 06:50:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
2024-10-14 14:23:51 +00:00
|
|
|
} else {
|
|
|
|
services.AddEvent(user.ID, "preferences.edit", "notifications", c.IP(), c.Get(fiber.HeaderUserAgent))
|
2024-09-17 06:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(notification)
|
|
|
|
}
|