diff --git a/internal/service/moment_service.go b/internal/service/moment_service.go index d382ed2..781ee56 100644 --- a/internal/service/moment_service.go +++ b/internal/service/moment_service.go @@ -8,6 +8,7 @@ import ( "encoding/json" "errors" "log" + "time" "xk-websocket-v2/internal/model" "gorm.io/gorm" @@ -688,9 +689,25 @@ func (s *MomentService) loadNotificationData(notif *model.MomentNotification) { /** * createNotification - * 功能:创建通知并推送 + * 功能:创建通知并推送(带去重检查) */ func (s *MomentService) createNotification(toUserID, fromUserID string, momentID uint, notifType int8, commentID *uint) { + // 去重检查:5秒内相同的通知视为重复 + var existing model.MomentNotification + query := s.DB.Where( + "user_id = ? AND from_user_id = ? AND moment_id = ? AND type = ? AND created_at > ?", + toUserID, fromUserID, momentID, notifType, time.Now().Add(-5*time.Second), + ) + // 如果有 commentID,也加入去重条件 + if commentID != nil { + query = query.Where("comment_id = ?", *commentID) + } + if err := query.First(&existing).Error; err == nil { + // 已存在相同通知,跳过创建 + log.Printf("🔄 跳过重复朋友圈通知: to=%s from=%s moment=%d type=%d", toUserID, fromUserID, momentID, notifType) + return + } + notif := &model.MomentNotification{ UserID: toUserID, FromUserID: fromUserID,