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