mirror of
https://github.com/versia-pub/versia-go.git
synced 2026-03-13 12:39:15 +01:00
refactor!: working WD-4 user discovery
This commit is contained in:
parent
cf0053312d
commit
61891d891a
91 changed files with 12768 additions and 5562 deletions
51
internal/handlers/user_handler/app_user_search.go
Normal file
51
internal/handlers/user_handler/app_user_search.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
package user_handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/lysand-org/versia-go/internal/api_schema"
|
||||
"github.com/lysand-org/versia-go/pkg/webfinger"
|
||||
"net"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func (i *Handler) SearchUser(c *fiber.Ctx) error {
|
||||
var req api_schema.SearchUserRequest
|
||||
if err := c.QueryParser(&req); err != nil {
|
||||
return api_schema.ErrInvalidRequestBody(nil)
|
||||
}
|
||||
|
||||
if err := i.bodyValidator.Validate(req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
u, err := i.userService.Search(c.UserContext(), req)
|
||||
if err != nil {
|
||||
// TODO: Move into service error
|
||||
if errors.Is(err, syscall.ECONNREFUSED) {
|
||||
return api_schema.ErrBadRequest(map[string]any{"reason": "Remote server is offline"})
|
||||
}
|
||||
|
||||
if errors.Is(err, webfinger.ErrUserNotFound) {
|
||||
return api_schema.ErrUserNotFound
|
||||
}
|
||||
|
||||
var dnsErr *net.DNSError
|
||||
if errors.As(err, &dnsErr) {
|
||||
if dnsErr.IsNotFound {
|
||||
return api_schema.ErrBadRequest(map[string]any{"reason": fmt.Sprintf("Could not resolve %s", dnsErr.Name)})
|
||||
}
|
||||
|
||||
if dnsErr.IsTimeout {
|
||||
return api_schema.ErrInternalServerError(map[string]any{"reason": "Local DNS server timed out"})
|
||||
}
|
||||
}
|
||||
|
||||
i.log.Error(err, "Failed to search for user", "username", req.Username)
|
||||
|
||||
return api_schema.ErrInternalServerError(nil)
|
||||
}
|
||||
|
||||
return c.JSON((*api_schema.LysandUser)(u.ToLysand()))
|
||||
}
|
||||
|
|
@ -8,9 +8,11 @@ import (
|
|||
)
|
||||
|
||||
type Handler struct {
|
||||
userService service.UserService
|
||||
federationService service.FederationService
|
||||
inboxService service.InboxService
|
||||
requestSigner service.RequestSigner
|
||||
|
||||
userService service.UserService
|
||||
inboxService service.InboxService
|
||||
|
||||
bodyValidator validators.BodyValidator
|
||||
requestValidator validators.RequestValidator
|
||||
|
|
@ -18,11 +20,13 @@ type Handler struct {
|
|||
log logr.Logger
|
||||
}
|
||||
|
||||
func New(userService service.UserService, federationService service.FederationService, inboxService service.InboxService, bodyValidator validators.BodyValidator, requestValidator validators.RequestValidator, log logr.Logger) *Handler {
|
||||
func New(federationService service.FederationService, requestSigner service.RequestSigner, userService service.UserService, inboxService service.InboxService, bodyValidator validators.BodyValidator, requestValidator validators.RequestValidator, log logr.Logger) *Handler {
|
||||
return &Handler{
|
||||
userService: userService,
|
||||
federationService: federationService,
|
||||
inboxService: inboxService,
|
||||
requestSigner: requestSigner,
|
||||
|
||||
userService: userService,
|
||||
inboxService: inboxService,
|
||||
|
||||
bodyValidator: bodyValidator,
|
||||
requestValidator: requestValidator,
|
||||
|
|
@ -41,6 +45,7 @@ func (i *Handler) Register(r fiber.Router) {
|
|||
r.Get("/api/app/users/:id", i.GetUser)
|
||||
r.Post("/api/app/users/", i.CreateUser)
|
||||
|
||||
r.Get("/api/users/search", i.SearchUser)
|
||||
r.Get("/api/users/:id", i.GetLysandUser)
|
||||
r.Post("/api/users/:id/inbox", i.LysandInbox)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,5 +27,5 @@ func (i *Handler) GetLysandUser(c *fiber.Ctx) error {
|
|||
return api_schema.ErrNotFound(map[string]any{"id": parsedRequestedUserID})
|
||||
}
|
||||
|
||||
return c.JSON(u.ToLysand())
|
||||
return i.requestSigner.Sign(c, u.Signer, u.ToLysand())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
package user_handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/lysand-org/versia-go/config"
|
||||
"github.com/lysand-org/versia-go/internal/api_schema"
|
||||
"github.com/lysand-org/versia-go/internal/helpers"
|
||||
"github.com/lysand-org/versia-go/pkg/webfinger"
|
||||
)
|
||||
|
|
@ -16,13 +18,19 @@ func (i *Handler) Webfinger(c *fiber.Ctx) error {
|
|||
}
|
||||
|
||||
if userID.Domain != config.C.PublicAddress.Host {
|
||||
return c.Status(fiber.StatusNotFound).JSON(webfinger.Response{
|
||||
return c.Status(fiber.StatusBadRequest).JSON(webfinger.Response{
|
||||
Error: helpers.StringPtr("The requested user is a remote user"),
|
||||
})
|
||||
}
|
||||
|
||||
wf, err := i.userService.GetWebfingerForUser(c.UserContext(), userID.ID)
|
||||
if err != nil {
|
||||
if errors.Is(err, api_schema.ErrUserNotFound) {
|
||||
return c.Status(fiber.StatusNotFound).JSON(webfinger.Response{
|
||||
Error: helpers.StringPtr("User could not be found"),
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(webfinger.Response{
|
||||
Error: helpers.StringPtr("Failed to query user"),
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue