80 lines
3.5 KiB
Go
80 lines
3.5 KiB
Go
|
|
package video
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"context"
|
|||
|
|
"time"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// Mention 是一条"提及通知"的写入意图。
|
|||
|
|
//
|
|||
|
|
// DedupeKey 由 NotificationDedupeKey(notifType, intentKey, recipientID) 生成,
|
|||
|
|
// 落到 notifications 表的唯一索引 uk_notifications_dedupe 上。它让"同一次
|
|||
|
|
// 评论意图产生的提及通知"无论被写几次都只有一行——与评论本身用的是同一个
|
|||
|
|
// 意图键,所以两侧的判重是同一个语义,而不是两套各自为政的计数器。
|
|||
|
|
type Mention struct {
|
|||
|
|
RecipientID uint
|
|||
|
|
SenderID uint
|
|||
|
|
TargetID uint
|
|||
|
|
Content string
|
|||
|
|
DedupeKey string
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// NotificationRow 是 notifications 表在 video 包里的投影。
|
|||
|
|
//
|
|||
|
|
// 为什么不直接 import worker.Notification:worker 包 import 了 video
|
|||
|
|
// (CommentWorker 依赖 CommentStore),反向 import 会成环。这张投影只需要
|
|||
|
|
// "写一行通知"所需的字段,schema 的权威定义仍在 worker.Notification,
|
|||
|
|
// 因此下面每个列标签都必须与它逐字一致——不一致时 AutoMigrate 会试图改动
|
|||
|
|
// 生产列类型,那是本方案最不希望引入的意外。
|
|||
|
|
type NotificationRow struct {
|
|||
|
|
ID uint `gorm:"primaryKey"`
|
|||
|
|
RecipientID uint `gorm:"index;not null"`
|
|||
|
|
SenderID uint `gorm:"not null"`
|
|||
|
|
Type string `gorm:"type:varchar(50);not null"`
|
|||
|
|
TargetID uint `gorm:"column:target_id"`
|
|||
|
|
Content string `gorm:"type:varchar(255)"`
|
|||
|
|
IsRead bool `gorm:"default:false"`
|
|||
|
|
CreatedAt time.Time `gorm:"autoCreateTime"`
|
|||
|
|
|
|||
|
|
// DedupeKey 与 worker.Notification.DedupeKey 指向同一个唯一索引
|
|||
|
|
// uk_notifications_dedupe。为 nil 时不参与判重(MySQL 不约束 NULL),
|
|||
|
|
// 这正是"没有意图键的旧路径"仍然能写入的原因。
|
|||
|
|
DedupeKey *string `gorm:"column:dedupe_key;type:varchar(128);uniqueIndex:uk_notifications_dedupe"`
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (NotificationRow) TableName() string { return "notifications" }
|
|||
|
|
|
|||
|
|
// 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
|
|||
|
|
}
|