fix: 待审通知实时回填审核态并重构消息页体验
This commit is contained in:
@@ -3,11 +3,13 @@ package service
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"git.iioio.com/freefire/jiang13-forum/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -24,13 +26,15 @@ func NewMessageService(filter *SensitiveFilter, settings *ForumSettingsService)
|
||||
}
|
||||
|
||||
type MessageSendInput struct {
|
||||
FromUserID uint
|
||||
ToUserID uint
|
||||
Subject string
|
||||
Content string
|
||||
Kind string
|
||||
RelatedPostID *uint
|
||||
RelatedReportID *uint
|
||||
FromUserID uint
|
||||
ToUserID uint
|
||||
Subject string
|
||||
Content string
|
||||
Kind string
|
||||
RelatedPostID *uint
|
||||
RelatedReportID *uint
|
||||
RelatedCommentID *uint
|
||||
RelatedFloor *int
|
||||
}
|
||||
|
||||
// Send 发送私信(用户互发或系统通知)
|
||||
@@ -82,14 +86,16 @@ func (s *MessageService) Send(in MessageSendInput) (*model.PrivateMessage, error
|
||||
}
|
||||
|
||||
msg := &model.PrivateMessage{
|
||||
FromUserID: in.FromUserID,
|
||||
ToUserID: in.ToUserID,
|
||||
Subject: subject,
|
||||
Content: content,
|
||||
Kind: kind,
|
||||
RelatedPostID: in.RelatedPostID,
|
||||
RelatedReportID: in.RelatedReportID,
|
||||
IsRead: false,
|
||||
FromUserID: in.FromUserID,
|
||||
ToUserID: in.ToUserID,
|
||||
Subject: subject,
|
||||
Content: content,
|
||||
Kind: kind,
|
||||
RelatedPostID: in.RelatedPostID,
|
||||
RelatedReportID: in.RelatedReportID,
|
||||
RelatedCommentID: in.RelatedCommentID,
|
||||
RelatedFloor: in.RelatedFloor,
|
||||
IsRead: false,
|
||||
}
|
||||
if err := model.DB.Create(msg).Error; err != nil {
|
||||
return nil, err
|
||||
@@ -98,22 +104,54 @@ func (s *MessageService) Send(in MessageSendInput) (*model.PrivateMessage, error
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
// SystemNotifyRefs 系统通知关联目标(帖子 / 评论 / 举报)
|
||||
type SystemNotifyRefs struct {
|
||||
PostID *uint
|
||||
ReportID *uint
|
||||
CommentID *uint
|
||||
Floor *int
|
||||
}
|
||||
|
||||
// SendSystem 系统私信(管理员/系统 → 用户)
|
||||
func (s *MessageService) SendSystem(toUserID uint, subject, content, kind string, relatedPostID, relatedReportID *uint) (*model.PrivateMessage, error) {
|
||||
return s.SendSystemWithRefs(toUserID, subject, content, kind, SystemNotifyRefs{
|
||||
PostID: relatedPostID,
|
||||
ReportID: relatedReportID,
|
||||
})
|
||||
}
|
||||
|
||||
// SendSystemWithRefs 系统私信(可附带评论楼层深链)
|
||||
func (s *MessageService) SendSystemWithRefs(toUserID uint, subject, content, kind string, refs SystemNotifyRefs) (*model.PrivateMessage, error) {
|
||||
if kind == "" {
|
||||
kind = model.MessageKindSystem
|
||||
}
|
||||
return s.Send(MessageSendInput{
|
||||
FromUserID: 0,
|
||||
ToUserID: toUserID,
|
||||
Subject: subject,
|
||||
Content: content,
|
||||
Kind: kind,
|
||||
RelatedPostID: relatedPostID,
|
||||
RelatedReportID: relatedReportID,
|
||||
FromUserID: 0,
|
||||
ToUserID: toUserID,
|
||||
Subject: subject,
|
||||
Content: content,
|
||||
Kind: kind,
|
||||
RelatedPostID: refs.PostID,
|
||||
RelatedReportID: refs.ReportID,
|
||||
RelatedCommentID: refs.CommentID,
|
||||
RelatedFloor: refs.Floor,
|
||||
})
|
||||
}
|
||||
|
||||
// MarkMessageRead 将单条消息标为已读(仅收件人本人)
|
||||
func (s *MessageService) MarkMessageRead(userID, messageID uint) error {
|
||||
if messageID == 0 {
|
||||
return errors.New("无效的消息")
|
||||
}
|
||||
res := model.DB.Model(&model.PrivateMessage{}).
|
||||
Where("id = ? AND to_user_id = ? AND is_read = ?", messageID, userID, false).
|
||||
Update("is_read", true)
|
||||
if res.Error != nil {
|
||||
return res.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarkAllRead 全部标为已读
|
||||
func (s *MessageService) MarkAllRead(userID uint) error {
|
||||
return model.DB.Model(&model.PrivateMessage{}).
|
||||
@@ -175,9 +213,212 @@ func (s *MessageService) ListNotifications(userID uint, page, size int, kind str
|
||||
if list == nil {
|
||||
list = []model.PrivateMessage{}
|
||||
}
|
||||
s.enrichModerationStatus(list)
|
||||
return list, total, nil
|
||||
}
|
||||
|
||||
// enrichModerationStatus 为待审通知回填目标当前审核状态
|
||||
func (s *MessageService) enrichModerationStatus(list []model.PrivateMessage) {
|
||||
if len(list) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
resolvedByIndex := enrichModerationCommentIDs(list)
|
||||
|
||||
commentIDs := make([]uint, 0, len(list))
|
||||
postIDs := make([]uint, 0, len(list))
|
||||
// 历史评论通知:按帖+楼层回查(兜底)
|
||||
type pfKey struct {
|
||||
PostID uint
|
||||
Floor int
|
||||
}
|
||||
pfNeeded := make([]pfKey, 0, len(list))
|
||||
seenC := map[uint]struct{}{}
|
||||
seenP := map[uint]struct{}{}
|
||||
seenPF := map[pfKey]struct{}{}
|
||||
|
||||
for i := range list {
|
||||
m := &list[i]
|
||||
if m.Kind != model.MessageKindModeration {
|
||||
continue
|
||||
}
|
||||
if m.RelatedCommentID != nil && *m.RelatedCommentID > 0 {
|
||||
id := *m.RelatedCommentID
|
||||
if _, ok := seenC[id]; !ok {
|
||||
seenC[id] = struct{}{}
|
||||
commentIDs = append(commentIDs, id)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if cid, ok := resolvedByIndex[i]; ok && cid > 0 {
|
||||
if _, ok := seenC[cid]; !ok {
|
||||
seenC[cid] = struct{}{}
|
||||
commentIDs = append(commentIDs, cid)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if m.RelatedPostID == nil || *m.RelatedPostID == 0 {
|
||||
continue
|
||||
}
|
||||
pid := *m.RelatedPostID
|
||||
if looksLikeModerationComment(m.Subject, m.Content) {
|
||||
floor := 0
|
||||
if m.RelatedFloor != nil && *m.RelatedFloor > 0 {
|
||||
floor = *m.RelatedFloor
|
||||
} else {
|
||||
floor = parseNotifyFloor(m.Content)
|
||||
}
|
||||
if floor > 0 {
|
||||
k := pfKey{PostID: pid, Floor: floor}
|
||||
if _, ok := seenPF[k]; !ok {
|
||||
seenPF[k] = struct{}{}
|
||||
pfNeeded = append(pfNeeded, k)
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
if _, ok := seenP[pid]; !ok {
|
||||
seenP[pid] = struct{}{}
|
||||
postIDs = append(postIDs, pid)
|
||||
}
|
||||
}
|
||||
|
||||
commentStatus := map[uint]string{}
|
||||
if len(commentIDs) > 0 {
|
||||
type row struct {
|
||||
ID uint
|
||||
Status string
|
||||
DeletedAt gorm.DeletedAt
|
||||
}
|
||||
var rows []row
|
||||
_ = model.DB.Unscoped().Model(&model.Comment{}).
|
||||
Select("id", "status", "deleted_at").
|
||||
Where("id IN ?", commentIDs).
|
||||
Find(&rows)
|
||||
for _, r := range rows {
|
||||
commentStatus[r.ID] = contentStatusOrDeleted(r.Status, r.DeletedAt)
|
||||
}
|
||||
for _, id := range commentIDs {
|
||||
if _, ok := commentStatus[id]; !ok {
|
||||
commentStatus[id] = "deleted"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
statusByPF := map[pfKey]string{}
|
||||
if len(pfNeeded) > 0 {
|
||||
postSet := map[uint]struct{}{}
|
||||
for _, k := range pfNeeded {
|
||||
postSet[k.PostID] = struct{}{}
|
||||
}
|
||||
pids := make([]uint, 0, len(postSet))
|
||||
for id := range postSet {
|
||||
pids = append(pids, id)
|
||||
}
|
||||
type row struct {
|
||||
PostID uint
|
||||
Floor int
|
||||
Status string
|
||||
DeletedAt gorm.DeletedAt
|
||||
}
|
||||
var rows []row
|
||||
_ = model.DB.Unscoped().Model(&model.Comment{}).
|
||||
Select("post_id", "floor", "status", "deleted_at").
|
||||
Where("post_id IN ?", pids).
|
||||
Find(&rows)
|
||||
for _, r := range rows {
|
||||
k := pfKey{PostID: r.PostID, Floor: r.Floor}
|
||||
// 同楼多条时后者覆盖;正常业务一帖一楼唯一
|
||||
statusByPF[k] = contentStatusOrDeleted(r.Status, r.DeletedAt)
|
||||
}
|
||||
for _, k := range pfNeeded {
|
||||
if _, ok := statusByPF[k]; !ok {
|
||||
statusByPF[k] = "deleted"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
postStatus := map[uint]string{}
|
||||
if len(postIDs) > 0 {
|
||||
type row struct {
|
||||
ID uint
|
||||
Status string
|
||||
DeletedAt gorm.DeletedAt
|
||||
}
|
||||
var rows []row
|
||||
_ = model.DB.Unscoped().Model(&model.Post{}).
|
||||
Select("id", "status", "deleted_at").
|
||||
Where("id IN ?", postIDs).
|
||||
Find(&rows)
|
||||
for _, r := range rows {
|
||||
postStatus[r.ID] = contentStatusOrDeleted(r.Status, r.DeletedAt)
|
||||
}
|
||||
for _, id := range postIDs {
|
||||
if _, ok := postStatus[id]; !ok {
|
||||
postStatus[id] = "deleted"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for i := range list {
|
||||
m := &list[i]
|
||||
if m.Kind != model.MessageKindModeration {
|
||||
continue
|
||||
}
|
||||
if m.RelatedCommentID != nil && *m.RelatedCommentID > 0 {
|
||||
m.RelatedStatus = commentStatus[*m.RelatedCommentID]
|
||||
continue
|
||||
}
|
||||
if cid, ok := resolvedByIndex[i]; ok && cid > 0 {
|
||||
m.RelatedStatus = commentStatus[cid]
|
||||
continue
|
||||
}
|
||||
if m.RelatedPostID == nil || *m.RelatedPostID == 0 {
|
||||
continue
|
||||
}
|
||||
pid := *m.RelatedPostID
|
||||
if looksLikeModerationComment(m.Subject, m.Content) {
|
||||
floor := 0
|
||||
if m.RelatedFloor != nil && *m.RelatedFloor > 0 {
|
||||
floor = *m.RelatedFloor
|
||||
} else {
|
||||
floor = parseNotifyFloor(m.Content)
|
||||
}
|
||||
if floor > 0 {
|
||||
m.RelatedStatus = statusByPF[pfKey{PostID: pid, Floor: floor}]
|
||||
}
|
||||
continue
|
||||
}
|
||||
m.RelatedStatus = postStatus[pid]
|
||||
}
|
||||
}
|
||||
|
||||
var notifyFloorRe = regexp.MustCompile(`#(\d+)\s*楼`)
|
||||
|
||||
// parseNotifyFloor 从待审评论文案解析楼号(如「#2 楼评论」「#1 楼下」)
|
||||
func parseNotifyFloor(content string) int {
|
||||
m := notifyFloorRe.FindStringSubmatch(content)
|
||||
if len(m) < 2 {
|
||||
return 0
|
||||
}
|
||||
var n int
|
||||
_, _ = fmt.Sscanf(m[1], "%d", &n)
|
||||
if n < 0 {
|
||||
return 0
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func contentStatusOrDeleted(status string, deletedAt gorm.DeletedAt) string {
|
||||
if deletedAt.Valid {
|
||||
return "deleted"
|
||||
}
|
||||
if status != "" {
|
||||
return status
|
||||
}
|
||||
return model.ContentStatusPublished
|
||||
}
|
||||
|
||||
// MarkNotificationsRead 将系统通知全部标为已读
|
||||
func (s *MessageService) MarkNotificationsRead(userID uint) error {
|
||||
return s.MarkConversationRead(userID, 0)
|
||||
|
||||
166
service/message_moderation.go
Normal file
166
service/message_moderation.go
Normal file
@@ -0,0 +1,166 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.iioio.com/freefire/jiang13-forum/model"
|
||||
)
|
||||
|
||||
// looksLikeModerationComment 判断待审通知是否指向评论(含嵌套回复)
|
||||
func looksLikeModerationComment(subject, content string) bool {
|
||||
if strings.Contains(subject, "评论") || strings.Contains(content, "评论") {
|
||||
return true
|
||||
}
|
||||
return strings.Contains(content, "回复") || strings.Contains(content, "楼下")
|
||||
}
|
||||
|
||||
// isNestedModerationContent 嵌套回复待审(正文为「#N 楼下…」)
|
||||
func isNestedModerationContent(content string) bool {
|
||||
return strings.Contains(content, "楼下")
|
||||
}
|
||||
|
||||
// resolveModerationCommentRef 为历史待审评论通知推断目标评论 ID 与自身楼号
|
||||
func resolveModerationCommentRef(postID uint, content string, notifyAt time.Time) (commentID uint, floor int) {
|
||||
if postID == 0 || model.DB == nil {
|
||||
return 0, 0
|
||||
}
|
||||
displayFloor := parseNotifyFloor(content)
|
||||
if displayFloor <= 0 {
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
if isNestedModerationContent(content) {
|
||||
var parent struct {
|
||||
ID uint
|
||||
}
|
||||
err := model.DB.Unscoped().Model(&model.Comment{}).
|
||||
Select("id").
|
||||
Where("post_id = ? AND floor = ?", postID, displayFloor).
|
||||
First(&parent).Error
|
||||
if err != nil || parent.ID == 0 {
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
type childRow struct {
|
||||
ID uint
|
||||
Floor int
|
||||
CreatedAt time.Time
|
||||
}
|
||||
var children []childRow
|
||||
_ = model.DB.Unscoped().Model(&model.Comment{}).
|
||||
Select("id", "floor", "created_at").
|
||||
Where("post_id = ? AND reply_to = ?", postID, parent.ID).
|
||||
Find(&children).Error
|
||||
if len(children) == 0 {
|
||||
return 0, 0
|
||||
}
|
||||
if len(children) == 1 {
|
||||
return children[0].ID, children[0].Floor
|
||||
}
|
||||
best := children[0]
|
||||
bestDiff := math.MaxFloat64
|
||||
for _, c := range children {
|
||||
diff := math.Abs(float64(c.CreatedAt.Sub(notifyAt)))
|
||||
if diff < bestDiff {
|
||||
bestDiff = diff
|
||||
best = c
|
||||
}
|
||||
}
|
||||
// 通知与评论创建时间相差超过 7 天则放弃,避免误配旧回复
|
||||
if bestDiff > float64(7*24*time.Hour) {
|
||||
return 0, 0
|
||||
}
|
||||
return best.ID, best.Floor
|
||||
}
|
||||
|
||||
var row struct {
|
||||
ID uint
|
||||
Floor int
|
||||
}
|
||||
err := model.DB.Unscoped().Model(&model.Comment{}).
|
||||
Select("id", "floor").
|
||||
Where("post_id = ? AND floor = ?", postID, displayFloor).
|
||||
First(&row).Error
|
||||
if err != nil || row.ID == 0 {
|
||||
return 0, 0
|
||||
}
|
||||
return row.ID, row.Floor
|
||||
}
|
||||
|
||||
// BackfillModerationNotifyRefs 为历史 moderation 通知补写 related_comment_id / related_floor
|
||||
func BackfillModerationNotifyRefs() error {
|
||||
if model.DB == nil {
|
||||
return nil
|
||||
}
|
||||
var rows []model.PrivateMessage
|
||||
err := model.DB.Where("kind = ? AND (related_comment_id IS NULL OR related_comment_id = 0)", model.MessageKindModeration).
|
||||
Find(&rows).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, m := range rows {
|
||||
if m.RelatedPostID == nil || *m.RelatedPostID == 0 {
|
||||
continue
|
||||
}
|
||||
if !looksLikeModerationComment(m.Subject, m.Content) {
|
||||
continue
|
||||
}
|
||||
cid, fl := resolveModerationCommentRef(*m.RelatedPostID, m.Content, m.CreatedAt)
|
||||
if cid == 0 {
|
||||
continue
|
||||
}
|
||||
floor := fl
|
||||
updates := map[string]interface{}{
|
||||
"related_comment_id": cid,
|
||||
"related_floor": floor,
|
||||
}
|
||||
_ = model.DB.Model(&model.PrivateMessage{}).Where("id = ?", m.ID).Updates(updates).Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// enrichModerationCommentIDs 为无 related_comment_id 的评论类待审通知解析评论 ID
|
||||
func enrichModerationCommentIDs(list []model.PrivateMessage) map[int]uint {
|
||||
out := make(map[int]uint)
|
||||
for i := range list {
|
||||
m := &list[i]
|
||||
if m.Kind != model.MessageKindModeration {
|
||||
continue
|
||||
}
|
||||
if m.RelatedCommentID != nil && *m.RelatedCommentID > 0 {
|
||||
continue
|
||||
}
|
||||
if m.RelatedPostID == nil || *m.RelatedPostID == 0 {
|
||||
continue
|
||||
}
|
||||
if !looksLikeModerationComment(m.Subject, m.Content) {
|
||||
continue
|
||||
}
|
||||
// 嵌套回复优先按子评论匹配,避免 displayFloor 查到父评论状态
|
||||
if isNestedModerationContent(m.Content) {
|
||||
cid, _ := resolveModerationCommentRef(*m.RelatedPostID, m.Content, m.CreatedAt)
|
||||
if cid > 0 {
|
||||
out[i] = cid
|
||||
}
|
||||
continue
|
||||
}
|
||||
// 顶层评论:有 related_floor 时按楼号查 ID
|
||||
if m.RelatedFloor != nil && *m.RelatedFloor > 0 {
|
||||
var row struct{ ID uint }
|
||||
if err := model.DB.Unscoped().Model(&model.Comment{}).
|
||||
Select("id").
|
||||
Where("post_id = ? AND floor = ?", *m.RelatedPostID, *m.RelatedFloor).
|
||||
First(&row).Error; err == nil && row.ID > 0 {
|
||||
out[i] = row.ID
|
||||
}
|
||||
continue
|
||||
}
|
||||
cid, _ := resolveModerationCommentRef(*m.RelatedPostID, m.Content, m.CreatedAt)
|
||||
if cid > 0 {
|
||||
out[i] = cid
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
61
service/message_moderation_integration_test.go
Normal file
61
service/message_moderation_integration_test.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.iioio.com/freefire/jiang13-forum/model"
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// TestProductionDBModerationEnrich 用本地生产库拷贝验证 related_status 回填(无库则跳过)
|
||||
func TestProductionDBModerationEnrich(t *testing.T) {
|
||||
dbPath := filepath.Join("..", "dist", "data", "jiang13.db")
|
||||
if _, err := os.Stat(dbPath); err != nil {
|
||||
t.Skip("dist/data/jiang13.db 不存在,跳过")
|
||||
}
|
||||
|
||||
db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
model.DB = db
|
||||
|
||||
if err := BackfillModerationNotifyRefs(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var adminID uint
|
||||
if err := db.Model(&model.User{}).Where("role = ?", model.RoleAdmin).Order("id asc").Limit(1).Pluck("id", &adminID).Error; err != nil || adminID == 0 {
|
||||
t.Skip("无管理员用户,跳过")
|
||||
}
|
||||
|
||||
svc := &MessageService{}
|
||||
list, _, err := svc.ListNotifications(adminID, 1, 100, "moderation")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(list) == 0 {
|
||||
t.Skip("无 moderation 通知")
|
||||
}
|
||||
|
||||
pendingUI := 0
|
||||
published := 0
|
||||
for _, m := range list {
|
||||
if m.RelatedStatus == model.ContentStatusPublished {
|
||||
published++
|
||||
} else if m.RelatedStatus == "" || m.RelatedStatus == model.ContentStatusPending {
|
||||
pendingUI++
|
||||
t.Logf("仍无 published 状态: id=%d subject=%q status=%q content=%q", m.ID, m.Subject, m.RelatedStatus, m.Content)
|
||||
}
|
||||
}
|
||||
t.Logf("moderation=%d published=%d pending_or_empty=%d", len(list), published, pendingUI)
|
||||
if published == 0 {
|
||||
t.Fatal("没有任何 moderation 通知回填为 published")
|
||||
}
|
||||
if pendingUI > 0 {
|
||||
t.Fatalf("%d 条通知仍会被 UI 判为待审", pendingUI)
|
||||
}
|
||||
}
|
||||
102
service/message_moderation_test.go
Normal file
102
service/message_moderation_test.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.iioio.com/freefire/jiang13-forum/model"
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestParseNotifyFloor(t *testing.T) {
|
||||
cases := map[string]int{
|
||||
"用户 X 在《Y》提交了待审核 #2 楼评论": 2,
|
||||
"用户 X 在《Y》#3 楼下提交了待审核回复": 3,
|
||||
"无楼号": 0,
|
||||
}
|
||||
for content, want := range cases {
|
||||
if got := parseNotifyFloor(content); got != want {
|
||||
t.Errorf("parseNotifyFloor(%q) = %d, want %d", content, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnrichModerationStatusPublished(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.User{}, &model.Post{}, &model.Comment{}, &model.PrivateMessage{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
model.DB = db
|
||||
|
||||
post := model.Post{Title: "测试帖", Status: model.ContentStatusPublished, UserID: 1}
|
||||
if err := db.Create(&post).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
comment := model.Comment{
|
||||
PostID: post.ID, UserID: 2, Floor: 2,
|
||||
Status: model.ContentStatusPublished,
|
||||
}
|
||||
if err := db.Create(&comment).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
pid := post.ID
|
||||
msg := model.PrivateMessage{
|
||||
FromUserID: 0,
|
||||
ToUserID: 1,
|
||||
Subject: "新的待审核评论",
|
||||
Content: "用户 A 在《测试帖》提交了待审核 #2 楼评论",
|
||||
Kind: model.MessageKindModeration,
|
||||
RelatedPostID: &pid,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
if err := db.Create(&msg).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
svc := &MessageService{}
|
||||
list := []model.PrivateMessage{msg}
|
||||
svc.enrichModerationStatus(list)
|
||||
if list[0].RelatedStatus != model.ContentStatusPublished {
|
||||
t.Fatalf("RelatedStatus = %q, want published", list[0].RelatedStatus)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveNestedModerationCommentRef(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.Post{}, &model.Comment{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
model.DB = db
|
||||
|
||||
post := model.Post{Title: "嵌套", Status: model.ContentStatusPublished}
|
||||
if err := db.Create(&post).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
parent := model.Comment{PostID: post.ID, Floor: 1, Status: model.ContentStatusPublished}
|
||||
child := model.Comment{
|
||||
PostID: post.ID, Floor: 3, Status: model.ContentStatusPublished,
|
||||
}
|
||||
if err := db.Create(&parent).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rt := parent.ID
|
||||
child.ReplyTo = &rt
|
||||
child.CreatedAt = time.Now()
|
||||
if err := db.Create(&child).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
notifyAt := child.CreatedAt.Add(2 * time.Second)
|
||||
cid, floor := resolveModerationCommentRef(post.ID, "用户 A 在《嵌套》#1 楼下提交了待审核回复", notifyAt)
|
||||
if cid != child.ID || floor != 3 {
|
||||
t.Fatalf("resolve = (%d, %d), want (%d, 3)", cid, floor, child.ID)
|
||||
}
|
||||
}
|
||||
@@ -96,9 +96,15 @@ func (s *NotifyService) NotifyCommentPublished(comment *model.Comment) {
|
||||
subject := "收到新回复"
|
||||
content := FormatReplyContent(authorName, title, displayFloor, isNested)
|
||||
pid := comment.PostID
|
||||
_, _ = s.messages.SendSystem(toUserID, subject, content, model.MessageKindReply, &pid, nil)
|
||||
cid := comment.ID
|
||||
floor := comment.Floor
|
||||
_, _ = s.messages.SendSystemWithRefs(toUserID, subject, content, model.MessageKindReply, SystemNotifyRefs{
|
||||
PostID: &pid,
|
||||
CommentID: &cid,
|
||||
Floor: &floor,
|
||||
})
|
||||
|
||||
s.sendReplyMail(toUserID, authorName, title, comment.PostID, displayFloor, isNested, comment.Content)
|
||||
s.sendReplyMail(toUserID, authorName, title, comment.PostID, comment.Floor, displayFloor, isNested, comment.Content)
|
||||
}
|
||||
|
||||
// NotifyCommentMentions 评论公开后通知被 @提及的用户(跳过已收到回复通知的人)
|
||||
@@ -125,6 +131,8 @@ func (s *NotifyService) NotifyCommentMentions(comment *model.Comment) {
|
||||
}
|
||||
displayFloor := s.resolveDisplayFloor(comment)
|
||||
pid := comment.PostID
|
||||
cid := comment.ID
|
||||
floor := comment.Floor
|
||||
subject := "有人 @了你"
|
||||
content := FormatMentionContent(authorName, title, displayFloor)
|
||||
|
||||
@@ -132,7 +140,11 @@ func (s *NotifyService) NotifyCommentMentions(comment *model.Comment) {
|
||||
if uid == 0 || uid == comment.UserID || uid == replyTo {
|
||||
continue
|
||||
}
|
||||
_, _ = s.messages.SendSystem(uid, subject, content, model.MessageKindMention, &pid, nil)
|
||||
_, _ = s.messages.SendSystemWithRefs(uid, subject, content, model.MessageKindMention, SystemNotifyRefs{
|
||||
PostID: &pid,
|
||||
CommentID: &cid,
|
||||
Floor: &floor,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,8 +161,9 @@ func (s *NotifyService) NotifyPendingPost(post *model.Post) {
|
||||
subject := "新的待审核帖子"
|
||||
content := FormatPendingPostContent(authorName, title, post.ID)
|
||||
pid := post.ID
|
||||
s.notifyAdmins(subject, content, model.MessageKindModeration, &pid, func(siteName, baseURL string) (string, string, string) {
|
||||
return BuildModerationMail(siteName, "帖子", authorName, title, post.ID, 0, false, AbsoluteURL(baseURL, "/admin/posts"))
|
||||
s.notifyAdmins(subject, content, model.MessageKindModeration, SystemNotifyRefs{PostID: &pid}, func(siteName, baseURL string) (string, string, string) {
|
||||
adminPath := fmt.Sprintf("/admin/posts?id=%d", post.ID)
|
||||
return BuildModerationMail(siteName, "帖子", authorName, title, post.ID, 0, false, AbsoluteURL(baseURL, adminPath))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -173,14 +186,21 @@ func (s *NotifyService) NotifyPendingComment(comment *model.Comment) {
|
||||
isNested := comment.ReplyTo != nil && *comment.ReplyTo > 0
|
||||
content := FormatPendingCommentContent(authorName, title, displayFloor, isNested)
|
||||
pid := comment.PostID
|
||||
s.notifyAdmins(subject, content, model.MessageKindModeration, &pid, func(siteName, baseURL string) (string, string, string) {
|
||||
return BuildModerationMail(siteName, "评论", authorName, title, comment.PostID, displayFloor, isNested, AbsoluteURL(baseURL, "/admin/comments"))
|
||||
cid := comment.ID
|
||||
floor := comment.Floor
|
||||
adminPath := fmt.Sprintf("/admin/comments?id=%d", comment.ID)
|
||||
s.notifyAdmins(subject, content, model.MessageKindModeration, SystemNotifyRefs{
|
||||
PostID: &pid,
|
||||
CommentID: &cid,
|
||||
Floor: &floor,
|
||||
}, func(siteName, baseURL string) (string, string, string) {
|
||||
return BuildModerationMail(siteName, "评论", authorName, title, comment.PostID, displayFloor, isNested, AbsoluteURL(baseURL, adminPath))
|
||||
})
|
||||
}
|
||||
|
||||
func (s *NotifyService) notifyAdmins(
|
||||
subject, content, kind string,
|
||||
relatedPostID *uint,
|
||||
refs SystemNotifyRefs,
|
||||
buildMail func(siteName, baseURL string) (subj, text, html string),
|
||||
) {
|
||||
admins, err := s.listAdmins()
|
||||
@@ -197,7 +217,7 @@ func (s *NotifyService) notifyAdmins(
|
||||
}
|
||||
|
||||
for _, admin := range admins {
|
||||
_, _ = s.messages.SendSystem(admin.ID, subject, content, kind, relatedPostID, nil)
|
||||
_, _ = s.messages.SendSystemWithRefs(admin.ID, subject, content, kind, refs)
|
||||
email := strings.TrimSpace(admin.Email)
|
||||
if email == "" || mailSubj == "" {
|
||||
continue
|
||||
@@ -211,7 +231,7 @@ func (s *NotifyService) notifyAdmins(
|
||||
}
|
||||
}
|
||||
|
||||
func (s *NotifyService) sendReplyMail(toUserID uint, authorName, postTitle string, postID uint, displayFloor int, isNested bool, rawContent string) {
|
||||
func (s *NotifyService) sendReplyMail(toUserID uint, authorName, postTitle string, postID uint, ownFloor, displayFloor int, isNested bool, rawContent string) {
|
||||
if s.mail == nil || !s.settings.MailReady() {
|
||||
return
|
||||
}
|
||||
@@ -227,6 +247,10 @@ func (s *NotifyService) sendReplyMail(toUserID uint, authorName, postTitle strin
|
||||
baseURL := s.settings.SitePublicBaseURL("")
|
||||
postPath := s.settings.Permalink().PostPath(postID)
|
||||
link := AbsoluteURL(baseURL, postPath)
|
||||
// 直达评论自身楼层(嵌套回复也有独立 floor)
|
||||
if ownFloor > 0 {
|
||||
link = fmt.Sprintf("%s#floor-%d", link, ownFloor)
|
||||
}
|
||||
excerpt := truncateNotifyExcerpt(rawContent, 120)
|
||||
subj, text, html := BuildReplyMail(siteName, authorName, postTitle, displayFloor, isNested, excerpt, link)
|
||||
_ = s.mail.SendHTML(email, subj, text, html)
|
||||
|
||||
@@ -305,14 +305,20 @@ func (s *ReportService) Handle(in HandleReportInput) (*model.PostReport, error)
|
||||
if commentAuthorID > 0 {
|
||||
pid := postID
|
||||
rid := rep.ID
|
||||
cid := *rep.CommentID
|
||||
floor := commentFloor
|
||||
body := fmt.Sprintf("你在帖子《%s》下的评论(#%d)未通过审核。\n\n原因:\n%s", postTitle, commentFloor, reason)
|
||||
_, _ = s.messages.SendSystem(
|
||||
_, _ = s.messages.SendSystemWithRefs(
|
||||
commentAuthorID,
|
||||
fmt.Sprintf("评论未通过审核 · 《%s》", postTitle),
|
||||
body,
|
||||
model.MessageKindReject,
|
||||
&pid,
|
||||
&rid,
|
||||
SystemNotifyRefs{
|
||||
PostID: &pid,
|
||||
ReportID: &rid,
|
||||
CommentID: &cid,
|
||||
Floor: &floor,
|
||||
},
|
||||
)
|
||||
}
|
||||
default:
|
||||
@@ -345,13 +351,19 @@ func (s *ReportService) Handle(in HandleReportInput) (*model.PostReport, error)
|
||||
}
|
||||
pid := postID
|
||||
rid := rep.ID
|
||||
_, _ = s.messages.SendSystem(
|
||||
resultRefs := SystemNotifyRefs{PostID: &pid, ReportID: &rid}
|
||||
if isCommentReport && rep.CommentID != nil {
|
||||
cid := *rep.CommentID
|
||||
floor := commentFloor
|
||||
resultRefs.CommentID = &cid
|
||||
resultRefs.Floor = &floor
|
||||
}
|
||||
_, _ = s.messages.SendSystemWithRefs(
|
||||
rep.ReporterID,
|
||||
"举报处理结果通知",
|
||||
content,
|
||||
model.MessageKindReportResult,
|
||||
&pid,
|
||||
&rid,
|
||||
resultRefs,
|
||||
)
|
||||
|
||||
_ = model.DB.Preload("Post", func(tx *gorm.DB) *gorm.DB {
|
||||
|
||||
Reference in New Issue
Block a user