Files
VLoop/backend/internal/video/comment_store.go
huanghaosheng 151faf25fa refactor(notification): 通知实体抽成独立包
`video` 与 `worker` 各有一份 `notifications` 表的实体。
两份定义会漂移,所以合并到 `internal/notification`。
新增 `entity.go` 保存唯一一份列标签。

`worker`、`ssehub`、`comment_repo`、`db` 与两个集成测试改为引用新包。
`db` 包不再 import `worker`。
去重键仍是 `*string`,因此 NULL 才能共存。

验证:go test -count=1 ./... 全绿;集成测试三包全绿。
2026-10-30 23:48:43 +08:00

54 lines
2.2 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"
)
// 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
}