Leave realm API

This commit is contained in:
LittleSheep 2024-04-06 11:58:48 +08:00
parent f36592dd37
commit e74d38f479
4 changed files with 31 additions and 1 deletions

2
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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)
}
}

View File

@ -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)
}