Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ppolariss committed Jul 12, 2024
2 parents 93c99fc + f0619dd commit a930891
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 19 deletions.
70 changes: 52 additions & 18 deletions apis/hole/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,25 +450,59 @@ func ModifyHole(c *fiber.Ctx) error {
}

// modify hidden
if body.Unhidden != nil && *body.Unhidden && hole.Hidden {
hole.Hidden = false
changed = true

// reindex into Elasticsearch
var floors Floors
_ = DB.Where("hole_id = ?", hole.ID).Find(&floors)
var floorModels []FloorModel
for _, floor := range floors {
floorModels = append(floorModels, FloorModel{
ID: floor.ID,
UpdatedAt: floor.UpdatedAt,
Content: floor.Content,
})
if body.Hidden != nil {
if *body.Hidden && !hole.Hidden {
hole.Hidden = true
changed = true

// delete floors from Elasticsearch
var floors Floors
_ = DB.Where("hole_id = ?", hole.ID).Find(&floors)
go BulkDelete(Models2IDSlice(floors))

// log
MyLog("Hole", "Modify", holeID, user.ID, RoleAdmin, "Hidden: ")
} else if !*body.Hidden && hole.Hidden {
hole.Hidden = false
changed = true

// reindex into Elasticsearch
var floors Floors
_ = DB.Where("hole_id = ?", hole.ID).Find(&floors)
var floorModels []FloorModel
for _, floor := range floors {
floorModels = append(floorModels, FloorModel{
ID: floor.ID,
UpdatedAt: floor.UpdatedAt,
Content: floor.Content,
})
}
go BulkInsert(floorModels)

// log
MyLog("Hole", "Modify", holeID, user.ID, RoleAdmin, "Unhidden: ")
}
} else {
if body.Unhidden != nil && *body.Unhidden && hole.Hidden {
hole.Hidden = false
changed = true

// reindex into Elasticsearch
var floors Floors
_ = DB.Where("hole_id = ?", hole.ID).Find(&floors)
var floorModels []FloorModel
for _, floor := range floors {
floorModels = append(floorModels, FloorModel{
ID: floor.ID,
UpdatedAt: floor.UpdatedAt,
Content: floor.Content,
})
}
go BulkInsert(floorModels)

// log
MyLog("Hole", "Modify", holeID, user.ID, RoleAdmin, "Unhidden: ")
}
go BulkInsert(floorModels)

// log
MyLog("Hole", "Modify", holeID, user.ID, RoleAdmin, "Unhidden: ")
}

// modify tags
Expand Down
6 changes: 5 additions & 1 deletion apis/hole/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type CreateOldResponse struct {
type ModifyModel struct {
TagCreateModelSlice
DivisionID *int `json:"division_id" validate:"omitempty,min=1"` // Admin and owner only
Hidden *bool `json:"hidden"` // Admin only
Unhidden *bool `json:"unhidden"` // admin only
Lock *bool `json:"lock"` // admin only
}
Expand All @@ -76,6 +77,9 @@ func (body ModifyModel) CheckPermission(user *models.User, hole *models.Hole) er
if body.DivisionID != nil && !user.IsAdmin {
return common.Forbidden("非管理员禁止修改分区")
}
if body.Hidden != nil && !user.IsAdmin {
return common.Forbidden("非管理员禁止隐藏帖子")
}
if body.Unhidden != nil && !user.IsAdmin {
return common.BadRequest("非管理员禁止取消隐藏")
}
Expand All @@ -92,5 +96,5 @@ func (body ModifyModel) CheckPermission(user *models.User, hole *models.Hole) er
}

func (body ModifyModel) DoNothing() bool {
return body.Unhidden == nil && body.Tags == nil && body.DivisionID == nil && body.Lock == nil
return body.Hidden == nil && body.Unhidden == nil && body.Tags == nil && body.DivisionID == nil && body.Lock == nil
}
4 changes: 4 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3331,6 +3331,10 @@ const docTemplate = `{
"type": "integer",
"minimum": 1
},
"hidden": {
"description": "Admin only",
"type": "boolean"
},
"lock": {
"description": "admin only",
"type": "boolean"
Expand Down
4 changes: 4 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -3324,6 +3324,10 @@
"type": "integer",
"minimum": 1
},
"hidden": {
"description": "Admin only",
"type": "boolean"
},
"lock": {
"description": "admin only",
"type": "boolean"
Expand Down
3 changes: 3 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ definitions:
description: Admin and owner only
minimum: 1
type: integer
hidden:
description: Admin only
type: boolean
lock:
description: admin only
type: boolean
Expand Down

0 comments on commit a930891

Please sign in to comment.