Allow user block user initially

This commit is contained in:
LittleSheep 2024-09-26 22:09:39 +08:00
parent cb3cc3f540
commit 94c15f58ec
3 changed files with 55 additions and 1 deletions

View File

@ -67,12 +67,14 @@ func MapAPIs(app *fiber.App, baseURL string) {
relations := me.Group("/relations").Name("Relations") relations := me.Group("/relations").Name("Relations")
{ {
relations.Post("/", makeFriendship)
relations.Post("/block", makeBlockship)
relations.Get("/", listRelationship) relations.Get("/", listRelationship)
relations.Get("/:relatedId", getRelationship) relations.Get("/:relatedId", getRelationship)
relations.Put("/:relatedId", editRelationship) relations.Put("/:relatedId", editRelationship)
relations.Delete("/:relatedId", deleteRelationship) relations.Delete("/:relatedId", deleteRelationship)
relations.Post("/", makeFriendship)
relations.Post("/:relatedId", makeFriendship) relations.Post("/:relatedId", makeFriendship)
relations.Post("/:relatedId/accept", acceptFriend) relations.Post("/:relatedId/accept", acceptFriend)
relations.Post("/:relatedId/decline", declineFriend) relations.Post("/:relatedId/decline", declineFriend)

View File

@ -136,6 +136,38 @@ func makeFriendship(c *fiber.Ctx) error {
} }
} }
func makeBlockship(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
relatedName := c.Query("related")
relatedId, _ := c.ParamsInt("relatedId", 0)
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 err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
} else {
return fiber.NewError(fiber.StatusBadRequest, "must one of username or user id")
}
friend, err := services.NewBlockship(user, related)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.JSON(friend)
}
}
func acceptFriend(c *fiber.Ctx) error { func acceptFriend(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil { if err := exts.EnsureAuthenticated(c); err != nil {
return err return err

View File

@ -81,6 +81,26 @@ func DeleteRelationship(relationship models.AccountRelationship) error {
return nil return nil
} }
func NewBlockship(userA models.Account, userB models.Account) (models.AccountRelationship, error) {
var err error
var rel models.AccountRelationship
if rel, err = GetRelationWithTwoNode(userA.ID, userB.ID, true); err == nil {
rel.Status = models.RelationshipBlocked
} else {
rel = models.AccountRelationship{
AccountID: userA.ID,
RelatedID: userB.ID,
Status: models.RelationshipBlocked,
}
}
if err := database.C.Save(&rel).Error; err != nil {
return rel, err
}
return rel, nil
}
func NewFriend(userA models.Account, userB models.Account, skipPending ...bool) (models.AccountRelationship, error) { func NewFriend(userA models.Account, userB models.Account, skipPending ...bool) (models.AccountRelationship, error) {
relA := models.AccountRelationship{ relA := models.AccountRelationship{
AccountID: userA.ID, AccountID: userA.ID,