同一条用户意图(同 author_id + request_id)在 comments 上只落一行: 唯一索引 uk_comments_author_key 是唯一的判重点,API 与 Worker 共用 同一个键,因此「MQ 假失败后兜底直写」与「消息稍后被消费」不再双写。 - comment_service: 引入 CommentStore/CommentPublisher/PopularityPublisher 三个最小接口;Publish 先解析意图键,再把同一个键喂给评论事件、热度事件 与兜底直写。只有评论事件没入队时才直写(闸 1),热度兜底独立门控。 - comment_repo: CreateIdempotentWithPopularity 在单事务内插入评论并按 created 门控 videos.popularity;CreateMentions 显式补 CreatedAt 并按 dedupe_key 去重;FindAccountIDs 一次 username IN ? 批量解析。 - commentworker: applyPublish 用 event_id 作幂等键并交给同一个事务, !created 记 dedupe=hit;applyDelete 改 DeleteByID,重复删除是空操作。 - comment_handler: 接收 request_id、响应回显,不返回评论 ID(MQ 路径上 评论此刻可能尚未落库)。 - 提及通知带上绑定到意图键的 dedupe_key,重复投递只写一条。 验证:go test ./... 全绿;-tags integration 下 3 个用例在真实 MySQL 上 通过(重复事件 1 行评论 + 热度只加 1、空 event_id 落 NULL 键、重复删除)
112 lines
3.3 KiB
Go
112 lines
3.3 KiB
Go
package video
|
||
|
||
import (
|
||
"feedsystem_video_go/internal/account"
|
||
"feedsystem_video_go/internal/apierror"
|
||
"feedsystem_video_go/internal/middleware/jwt"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
type CommentHandler struct {
|
||
service *CommentService
|
||
accountService *account.AccountService
|
||
}
|
||
|
||
func NewCommentHandler(service *CommentService, accountService *account.AccountService) *CommentHandler {
|
||
return &CommentHandler{service: service, accountService: accountService}
|
||
}
|
||
func (h *CommentHandler) PublishComment(c *gin.Context) {
|
||
var req PublishCommentRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
if req.Content == "" {
|
||
c.JSON(400, gin.H{"error": "content is required"})
|
||
return
|
||
}
|
||
if req.VideoID <= 0 {
|
||
c.JSON(400, gin.H{"error": "video_id is required"})
|
||
return
|
||
}
|
||
authorId, err := jwt.GetAccountID(c)
|
||
if err != nil {
|
||
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
user, err := h.accountService.FindByID(c.Request.Context(), authorId)
|
||
if err != nil {
|
||
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
comment := &Comment{
|
||
Username: user.Username,
|
||
VideoID: req.VideoID,
|
||
AuthorID: authorId,
|
||
Content: req.Content,
|
||
}
|
||
// 客户端可以自带 request_id 来表达"这就是我刚才那次提交"。
|
||
// 不传则由服务端生成:同一个请求内的所有写入仍然共用一个键(G6)。
|
||
if req.RequestID != "" {
|
||
requestID := req.RequestID
|
||
comment.IdempotencyKey = &requestID
|
||
}
|
||
if err := h.service.Publish(c.Request.Context(), comment); err != nil {
|
||
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
// 回显 request_id:客户端重试时可以带上同一个值,服务端据此判重。
|
||
// 刻意不返回评论 ID——MQ 路径上评论此刻可能还没落库,返回一个还不存在的
|
||
// id 只会制造"点进去 404"的错觉。
|
||
resp := gin.H{"message": "comment published successfully"}
|
||
if comment.IdempotencyKey != nil {
|
||
resp["request_id"] = *comment.IdempotencyKey
|
||
}
|
||
c.JSON(200, resp)
|
||
}
|
||
|
||
func (h *CommentHandler) DeleteComment(c *gin.Context) {
|
||
var req DeleteCommentRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
accountID, err := jwt.GetAccountID(c)
|
||
if err != nil {
|
||
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
if req.CommentID <= 0 {
|
||
c.JSON(400, gin.H{"error": "comment_id is required"})
|
||
return
|
||
}
|
||
if err := h.service.Delete(c.Request.Context(), req.CommentID, accountID); err != nil {
|
||
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
|
||
c.JSON(200, gin.H{"message": "comment deleted successfully"})
|
||
}
|
||
|
||
func (h *CommentHandler) GetAllComments(c *gin.Context) {
|
||
var req GetAllCommentsRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
if req.VideoID == 0 {
|
||
c.JSON(400, gin.H{"error": "video_id is required"})
|
||
return
|
||
}
|
||
comments, err := h.service.GetAll(c.Request.Context(), req.VideoID)
|
||
if err != nil {
|
||
c.JSON(apierror.ClassifyHTTPStatus(err), gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
if comments == nil {
|
||
comments = []Comment{}
|
||
}
|
||
c.JSON(200, comments)
|
||
}
|