💥 Passing relationship api arguments in body instead of querystring

This commit is contained in:
LittleSheep 2024-11-30 00:35:11 +08:00
parent 659548a544
commit 55b3a6f164
2 changed files with 32 additions and 27 deletions

9
.idea/workspace.xml generated
View File

@ -4,10 +4,9 @@
<option name="autoReloadType" value="ALL" />
</component>
<component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":truck: Rename daily-sign to check-in">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":sparkles: Result modifiers in check-in">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/authkit/models/check_in.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/authkit/models/check_in.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/check_in.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/check_in.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/http/api/relationships_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/http/api/relationships_api.go" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -160,7 +159,6 @@
</component>
<component name="VcsManagerConfiguration">
<option name="CHECK_CODE_SMELLS_BEFORE_PROJECT_COMMIT" value="false" />
<MESSAGE value=":truck: Update package name from Hypdrogen to Hypernet" />
<MESSAGE value=":sparkles: Support users related rpc calls" />
<MESSAGE value=":technologist: Improve DX by extending authkit" />
<MESSAGE value=":sparkles: Add list relative method into authkit" />
@ -185,7 +183,8 @@
<MESSAGE value=":bug: Fix group permission didn't extend properly" />
<MESSAGE value=":mute: Remove authenticate result logging" />
<MESSAGE value=":truck: Rename daily-sign to check-in" />
<option name="LAST_COMMIT_MESSAGE" value=":truck: Rename daily-sign to check-in" />
<MESSAGE value=":sparkles: Result modifiers in check-in" />
<option name="LAST_COMMIT_MESSAGE" value=":sparkles: Result modifiers in check-in" />
<option name="GROUP_MULTIFILE_MERGE_BY_DIRECTORY" value="true" />
</component>
<component name="VgoProject">

View File

@ -112,30 +112,33 @@ func makeFriendship(c *fiber.Ctx) error {
return err
}
user := c.Locals("user").(models.Account)
relatedName := c.Query("related")
relatedId, _ := c.ParamsInt("relatedId", 0)
var data struct {
Related string `json:"related" validate:"required"`
}
if err := exts.BindAndValidate(c, &data); err != nil {
return err
}
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 numericId, err := strconv.Atoi(data.Related); err == nil {
related, err = services.GetAccount(uint(numericId))
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
} else {
return fiber.NewError(fiber.StatusBadRequest, "must one of username or user id")
related, err = services.LookupAccount(data.Related)
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
}
friend, err := services.NewFriend(user, related)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
services.AddEvent(user.ID, "relationships.friends.new", strconv.Itoa(relatedId), c.IP(), c.Get(fiber.HeaderUserAgent))
services.AddEvent(user.ID, "relationships.friends.new", strconv.Itoa(int(related.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.JSON(friend)
}
}
@ -145,30 +148,33 @@ func makeBlockship(c *fiber.Ctx) error {
return err
}
user := c.Locals("user").(models.Account)
relatedName := c.Query("related")
relatedId, _ := c.ParamsInt("relatedId", 0)
var data struct {
Related string `json:"related" validate:"required"`
}
if err := exts.BindAndValidate(c, &data); err != nil {
return err
}
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 numericId, err := strconv.Atoi(data.Related); err == nil {
related, err = services.GetAccount(uint(numericId))
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
} else {
return fiber.NewError(fiber.StatusBadRequest, "must one of username or user id")
related, err = services.LookupAccount(data.Related)
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
}
friend, err := services.NewBlockship(user, related)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
services.AddEvent(user.ID, "relationships.blocks.new", strconv.Itoa(relatedId), c.IP(), c.Get(fiber.HeaderUserAgent))
services.AddEvent(user.ID, "relationships.blocks.new", strconv.Itoa(int(related.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.JSON(friend)
}
}