2026-05-20 16:34:19 +08:00
|
|
|
|
package video
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
2026-11-01 15:15:15 +08:00
|
|
|
|
"errors"
|
2026-10-30 13:21:09 +08:00
|
|
|
|
"time"
|
2026-05-20 16:34:19 +08:00
|
|
|
|
|
2026-10-30 23:48:43 +08:00
|
|
|
|
"feedsystem_video_go/internal/notification"
|
|
|
|
|
|
|
2026-05-20 16:34:19 +08:00
|
|
|
|
"gorm.io/gorm"
|
2026-10-30 13:21:09 +08:00
|
|
|
|
"gorm.io/gorm/clause"
|
2026-05-20 16:34:19 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type CommentRepository struct {
|
|
|
|
|
|
db *gorm.DB
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewCommentRepository(db *gorm.DB) *CommentRepository {
|
|
|
|
|
|
return &CommentRepository{db: db}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *CommentRepository) CreateComment(ctx context.Context, comment *Comment) error {
|
|
|
|
|
|
return r.db.WithContext(ctx).Create(comment).Error
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-01 16:47:31 +08:00
|
|
|
|
// CreateIdempotentWithPopularity 由 uk_comments_author_key 判重,
|
|
|
|
|
|
// 热度增量与插入同事务,只在 created=true 时执行。
|
|
|
|
|
|
//
|
|
|
|
|
|
// DSN 不能开 clientFoundRows,否则 RowsAffected 恒为 1,判重失效。
|
2026-10-30 13:21:09 +08:00
|
|
|
|
func (r *CommentRepository) CreateIdempotentWithPopularity(ctx context.Context, c *Comment, delta int64) (bool, error) {
|
|
|
|
|
|
var created bool
|
|
|
|
|
|
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
|
|
|
|
res := tx.Clauses(clause.OnConflict{DoNothing: true}).Create(c)
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
|
|
return res.Error
|
|
|
|
|
|
}
|
|
|
|
|
|
if res.RowsAffected == 0 {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
created = true
|
|
|
|
|
|
if delta == 0 {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return tx.Model(&Video{}).
|
|
|
|
|
|
Where("id = ?", c.VideoID).
|
|
|
|
|
|
UpdateColumn("popularity", gorm.Expr("GREATEST(popularity + ?, 0)", delta)).Error
|
|
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return false, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return created, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-01 15:15:15 +08:00
|
|
|
|
// DeleteWithPopularity 删除评论,并在同一事务里把所属视频的 popularity 减 1。
|
|
|
|
|
|
//
|
2026-11-01 16:47:31 +08:00
|
|
|
|
// 返回被删的行:调用方要它的 created_at 落桶、要 video_id 定位视频,
|
|
|
|
|
|
// 这两个值从库里取比从事件里取可信。
|
2026-11-01 15:15:15 +08:00
|
|
|
|
func (r *CommentRepository) DeleteWithPopularity(ctx context.Context, id uint) (*Comment, bool, error) {
|
2026-10-30 13:21:09 +08:00
|
|
|
|
if id == 0 {
|
2026-11-01 15:15:15 +08:00
|
|
|
|
return nil, false, nil
|
2026-10-30 13:21:09 +08:00
|
|
|
|
}
|
2026-11-01 15:15:15 +08:00
|
|
|
|
var deleted *Comment
|
|
|
|
|
|
ok := false
|
|
|
|
|
|
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
|
|
|
|
var row Comment
|
|
|
|
|
|
if err := tx.First(&row, id).Error; err != nil {
|
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
res := tx.Delete(&Comment{}, row.ID)
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
|
|
return res.Error
|
|
|
|
|
|
}
|
|
|
|
|
|
if res.RowsAffected == 0 {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
deleted = &row
|
|
|
|
|
|
ok = true
|
|
|
|
|
|
|
|
|
|
|
|
return tx.Model(&Video{}).
|
|
|
|
|
|
Where("id = ?", row.VideoID).
|
|
|
|
|
|
UpdateColumn("popularity", gorm.Expr("GREATEST(popularity - 1, 0)")).Error
|
|
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, false, err
|
2026-10-30 13:21:09 +08:00
|
|
|
|
}
|
2026-11-01 15:15:15 +08:00
|
|
|
|
return deleted, ok, nil
|
2026-10-30 13:21:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-20 16:34:19 +08:00
|
|
|
|
func (r *CommentRepository) GetAllComments(ctx context.Context, videoID uint) ([]Comment, error) {
|
|
|
|
|
|
var comments []Comment
|
|
|
|
|
|
err := r.db.WithContext(ctx).
|
|
|
|
|
|
Where("video_id = ?", videoID).
|
|
|
|
|
|
Order("created_at asc").
|
|
|
|
|
|
Limit(200).
|
|
|
|
|
|
Find(&comments).Error
|
|
|
|
|
|
return comments, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *CommentRepository) IsExist(ctx context.Context, id uint) (bool, error) {
|
|
|
|
|
|
var comment Comment
|
|
|
|
|
|
if err := r.db.WithContext(ctx).First(&comment, id).Error; err != nil {
|
|
|
|
|
|
if err == gorm.ErrRecordNotFound {
|
|
|
|
|
|
return false, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return false, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return true, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *CommentRepository) GetByID(ctx context.Context, id uint) (*Comment, error) {
|
|
|
|
|
|
var comment Comment
|
|
|
|
|
|
if err := r.db.WithContext(ctx).First(&comment, id).Error; err != nil {
|
|
|
|
|
|
if err == gorm.ErrRecordNotFound {
|
|
|
|
|
|
return nil, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return &comment, nil
|
|
|
|
|
|
}
|
2026-10-30 13:21:09 +08:00
|
|
|
|
|
|
|
|
|
|
func (r *CommentRepository) CreateMentions(ctx context.Context, mentions []Mention) error {
|
|
|
|
|
|
if len(mentions) == 0 {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
now := time.Now()
|
2026-10-30 23:48:43 +08:00
|
|
|
|
rows := make([]notification.Notification, 0, len(mentions))
|
2026-10-30 13:21:09 +08:00
|
|
|
|
for _, m := range mentions {
|
2026-10-30 23:48:43 +08:00
|
|
|
|
rows = append(rows, notification.Notification{
|
2026-10-30 13:21:09 +08:00
|
|
|
|
RecipientID: m.RecipientID,
|
|
|
|
|
|
SenderID: m.SenderID,
|
|
|
|
|
|
Type: "mention",
|
|
|
|
|
|
TargetID: m.TargetID,
|
|
|
|
|
|
Content: m.Content,
|
|
|
|
|
|
CreatedAt: now,
|
|
|
|
|
|
DedupeKey: nilIfEmpty(m.DedupeKey),
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
return r.db.WithContext(ctx).
|
|
|
|
|
|
Clauses(clause.OnConflict{DoNothing: true}).
|
|
|
|
|
|
Create(&rows).Error
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-31 00:03:53 +08:00
|
|
|
|
// FindAccountIDs 一次查询解析一批用户名;不存在的用户名不出现在结果里。
|
2026-10-30 13:21:09 +08:00
|
|
|
|
func (r *CommentRepository) FindAccountIDs(ctx context.Context, usernames []string) (map[string]uint, error) {
|
|
|
|
|
|
out := make(map[string]uint, len(usernames))
|
|
|
|
|
|
if len(usernames) == 0 {
|
|
|
|
|
|
return out, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
var rows []struct {
|
|
|
|
|
|
ID uint
|
|
|
|
|
|
Username string
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := r.db.WithContext(ctx).
|
|
|
|
|
|
Table("accounts").
|
|
|
|
|
|
Select("id, username").
|
|
|
|
|
|
Where("username IN ?", usernames).
|
|
|
|
|
|
Scan(&rows).Error; err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, row := range rows {
|
|
|
|
|
|
out[row.Username] = row.ID
|
|
|
|
|
|
}
|
|
|
|
|
|
return out, nil
|
|
|
|
|
|
}
|