🐛 Fixed friendship check
This commit is contained in:
		| @@ -72,21 +72,3 @@ type AccountContact struct { | ||||
| 	VerifiedAt *time.Time `json:"verified_at"` | ||||
| 	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
									
								
							
							
						
						
									
										20
									
								
								pkg/models/friendships.go
									
									
									
									
									
										Normal 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"` | ||||
| } | ||||
| @@ -81,15 +81,12 @@ func editFriendship(c *fiber.Ctx) error { | ||||
| 	friendship, err := services.GetFriendWithTwoSides(user.ID, related.ID) | ||||
| 	if err != nil { | ||||
| 		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) | ||||
|  | ||||
| 	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()) | ||||
| 	} else { | ||||
| 		return c.JSON(friendship) | ||||
|   | ||||
| @@ -80,6 +80,22 @@ func NewFriend(user models.Account, related models.Account, status models.Friend | ||||
| 	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) { | ||||
| 	if err := database.C.Save(&relationship).Error; err != nil { | ||||
| 		return relationship, err | ||||
|   | ||||
		Reference in New Issue
	
	Block a user