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

97 lines
2.8 KiB
Go
Raw Normal View History

package api
2024-01-31 16:14:25 +00:00
import (
"fmt"
"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/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
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").(*sec.UserInfo)
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
}
if err := database.C.Where("id = ?", user.ID).Updates(&models.Account{Avatar: &data.AttachmentID}).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").(*sec.UserInfo)
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
}
if err := database.C.Where("id = ?", user.ID).Updates(&models.Account{Banner: &data.AttachmentID}).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").(*sec.UserInfo)
var account models.Account
if err := database.C.Where("id = ?", user.ID).First(&account).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("account was not found: %v", err))
}
if content := account.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").(*sec.UserInfo)
var account models.Account
if err := database.C.Where("id = ?", user.ID).First(&account).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("account was not found: %v", err))
}
if content := account.GetBanner(); content == nil {
return c.SendStatus(fiber.StatusNotFound)
} else {
2024-06-26 09:16:14 +00:00
return c.Redirect(*content, fiber.StatusFound)
}
}