feat(P2): SSE 实时消息通知 — 点赞/评论/关注事件推送 + Notification 表

This commit is contained in:
Sisyphus
2026-04-25 20:02:27 +08:00
parent ec56c73016
commit 41454de49f
3 changed files with 356 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
package http
import (
"context"
"feedsystem_video_go/internal/account"
"feedsystem_video_go/internal/feed"
"feedsystem_video_go/internal/middleware/jwt"
@@ -179,5 +180,45 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client, rmq *rabbitmq.RabbitMQ) *g
}
worker.StartOutboxPoller(db, timelineMQ)
worker.StartConsumer(timelineMQ, "video.timeline.update.queue", cache)
// SSE notification
if rmq != nil && rmq.Ch != nil {
rmq.DeclareTopic("like.events", "notification.like", "like.like")
rmq.DeclareTopic("comment.events", "notification.comment", "comment.publish")
rmq.DeclareTopic("social.events", "notification.social", "social.follow")
}
sseHub := worker.NewSSEHub(db)
notifGroup := r.Group("/notification")
notifGroup.Use(sseHub.SSERequireAuth())
sseHub.RegisterRoutes(r, notifGroup)
go func() {
if rmq != nil && rmq.Ch != nil {
hub := sseHub
ctx := context.Background()
// consume from like queue
go func() {
w := worker.NewNotificationWorker(rmq.Ch, db, "notification.like", hub)
if err := w.Run(ctx); err != nil {
log.Printf("notification-like worker: %v", err)
}
}()
go func() {
w := worker.NewNotificationWorker(rmq.Ch, db, "notification.comment", hub)
if err := w.Run(ctx); err != nil {
log.Printf("notification-comment worker: %v", err)
}
}()
go func() {
w := worker.NewNotificationWorker(rmq.Ch, db, "notification.social", hub)
if err := w.Run(ctx); err != nil {
log.Printf("notification-social worker: %v", err)
}
}()
} else {
log.Printf("Notification SSE disabled (MQ not available)")
}
}()
return r
}