💥 Move remove member api arguments from body to querystring just as messaging

This commit is contained in:
LittleSheep 2024-12-01 01:26:33 +08:00
parent 192a03e9d1
commit 10e9813bea
4 changed files with 26 additions and 31 deletions

8
.idea/workspace.xml generated
View File

@ -4,9 +4,11 @@
<option name="autoReloadType" value="ALL" /> <option name="autoReloadType" value="ALL" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":sparkles: Better relationships stauts query"> <list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":truck: Move make friendship api">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/http/api/index.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/http/api/index.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/pkg/internal/http/api/index.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/http/api/index.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/http/api/realm_members_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/http/api/realm_members_api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/realms.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/realms.go" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -159,7 +161,6 @@
</component> </component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<option name="CHECK_CODE_SMELLS_BEFORE_PROJECT_COMMIT" value="false" /> <option name="CHECK_CODE_SMELLS_BEFORE_PROJECT_COMMIT" value="false" />
<MESSAGE value=":sparkles: Realm operations now available in authkit" />
<MESSAGE value=":bug: Fix grpc namespace conflict" /> <MESSAGE value=":bug: Fix grpc namespace conflict" />
<MESSAGE value=":recycle: Replace nil context with context.Background in authkit" /> <MESSAGE value=":recycle: Replace nil context with context.Background in authkit" />
<MESSAGE value=":sparkles: Authkit support check user related permission" /> <MESSAGE value=":sparkles: Authkit support check user related permission" />
@ -184,7 +185,8 @@
<MESSAGE value=":boom: Passing relationship api arguments in body instead of querystring" /> <MESSAGE value=":boom: Passing relationship api arguments in body instead of querystring" />
<MESSAGE value=":sparkles: Better check in experience random algorithm" /> <MESSAGE value=":sparkles: Better check in experience random algorithm" />
<MESSAGE value=":sparkles: Better relationships stauts query" /> <MESSAGE value=":sparkles: Better relationships stauts query" />
<option name="LAST_COMMIT_MESSAGE" value=":sparkles: Better relationships stauts query" /> <MESSAGE value=":truck: Move make friendship api" />
<option name="LAST_COMMIT_MESSAGE" value=":truck: Move make friendship api" />
<option name="GROUP_MULTIFILE_MERGE_BY_DIRECTORY" value="true" /> <option name="GROUP_MULTIFILE_MERGE_BY_DIRECTORY" value="true" />
</component> </component>
<component name="VgoProject"> <component name="VgoProject">

View File

@ -124,7 +124,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
realms.Get("/me", listOwnedRealm) realms.Get("/me", listOwnedRealm)
realms.Get("/me/available", listAvailableRealm) realms.Get("/me/available", listAvailableRealm)
realms.Get("/:realm", getRealm) realms.Get("/:realm", getRealm)
realms.Get("/:realm/members", listRealmMembers) realms.Get("/:realm/members/:memberId", listRealmMembers)
realms.Get("/:realm/members/me", getMyRealmMember) realms.Get("/:realm/members/me", getMyRealmMember)
realms.Post("/", createRealm) realms.Post("/", createRealm)
realms.Put("/:realmId", editRealm) realms.Put("/:realmId", editRealm)

View File

@ -77,14 +77,6 @@ func removeRealmMember(c *fiber.Ctx) error {
user := c.Locals("user").(models.Account) user := c.Locals("user").(models.Account)
alias := c.Params("realm") 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) realm, err := services.GetRealmWithAlias(alias)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error()) return fiber.NewError(fiber.StatusNotFound, err.Error())
@ -97,7 +89,15 @@ func removeRealmMember(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusNotFound, err.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()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else { } else {
return c.SendStatus(fiber.StatusOK) 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") return fiber.NewError(fiber.StatusBadRequest, "you cannot leave your own realm")
} }
var account models.Account var member models.RealmMember
if err := database.C.Where(&models.Account{ if err := database.C.Where(&models.RealmMember{
BaseModel: models.BaseModel{ID: user.ID}, RealmID: realm.ID,
}).First(&account).Error; err != nil { AccountID: user.ID,
}).First(&member).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error()) 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()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else { } else {
return c.SendStatus(fiber.StatusOK) return c.SendStatus(fiber.StatusOK)

View File

@ -101,7 +101,7 @@ func AddRealmMember(user models.Account, affected models.Account, target models.
if member, err := GetRealmMember(user.ID, target.ID); err != nil { if member, err := GetRealmMember(user.ID, target.ID); err != nil {
return fmt.Errorf("only realm member can add people: %v", err) return fmt.Errorf("only realm member can add people: %v", err)
} else if member.PowerLevel < 50 { } 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) rel, err := GetRelationWithTwoNode(affected.ID, user.ID)
if err != nil || HasPermNodeWithDefault( if err != nil || HasPermNodeWithDefault(
@ -122,24 +122,16 @@ func AddRealmMember(user models.Account, affected models.Account, target models.
return err return err
} }
func RemoveRealmMember(user models.Account, affected models.Account, target models.Realm) error { func RemoveRealmMember(user models.Account, affected models.RealmMember, target models.Realm) error {
if user.ID != affected.ID { if user.ID != affected.AccountID {
if member, err := GetRealmMember(user.ID, target.ID); err != nil { if member, err := GetRealmMember(user.ID, target.ID); err != nil {
return fmt.Errorf("only realm member can remove other member: %v", err) return fmt.Errorf("only realm member can remove other member: %v", err)
} else if member.PowerLevel < 50 { } 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 return database.C.Delete(&affected).Error
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
} }
func EditRealm(realm models.Realm) (models.Realm, error) { func EditRealm(realm models.Realm) (models.Realm, error) {