From 521e67a1827232cbfc6aa9eee08b11b198568ef3 Mon Sep 17 00:00:00 2001 From: polaris <2673986789@qq.com> Date: Thu, 11 Jul 2024 16:53:20 +0800 Subject: [PATCH 1/2] feat: #148 --- apis/hole/apis.go | 39 ++++++++++++++++++++++++++------------- apis/hole/schemas.go | 6 +++++- docs/docs.go | 4 ++++ docs/swagger.json | 4 ++++ docs/swagger.yaml | 3 +++ 5 files changed, 42 insertions(+), 14 deletions(-) diff --git a/apis/hole/apis.go b/apis/hole/apis.go index e72104f..733eaa5 100644 --- a/apis/hole/apis.go +++ b/apis/hole/apis.go @@ -450,25 +450,38 @@ func ModifyHole(c *fiber.Ctx) error { } // modify hidden - if body.Unhidden != nil && *body.Unhidden && hole.Hidden { - hole.Hidden = false + if body.Hidden != nil && *body.Hidden && !hole.Hidden { + hole.Hidden = true changed = true - // reindex into Elasticsearch + // delete floors from 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) + go BulkDelete(Models2IDSlice(floors)) // log - MyLog("Hole", "Modify", holeID, user.ID, RoleAdmin, "Unhidden: ") + MyLog("Hole", "Modify", holeID, user.ID, RoleAdmin, "Hidden: ") + } 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: ") + } } // modify tags diff --git a/apis/hole/schemas.go b/apis/hole/schemas.go index a3b4d22..8c5d98d 100644 --- a/apis/hole/schemas.go +++ b/apis/hole/schemas.go @@ -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 } @@ -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("非管理员禁止取消隐藏") } @@ -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 } diff --git a/docs/docs.go b/docs/docs.go index 28b1726..1a47775 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -3331,6 +3331,10 @@ const docTemplate = `{ "type": "integer", "minimum": 1 }, + "hidden": { + "description": "Admin only", + "type": "boolean" + }, "lock": { "description": "admin only", "type": "boolean" diff --git a/docs/swagger.json b/docs/swagger.json index 6a41192..ddef40c 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -3324,6 +3324,10 @@ "type": "integer", "minimum": 1 }, + "hidden": { + "description": "Admin only", + "type": "boolean" + }, "lock": { "description": "admin only", "type": "boolean" diff --git a/docs/swagger.yaml b/docs/swagger.yaml index ccf4ac1..437aec8 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -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 From 3c6d3b098164e2988fa4ba24ffc951729b969496 Mon Sep 17 00:00:00 2001 From: polaris <2673986789@qq.com> Date: Fri, 12 Jul 2024 12:38:48 +0800 Subject: [PATCH 2/2] fix: #148 --- apis/hole/apis.go | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/apis/hole/apis.go b/apis/hole/apis.go index 733eaa5..f2677de 100644 --- a/apis/hole/apis.go +++ b/apis/hole/apis.go @@ -450,17 +450,38 @@ func ModifyHole(c *fiber.Ctx) error { } // modify hidden - if body.Hidden != nil && *body.Hidden && !hole.Hidden { - hole.Hidden = true - changed = true + 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)) + // 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: ") + // 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