Passport/pkg/internal/http/api/avatar_api.go

89 lines
2.2 KiB
Go
Raw Normal View History

package api
2024-01-31 16:14:25 +00:00
import (
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
2024-10-27 04:50:07 +00:00
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
2024-01-31 16:14:25 +00:00
"github.com/gofiber/fiber/v2"
2024-10-14 14:23:51 +00:00
"strconv"
2024-01-31 16:14:25 +00:00
)
func setAvatar(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
var data struct {
AttachmentID string `json:"attachment" validate:"required"`
2024-05-18 12:39:46 +00:00
}
if err := exts.BindAndValidate(c, &data); err != nil {
2024-05-18 12:39:46 +00:00
return err
2024-03-20 12:56:07 +00:00
}
user.Avatar = &data.AttachmentID
if err := database.C.Save(&user).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
2024-10-14 14:23:51 +00:00
services.AddEvent(user.ID, "profile.edit.avatar", strconv.Itoa(int(user.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
services.InvalidAuthCacheWithUser(user.ID)
2024-03-20 12:56:07 +00:00
}
return c.SendStatus(fiber.StatusOK)
}
func setBanner(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
2024-05-18 12:39:46 +00:00
var data struct {
AttachmentID string `json:"attachment" validate:"required"`
2024-05-18 12:39:46 +00:00
}
if err := exts.BindAndValidate(c, &data); err != nil {
2024-05-18 12:39:46 +00:00
return err
2024-03-20 12:56:07 +00:00
}
user.Banner = &data.AttachmentID
if err := database.C.Save(&user).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
2024-10-14 14:23:51 +00:00
services.AddEvent(user.ID, "profile.edit.banner", strconv.Itoa(int(user.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
services.InvalidAuthCacheWithUser(user.ID)
2024-01-31 16:14:25 +00:00
}
return c.SendStatus(fiber.StatusOK)
}
func getAvatar(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
if content := user.GetAvatar(); content == nil {
return c.SendStatus(fiber.StatusNotFound)
} else {
2024-06-26 09:16:14 +00:00
return c.Redirect(*content, fiber.StatusFound)
}
}
func getBanner(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
if content := user.GetBanner(); content == nil {
return c.SendStatus(fiber.StatusNotFound)
} else {
2024-06-26 09:16:14 +00:00
return c.Redirect(*content, fiber.StatusFound)
}
}