fix(social): 关注与取关改为幂等写入

SocialRepository.Follow 和 Unfollow 返回本次是否真的写入。判重交给唯一索引 idx_social_follower_vlogger。

因此重复关注和重复取关返回 200。重复请求不再落成 500。

缓存失效与通知只在真的变更时发生。自关注返回 400。响应体新增 created 与 deleted 字段。

SocialWorker 的调用只适配新签名,职责不变。删除它由后续提交完成。

验证:go build ./... 通过,go test -count=1 ./internal/social/... ./internal/worker/... 全绿。
This commit is contained in:
2026-11-11 16:18:49 +08:00
parent c73c8861db
commit f658b7e76e
7 changed files with 463 additions and 71 deletions

View File

@@ -4,91 +4,92 @@ import (
"context"
"errors"
"feedsystem_video_go/internal/account"
"feedsystem_video_go/internal/middleware/rabbitmq"
rediscache "feedsystem_video_go/internal/middleware/redis"
"log"
)
// ErrCannotFollowSelf 是客户端错误:自关注不写库,也不发消息。
var ErrCannotFollowSelf = errors.New("can not follow self")
// socialStore 只声明 SocialService 用到的方法,测试里用 fake 驱动。
type socialStore interface {
Follow(ctx context.Context, social *Social) (bool, error)
Unfollow(ctx context.Context, social *Social) (bool, error)
IsFollowed(ctx context.Context, social *Social) (bool, error)
GetAllFollowers(ctx context.Context, vloggerID uint) ([]*account.Account, error)
GetAllVloggers(ctx context.Context, followerID uint) ([]*account.Account, error)
CountFollowers(ctx context.Context, vloggerID uint) (int64, error)
CountVloggers(ctx context.Context, followerID uint) (int64, error)
}
type accountFinder interface {
FindByID(ctx context.Context, id uint) (*account.Account, error)
}
// followNotifier 只暴露关注通知。真实实现 *rabbitmq.SocialMQ 在 nil 接收者上返回 error,
// 所以 MQ 未启用时这里只会多一条日志。
type followNotifier interface {
Follow(ctx context.Context, followerID, vloggerID uint) error
}
type SocialService struct {
repo *SocialRepository
accountrepo *account.AccountRepository
socialMQ *rabbitmq.SocialMQ
repo socialStore
accountrepo accountFinder
socialMQ followNotifier
cache *rediscache.Client
}
func NewSocialService(repo *SocialRepository, accountrepo *account.AccountRepository, socialMQ *rabbitmq.SocialMQ, cache *rediscache.Client) *SocialService {
func NewSocialService(repo socialStore, accountrepo accountFinder, socialMQ followNotifier, cache *rediscache.Client) *SocialService {
return &SocialService{repo: repo, accountrepo: accountrepo, socialMQ: socialMQ, cache: cache}
}
func (s *SocialService) Follow(ctx context.Context, social *Social) error {
_, err := s.accountrepo.FindByID(ctx, social.FollowerID)
if err != nil {
return err
}
_, err = s.accountrepo.FindByID(ctx, social.VloggerID)
if err != nil {
return err
}
// Follow 返回本次是否真的插入关系:重复关注返回 (false, nil)。
func (s *SocialService) Follow(ctx context.Context, social *Social) (bool, error) {
if social.FollowerID == social.VloggerID {
return errors.New("can not follow self")
return false, ErrCannotFollowSelf
}
isFollowed, err := s.repo.IsFollowed(ctx, social)
if _, err := s.accountrepo.FindByID(ctx, social.FollowerID); err != nil {
return false, err
}
if _, err := s.accountrepo.FindByID(ctx, social.VloggerID); err != nil {
return false, err
}
created, err := s.repo.Follow(ctx, social)
if err != nil {
return err
return false, err
}
if isFollowed {
return errors.New("already followed")
if !created {
// 关系已在,缓存和通知都已经是“已关注”状态。
return false, nil
}
// 先写 DB,确保数据持久化
if err := s.repo.Follow(ctx, social); err != nil {
return err
}
// DB 成功后,失效该用户的关注列表缓存
s.invalidateFollowingFeedCache(context.Background(), social.FollowerID)
// 最后发 MQ(用于通知),失败只记日志不影响业务
if s.socialMQ != nil {
if err := s.socialMQ.Follow(ctx, social.FollowerID, social.VloggerID); err != nil {
log.Printf("social MQ Follow 发布失败: %v", err)
}
if err := s.socialMQ.Follow(ctx, social.FollowerID, social.VloggerID); err != nil {
log.Printf("social MQ Follow 发布失败: %v", err)
}
return nil
return true, nil
}
func (s *SocialService) Unfollow(ctx context.Context, social *Social) error {
_, err := s.accountrepo.FindByID(ctx, social.FollowerID)
if err != nil {
return err
// Unfollow 返回本次是否真的删掉关系:重复取关返回 (false, nil)。
func (s *SocialService) Unfollow(ctx context.Context, social *Social) (bool, error) {
if _, err := s.accountrepo.FindByID(ctx, social.FollowerID); err != nil {
return false, err
}
_, err = s.accountrepo.FindByID(ctx, social.VloggerID)
if err != nil {
return err
}
isFollowed, err := s.repo.IsFollowed(ctx, social)
if err != nil {
return err
}
if !isFollowed {
return errors.New("not followed")
if _, err := s.accountrepo.FindByID(ctx, social.VloggerID); err != nil {
return false, err
}
// 先写 DB
if err := s.repo.Unfollow(ctx, social); err != nil {
return err
deleted, err := s.repo.Unfollow(ctx, social)
if err != nil {
return false, err
}
if !deleted {
return false, nil
}
// 失效缓存
s.invalidateFollowingFeedCache(context.Background(), social.FollowerID)
// 最后发 MQ
if s.socialMQ != nil {
if err := s.socialMQ.UnFollow(ctx, social.FollowerID, social.VloggerID); err != nil {
log.Printf("social MQ UnFollow 发布失败: %v", err)
}
}
return nil
return true, nil
}
// 失效关注列表缓存,确保下次查询时能获取到最新的关注列表