fix: 评论系统全量检查修复(7 项 Gap 全部修复)
- Delete 权限:新增 requesterID/isAdmin 参数,Service 层鉴权(仅本人/管理员可删)
- 通知内容:改用 GetUsernameByID 显示真实用户名,非数字 UID
- SearchUsers:Repository 返回 exp,Service 调用 GetLevelByExp 计算等级
- 通知高亮:Notification 新增 CommentID 字段,消息链接支持 #comment-{id}
- 已删除提示:前端 hash 检测 + 3 秒超时 Toast "该评论已不存在"
- Admin CSS:移除不存在的 comments.css 引用
- 错误哨兵:改用 common.ErrCommentNotFound/ErrCommentEmpty/PermissionDenied
This commit is contained in:
@ -28,7 +28,7 @@ func NewCommentService(repo commentStore, notifier commentNotifier) *CommentServ
|
||||
// CreateRoot 创建顶级评论
|
||||
func (s *CommentService) CreateRoot(userID uint, postID uint, body string) (*model.Comment, error) {
|
||||
if strings.TrimSpace(body) == "" {
|
||||
return nil, errors.New("评论内容不能为空")
|
||||
return nil, common.ErrCommentEmpty
|
||||
}
|
||||
|
||||
postAuthorID, err := s.repo.FindPostAuthorID(postID)
|
||||
@ -60,11 +60,14 @@ func (s *CommentService) CreateRoot(userID uint, postID uint, body string) (*mod
|
||||
|
||||
// 通知文章作者(评论者 ≠ 作者),RelatedID 存 postID 以便消息页生成跳转链接
|
||||
if s.notifier != nil && userID != postAuthorID {
|
||||
commenterUID, _ := s.repo.GetUserUIDByID(userID)
|
||||
commenterName, _ := s.repo.GetUsernameByID(userID)
|
||||
if commenterName == "" {
|
||||
commenterName = "用户"
|
||||
}
|
||||
s.notifier.Create(postAuthorID, model.NotifyComment,
|
||||
"新评论",
|
||||
fmt.Sprintf("%s 评论了你的文章", commenterUID),
|
||||
&comment.PostID)
|
||||
fmt.Sprintf("%s 评论了你的文章", commenterName),
|
||||
&comment.PostID, &comment.ID)
|
||||
}
|
||||
|
||||
return comment, nil
|
||||
@ -73,13 +76,13 @@ func (s *CommentService) CreateRoot(userID uint, postID uint, body string) (*mod
|
||||
// CreateReply 创建回复
|
||||
func (s *CommentService) CreateReply(userID uint, parentID uint, body string) (*model.Comment, error) {
|
||||
if strings.TrimSpace(body) == "" {
|
||||
return nil, errors.New("回复内容不能为空")
|
||||
return nil, common.ErrCommentEmpty
|
||||
}
|
||||
|
||||
parent, err := s.repo.FindByID(parentID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errors.New("父评论不存在")
|
||||
return nil, common.ErrCommentNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("查询父评论失败: %w", err)
|
||||
}
|
||||
@ -108,22 +111,25 @@ func (s *CommentService) CreateReply(userID uint, parentID uint, body string) (*
|
||||
|
||||
// 通知被回复者(不自己回复自己),RelatedID 存 postID 以便消息页生成跳转链接
|
||||
if s.notifier != nil && parent.UserID != userID {
|
||||
commenterUID, _ := s.repo.GetUserUIDByID(userID)
|
||||
commenterName, _ := s.repo.GetUsernameByID(userID)
|
||||
if commenterName == "" {
|
||||
commenterName = "用户"
|
||||
}
|
||||
s.notifier.Create(parent.UserID, model.NotifyCommentReply,
|
||||
"新回复",
|
||||
fmt.Sprintf("%s 回复了你的评论", commenterUID),
|
||||
&parent.PostID)
|
||||
fmt.Sprintf("%s 回复了你的评论", commenterName),
|
||||
&parent.PostID, &comment.ID)
|
||||
}
|
||||
|
||||
return comment, nil
|
||||
}
|
||||
|
||||
// Delete 软删除评论
|
||||
func (s *CommentService) Delete(commentID uint) error {
|
||||
// Delete 软删除评论(requesterID 为请求者,isAdmin 表示是否为管理员)
|
||||
func (s *CommentService) Delete(commentID uint, requesterID uint, isAdmin bool) error {
|
||||
comment, err := s.repo.FindByID(commentID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.New("评论不存在")
|
||||
return common.ErrCommentNotFound
|
||||
}
|
||||
return fmt.Errorf("查询评论失败: %w", err)
|
||||
}
|
||||
@ -132,6 +138,11 @@ func (s *CommentService) Delete(commentID uint) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 权限检查:仅本人或管理员可删除
|
||||
if !isAdmin && comment.UserID != requesterID {
|
||||
return common.ErrPermissionDenied
|
||||
}
|
||||
|
||||
if err := s.repo.SoftDelete(commentID); err != nil {
|
||||
return fmt.Errorf("删除评论失败: %w", err)
|
||||
}
|
||||
@ -182,7 +193,7 @@ func (s *CommentService) SearchUsers(keyword string) ([]model.UserSearchResult,
|
||||
users[i] = model.UserSearchResult{
|
||||
UID: row.UID,
|
||||
Username: row.Username,
|
||||
Level: 0,
|
||||
Level: model.GetLevelByExp(row.Exp),
|
||||
}
|
||||
}
|
||||
return users, nil
|
||||
|
||||
Reference in New Issue
Block a user