diff --git a/internal/model/comment.go b/internal/model/comment.go index ac292cd..6beeebb 100644 --- a/internal/model/comment.go +++ b/internal/model/comment.go @@ -8,7 +8,7 @@ type Comment struct { PostID uint `gorm:"index;not null" json:"post_id"` RootID uint `gorm:"index;not null" json:"root_id"` // 根评论ID,顶级评论指向自身 ParentID *uint `gorm:"index" json:"parent_id"` // 父评论ID,NULL=顶级评论 - ReplyToUID string `gorm:"type:varchar(36);default:''" json:"reply_to_uid"` // 被回复者UID + ReplyToUID uint `gorm:"default:0" json:"reply_to_uid"` // 被回复者UID Body string `gorm:"type:text;not null" json:"body"` // 评论内容(纯文本 + 图片URL) IsDeleted bool `gorm:"default:false;index" json:"is_deleted"` LikesCount int `gorm:"default:0" json:"likes_count"` @@ -31,12 +31,12 @@ type Comment struct { type CommentMention struct { ID uint `gorm:"primarykey" json:"id"` CommentID uint `gorm:"uniqueIndex:idx_comment_uid,priority:1;not null" json:"comment_id"` - UID string `gorm:"type:varchar(36);uniqueIndex:idx_comment_uid,priority:2;not null" json:"uid"` + UID uint `gorm:"uniqueIndex:idx_comment_uid,priority:2;not null" json:"uid"` } // UserSearchResult @搜索用户结果 type UserSearchResult struct { - UID string `json:"uid"` + UID uint `json:"uid"` Username string `json:"username"` Level int `json:"level"` } diff --git a/internal/repository/comment_mention_repo.go b/internal/repository/comment_mention_repo.go index 46a882c..f8ee0ab 100644 --- a/internal/repository/comment_mention_repo.go +++ b/internal/repository/comment_mention_repo.go @@ -1,8 +1,6 @@ package repository import ( - "strconv" - "metazone.cc/metalab/internal/model" "metazone.cc/metalab/internal/service" ) @@ -21,7 +19,7 @@ func (r *CommentRepo) FindMentionsByComment(commentID uint) ([]model.CommentMent // SearchUsersByLevel 搜索指定等级及以上用户(用于@艾特),返回 uid/username/exp func (r *CommentRepo) SearchUsersByLevel(keyword string, minLevel int, limit int) ([]struct { - UID string + UID uint Username string Exp int }, error) { @@ -42,30 +40,20 @@ func (r *CommentRepo) SearchUsersByLevel(keyword string, minLevel int, limit int Find(&list).Error var res []struct { - UID string + UID uint Username string Exp int } for _, u := range list { res = append(res, struct { - UID string + UID uint Username string Exp int - }{UID: strconv.FormatUint(uint64(u.UID), 10), Username: u.Username, Exp: u.Exp}) + }{UID: u.UID, Username: u.Username, Exp: u.Exp}) } return res, err } -// GetUserUIDByID 通过 uint ID 获取 user 的 UID 字符串 -func (r *CommentRepo) GetUserUIDByID(userID uint) (string, error) { - var uid uint - err := r.db.Table("users").Select("id").Where("id = ?", userID).Scan(&uid).Error - if err != nil { - return "", err - } - return strconv.FormatUint(uint64(uid), 10), nil -} - // GetUsernameByID 通过 userID 获取用户名(用于通知内容) func (r *CommentRepo) GetUsernameByID(userID uint) (string, error) { var username string diff --git a/internal/repository/comment_repo.go b/internal/repository/comment_repo.go index 43fd0a5..8a83cf5 100644 --- a/internal/repository/comment_repo.go +++ b/internal/repository/comment_repo.go @@ -96,7 +96,7 @@ func (r *CommentRepo) fillReplyToNames(list []model.Comment) { return } type nameRow struct { - UID string + UID uint Username string } var rows []nameRow @@ -105,12 +105,12 @@ func (r *CommentRepo) fillReplyToNames(list []model.Comment) { Where("id IN (SELECT reply_to_uid FROM comments WHERE root_id IN (?) AND parent_id IS NOT NULL)", r.rootIDsFromList(list)). Find(&rows) - m := make(map[string]string) + m := make(map[uint]string) for _, row := range rows { m[row.UID] = row.Username } for i := range list { - if list[i].ReplyToUID != "" { + if list[i].ReplyToUID != 0 { list[i].ReplyToName = m[list[i].ReplyToUID] } } diff --git a/internal/service/comment_service.go b/internal/service/comment_service.go index 77463e2..7006571 100644 --- a/internal/service/comment_service.go +++ b/internal/service/comment_service.go @@ -88,15 +88,12 @@ func (s *CommentService) CreateReply(userID uint, parentID uint, body string) (* } comment := &model.Comment{ - PostID: parent.PostID, - RootID: parent.RootID, - ParentID: &parentID, - Body: body, - UserID: userID, - } - // 获取被回复者UID - if replyToUID, err := s.repo.GetUserUIDByID(parent.UserID); err == nil { - comment.ReplyToUID = replyToUID + PostID: parent.PostID, + RootID: parent.RootID, + ParentID: &parentID, + Body: body, + UserID: userID, + ReplyToUID: parent.UserID, } if err := s.repo.Create(comment); err != nil { diff --git a/internal/service/repository.go b/internal/service/repository.go index d94290f..d3a3ddf 100644 --- a/internal/service/repository.go +++ b/internal/service/repository.go @@ -154,11 +154,10 @@ type commentStore interface { FindPostIDByComment(commentID uint) (uint, error) FindPostAuthorID(postID uint) (uint, error) SearchUsersByLevel(keyword string, minLevel, limit int) ([]struct { - UID string + UID uint Username string Exp int }, error) - GetUserUIDByID(userID uint) (string, error) GetUsernameByID(userID uint) (string, error) ListAllComments(keyword string, showDeleted bool, offset, limit int) ([]model.AdminCommentRow, int64, error) } diff --git a/templates/MetaLab-2026/html/posts/show.html b/templates/MetaLab-2026/html/posts/show.html index f870edc..0a739e7 100644 --- a/templates/MetaLab-2026/html/posts/show.html +++ b/templates/MetaLab-2026/html/posts/show.html @@ -931,7 +931,7 @@ let u = users[i]; var item = document.createElement('div'); item.className = 'mention-item'; - item.innerHTML = '' + escapeHtml(u.username) + '' + escapeHtml(u.id) + ''; + item.innerHTML = '' + escapeHtml(u.username) + '' + escapeHtml(u.uid) + ''; item.addEventListener('mousedown', function(e) { e.preventDefault(); selectMention(u);