🐛 Fixed friendship check

This commit is contained in:
LittleSheep 2024-04-06 23:34:02 +08:00
parent 44d2ec9c4a
commit d1f54099c4
4 changed files with 38 additions and 23 deletions

View File

@ -72,21 +72,3 @@ type AccountContact struct {
VerifiedAt *time.Time `json:"verified_at"` VerifiedAt *time.Time `json:"verified_at"`
AccountID uint `json:"account_id"` AccountID uint `json:"account_id"`
} }
type FriendshipStatus = int8
const (
FriendshipPending = FriendshipStatus(iota)
FriendshipActive
FriendshipBlocked
)
type AccountFriendship struct {
BaseModel
AccountID uint `json:"account_id"`
RelatedID uint `json:"related_id"`
Account Account `json:"account"`
Related Account `json:"related"`
Status FriendshipStatus `json:"status"`
}

20
pkg/models/friendships.go Normal file
View File

@ -0,0 +1,20 @@
package models
type FriendshipStatus = int8
const (
FriendshipPending = FriendshipStatus(iota)
FriendshipActive
FriendshipBlocked
)
type AccountFriendship struct {
BaseModel
AccountID uint `json:"account_id"`
RelatedID uint `json:"related_id"`
BlockedBy *uint `json:"blocked_by"`
Account Account `json:"account"`
Related Account `json:"related"`
Status FriendshipStatus `json:"status"`
}

View File

@ -81,15 +81,12 @@ func editFriendship(c *fiber.Ctx) error {
friendship, err := services.GetFriendWithTwoSides(user.ID, related.ID) friendship, err := services.GetFriendWithTwoSides(user.ID, related.ID)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error()) return fiber.NewError(fiber.StatusNotFound, err.Error())
} else if friendship.Status == models.FriendshipPending && data.Status == uint8(models.FriendshipActive) {
if friendship.RelatedID != user.ID {
return fiber.NewError(fiber.StatusNotFound, "only related person can accept friendship")
}
} }
originalStatus := friendship.Status
friendship.Status = models.FriendshipStatus(data.Status) friendship.Status = models.FriendshipStatus(data.Status)
if friendship, err := services.EditFriend(friendship); err != nil { if friendship, err := services.EditFriendWithCheck(friendship, user, originalStatus); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else { } else {
return c.JSON(friendship) return c.JSON(friendship)

View File

@ -80,6 +80,22 @@ func NewFriend(user models.Account, related models.Account, status models.Friend
return relationship, nil return relationship, nil
} }
func EditFriendWithCheck(relationship models.AccountFriendship, user models.Account, originalStatus models.FriendshipStatus) (models.AccountFriendship, error) {
if relationship.Status != originalStatus {
if originalStatus == models.FriendshipBlocked && relationship.BlockedBy != nil && user.ID != *relationship.BlockedBy {
return relationship, fmt.Errorf("the friendship has been blocked by the otherside, you cannot modify it status")
}
if relationship.Status == models.FriendshipPending && relationship.RelatedID != user.ID {
return relationship, fmt.Errorf("only related person can accept friendship")
}
}
if originalStatus != models.FriendshipBlocked && relationship.Status == models.FriendshipBlocked {
relationship.BlockedBy = &user.ID
}
return EditFriend(relationship)
}
func EditFriend(relationship models.AccountFriendship) (models.AccountFriendship, error) { func EditFriend(relationship models.AccountFriendship) (models.AccountFriendship, error) {
if err := database.C.Save(&relationship).Error; err != nil { if err := database.C.Save(&relationship).Error; err != nil {
return relationship, err return relationship, err