2025-12-09 15:30:16 +08:00
|
|
|
|
package video
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"errors"
|
2025-12-30 01:23:54 +08:00
|
|
|
|
"feedsystem_video_go/internal/middleware/rabbitmq"
|
|
|
|
|
|
rediscache "feedsystem_video_go/internal/middleware/redis"
|
2026-10-30 13:25:48 +08:00
|
|
|
|
"log"
|
2025-12-09 15:30:16 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type LikeService struct {
|
2026-11-01 15:15:15 +08:00
|
|
|
|
repo *LikeRepository
|
|
|
|
|
|
VideoRepo *VideoRepository
|
|
|
|
|
|
cache *rediscache.Client
|
|
|
|
|
|
likeMQ *rabbitmq.LikeMQ
|
2025-12-09 15:30:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-01 15:15:15 +08:00
|
|
|
|
func NewLikeService(repo *LikeRepository, videoRepo *VideoRepository, cache *rediscache.Client, likeMQ *rabbitmq.LikeMQ) *LikeService {
|
|
|
|
|
|
return &LikeService{repo: repo, VideoRepo: videoRepo, cache: cache, likeMQ: likeMQ}
|
2025-12-23 02:20:08 +08:00
|
|
|
|
}
|
2025-12-16 18:52:09 +08:00
|
|
|
|
|
2025-12-23 02:20:08 +08:00
|
|
|
|
func (s *LikeService) Like(ctx context.Context, like *Like) error {
|
2025-12-30 01:23:54 +08:00
|
|
|
|
if like == nil {
|
|
|
|
|
|
return errors.New("like is nil")
|
|
|
|
|
|
}
|
|
|
|
|
|
if like.VideoID == 0 || like.AccountID == 0 {
|
|
|
|
|
|
return errors.New("video_id and account_id are required")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if s.VideoRepo != nil {
|
|
|
|
|
|
ok, err := s.VideoRepo.IsExist(ctx, like.VideoID)
|
|
|
|
|
|
if err != nil {
|
2025-12-23 02:20:08 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
2025-12-30 01:23:54 +08:00
|
|
|
|
if !ok {
|
|
|
|
|
|
return errors.New("video not found")
|
2025-12-23 02:20:08 +08:00
|
|
|
|
}
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
isLiked, err := s.repo.IsLiked(ctx, like.VideoID, like.AccountID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
if isLiked {
|
|
|
|
|
|
return errors.New("user has liked this video")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
like.CreatedAt = time.Now()
|
2026-10-30 13:25:48 +08:00
|
|
|
|
|
2026-11-01 15:15:15 +08:00
|
|
|
|
// 意图键只用于日志关联(LikeEvent.EventID),不再参与去重:
|
|
|
|
|
|
// 判重由 likes 的唯一索引在事务里做,重复投递不会重复计数。
|
2026-10-30 13:25:48 +08:00
|
|
|
|
intentKey, err := NewIntentKey()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-01 15:15:15 +08:00
|
|
|
|
enqueued := false
|
2025-12-30 01:23:54 +08:00
|
|
|
|
if s.likeMQ != nil {
|
2026-11-01 15:15:15 +08:00
|
|
|
|
if err := s.likeMQ.Like(ctx, like.AccountID, like.VideoID, intentKey); err != nil {
|
|
|
|
|
|
log.Printf("like: enqueue like failed, fallback to direct write: %v", err)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
enqueued = true
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-11-01 15:15:15 +08:00
|
|
|
|
if enqueued {
|
|
|
|
|
|
// 入队成功即返回:落库与热榜都由 worker 在同一条事务路径上完成。
|
|
|
|
|
|
return nil
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-01 15:15:15 +08:00
|
|
|
|
created, err := s.repo.ApplyLike(ctx, like)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
2026-11-01 15:15:15 +08:00
|
|
|
|
if !created {
|
|
|
|
|
|
return errors.New("user has liked this video")
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := ApplyHotDelta(ctx, s.cache, like.VideoID, 1, like.CreatedAt); err != nil {
|
|
|
|
|
|
log.Printf("like: 热榜更新失败(已降级)video=%d: %v", like.VideoID, err)
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
return nil
|
2025-12-09 15:30:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *LikeService) Unlike(ctx context.Context, like *Like) error {
|
2025-12-30 01:23:54 +08:00
|
|
|
|
if like == nil {
|
|
|
|
|
|
return errors.New("like is nil")
|
|
|
|
|
|
}
|
|
|
|
|
|
if like.VideoID == 0 || like.AccountID == 0 {
|
|
|
|
|
|
return errors.New("video_id and account_id are required")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if s.VideoRepo != nil {
|
|
|
|
|
|
ok, err := s.VideoRepo.IsExist(ctx, like.VideoID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return errors.New("video not found")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
isLiked, err := s.repo.IsLiked(ctx, like.VideoID, like.AccountID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
if !isLiked {
|
|
|
|
|
|
return errors.New("user has not liked this video")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-30 13:25:48 +08:00
|
|
|
|
intentKey, err := NewIntentKey()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-01 15:15:15 +08:00
|
|
|
|
enqueued := false
|
2025-12-30 01:23:54 +08:00
|
|
|
|
if s.likeMQ != nil {
|
2026-11-01 15:15:15 +08:00
|
|
|
|
if err := s.likeMQ.Unlike(ctx, like.AccountID, like.VideoID, intentKey); err != nil {
|
|
|
|
|
|
log.Printf("unlike: enqueue unlike failed, fallback to direct write: %v", err)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
enqueued = true
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-11-01 15:15:15 +08:00
|
|
|
|
if enqueued {
|
|
|
|
|
|
return nil
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-01 15:15:15 +08:00
|
|
|
|
row, deleted, err := s.repo.ApplyUnlike(ctx, like.VideoID, like.AccountID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
2026-11-01 15:15:15 +08:00
|
|
|
|
if !deleted {
|
|
|
|
|
|
return errors.New("user has not liked this video")
|
|
|
|
|
|
}
|
|
|
|
|
|
// 减回点赞那一分钟:热榜是窗口,不能按此刻的钟回退。
|
|
|
|
|
|
if err := ApplyHotDelta(ctx, s.cache, like.VideoID, -1, row.CreatedAt); err != nil {
|
|
|
|
|
|
log.Printf("unlike: 热榜更新失败(已降级)video=%d: %v", like.VideoID, err)
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
return nil
|
2025-12-09 15:30:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *LikeService) IsLiked(ctx context.Context, videoID, accountID uint) (bool, error) {
|
|
|
|
|
|
return s.repo.IsLiked(ctx, videoID, accountID)
|
|
|
|
|
|
}
|
2025-12-26 18:23:55 +08:00
|
|
|
|
|
|
|
|
|
|
func (s *LikeService) ListLikedVideos(ctx context.Context, accountID uint) ([]Video, error) {
|
|
|
|
|
|
return s.repo.ListLikedVideos(ctx, accountID)
|
|
|
|
|
|
}
|