Skip to content

Commit

Permalink
Merge pull request #137 from OpenTreeHole/fix_load_floor_check_sensitive
Browse files Browse the repository at this point in the history
Fix load floor check sensitive
  • Loading branch information
JingYiJun authored Apr 2, 2024
2 parents f762e3d + ea76dbf commit f53e213
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 60 deletions.
4 changes: 2 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, &hole)
err = floor.Create(DB, &hole, c)
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, &hole)
err = floor.Create(DB, &hole, c)
if err != nil {
return err
}
Expand Down
8 changes: 6 additions & 2 deletions apis/hole/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func CreateHole(c *fiber.Ctx) error {
UserID: user.ID,
DivisionID: divisionID,
}
err = hole.Create(DB, user, body.ToName())
err = hole.Create(DB, user, body.ToName(), c)
if err != nil {
return err
}
Expand Down Expand Up @@ -365,11 +365,15 @@ func CreateHoleOld(c *fiber.Ctx) error {
UserID: user.ID,
DivisionID: body.DivisionID,
}
err = hole.Create(DB, user, body.ToName())
err = hole.Create(DB, user, body.ToName(), c)
if err != nil {
return err
}

err = hole.Preprocess(c)
if err != nil {
return err
}
return c.Status(201).JSON(&CreateOldResponse{
Data: hole,
Message: "发表成功",
Expand Down
2 changes: 1 addition & 1 deletion apis/hole/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (body ModifyModel) CheckPermission(user *models.User, hole *models.Hole) er
if body.Unhidden != nil && !user.IsAdmin {
return common.BadRequest("非管理员禁止取消隐藏")
}
if body.Tags != nil && !(user.IsAdmin || user.ID == hole.UserID) {
if body.Tags != nil && !(user.IsAdmin) {
return common.Forbidden()
}
if body.Tags != nil && len(body.Tags) == 0 {
Expand Down
6 changes: 3 additions & 3 deletions apis/tag/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func CreateTag(c *fiber.Ctx) error {
} else {
c.Status(201)
}
return c.JSON(&tag)
return Serialize(c, &tag)
}

// ModifyTag
Expand Down Expand Up @@ -184,7 +184,7 @@ func ModifyTag(c *fiber.Ctx) error {
return err
}
MyLog("Tag", "Modify", tag.ID, userID, RoleAdmin)
return c.JSON(&tag)
return Serialize(c, &tag)
}

// DeleteTag
Expand Down Expand Up @@ -271,5 +271,5 @@ func DeleteTag(c *fiber.Ctx) error {
return err
}
MyLog("Tag", "Delete", id, userID, RoleAdmin)
return c.JSON(&newTag)
return Serialize(c, &newTag)
}
75 changes: 56 additions & 19 deletions models/floor.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,34 +112,28 @@ func (floor *Floor) Preprocess(c *fiber.Ctx) error {
return Floors{floor}.Preprocess(c)
}

func (floors Floors) Preprocess(c *fiber.Ctx) error {
func (floors Floors) loadFloorLikes(c *fiber.Ctx) (err error) {
userID, err := common.GetUserID(c)
if err != nil {
return err
return
}

// get floors' like
floorIDs := make([]int, len(floors))
IDFloorMapping := make(map[int]*Floor)
for i, floor := range floors {
// set floors content to empty if sensitive
if floor.Sensitive() {
floors[i].Content = ""
}
floors[i].IsMe = userID == floor.UserID
floorIDs[i] = floor.ID
IDFloorMapping[floor.ID] = floors[i]
}

var floorLikes []FloorLike
result := DB.
Clauses(dbresolver.Write).
err = DB.Clauses(dbresolver.Write).
Where("floor_id IN (?)", floorIDs).
Where("user_id = ?", userID).
Find(&floorLikes)
if result.Error != nil {
return err
Find(&floorLikes).Error
if err != nil {
return
}

for _, floorLike := range floorLikes {
if floor, ok := IDFloorMapping[floorLike.FloorID]; ok {
floor.Liked = floorLike.LikeData
Expand All @@ -151,22 +145,61 @@ func (floors Floors) Preprocess(c *fiber.Ctx) error {
}
}
}
return
}

func (floors Floors) Preprocess(c *fiber.Ctx) (err error) {
userID, err := common.GetUserID(c)
if err != nil {
return
}

// get floors' like
err = floors.loadFloorLikes(c)
if err != nil {
return
}

// set floors IsMe
for _, floor := range floors {
floor.IsMe = userID == floor.UserID
}

// set some default values
for _, floor := range floors {
floor.SetDefaults()
err = floor.SetDefaults(c)
if err != nil {
return
}
}
return nil
return
}

func (floor *Floor) SetDefaults() {
func (floor *Floor) SetDefaults(c *fiber.Ctx) (err error) {
floor.FloorID = floor.ID
user, err := GetUser(c)
if err != nil {
return
}

floor.Anonyname = utils.GetFuzzName(floor.Anonyname)
if !user.IsAdmin && floor.Sensitive() {
if floor.UserID == user.ID {
floor.Content = "该内容被猫猫吃掉了"
} else {
floor.Content = ""
floor.Anonyname = ""
}
}

if floor.Mention == nil {
floor.Mention = Floors{}
} else if len(floor.Mention) > 0 {
for _, mentionFloor := range floor.Mention {
mentionFloor.SetDefaults()
err = mentionFloor.SetDefaults(c)
if err != nil {
return
}
}
}

Expand All @@ -193,13 +226,14 @@ func (floor *Floor) SetDefaults() {
// floor.FoldFrontend = []string{alterContent}
// }
//}
return
}

/******************************
Create
*******************************/

func (floor *Floor) Create(tx *gorm.DB, hole *Hole) (err error) {
func (floor *Floor) Create(tx *gorm.DB, hole *Hole, c *fiber.Ctx) (err error) {
// sensitive check
sensitiveCheckResp, err := sensitive.CheckSensitive(sensitive.ParamsForCheck{
Content: floor.Content,
Expand Down Expand Up @@ -247,7 +281,10 @@ func (floor *Floor) Create(tx *gorm.DB, hole *Hole) (err error) {
return err
}

floor.SetDefaults()
err = floor.SetDefaults(c)
if err != nil {
return err
}

// Send Notification
var messages Notifications
Expand Down
91 changes: 61 additions & 30 deletions models/hole.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ func loadFloors(holes Holes) error {
return hole.ID == floors[0].HoleID
})
for _, floor := range floors {
floor.SetDefaults()
if floor.HoleID != holes[index].ID {
holes[index].Floors = floors[left:right]
left = right
Expand Down Expand Up @@ -227,43 +226,54 @@ func (holes Holes) Preprocess(c *fiber.Ctx) error {
}
}

user, err := GetUser(c)
// preprocess floors after load from hole cache
floors := make(Floors, 0)
for _, hole := range holes {
hole.SetHoleFloor()
floors = append(floors, hole.Floors...)
}
err := floors.Preprocess(c)
if err != nil {
return err
}

// only admin can see hole is hidden
if !user.IsAdmin {
for i, hole := range holes {
err = holes[i].Floors.Preprocess(c)
if err != nil {
return err
}
hole.Hidden = false
}
}
//user, err := GetUser(c)
//if err != nil {
// return err
//}
//
//// only admin can see hole is hidden
//if !user.IsAdmin {
// for i, hole := range holes {
// err = holes[i].Floors.Preprocess(c)
// if err != nil {
// return err
// }
// hole.Hidden = false
// }
//}

return nil
}

func UpdateHoleCache(holes Holes) error {
err := loadFloors(holes)
func UpdateHoleCache(holes Holes) (err error) {
err = loadFloors(holes)
if err != nil {
return err
return
}

err = loadTags(holes)
if err != nil {
return err
return
}

for _, hole := range holes {
err = utils.SetCache(hole.CacheName(), hole, HoleCacheExpire)
if err != nil {
return err
return
}
}
return nil
return
}

func MakeQuerySet(c *fiber.Ctx) (*gorm.DB, error) {
Expand Down Expand Up @@ -298,22 +308,40 @@ func (holes Holes) MakeQuerySet(offset common.CustomTime, size int, order string
create and modify hole methods
************************/

// SetHoleFloor godoc
// set hole.HoleFloor from hole.Floors or hole.HoleFloor.Floors
// if Floors is not empty, set HoleFloor.Floors from Floors, in case loading from database
// if Floors is empty, set HoleFloor.Floors from HoleFloor.Floors, in case loading from cache
func (hole *Hole) SetHoleFloor() {
holeFloorSize := len(hole.Floors)
if holeFloorSize == 0 {
return
}
if len(hole.Floors) != 0 {
holeFloorSize := len(hole.Floors)

hole.HoleFloor.FirstFloor = hole.Floors[0]
hole.HoleFloor.LastFloor = hole.Floors[holeFloorSize-1]
if holeFloorSize <= config.Config.HoleFloorSize {
hole.HoleFloor.Floors = hole.Floors
} else {
hole.HoleFloor.Floors = hole.Floors[0 : holeFloorSize-1]
hole.HoleFloor.FirstFloor = hole.Floors[0]
hole.HoleFloor.LastFloor = hole.Floors[holeFloorSize-1]
if holeFloorSize <= config.Config.HoleFloorSize {
hole.HoleFloor.Floors = hole.Floors
} else {
hole.HoleFloor.Floors = hole.Floors[0 : holeFloorSize-1]
}
} else if len(hole.HoleFloor.Floors) != 0 {
holeFloorSize := len(hole.Floors)

hole.HoleFloor.FirstFloor = hole.HoleFloor.Floors[0]
hole.HoleFloor.LastFloor = hole.HoleFloor.Floors[holeFloorSize-1]
hole.Floors = hole.HoleFloor.Floors
}

//for _, floor := range hole.HoleFloor.Floors {
// floor.SetDefaults(c)
//}
//hole.HoleFloor.FirstFloor.SetDefaults(c)
//if hole.HoleFloor.FirstFloor.Content == "" {
// hole.Hidden = true
//}
//hole.HoleFloor.LastFloor.SetDefaults(c)
}

func (hole *Hole) Create(tx *gorm.DB, user *User, tagNames []string) (err error) {
func (hole *Hole) Create(tx *gorm.DB, user *User, tagNames []string, c *fiber.Ctx) (err error) {
// Create hole.Tags, in different sql session
hole.Tags, err = FindOrCreateTags(tx, user, tagNames)
if err != nil {
Expand Down Expand Up @@ -363,7 +391,10 @@ func (hole *Hole) Create(tx *gorm.DB, user *User, tagNames []string) (err error)
hole.SetHoleFloor()

// half preprocess hole.Floor
firstFloor.SetDefaults()
err = firstFloor.SetDefaults(c)
if err != nil {
return err
}

// index
if !firstFloor.Sensitive() {
Expand Down
12 changes: 9 additions & 3 deletions models/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,16 @@ func (report *Report) GetID() int {

type Reports []*Report

func (report *Report) Preprocess(_ *fiber.Ctx) error {
report.Floor.SetDefaults()
func (report *Report) Preprocess(c *fiber.Ctx) (err error) {
err = report.Floor.SetDefaults(c)
if err != nil {
return err
}
for i := range report.Floor.Mention {
report.Floor.Mention[i].SetDefaults()
err = report.Floor.Mention[i].SetDefaults(c)
if err != nil {
return err
}
}
report.HoleID = report.Floor.HoleID
return nil
Expand Down

0 comments on commit f53e213

Please sign in to comment.