feat(comments): 支持编辑与管理可见历史,采纳展示改用楼层号

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-16 08:05:41 +08:00
parent d032668c66
commit 1b2b87c976
14 changed files with 716 additions and 42 deletions

View File

@@ -16,6 +16,8 @@ var (
ErrCommentDeleteMeta = errors.New("请填写删除理由")
ErrCommentInvalidType = errors.New("无效的删除类型")
ErrCommentNotDeleted = errors.New("评论未被删除")
ErrCommentNotChanged = errors.New("评论内容未变更")
ErrCommentDeleted = errors.New("已删除的评论不可编辑")
)
// CommentService 评论服务
@@ -32,6 +34,7 @@ type CommentNode struct {
model.Comment
Deleted bool `json:"deleted,omitempty"` // 软删占位
StaffDeleted bool `json:"staff_deleted,omitempty"` // DeletedBy≠评论者
Edited bool `json:"edited,omitempty"` // 相对创建已编辑
DeletedByUser *CommentActorBrief `json:"deleted_by_user,omitempty"` // 管理删执行者(公开)
Replies []CommentNode `json:"replies,omitempty"`
}
@@ -177,8 +180,9 @@ func (s *CommentService) loadCommentActorsByIDs(ids []uint) map[uint]CommentActo
// toCommentNode 软删占位:清空公开正文;版主保留原文;自删理由仅版主可见。
func toCommentNode(c model.Comment, replies []CommentNode, revealAudit bool, deleters map[uint]CommentActorBrief) CommentNode {
edited := c.UpdatedAt.Sub(c.CreatedAt) > time.Minute
if !c.DeletedAt.Valid {
return CommentNode{Comment: c, Replies: replies}
return CommentNode{Comment: c, Edited: edited, Replies: replies}
}
staff := c.DeletedBy != 0 && c.DeletedBy != c.UserID
if !revealAudit {
@@ -201,11 +205,50 @@ func toCommentNode(c model.Comment, replies []CommentNode, revealAudit bool, del
Comment: c,
Deleted: true,
StaffDeleted: staff,
Edited: edited,
DeletedByUser: deleter,
Replies: replies,
}
}
// FloorNumber 本帖主评楼层号(含软删占位,按 created_at/id 升序);子评取其所属主楼。无则 0。
func FloorNumber(db *gorm.DB, postID, commentID uint) int {
if db == nil || postID == 0 || commentID == 0 {
return 0
}
var c model.Comment
if err := db.Unscoped().Select("id", "post_id", "parent_id", "root_id", "created_at").
Where("id = ? AND post_id = ?", commentID, postID).First(&c).Error; err != nil {
return 0
}
floorID := c.ID
floorCreated := c.CreatedAt
if c.ParentID != nil {
rootID := c.ID
if c.RootID != nil {
rootID = *c.RootID
}
var root model.Comment
if err := db.Unscoped().Select("id", "created_at").
Where("id = ? AND post_id = ? AND parent_id IS NULL", rootID, postID).First(&root).Error; err != nil {
return 0
}
floorID = root.ID
floorCreated = root.CreatedAt
}
var ahead int64
db.Unscoped().Model(&model.Comment{}).
Where("post_id = ? AND parent_id IS NULL AND (created_at < ? OR (created_at = ? AND id < ?))",
postID, floorCreated, floorCreated, floorID).
Count(&ahead)
return int(ahead) + 1
}
// (s *CommentService) FloorNumber 见包级 FloorNumber
func (s *CommentService) FloorNumber(postID, commentID uint) int {
return FloorNumber(s.db, postID, commentID)
}
// applyCommentListVisibility 评论流可见性:published,或 pending 且(作者 / 该板可审);
// 软删行:已发布的对所有人占位;待审软删仅作者/可审者可见占位。
func applyCommentListVisibility(db *gorm.DB, boardID, viewerID uint, actor *Actor) *gorm.DB {
@@ -501,12 +544,141 @@ func (s *CommentService) Purge(actor *Actor, commentID, userID uint) error {
Where("id IN ? AND status = ? AND deleted_at IS NULL", ids, model.ContentStatusPublished).
Count(&livePublished)
if err := s.db.Unscoped().Delete(&model.Comment{}, ids).Error; err != nil {
return err
}
if livePublished > 0 {
s.db.Model(&model.Post{}).Where("id = ?", comment.PostID).
UpdateColumn("comment_count", gorm.Expr("GREATEST(comment_count - ?, 0)", livePublished))
}
return nil
return s.db.Transaction(func(tx *gorm.DB) error {
if err := tx.Where("comment_id IN ?", ids).Delete(&model.CommentEditHistory{}).Error; err != nil {
return err
}
if err := tx.Unscoped().Delete(&model.Comment{}, ids).Error; err != nil {
return err
}
if livePublished > 0 {
return tx.Model(&model.Post{}).Where("id = ?", comment.PostID).
UpdateColumn("comment_count", gorm.Expr("GREATEST(comment_count - ?, 0)", livePublished)).Error
}
return nil
})
}
// Update 编辑评论(作者本人或版主;已软删不可改);先写入旧正文快照再更新。
func (s *CommentService) Update(actor *Actor, commentID, userID uint, content string) (*CommentNode, error) {
content = strings.TrimSpace(content)
if content == "" {
return nil, errors.New("评论内容不能为空")
}
var comment model.Comment
if err := s.db.Unscoped().First(&comment, commentID).Error; err != nil {
return nil, ErrCommentNotFound
}
if comment.DeletedAt.Valid {
return nil, ErrCommentDeleted
}
if comment.UserID != userID {
var post model.Post
if err := s.db.Select("id", "board_id").First(&post, comment.PostID).Error; err != nil {
return nil, ErrCommentNotFound
}
if actor == nil || !actor.CanModerateBoard(post.BoardID) {
return nil, ErrCommentForbidden
}
}
if comment.Content == content {
return nil, ErrCommentNotChanged
}
old := comment.Content
if err := s.db.Transaction(func(tx *gorm.DB) error {
hist := model.CommentEditHistory{
CommentID: comment.ID,
EditorID: userID,
OldContent: old,
}
if err := tx.Create(&hist).Error; err != nil {
return err
}
return tx.Model(&comment).Update("content", content).Error
}); err != nil {
return nil, err
}
if err := s.db.Preload("User").First(&comment, comment.ID).Error; err != nil {
return nil, err
}
node := toCommentNode(comment, nil, false, nil)
return &node, nil
}
// CommentEditHistoryItem 管理可见的评论修订记录
type CommentEditHistoryItem struct {
ID uint `json:"id"`
Editor CommentActorBrief `json:"editor"`
OldContent string `json:"old_content"`
CreatedAt time.Time `json:"created_at"`
}
// ListEditHistory 评论编辑历史(仅版主;含软删评论;按创建时间倒序分页)
func (s *CommentService) ListEditHistory(actor *Actor, commentID uint, page, size int) ([]CommentEditHistoryItem, int64, error) {
if page < 1 {
page = 1
}
if size < 1 || size > 50 {
size = 10
}
var comment model.Comment
if err := s.db.Unscoped().Select("id", "post_id").First(&comment, commentID).Error; err != nil {
return nil, 0, ErrCommentNotFound
}
var post model.Post
if err := s.db.Unscoped().Select("id", "board_id").First(&post, comment.PostID).Error; err != nil {
return nil, 0, ErrCommentNotFound
}
if actor == nil || !actor.CanModerateBoard(post.BoardID) {
return nil, 0, ErrCommentForbidden
}
q := s.db.Model(&model.CommentEditHistory{}).Where("comment_id = ?", commentID)
var total int64
if err := q.Count(&total).Error; err != nil {
return nil, 0, err
}
var rows []model.CommentEditHistory
if err := s.db.Where("comment_id = ?", commentID).
Order("created_at DESC").
Offset((page - 1) * size).Limit(size).
Find(&rows).Error; err != nil {
return nil, 0, err
}
editorIDs := make([]uint, 0, len(rows))
seen := map[uint]struct{}{}
for _, r := range rows {
if _, ok := seen[r.EditorID]; ok {
continue
}
seen[r.EditorID] = struct{}{}
editorIDs = append(editorIDs, r.EditorID)
}
editors := map[uint]CommentActorBrief{}
if len(editorIDs) > 0 {
var users []model.User
s.db.Select("id", "username", "nickname", "avatar").Where("id IN ?", editorIDs).Find(&users)
for _, u := range users {
editors[u.ID] = CommentActorBrief{
ID: u.ID, Username: u.Username, Nickname: u.Nickname, Avatar: u.Avatar,
}
}
}
items := make([]CommentEditHistoryItem, 0, len(rows))
for _, r := range rows {
ed := editors[r.EditorID]
if ed.ID == 0 {
ed = CommentActorBrief{ID: r.EditorID, Username: "已注销", Nickname: "已注销"}
}
items = append(items, CommentEditHistoryItem{
ID: r.ID,
Editor: ed,
OldContent: r.OldContent,
CreatedAt: r.CreatedAt,
})
}
return items, total, nil
}