refactor(notification): 通知实体抽成独立包
`video` 与 `worker` 各有一份 `notifications` 表的实体。 两份定义会漂移,所以合并到 `internal/notification`。 新增 `entity.go` 保存唯一一份列标签。 `worker`、`ssehub`、`comment_repo`、`db` 与两个集成测试改为引用新包。 `db` 包不再 import `worker`。 去重键仍是 `*string`,因此 NULL 才能共存。 验证:go test -count=1 ./... 全绿;集成测试三包全绿。
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"feedsystem_video_go/internal/auth"
|
||||
"feedsystem_video_go/internal/notification"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
@@ -18,15 +19,15 @@ import (
|
||||
|
||||
type SSEHub struct {
|
||||
mu sync.RWMutex
|
||||
clients map[uint][]chan *Notification
|
||||
clients map[uint][]chan *notification.Notification
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewSSEHub(db *gorm.DB) *SSEHub {
|
||||
return &SSEHub{clients: make(map[uint][]chan *Notification), db: db}
|
||||
return &SSEHub{clients: make(map[uint][]chan *notification.Notification), db: db}
|
||||
}
|
||||
|
||||
func (h *SSEHub) Push(userID uint, n *Notification) {
|
||||
func (h *SSEHub) Push(userID uint, n *notification.Notification) {
|
||||
h.mu.RLock()
|
||||
defer h.mu.RUnlock()
|
||||
chs, ok := h.clients[userID]
|
||||
@@ -41,15 +42,15 @@ func (h *SSEHub) Push(userID uint, n *Notification) {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *SSEHub) Subscribe(userID uint) chan *Notification {
|
||||
ch := make(chan *Notification, 20)
|
||||
func (h *SSEHub) Subscribe(userID uint) chan *notification.Notification {
|
||||
ch := make(chan *notification.Notification, 20)
|
||||
h.mu.Lock()
|
||||
h.clients[userID] = append(h.clients[userID], ch)
|
||||
h.mu.Unlock()
|
||||
return ch
|
||||
}
|
||||
|
||||
func (h *SSEHub) Unsubscribe(userID uint, ch chan *Notification) {
|
||||
func (h *SSEHub) Unsubscribe(userID uint, ch chan *notification.Notification) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
chs := h.clients[userID]
|
||||
@@ -153,7 +154,7 @@ func (h *SSEHub) ListHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var notifications []Notification
|
||||
var notifications []notification.Notification
|
||||
if err := h.db.WithContext(c.Request.Context()).
|
||||
Where("recipient_id = ?", userID).
|
||||
Order("created_at desc").
|
||||
@@ -163,7 +164,7 @@ func (h *SSEHub) ListHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
if notifications == nil {
|
||||
notifications = []Notification{}
|
||||
notifications = []notification.Notification{}
|
||||
}
|
||||
c.JSON(200, gin.H{"notifications": notifications})
|
||||
}
|
||||
@@ -185,9 +186,9 @@ func (h *SSEHub) MarkReadHandler(c *gin.Context) {
|
||||
|
||||
var err error
|
||||
if req.ID != nil {
|
||||
err = h.db.WithContext(c.Request.Context()).Model(&Notification{}).Where("id = ? AND recipient_id = ?", *req.ID, userID).Update("is_read", true).Error
|
||||
err = h.db.WithContext(c.Request.Context()).Model(¬ification.Notification{}).Where("id = ? AND recipient_id = ?", *req.ID, userID).Update("is_read", true).Error
|
||||
} else {
|
||||
err = h.db.WithContext(c.Request.Context()).Model(&Notification{}).Where("recipient_id = ?", userID).Update("is_read", true).Error
|
||||
err = h.db.WithContext(c.Request.Context()).Model(¬ification.Notification{}).Where("recipient_id = ?", userID).Update("is_read", true).Error
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
@@ -204,7 +205,7 @@ func (h *SSEHub) UnreadCountHandler(c *gin.Context) {
|
||||
}
|
||||
|
||||
var count int64
|
||||
if err := h.db.WithContext(c.Request.Context()).Model(&Notification{}).Where("recipient_id = ? AND is_read = ?", userID, false).Count(&count).Error; err != nil {
|
||||
if err := h.db.WithContext(c.Request.Context()).Model(¬ification.Notification{}).Where("recipient_id = ? AND is_read = ?", userID, false).Count(&count).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user