Files
VLoop/backend/internal/video/comment_store.go

54 lines
2.2 KiB
Go
Raw Normal View History

package video
import (
"context"
)
// Mention 是一条"提及通知"的写入意图。
//
// DedupeKey 由 NotificationDedupeKey(notifType, intentKey, recipientID) 生成,
// 落到 notifications 表的唯一索引 uk_notifications_dedupe 上。它让"同一次
// 评论意图产生的提及通知"无论被写几次都只有一行——与评论本身用的是同一个
// 意图键,所以两侧的判重是同一个语义,而不是两套各自为政的计数器。
type Mention struct {
RecipientID uint
SenderID uint
TargetID uint
Content string
DedupeKey string
}
// CommentStore 是 CommentService 与 CommentWorker 依赖的窄接口。
//
// 抽接口不是为了"面向接口编程",而是为了给"判重与写入必须发生在同一条 SQL
// 的同一个事务里"这条约束一个显式的名字:任何实现都必须提供
// CreateIdempotentWithPopularity,而不是先 Create 再补一条热度 UPDATE——
// 后者在两条语句之间崩溃就会留下"评论在、热度不在"的半成品。
type CommentStore interface {
// CreateIdempotentWithPopularity 在同一个事务里完成
// "插入评论(幂等)" + "热度增量(仅首次插入时)"。
// created=false 表示这条意图已经落过库,调用方不得再加热度。
CreateIdempotentWithPopularity(ctx context.Context, c *Comment, delta int64) (created bool, err error)
GetByID(ctx context.Context, id uint) (*Comment, error)
DeleteComment(ctx context.Context, comment *Comment) error
DeleteByID(ctx context.Context, id uint) (deleted bool, err error)
GetAllComments(ctx context.Context, videoID uint) ([]Comment, error)
// CreateMentions 按 DedupeKey 幂等写入提及通知。
CreateMentions(ctx context.Context, mentions []Mention) error
// FindAccountIDs 一次查询把用户名批量解析成账号 ID。
FindAccountIDs(ctx context.Context, usernames []string) (map[string]uint, error)
}
// nilIfEmpty 把空串转成 NULL。
//
// 用于 idempotency_key / dedupe_key 这类"可空唯一列":空串是一个真实的值,
// 多条空串会互相撞唯一索引;NULL 才是"没有这个键"。
func nilIfEmpty(s string) *string {
if s == "" {
return nil
}
return &s
}