2024-06-22 05:04:21 +00:00
|
|
|
package api
|
2024-01-31 16:14:25 +00:00
|
|
|
|
|
|
|
import (
|
2024-10-30 15:26:13 +00:00
|
|
|
"fmt"
|
2024-10-30 16:15:25 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
|
2024-06-17 14:21:34 +00:00
|
|
|
"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"
|
2024-06-17 14:21:34 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
|
2024-10-30 15:26:13 +00:00
|
|
|
"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 {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 15:26:13 +00:00
|
|
|
user := c.Locals("user").(*sec.UserInfo)
|
2024-06-22 05:04:21 +00:00
|
|
|
|
2024-05-18 09:24:14 +00:00
|
|
|
var data struct {
|
2024-08-18 14:08:58 +00:00
|
|
|
AttachmentID string `json:"attachment" validate:"required"`
|
2024-05-18 12:39:46 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 05:04:21 +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
|
|
|
}
|
|
|
|
|
2024-10-30 15:26:13 +00:00
|
|
|
if err := database.C.Where("id = ?", user.ID).Updates(&models.Account{Avatar: &data.AttachmentID}).Error; err != nil {
|
2024-05-18 09:24:14 +00:00
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
2024-05-22 14:51:30 +00:00
|
|
|
} 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))
|
2024-05-22 14:51:30 +00:00
|
|
|
services.InvalidAuthCacheWithUser(user.ID)
|
2024-03-20 12:56:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setBanner(c *fiber.Ctx) error {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 15:26:13 +00:00
|
|
|
user := c.Locals("user").(*sec.UserInfo)
|
2024-05-18 12:39:46 +00:00
|
|
|
|
2024-05-18 09:24:14 +00:00
|
|
|
var data struct {
|
2024-08-18 14:08:58 +00:00
|
|
|
AttachmentID string `json:"attachment" validate:"required"`
|
2024-05-18 12:39:46 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 05:04:21 +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
|
|
|
}
|
|
|
|
|
2024-10-30 15:26:13 +00:00
|
|
|
if err := database.C.Where("id = ?", user.ID).Updates(&models.Account{Banner: &data.AttachmentID}).Error; err != nil {
|
2024-05-18 09:24:14 +00:00
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
2024-05-22 14:51:30 +00:00
|
|
|
} 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))
|
2024-05-22 14:51:30 +00:00
|
|
|
services.InvalidAuthCacheWithUser(user.ID)
|
2024-01-31 16:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
2024-06-26 09:07:20 +00:00
|
|
|
|
|
|
|
func getAvatar(c *fiber.Ctx) error {
|
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 15:26:13 +00:00
|
|
|
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))
|
|
|
|
}
|
2024-06-26 09:07:20 +00:00
|
|
|
|
2024-10-30 15:26:13 +00:00
|
|
|
if content := account.GetAvatar(); content == nil {
|
2024-06-26 09:07:20 +00:00
|
|
|
return c.SendStatus(fiber.StatusNotFound)
|
|
|
|
} else {
|
2024-06-26 09:16:14 +00:00
|
|
|
return c.Redirect(*content, fiber.StatusFound)
|
2024-06-26 09:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBanner(c *fiber.Ctx) error {
|
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 15:26:13 +00:00
|
|
|
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))
|
|
|
|
}
|
2024-06-26 09:07:20 +00:00
|
|
|
|
2024-10-30 15:26:13 +00:00
|
|
|
if content := account.GetBanner(); content == nil {
|
2024-06-26 09:07:20 +00:00
|
|
|
return c.SendStatus(fiber.StatusNotFound)
|
|
|
|
} else {
|
2024-06-26 09:16:14 +00:00
|
|
|
return c.Redirect(*content, fiber.StatusFound)
|
2024-06-26 09:07:20 +00:00
|
|
|
}
|
|
|
|
}
|