2024-06-22 05:04:21 +00:00
|
|
|
package api
|
2024-01-31 16:14:25 +00:00
|
|
|
|
|
|
|
import (
|
2024-05-18 09:24:14 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-06-22 10:09:08 +00:00
|
|
|
"git.solsynth.dev/hydrogen/paperclip/pkg/proto"
|
2024-06-17 14:21:34 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
|
2024-06-19 15:25:40 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/gap"
|
2024-06-17 14:21:34 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
2024-06-22 05:04:21 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
|
2024-06-17 14:21:34 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
|
2024-01-31 16:14:25 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-05-18 09:24:14 +00:00
|
|
|
"github.com/samber/lo"
|
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
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
|
2024-05-18 09:24:14 +00:00
|
|
|
var data struct {
|
2024-05-20 14:41:54 +00:00
|
|
|
AttachmentID uint `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-06-19 15:25:40 +00:00
|
|
|
pc, err := gap.DiscoverPaperclip()
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, "attachments services was not available")
|
|
|
|
}
|
2024-06-22 10:09:08 +00:00
|
|
|
if _, err := proto.NewAttachmentsClient(pc).CheckAttachmentExists(context.Background(), &proto.AttachmentLookupRequest{
|
2024-05-20 14:41:54 +00:00
|
|
|
Id: lo.ToPtr(uint64(data.AttachmentID)),
|
2024-05-18 09:24:14 +00:00
|
|
|
Usage: lo.ToPtr("p.avatar"),
|
|
|
|
}); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("avatar was not found in repository: %v", err))
|
|
|
|
}
|
2024-01-31 16:14:25 +00:00
|
|
|
|
2024-05-20 14:41:54 +00:00
|
|
|
user.Avatar = &data.AttachmentID
|
2024-03-20 12:56:07 +00:00
|
|
|
|
2024-05-18 09:24:14 +00:00
|
|
|
if err := database.C.Save(&user).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
2024-05-22 14:51:30 +00:00
|
|
|
} else {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
2024-05-18 12:39:46 +00:00
|
|
|
|
2024-05-18 09:24:14 +00:00
|
|
|
var data struct {
|
2024-05-20 14:41:54 +00:00
|
|
|
AttachmentID uint `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-06-19 15:25:40 +00:00
|
|
|
pc, err := gap.DiscoverPaperclip()
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, "attachments services was not available")
|
|
|
|
}
|
2024-06-22 10:09:08 +00:00
|
|
|
if _, err := proto.NewAttachmentsClient(pc).CheckAttachmentExists(context.Background(), &proto.AttachmentLookupRequest{
|
2024-05-20 14:41:54 +00:00
|
|
|
Id: lo.ToPtr(uint64(data.AttachmentID)),
|
2024-05-18 09:24:14 +00:00
|
|
|
Usage: lo.ToPtr("p.banner"),
|
|
|
|
}); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("banner was not found in repository: %v", err))
|
2024-03-20 12:56:07 +00:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:41:54 +00:00
|
|
|
user.Banner = &data.AttachmentID
|
2024-03-20 12:56:07 +00:00
|
|
|
|
2024-05-18 09:24:14 +00:00
|
|
|
if err := database.C.Save(&user).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
2024-05-22 14:51:30 +00:00
|
|
|
} else {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
|
|
|
|
if content := user.GetAvatar(); content == nil {
|
|
|
|
return c.SendStatus(fiber.StatusNotFound)
|
|
|
|
} else {
|
|
|
|
return c.Redirect(*content, fiber.StatusNotFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return c.Redirect(*content, fiber.StatusNotFound)
|
|
|
|
}
|
|
|
|
}
|