From e74d38f479a20952266259556fcb7806e1ea262c Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 6 Apr 2024 11:58:48 +0800 Subject: [PATCH] :sparkles: Leave realm API --- go.mod | 2 +- go.sum | 2 ++ pkg/server/realm_members_api.go | 27 +++++++++++++++++++++++++++ pkg/server/startup.go | 1 + 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 644b6c3..e50ac9d 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.solsynth.dev/hydrogen/interactive go 1.21.6 require ( - git.solsynth.dev/hydrogen/identity v0.0.0-20240331080359-e8aac7bb6627 + git.solsynth.dev/hydrogen/identity v0.0.0-20240405190322-7f5183f83bbf github.com/go-playground/validator/v10 v10.17.0 github.com/gofiber/fiber/v2 v2.52.4 github.com/gofiber/template/html/v2 v2.1.1 diff --git a/go.sum b/go.sum index ed7214b..39b520d 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ git.solsynth.dev/hydrogen/identity v0.0.0-20240331080359-e8aac7bb6627 h1:BUhDqy/Whw1yVbDpmGk7clzRAC1jo+AOsCwuwhCVwkg= git.solsynth.dev/hydrogen/identity v0.0.0-20240331080359-e8aac7bb6627/go.mod h1:GxcduEpQWQ2mO37A9uRtseS680uMLi957GDywRBAJHg= +git.solsynth.dev/hydrogen/identity v0.0.0-20240405190322-7f5183f83bbf h1:dj9XcgDsI7LGkHmDxFBGwYGveBYYklda1hS2z/h8Z9w= +git.solsynth.dev/hydrogen/identity v0.0.0-20240405190322-7f5183f83bbf/go.mod h1:GxcduEpQWQ2mO37A9uRtseS680uMLi957GDywRBAJHg= github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= diff --git a/pkg/server/realm_members_api.go b/pkg/server/realm_members_api.go index cdb561c..bee6fd1 100644 --- a/pkg/server/realm_members_api.go +++ b/pkg/server/realm_members_api.go @@ -84,3 +84,30 @@ func kickRealm(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusOK) } } + +func leaveRealm(c *fiber.Ctx) error { + user := c.Locals("principal").(models.Account) + realmId, _ := c.ParamsInt("realmId", 0) + + var realm models.Realm + if err := database.C.Where(&models.Realm{ + BaseModel: models.BaseModel{ID: uint(realmId)}, + }).First(&realm).Error; err != nil { + return fiber.NewError(fiber.StatusNotFound, err.Error()) + } else if user.ID == realm.AccountID { + return fiber.NewError(fiber.StatusBadRequest, "you cannot leave your own realm") + } + + var account models.Account + if err := database.C.Where(&models.Account{ + BaseModel: models.BaseModel{ID: user.ID}, + }).First(&account).Error; err != nil { + return fiber.NewError(fiber.StatusNotFound, err.Error()) + } + + if err := services.KickRealmMember(account, realm); err != nil { + return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } else { + return c.SendStatus(fiber.StatusOK) + } +} diff --git a/pkg/server/startup.go b/pkg/server/startup.go index 12038d1..07de0bb 100644 --- a/pkg/server/startup.go +++ b/pkg/server/startup.go @@ -121,6 +121,7 @@ func NewServer() { realms.Post("/", authMiddleware, createRealm) realms.Post("/:realmId/invite", authMiddleware, inviteRealm) realms.Post("/:realmId/kick", authMiddleware, kickRealm) + realms.Post("/:realmId/leave", authMiddleware, kickRealm) realms.Put("/:realmId", authMiddleware, editRealm) realms.Delete("/:realmId", authMiddleware, deleteRealm) }