feat: 为Follow和UnFollow加上MQ

This commit is contained in:
Leon
2025-12-29 04:13:53 +08:00
parent 4225b026d7
commit 7200b55ca2
2 changed files with 19 additions and 4 deletions

View File

@@ -4,15 +4,17 @@ import (
"feedsystem_video_go/internal/account" "feedsystem_video_go/internal/account"
"feedsystem_video_go/internal/feed" "feedsystem_video_go/internal/feed"
"feedsystem_video_go/internal/middleware/jwt" "feedsystem_video_go/internal/middleware/jwt"
"feedsystem_video_go/internal/middleware/rabbitmq"
rediscache "feedsystem_video_go/internal/middleware/redis" rediscache "feedsystem_video_go/internal/middleware/redis"
"feedsystem_video_go/internal/social" "feedsystem_video_go/internal/social"
"feedsystem_video_go/internal/video" "feedsystem_video_go/internal/video"
"log"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gorm.io/gorm" "gorm.io/gorm"
) )
func SetRouter(db *gorm.DB, cache *rediscache.Client) *gin.Engine { func SetRouter(db *gorm.DB, cache *rediscache.Client, rmq *rabbitmq.RabbitMQ) *gin.Engine {
r := gin.Default() r := gin.Default()
r.Static("/static", "./.run/uploads") r.Static("/static", "./.run/uploads")
// account // account
@@ -77,8 +79,13 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client) *gin.Engine {
protectedCommentGroup.POST("/delete", commentHandler.DeleteComment) protectedCommentGroup.POST("/delete", commentHandler.DeleteComment)
} }
// social // social
socialMQ, err := rabbitmq.NewSocialMQ(rmq)
if err != nil {
log.Printf("SocialMQ init failed (mq disabled): %v", err)
socialMQ = nil
}
socialRepository := social.NewSocialRepository(db) socialRepository := social.NewSocialRepository(db)
socialService := social.NewSocialService(socialRepository, accountRepository) socialService := social.NewSocialService(socialRepository, accountRepository, socialMQ)
socialHandler := social.NewSocialHandler(socialService) socialHandler := social.NewSocialHandler(socialService)
socialGroup := r.Group("/social") socialGroup := r.Group("/social")
protectedSocialGroup := socialGroup.Group("") protectedSocialGroup := socialGroup.Group("")

View File

@@ -4,15 +4,17 @@ import (
"context" "context"
"errors" "errors"
"feedsystem_video_go/internal/account" "feedsystem_video_go/internal/account"
"feedsystem_video_go/internal/middleware/rabbitmq"
) )
type SocialService struct { type SocialService struct {
repo *SocialRepository repo *SocialRepository
accountrepo *account.AccountRepository accountrepo *account.AccountRepository
socialMQ *rabbitmq.SocialMQ
} }
func NewSocialService(repo *SocialRepository, accountrepo *account.AccountRepository) *SocialService { func NewSocialService(repo *SocialRepository, accountrepo *account.AccountRepository, socialMQ *rabbitmq.SocialMQ) *SocialService {
return &SocialService{repo: repo, accountrepo: accountrepo} return &SocialService{repo: repo, accountrepo: accountrepo, socialMQ: socialMQ}
} }
func (s *SocialService) Follow(ctx context.Context, social *Social) error { func (s *SocialService) Follow(ctx context.Context, social *Social) error {
@@ -34,6 +36,9 @@ func (s *SocialService) Follow(ctx context.Context, social *Social) error {
if isFollowed { if isFollowed {
return errors.New("already followed") return errors.New("already followed")
} }
if s.socialMQ != nil {
s.socialMQ.Follow(ctx, social.FollowerID, social.VloggerID)
}
return s.repo.Follow(ctx, social) return s.repo.Follow(ctx, social)
} }
@@ -53,6 +58,9 @@ func (s *SocialService) Unfollow(ctx context.Context, social *Social) error {
if !isFollowed { if !isFollowed {
return errors.New("not followed") return errors.New("not followed")
} }
if s.socialMQ != nil {
s.socialMQ.UnFollow(ctx, social.FollowerID, social.VloggerID)
}
return s.repo.Unfollow(ctx, social) return s.repo.Unfollow(ctx, social)
} }