Files
VLoop/backend/internal/video/like_repo.go
huanghaosheng d782ccbb53 refactor(p0-5): 收敛热度相关注释
背景叙述与历史取舍已写进 `docs/design/hot-ranking.md`,从源码删除。
只保留不变式与安全约束:`clientFoundRows` 禁令、唯一索引判重、
成员补零的排序约定、快照用 `RENAMENX` 只建一次、`EXPIRE` 失败即删键。

`video_service.go` 的 `NewVideoService` 注释原本复述已删方法的来历,一并收敛。
`zset.go` 的 `ZUnionStore` 与 `RenameNX` 注释各压到四行以内。

验证:go build ./... 通过。
验证:go vet ./... 通过。
验证:go test -count=1 ./... 全绿(本机无 cgo,未跑 -race)。
验证:go test -tags integration -p 1 -count=1 ./internal/feed/ ./internal/video/ ./internal/worker/ 全绿。
2026-11-01 16:47:31 +08:00

142 lines
3.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package video
import (
"context"
"errors"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type LikeRepository struct {
db *gorm.DB
}
func NewLikeRepository(db *gorm.DB) *LikeRepository {
return &LikeRepository{db: db}
}
// ApplyLike 插入点赞行,同一事务里给 likes_count 与 popularity 各 +1。
//
// 判重交给唯一索引,不做"先查再插":并发下必然漏判。
// created=false 表示行本来就存在,此时两个计数都不能再动。
//
// DSN 不能开 clientFoundRows,否则 RowsAffected 恒为 1,判重失效。
func (r *LikeRepository) ApplyLike(ctx context.Context, like *Like) (created bool, err error) {
if like == nil || like.VideoID == 0 || like.AccountID == 0 {
return false, nil
}
err = r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
res := tx.Clauses(clause.OnConflict{DoNothing: true}).Create(like)
if res.Error != nil {
return res.Error
}
if res.RowsAffected == 0 {
return nil
}
created = true
if err := tx.Model(&Video{}).Where("id = ?", like.VideoID).
UpdateColumn("likes_count", gorm.Expr("GREATEST(likes_count + 1, 0)")).Error; err != nil {
return err
}
return tx.Model(&Video{}).Where("id = ?", like.VideoID).
UpdateColumn("popularity", gorm.Expr("GREATEST(popularity + 1, 0)")).Error
})
if err != nil {
return false, err
}
return created, nil
}
// ApplyUnlike 删除点赞行并把两个计数各 -1,返回被删的那一行。
//
// created_at 必须来自库里:取消点赞要把分数减回点赞那一刻的桶,
// 用调用方此刻的钟会留下一个永远偏高的旧桶。
func (r *LikeRepository) ApplyUnlike(ctx context.Context, videoID, accountID uint) (like *Like, deleted bool, err error) {
if videoID == 0 || accountID == 0 {
return nil, false, nil
}
err = r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
var row Like
if err := tx.Where("video_id = ? AND account_id = ?", videoID, accountID).First(&row).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil
}
return err
}
res := tx.Delete(&Like{}, row.ID)
if res.Error != nil {
return res.Error
}
if res.RowsAffected == 0 {
return nil
}
deleted = true
like = &row
if err := tx.Model(&Video{}).Where("id = ?", videoID).
UpdateColumn("likes_count", gorm.Expr("GREATEST(likes_count - 1, 0)")).Error; err != nil {
return err
}
return tx.Model(&Video{}).Where("id = ?", videoID).
UpdateColumn("popularity", gorm.Expr("GREATEST(popularity - 1, 0)")).Error
})
if err != nil {
return nil, false, err
}
return like, deleted, nil
}
func (r *LikeRepository) IsLiked(ctx context.Context, videoID, accountID uint) (bool, error) {
var count int64
err := r.db.WithContext(ctx).Model(&Like{}).
Where("video_id = ? AND account_id = ?", videoID, accountID).
Count(&count).Error
if err != nil {
return false, err
}
return count > 0, nil
}
// 查询给定视频 ID 列表中,哪些视频被指定账户点赞过
func (r *LikeRepository) BatchGetLiked(ctx context.Context, videoIDs []uint, accountID uint) (map[uint]bool, error) {
likeMap := make(map[uint]bool)
if len(videoIDs) == 0 {
return likeMap, nil
}
if accountID == 0 {
return likeMap, nil
}
var likes []Like
err := r.db.WithContext(ctx).Model(&Like{}).
Where("video_id IN ? AND account_id = ?", videoIDs, accountID).
Find(&likes).Error
if err != nil {
return nil, err
}
for _, like := range likes {
likeMap[like.VideoID] = true
}
return likeMap, nil
}
func (r *LikeRepository) ListLikedVideos(ctx context.Context, accountID uint) ([]Video, error) {
var videos []Video
if accountID == 0 {
return videos, nil
}
err := r.db.WithContext(ctx).
Model(&Video{}).
Joins("JOIN likes ON likes.video_id = videos.id").
Where("likes.account_id = ?", accountID).
Order("likes.created_at desc").
Limit(200).
Find(&videos).Error
if err != nil {
return nil, err
}
return videos, nil
}