Passport/pkg/server/avatar_api.go

74 lines
2.0 KiB
Go
Raw Normal View History

2024-01-31 16:14:25 +00:00
package server
import (
"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"
"git.solsynth.dev/hydrogen/passport/pkg/grpc"
2024-04-13 05:48:19 +00:00
"git.solsynth.dev/hydrogen/passport/pkg/models"
"git.solsynth.dev/hydrogen/passport/pkg/services"
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"
"github.com/samber/lo"
2024-01-31 16:14:25 +00:00
)
func setAvatar(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
var data struct {
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
}
if _, err := grpc.Attachments.CheckAttachmentExists(context.Background(), &pcpb.AttachmentLookupRequest{
Id: lo.ToPtr(uint64(data.AttachmentID)),
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
user.Avatar = &data.AttachmentID
2024-03-20 12:56:07 +00:00
if err := database.C.Save(&user).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
services.InvalidAuthCacheWithUser(user.ID)
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
var data struct {
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
}
if _, err := grpc.Attachments.CheckAttachmentExists(context.Background(), &pcpb.AttachmentLookupRequest{
Id: lo.ToPtr(uint64(data.AttachmentID)),
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
}
user.Banner = &data.AttachmentID
2024-03-20 12:56:07 +00:00
if err := database.C.Save(&user).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
services.InvalidAuthCacheWithUser(user.ID)
2024-01-31 16:14:25 +00:00
}
return c.SendStatus(fiber.StatusOK)
}