Files
backend/internal/model/comment.go
lafay 05c7f8a5eb
All checks were successful
Build Backend / build (push) Successful in 19m52s
Build Backend / build-docker (push) Successful in 1m29s
feat(api): add message segments support for posts and comments
Add rich message segment support enabling structured content (text, images, votes, mentions) in posts and comments. Includes segments field in models, DTOs, handlers, and services. Also adds @mention notification processing and migrates vote data to embedded segments.
2026-04-23 22:29:34 +08:00

85 lines
3.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
// CommentStatus 评论状态
type CommentStatus string
const (
CommentStatusDraft CommentStatus = "draft"
CommentStatusPending CommentStatus = "pending"
CommentStatusPublished CommentStatus = "published"
CommentStatusRejected CommentStatus = "rejected"
CommentStatusDeleted CommentStatus = "deleted"
)
// Comment 评论实体
type Comment struct {
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
PostID string `json:"post_id" gorm:"type:varchar(36);not null;index:idx_comments_post_parent_status_created,priority:1"`
UserID string `json:"user_id" gorm:"type:varchar(36);index;not null"`
ParentID *string `json:"parent_id" gorm:"type:varchar(36);index:idx_comments_post_parent_status_created,priority:2"` // 父评论 ID支持嵌套
RootID *string `json:"root_id" gorm:"type:varchar(36);index:idx_comments_root_status_created,priority:1"` // 根评论 ID用于高效查询
Content string `json:"content" gorm:"type:text;not null"`
Segments MessageSegments `json:"segments,omitempty" gorm:"type:text"`
Images string `json:"images" gorm:"type:text"` // 图片URL列表JSON数组格式
// 关联
User *User `json:"-" gorm:"foreignKey:UserID"`
Replies []*Comment `json:"-" gorm:"-"` // 子回复(手动加载,非 GORM 关联)
// 审核状态
Status CommentStatus `json:"status" gorm:"type:varchar(20);default:published;index:idx_comments_post_parent_status_created,priority:3;index:idx_comments_root_status_created,priority:2"`
ReviewedAt *time.Time `json:"reviewed_at" gorm:"type:timestamp"`
ReviewedBy string `json:"reviewed_by" gorm:"type:varchar(50)"`
RejectReason string `json:"reject_reason" gorm:"type:varchar(500)"`
// 统计
LikesCount int `json:"likes_count" gorm:"default:0"`
RepliesCount int `json:"replies_count" gorm:"default:0"`
// 软删除
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
// 时间戳
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;index:idx_comments_post_parent_status_created,priority:4,sort:asc;index:idx_comments_root_status_created,priority:3,sort:asc"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
}
// BeforeCreate 创建前生成UUID
func (c *Comment) BeforeCreate(tx *gorm.DB) error {
if c.ID == "" {
c.ID = uuid.New().String()
}
return nil
}
func (Comment) TableName() string {
return "comments"
}
// CommentLike 评论点赞
type CommentLike struct {
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
CommentID string `json:"comment_id" gorm:"type:varchar(36);not null;uniqueIndex:idx_comment_like_user,priority:1"`
UserID string `json:"user_id" gorm:"type:varchar(36);not null;uniqueIndex:idx_comment_like_user,priority:2"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
}
// BeforeCreate 创建前生成UUID
func (cl *CommentLike) BeforeCreate(tx *gorm.DB) error {
if cl.ID == "" {
cl.ID = uuid.New().String()
}
return nil
}
func (CommentLike) TableName() string {
return "comment_likes"
}