2024-09-17 06:50:05 +00:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
|
2024-10-13 04:36:51 +00:00
|
|
|
cfg, err := services.GetAuthPreference(user)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
|
|
|
|
var data models.AuthConfig
|
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-10-13 04:36:51 +00:00
|
|
|
cfg, err := services.UpdateAuthPreference(user, data)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
notification, err := services.GetNotificationPreference(user)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Config map[string]bool `json:"config"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
notification, err := services.UpdateNotificationPreference(user, data.Config)
|
|
|
|
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)
|
|
|
|
}
|