2024-01-31 16:14:25 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-05-18 09:24:14 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
pcpb "git.solsynth.dev/hydrogen/paperclip/pkg/grpc/proto"
|
2024-04-13 05:48:19 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/database"
|
2024-05-18 09:24:14 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/grpc"
|
2024-04-13 05:48:19 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/models"
|
2024-05-18 12:39:46 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/utils"
|
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 {
|
|
|
|
user := c.Locals("principal").(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
|
|
|
}
|
|
|
|
|
|
|
|
if err := utils.BindAndValidate(c, &data); err != nil {
|
|
|
|
return err
|
2024-03-20 12:56:07 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 09:24:14 +00:00
|
|
|
if _, err := grpc.Attachments.CheckAttachmentExists(context.Background(), &pcpb.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-03-20 12:56:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setBanner(c *fiber.Ctx) error {
|
|
|
|
user := c.Locals("principal").(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
|
|
|
}
|
|
|
|
|
|
|
|
if err := utils.BindAndValidate(c, &data); err != nil {
|
|
|
|
return err
|
2024-03-20 12:56:07 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 09:24:14 +00:00
|
|
|
if _, err := grpc.Attachments.CheckAttachmentExists(context.Background(), &pcpb.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-01-31 16:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|