From e50a338378bcc65b2610743debf5b4cf2bcf86d8 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 6 Apr 2024 01:40:01 +0800 Subject: [PATCH] :sparkles: Fuzzy friend invitation --- pkg/server/friendships_api.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/pkg/server/friendships_api.go b/pkg/server/friendships_api.go index e2c0167..c43acd2 100644 --- a/pkg/server/friendships_api.go +++ b/pkg/server/friendships_api.go @@ -35,11 +35,23 @@ func getFriendship(c *fiber.Ctx) error { func makeFriendship(c *fiber.Ctx) error { user := c.Locals("principal").(models.Account) + relatedName := c.Query("related") relatedId, _ := c.ParamsInt("relatedId", 0) - related, err := services.GetAccount(uint(relatedId)) - if err != nil { - return fiber.NewError(fiber.StatusNotFound, err.Error()) + var err error + var related models.Account + if relatedId > 0 { + related, err = services.GetAccount(uint(relatedId)) + if err != nil { + return fiber.NewError(fiber.StatusNotFound, err.Error()) + } + } else if len(relatedName) > 0 { + related, err = services.LookupAccount(relatedName) + if err != nil { + return fiber.NewError(fiber.StatusNotFound, err.Error()) + } + } else { + return fiber.NewError(fiber.StatusBadRequest, "must one of username or user id") } friend, err := services.NewFriend(user, related, models.FriendshipPending)