2024-08-11 03:51:22 +02:00
|
|
|
package user_handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/lysand-org/versia-go/internal/validators/val_impls"
|
2024-08-22 23:03:38 +02:00
|
|
|
"github.com/lysand-org/versia-go/pkg/versia"
|
2024-08-11 03:51:22 +02:00
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/lysand-org/versia-go/internal/api_schema"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (i *Handler) LysandInbox(c *fiber.Ctx) error {
|
|
|
|
|
if err := i.requestValidator.ValidateFiberCtx(c.UserContext(), c); err != nil {
|
|
|
|
|
if errors.Is(err, val_impls.ErrInvalidSignature) {
|
|
|
|
|
i.log.Error(err, "Invalid signature")
|
|
|
|
|
return c.SendStatus(fiber.StatusUnauthorized)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i.log.Error(err, "Failed to validate signature")
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var raw json.RawMessage
|
|
|
|
|
if err := json.Unmarshal(c.Body(), &raw); err != nil {
|
|
|
|
|
i.log.Error(err, "Failed to unmarshal inbox object")
|
|
|
|
|
return api_schema.ErrBadRequest(nil)
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 23:03:38 +02:00
|
|
|
obj, err := versia.ParseInboxObject(raw)
|
2024-08-11 03:51:22 +02:00
|
|
|
if err != nil {
|
|
|
|
|
i.log.Error(err, "Failed to parse inbox object")
|
|
|
|
|
|
2024-08-22 23:03:38 +02:00
|
|
|
if errors.Is(err, versia.UnknownEntityTypeError{}) {
|
2024-08-11 03:51:22 +02:00
|
|
|
return api_schema.ErrNotFound(map[string]any{
|
2024-08-22 23:03:38 +02:00
|
|
|
"error": fmt.Sprintf("Unknown object type: %s", err.(versia.UnknownEntityTypeError).Type),
|
2024-08-11 03:51:22 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userId, err := uuid.Parse(c.Params("id"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
|
|
|
"error": "Invalid user ID",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := i.inboxService.Handle(c.UserContext(), obj, userId); err != nil {
|
|
|
|
|
i.log.Error(err, "Failed to handle inbox", "user", userId)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
|
}
|