Skip to content

Commit

Permalink
feat: add sensitive check
Browse files Browse the repository at this point in the history
  • Loading branch information
JingYiJun committed Nov 10, 2023
1 parent 8337d32 commit a2c3e8c
Show file tree
Hide file tree
Showing 13 changed files with 682 additions and 46 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ git clone https://github.com/OpenTreeHole/treehole_next.git
cd treehole_next
# install swag and generate docs
go install github.com/swaggo/swag/cmd/swag@latest
swag init --parseInternal --parseDepth 1 # to generate the latest docs, this should be run before compiling
swag init --parseDependency --parseDepth 1 # to generate the latest docs, this should be run before compiling
# build for debug
go build -o treehole.exe
# build for release
Expand Down
128 changes: 126 additions & 2 deletions apis/floor/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func CreateFloor(c *fiber.Ctx) error {
SpecialTag: body.SpecialTag,
IsMe: true,
}
err = floor.Create(DB)
err = floor.Create(DB, &hole)
if err != nil {
return err
}
Expand Down Expand Up @@ -252,7 +252,7 @@ func CreateFloorOld(c *fiber.Ctx) error {
SpecialTag: body.SpecialTag,
IsMe: true,
}
err = floor.Create(DB)
err = floor.Create(DB, &hole)
if err != nil {
return err
}
Expand Down Expand Up @@ -342,6 +342,12 @@ func ModifyFloor(c *fiber.Ctx) error {
}
floor.Content = *body.Content

// sensitive check
err = floor.SensitiveCheck(tx, &hole)
if err != nil {
return err
}

// update floor.mention after update floor.content
err = tx.Where("floor_id = ?", floorID).Delete(&FloorMention{}).Error
if err != nil {
Expand Down Expand Up @@ -754,3 +760,121 @@ WHERE f.id IN (
}
return c.JSON(punishments)
}

// ListSensitiveFloors
//
// @Summary List sensitive floors, admin only
// @Tags Floor
// @Produce application/json
// @Router /floors/_sensitive [get]
// @Param object query SensitiveFloorRequest false "query"
// @Success 200 {array} SensitiveFloorResponse
// @Failure 404 {object} MessageModel
func ListSensitiveFloors(c *fiber.Ctx) (err error) {
// validate query
var query SensitiveFloorRequest
err = common.ValidateQuery(c, &query)
if err != nil {
return err
}

// get user
user, err := GetUser(c)
if err != nil {
return err
}

// permission, admin only
if !user.IsAdmin {
return common.Forbidden()
}

// get floors
var floors Floors
querySet := DB
if query.All == true {
querySet = querySet.Where("is_sensitive = true")
} else {
if query.Open == true {
querySet = querySet.Where("is_sensitive = true and is_actual_sensitive IS NULL")
} else {
querySet = querySet.Where("is_sensitive = true and is_actual_sensitive IS NOT NULL")
}
}

result := querySet.
Order("updated_at desc").
Find(&floors)
if result.Error != nil {
return result.Error
}

var responses = make([]SensitiveFloorResponse, len(floors))
for i := range responses {
responses[i].FromModel(floors[i])
}

return c.JSON(responses)
}

// ModifyFloorSensitive
//
// @Summary Modify A Floor's actual_sensitive, admin only
// @Tags Floor
// @Produce application/json
// @Router /floors/{id}/_sensitive [put]
// @Param id path int true "id"
// @Param json body ModifySensitiveFloorRequest true "json"
// @Success 200 {object} Floor
// @Failure 404 {object} MessageModel
func ModifyFloorSensitive(c *fiber.Ctx) (err error) {
// validate body
var body ModifySensitiveFloorRequest
err = common.ValidateBody(c, &body)
if err != nil {
return err
}

// parse floor_id
floorID, err := c.ParamsInt("id")
if err != nil {
return err
}

// get user
user, err := GetUser(c)
if err != nil {
return err
}

// permission check
if !user.IsAdmin {
return common.Forbidden()
}

var floor Floor
err = DB.Clauses(dbresolver.Write).Transaction(func(tx *gorm.DB) error {
err = tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&floor, floorID).Error
if err != nil {
return err
}

// modify actual_sensitive
floor.IsActualSensitive = &body.IsActualSensitive
MyLog("Floor", "Modify", floorID, user.ID, RoleAdmin, "actual_sensitive to: ", fmt.Sprintf("%v", body.IsActualSensitive))

// save actual_sensitive only
return tx.Model(&floor).Select("IsActualSensitive").Updates(&floor).Error
})
if err != nil {
return err
}

// clear cache
err = DeleteCache(fmt.Sprintf("hole_%v", floor.HoleID))
if err != nil {
return err
}

return Serialize(c, &floor)
}
21 changes: 12 additions & 9 deletions apis/floor/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@ func RegisterRoutes(app fiber.Router) {
app.Post("/floors/search", SearchFloors)
app.Get("/floors/search", SearchFloors)

app.Get("/holes/:id/floors", ListFloorsInAHole)
app.Get("/holes/:id<int>/floors", ListFloorsInAHole)
app.Get("/floors", ListFloorsOld)
app.Get("/floors/:id", GetFloor)
app.Post("/holes/:id/floors", utils.MiddlewareHasAnsweredQuestions, CreateFloor)
app.Get("/floors/:id<int>", GetFloor)
app.Post("/holes/:id<int>/floors", utils.MiddlewareHasAnsweredQuestions, CreateFloor)
app.Post("/floors", utils.MiddlewareHasAnsweredQuestions, CreateFloorOld)
app.Put("/floors/:id", ModifyFloor)
app.Post("/floors/:id/like/:like", ModifyFloorLike)
app.Delete("/floors/:id", DeleteFloor)
app.Put("/floors/:id<int>", ModifyFloor)
app.Post("/floors/:id<int>/like/:like<int>", ModifyFloorLike)
app.Delete("/floors/:id<int>", DeleteFloor)

app.Get("/users/me/floors", ListReplyFloors)

app.Get("/floors/:id/history", GetFloorHistory)
app.Post("/floors/:id/restore/:floor_history_id", RestoreFloor)
app.Get("/floors/:id<int>/history", GetFloorHistory)
app.Post("/floors/:id<int>/restore/:floor_history_id<int>", RestoreFloor)

app.Post("/config/search", SearchConfig)
app.Get("/floors/:id/punishment", GetPunishmentHistory)
app.Get("/floors/:id<int>/punishment", GetPunishmentHistory)

app.Get("/floors/_sensitive", ListSensitiveFloors)
app.Put("/floors/:id<int>/_sensitive", ModifyFloorSensitive)
}
33 changes: 33 additions & 0 deletions apis/floor/schemas.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package floor

import (
"time"

"github.com/opentreehole/go-common"

"treehole_next/models"
Expand Down Expand Up @@ -95,3 +97,34 @@ type RestoreModel struct {
type SearchConfigModel struct {
Open bool `json:"open"`
}

type SensitiveFloorRequest struct {
Size int `json:"size" query:"size" default:"10" validate:"max=10"`
Offset common.CustomTime `json:"offset" query:"offset" swaggertype:"string"`
Open bool `json:"open" query:"open"`
All bool `json:"all" query:"all"`
}

type SensitiveFloorResponse struct {
ID int `json:"id"`
CreatedAt time.Time `json:"time_created"`
UpdatedAt time.Time `json:"time_updated"`
Content string `json:"content"`
Modified int `json:"modified"`
IsActualSensitive *bool `json:"is_actual_sensitive"`
HoleID int `json:"hole_id"`
}

func (s *SensitiveFloorResponse) FromModel(floor *models.Floor) *SensitiveFloorResponse {
s.ID = floor.ID
s.CreatedAt = floor.CreatedAt
s.UpdatedAt = floor.UpdatedAt
s.Content = floor.Content
s.Modified = floor.Modified
s.HoleID = floor.HoleID
return s
}

type ModifySensitiveFloorRequest struct {
IsActualSensitive bool `json:"is_actual_sensitive"`
}
28 changes: 23 additions & 5 deletions data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,34 @@ var MetaFile []byte

var NamesMapping map[string]string

var SensitiveWords []string

func init() {
err := initNamesMapping()
if err != nil {
log.Err(err).Msg("could not init names mapping")
}

err = initSensitiveWords()
if err != nil {
log.Err(err).Msg("could not init sensitive words")
}
}

func initNamesMapping() error {
NamesMappingData, err := os.ReadFile(`data/names_mapping.json`)
if err != nil {
log.Err(err).Msg("could not load names_mapping.json")
return
return err
}

err = json.Unmarshal(NamesMappingData, &NamesMapping)
return json.Unmarshal(NamesMappingData, &NamesMapping)
}

func initSensitiveWords() error {
sensitiveWordsData, err := os.ReadFile(`data/sensitive_words.json`)
if err != nil {
log.Err(err).Msg("could not unmarshal names_mapping.json")
return
return err
}

return json.Unmarshal(sensitiveWordsData, &SensitiveWords)
}
Loading

0 comments on commit a2c3e8c

Please sign in to comment.