45 lines
2.3 KiB
Go
45 lines
2.3 KiB
Go
|
|
// 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" }
|