Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions apis/floor/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"slices"
"time"
"treehole_next/apis/message"
"treehole_next/utils/sensitive"

"github.com/opentreehole/go-common"
Expand Down Expand Up @@ -624,6 +625,12 @@ func DeleteFloor(c *fiber.Ctx) error {

floor.Deleted = true
floor.Content = generateDeleteReason(body.Reason, user.ID == floor.UserID)
// TODO: 内容被删除后,向notification发请求,删除对应的通知
err = message.DeleteMessageByRelatedFloorID(floor.ID)
if err != nil {
return err
}

return tx.Save(&floor).Error
})
if err != nil {
Expand Down Expand Up @@ -849,15 +856,15 @@ func GetPunishmentHistory(c *fiber.Ctx) error {
// search DB for user punishment history
punishments := make([]string, 0, 10)
err = DB.Raw(
`SELECT f.content
`SELECT f.content
FROM floor f
WHERE f.id IN (
SELECT distinct floor.id
FROM floor
JOIN floor_history ON floor.id = floor_history.floor_id
WHERE floor.user_id <> floor_history.user_id
AND floor.user_id = ?
AND floor.deleted = true
SELECT distinct floor.id
FROM floor
JOIN floor_history ON floor.id = floor_history.floor_id
WHERE floor.user_id <> floor_history.user_id
AND floor.user_id = ?
AND floor.deleted = true
)`, userID).Scan(&punishments).Error
if err != nil {
return err
Expand Down Expand Up @@ -1030,15 +1037,15 @@ func ModifyFloorSensitive(c *fiber.Ctx) (err error) {
if err != nil {
return err
}

// update CreatedAt and UpdatedAt to prevent new posts from being buried due to review delays
if floor.Ranking == 0 {
var hole Hole
err = tx.First(&hole, floor.HoleID).Error
if err != nil {
return err
}

// ad-hoc logic: if the hole was created before 2024, don't update
// ref: https://github.com/OpenTreeHole/treehole_next/issues/192
if hole.CreatedAt.Year() <= 2024 {
Expand All @@ -1052,7 +1059,7 @@ func ModifyFloorSensitive(c *fiber.Ctx) (err error) {
return err
}
}

return nil
}

Expand All @@ -1061,6 +1068,10 @@ func ModifyFloorSensitive(c *fiber.Ctx) (err error) {
if err != nil {
return err
}
err = message.DeleteMessageByRelatedFloorID(floor.ID)
if err != nil {
return err
}

floor.Deleted = true
floor.Content = generateDeleteReason(reason, false)
Expand Down
9 changes: 9 additions & 0 deletions apis/hole/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"slices"
"strconv"
"time"
"treehole_next/apis/message"
"treehole_next/utils/sensitive"

"github.com/gofiber/fiber/v2"
Expand Down Expand Up @@ -718,6 +719,10 @@ func HideHole(c *fiber.Ctx) error {
var floors Floors
_ = DB.Where("hole_id = ?", hole.ID).Find(&floors)
go BulkDelete(Models2IDSlice(floors))
err = message.DeleteMessageByRelatedHoleID(hole.ID)
if err != nil {
return err
}

return c.Status(204).JSON(nil)
}
Expand Down Expand Up @@ -808,6 +813,10 @@ func DeleteHole(c *fiber.Ctx) error {
if err != nil {
log.Err(err).Msg("DeleteHole: delete cache divisions")
}
err = message.DeleteMessageByRelatedHoleID(hole.ID)
if err != nil {
return err
}

return c.Status(204).JSON(nil)
}
18 changes: 18 additions & 0 deletions apis/message/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,21 @@ func DeleteMessage(c *fiber.Ctx) error {
}
return c.Status(204).JSON(nil)
}

func DeleteMessageByRelatedFloorID(floorID int) error {
result := DB.Exec(`
DELETE FROM message_user
WHERE related_floor_id = ?`,
floorID,
)
return result.Error
}

func DeleteMessageByRelatedHoleID(holeID int) error {
result := DB.Exec(`
DELETE FROM message_user
WHERE related_hole_id = ?`,
holeID,
)
return result.Error
}
14 changes: 8 additions & 6 deletions apis/penalty/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ func BanUser(c *fiber.Ctx) error {
days,
body.Reason,
),
Title: "处罚通知",
Type: MessageTypePermission,
URL: fmt.Sprintf("/api/floors/%d", floor.ID),
Title: "处罚通知",
Type: MessageTypePermission,
URL: fmt.Sprintf("/api/floors/%d", floor.ID),
RelatedFloorID: &floor.ID,
}

// send
Expand Down Expand Up @@ -247,9 +248,10 @@ func BanUserForever(c *fiber.Ctx) error {
days,
body.Reason,
),
Title: "处罚通知",
Type: MessageTypePermission,
URL: fmt.Sprintf("/api/floors/%d", floor.ID),
Title: "处罚通知",
Type: MessageTypePermission,
URL: fmt.Sprintf("/api/floors/%d", floor.ID),
RelatedFloorID: &floor.ID,
}

// send
Expand Down
8 changes: 5 additions & 3 deletions apis/report/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,11 @@ func BanReporter(c *fiber.Ctx) error {
days,
body.Reason,
),
Title: "处罚通知",
Type: MessageTypePermission,
URL: fmt.Sprintf("/api/reports/%d", report.ID),
Title: "处罚通知",
Type: MessageTypePermission,
URL: fmt.Sprintf("/api/reports/%d", report.ID),
RelatedFloorID: &report.FloorID,
RelatedHoleID: &report.HoleID,
}

// send
Expand Down
65 changes: 35 additions & 30 deletions models/floor.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,12 +513,13 @@ func (floor *Floor) SendSubscription(tx *gorm.DB) Notification {

// Construct Notification
message := Notification{
Data: floor,
Recipients: userIDs,
Description: floor.Content,
Title: "您关注的帖子有新回复",
Type: MessageTypeFavorite,
URL: fmt.Sprintf("/api/floors/%d", floor.ID),
Data: floor,
Recipients: userIDs,
Description: floor.Content,
Title: "您关注的帖子有新回复",
Type: MessageTypeFavorite,
URL: fmt.Sprintf("/api/floors/%d", floor.ID),
RelatedFloorID: &floor.ID,
}

return message
Expand All @@ -540,12 +541,13 @@ func (floor *Floor) SendReply(tx *gorm.DB) Notification {

// construct message
message := Notification{
Data: floor,
Recipients: userIDs,
Description: floor.Content,
Title: "您的内容有新回复",
Type: MessageTypeReply,
URL: fmt.Sprintf("/api/floors/%d", floor.ID),
Data: floor,
Recipients: userIDs,
Description: floor.Content,
Title: "您的内容有新回复",
Type: MessageTypeReply,
URL: fmt.Sprintf("/api/floors/%d", floor.ID),
RelatedFloorID: &floor.ID,
}

return message
Expand All @@ -565,12 +567,13 @@ func (floor *Floor) SendMention(_ *gorm.DB) Notification {

// construct message
message := Notification{
Data: floor,
Recipients: userIDs,
Description: floor.Content,
Title: "您的内容被引用了",
Type: MessageTypeMention,
URL: fmt.Sprintf("/api/floors/%d", floor.ID),
Data: floor,
Recipients: userIDs,
Description: floor.Content,
Title: "您的内容被引用了",
Type: MessageTypeMention,
URL: fmt.Sprintf("/api/floors/%d", floor.ID),
RelatedFloorID: &floor.ID,
}

return message
Expand All @@ -582,12 +585,13 @@ func (floor *Floor) SendModify(_ *gorm.DB) error {

// construct message
message := Notification{
Data: floor,
Recipients: userIDs,
Description: floor.Content,
Title: "您的内容被管理员修改了",
Type: MessageTypeModify,
URL: fmt.Sprintf("/api/floors/%d", floor.ID),
Data: floor,
Recipients: userIDs,
Description: floor.Content,
Title: "您的内容被管理员修改了",
Type: MessageTypeModify,
URL: fmt.Sprintf("/api/floors/%d", floor.ID),
RelatedFloorID: &floor.ID,
}

// send
Expand All @@ -607,12 +611,13 @@ func (floor *Floor) SendSensitive(_ *gorm.DB) error {

// construct message
message := Notification{
Data: floor,
Recipients: userIDs,
Description: "Sensitive Review Required",
Title: "您有待审核的内容",
Type: MessageTypeSensitive,
URL: fmt.Sprintf("/api/floors/%d", floor.ID),
Data: floor,
Recipients: userIDs,
Description: "Sensitive Review Required",
Title: "您有待审核的内容",
Type: MessageTypeSensitive,
URL: fmt.Sprintf("/api/floors/%d", floor.ID),
RelatedHoleID: &floor.ID,
}

// send
Expand Down
22 changes: 12 additions & 10 deletions models/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ type Notifications []Notification

type Notification struct {
// Should be same as CrateModel in notification project
Title string `json:"message"`
Description string `json:"description"`
Data any `json:"data"`
Type MessageType `json:"code"`
URL string `json:"url"`
Recipients []int `json:"recipients"`
Title string `json:"message"`
Description string `json:"description"`
Data any `json:"data"`
Type MessageType `json:"code"`
URL string `json:"url"`
Recipients []int `json:"recipients"`
RelatedFloorID *int `json:"related_floor_id,omitempty"`
RelatedHoleID *int `json:"related_hole_id,omitempty"`
}

func readRespNotification(body io.ReadCloser) Notification {
Expand Down Expand Up @@ -155,7 +157,7 @@ func (message Notification) Send() (Message, error) {
if config.Config.NotificationUrl == "" {
return Message{}, nil
}
message.Title = utils.StripContent(message.Title, 32) //varchar(32)
message.Title = utils.StripContent(message.Title, 32) //varchar(32)
message.Description = utils.StripContent(cleanNotificationDescription(message.Description), 64) //varchar(64)
body.Title = message.Title
body.Description = message.Description
Expand Down Expand Up @@ -266,7 +268,7 @@ func UpdateAdminList(ctx context.Context) {
}

var (
reMention = regexp.MustCompile(`#{1,2}\d+`)
reMention = regexp.MustCompile(`#{1,2}\d+`)
reFormula = regexp.MustCompile(`(?s)\${1,2}.*?\${1,2}`)
reSticker = regexp.MustCompile(`!\[\]\(dx_\S+?\)`)
reImage = regexp.MustCompile(`!\[.*?\]\(.*?\)`)
Expand All @@ -275,8 +277,8 @@ var (
func cleanNotificationDescription(content string) string {
newContent := reMention.ReplaceAllString(content, "")
newContent = reFormula.ReplaceAllString(newContent, "[公式]")
newContent = reSticker.ReplaceAllString(newContent, "[表情]")
newContent = reImage.ReplaceAllString(newContent, "[图片]")
newContent = reSticker.ReplaceAllString(newContent, "[表情]")
newContent = reImage.ReplaceAllString(newContent, "[图片]")
newContent = strings.ReplaceAll(newContent, "\n", "")
if newContent == "" {
return content
Expand Down