Files
VLoop/backend/internal/notification/entity.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

45 lines
2.3 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 notification 存放通知表的实体定义。
//
// 它独立成包,是为了让"notifications 表的列结构"只有一个权威定义:
//
// - Worker 侧需要读写它(写通知、SSE 列表、标记已读、未读数);
// - video 包需要写它(评论里的 @ 提及通知)。
//
// video 不能 import worker(worker 已经 import video,会成环),而各自维护一份
// 结构体又会让列标签悄悄漂移——漂移不会报错,只会在 AutoMigrate 时试图改动
// 生产列类型。因此把实体放在两边都能引用的中立位置,依赖方向恒为
// worker → notification 与 video → notification。
//
// 本包不依赖项目内的任何其他包,只描述表结构。
package notification
import "time"
// Notification 是 notifications 表的一行。
type Notification struct {
ID uint `gorm:"primaryKey" json:"id"`
RecipientID uint `gorm:"index;not null" json:"recipient_id"`
SenderID uint `gorm:"not null" json:"sender_id"`
Type string `gorm:"type:varchar(50);not null" json:"type"`
TargetID uint `gorm:"column:target_id" json:"target_id"`
Content string `gorm:"type:varchar(255)" json:"content"`
IsRead bool `gorm:"default:false" json:"is_read"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
// DedupeKey 把"这条通知由哪一次用户意图产生"写进唯一索引。
//
// 同一条点赞/评论事件被投递两次(发布返回错误但实际已入队、重试、重放)时,
// 第二次会撞上 uk_notifications_dedupe 而不是再插一行——用户不会在通知
// 列表里看到两条一模一样的"点赞了你的视频",也不会收到两次 SSE 推送。
//
// 用 *string 而不是 string:没有意图键的事件(历史数据、旧客户端)宁可
// 不去重(NULL 不参与唯一约束),也不能把不同事件都判成同一条。
DedupeKey *string `gorm:"column:dedupe_key;type:varchar(128);uniqueIndex:uk_notifications_dedupe" json:"-"`
}
// TableName 固定表名。
//
// 默认命名(复数化)结果也是 notifications,但显式写出来可以避免有人把类型
// 改名之后表名跟着漂移——这张表的写入方有两个包,表名漂移的代价比多写一行大。
func (Notification) TableName() string { return "notifications" }