diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 14847f2..3355e44 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,9 +4,11 @@
-
+
+
+
@@ -159,7 +161,6 @@
-
@@ -184,7 +185,8 @@
-
+
+
diff --git a/pkg/internal/http/api/index.go b/pkg/internal/http/api/index.go
index 85aeb35..0e2a8be 100644
--- a/pkg/internal/http/api/index.go
+++ b/pkg/internal/http/api/index.go
@@ -124,7 +124,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
realms.Get("/me", listOwnedRealm)
realms.Get("/me/available", listAvailableRealm)
realms.Get("/:realm", getRealm)
- realms.Get("/:realm/members", listRealmMembers)
+ realms.Get("/:realm/members/:memberId", listRealmMembers)
realms.Get("/:realm/members/me", getMyRealmMember)
realms.Post("/", createRealm)
realms.Put("/:realmId", editRealm)
diff --git a/pkg/internal/http/api/realm_members_api.go b/pkg/internal/http/api/realm_members_api.go
index 389668e..f56bdda 100644
--- a/pkg/internal/http/api/realm_members_api.go
+++ b/pkg/internal/http/api/realm_members_api.go
@@ -77,14 +77,6 @@ func removeRealmMember(c *fiber.Ctx) error {
user := c.Locals("user").(models.Account)
alias := c.Params("realm")
- var data struct {
- Target string `json:"target" validate:"required"`
- }
-
- if err := exts.BindAndValidate(c, &data); err != nil {
- return err
- }
-
realm, err := services.GetRealmWithAlias(alias)
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
@@ -97,7 +89,15 @@ func removeRealmMember(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
- if err := services.RemoveRealmMember(user, account, realm); err != nil {
+ var member models.RealmMember
+ if err := database.C.Where(&models.RealmMember{
+ RealmID: realm.ID,
+ AccountID: account.ID,
+ }).First(&member).Error; err != nil {
+ return fiber.NewError(fiber.StatusNotFound, err.Error())
+ }
+
+ if err := services.RemoveRealmMember(user, member, realm); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.SendStatus(fiber.StatusOK)
@@ -118,14 +118,15 @@ func leaveRealm(c *fiber.Ctx) error {
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 {
+ var member models.RealmMember
+ if err := database.C.Where(&models.RealmMember{
+ RealmID: realm.ID,
+ AccountID: user.ID,
+ }).First(&member).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
- if err := services.RemoveRealmMember(user, account, realm); err != nil {
+ if err := services.RemoveRealmMember(user, member, realm); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.SendStatus(fiber.StatusOK)
diff --git a/pkg/internal/services/realms.go b/pkg/internal/services/realms.go
index 4a9e6e3..d16b8a3 100644
--- a/pkg/internal/services/realms.go
+++ b/pkg/internal/services/realms.go
@@ -101,7 +101,7 @@ func AddRealmMember(user models.Account, affected models.Account, target models.
if member, err := GetRealmMember(user.ID, target.ID); err != nil {
return fmt.Errorf("only realm member can add people: %v", err)
} else if member.PowerLevel < 50 {
- return fmt.Errorf("only realm moderator can add people")
+ return fmt.Errorf("only realm moderator can add member")
}
rel, err := GetRelationWithTwoNode(affected.ID, user.ID)
if err != nil || HasPermNodeWithDefault(
@@ -122,24 +122,16 @@ func AddRealmMember(user models.Account, affected models.Account, target models.
return err
}
-func RemoveRealmMember(user models.Account, affected models.Account, target models.Realm) error {
- if user.ID != affected.ID {
+func RemoveRealmMember(user models.Account, affected models.RealmMember, target models.Realm) error {
+ if user.ID != affected.AccountID {
if member, err := GetRealmMember(user.ID, target.ID); err != nil {
return fmt.Errorf("only realm member can remove other member: %v", err)
} else if member.PowerLevel < 50 {
- return fmt.Errorf("only realm moderator can invite people")
+ return fmt.Errorf("only realm moderator can kick member")
}
}
- var member models.RealmMember
- if err := database.C.Where(&models.RealmMember{
- RealmID: target.ID,
- AccountID: affected.ID,
- }).First(&member).Error; err != nil {
- return err
- }
-
- return database.C.Delete(&member).Error
+ return database.C.Delete(&affected).Error
}
func EditRealm(realm models.Realm) (models.Realm, error) {