2025-12-30 01:25:54 +08:00
|
|
|
|
package rabbitmq
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-10-14 14:04:14 +08:00
|
|
|
|
// CommentMQ 通过连接管理器发布评论事件(不再持有长生命周期 channel)。
|
2025-12-30 01:25:54 +08:00
|
|
|
|
type CommentMQ struct {
|
2026-10-14 14:04:14 +08:00
|
|
|
|
mgr *RabbitMQ
|
2025-12-30 01:25:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-28 21:54:35 +08:00
|
|
|
|
// 拓扑常量导出:见 likeMQ.go 中的说明。
|
2025-12-30 01:25:54 +08:00
|
|
|
|
const (
|
2026-10-28 21:54:35 +08:00
|
|
|
|
CommentExchange = "comment.events"
|
|
|
|
|
|
CommentQueue = "comment.events"
|
|
|
|
|
|
CommentBindingKey = "comment.*"
|
2025-12-30 01:25:54 +08:00
|
|
|
|
|
2026-10-28 21:54:35 +08:00
|
|
|
|
CommentPublishRK = "comment.publish"
|
|
|
|
|
|
CommentDeleteRK = "comment.delete"
|
2025-12-30 01:25:54 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type CommentEvent struct {
|
|
|
|
|
|
EventID string `json:"event_id"`
|
|
|
|
|
|
Action string `json:"action"`
|
|
|
|
|
|
CommentID uint `json:"comment_id,omitempty"`
|
|
|
|
|
|
Username string `json:"username,omitempty"`
|
|
|
|
|
|
VideoID uint `json:"video_id,omitempty"`
|
|
|
|
|
|
AuthorID uint `json:"author_id,omitempty"`
|
|
|
|
|
|
Content string `json:"content,omitempty"`
|
|
|
|
|
|
OccurredAt time.Time `json:"occurred_at"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewCommentMQ(base *RabbitMQ) (*CommentMQ, error) {
|
|
|
|
|
|
if base == nil {
|
|
|
|
|
|
return nil, errors.New("rabbitmq base is nil")
|
|
|
|
|
|
}
|
2026-10-28 21:54:35 +08:00
|
|
|
|
base.RegisterTopology(Topology{Exchange: CommentExchange, Queue: CommentQueue, BindingKey: CommentBindingKey})
|
2026-10-14 14:04:14 +08:00
|
|
|
|
return &CommentMQ{mgr: base}, nil
|
2025-12-30 01:25:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *CommentMQ) Publish(ctx context.Context, username string, videoID, authorID uint, content string) error {
|
2026-10-28 21:54:35 +08:00
|
|
|
|
return c.publish(ctx, "publish", CommentPublishRK, CommentEvent{
|
2025-12-30 01:25:54 +08:00
|
|
|
|
Username: username,
|
|
|
|
|
|
VideoID: videoID,
|
|
|
|
|
|
AuthorID: authorID,
|
|
|
|
|
|
Content: content,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *CommentMQ) Delete(ctx context.Context, commentID uint) error {
|
2026-10-28 21:54:35 +08:00
|
|
|
|
return c.publish(ctx, "delete", CommentDeleteRK, CommentEvent{
|
2025-12-30 01:25:54 +08:00
|
|
|
|
CommentID: commentID,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *CommentMQ) publish(ctx context.Context, action, routingKey string, evt CommentEvent) error {
|
2026-10-14 14:04:14 +08:00
|
|
|
|
if c == nil || c.mgr == nil {
|
2025-12-30 01:25:54 +08:00
|
|
|
|
return errors.New("comment mq is not initialized")
|
|
|
|
|
|
}
|
|
|
|
|
|
id, err := newEventID(16)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
evt.EventID = id
|
|
|
|
|
|
evt.Action = action
|
|
|
|
|
|
evt.OccurredAt = time.Now().UTC()
|
2026-10-28 21:54:35 +08:00
|
|
|
|
return c.mgr.PublishJSON(ctx, CommentExchange, routingKey, evt)
|
2025-12-30 01:25:54 +08:00
|
|
|
|
}
|