Files
VLoop/backend/internal/social/service.go
huanghaosheng f658b7e76e 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/... 全绿。
2026-11-11 16:18:49 +08:00

141 lines
4.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package social
import (
"context"
"errors"
"feedsystem_video_go/internal/account"
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 socialStore
accountrepo accountFinder
socialMQ followNotifier
cache *rediscache.Client
}
func NewSocialService(repo socialStore, accountrepo accountFinder, socialMQ followNotifier, cache *rediscache.Client) *SocialService {
return &SocialService{repo: repo, accountrepo: accountrepo, socialMQ: socialMQ, cache: cache}
}
// Follow 返回本次是否真的插入关系:重复关注返回 (false, nil)。
func (s *SocialService) Follow(ctx context.Context, social *Social) (bool, error) {
if social.FollowerID == social.VloggerID {
return false, ErrCannotFollowSelf
}
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 false, err
}
if !created {
// 关系已在,缓存和通知都已经是“已关注”状态。
return false, nil
}
s.invalidateFollowingFeedCache(context.Background(), social.FollowerID)
if err := s.socialMQ.Follow(ctx, social.FollowerID, social.VloggerID); err != nil {
log.Printf("social MQ Follow 发布失败: %v", err)
}
return true, nil
}
// 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
}
if _, err := s.accountrepo.FindByID(ctx, social.VloggerID); err != nil {
return false, 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)
return true, nil
}
// 失效关注列表缓存,确保下次查询时能获取到最新的关注列表
func (s *SocialService) invalidateFollowingFeedCache(ctx context.Context, accountID uint) {
if s.cache == nil {
return
}
pattern := s.cache.Key("feed:listByFollowing:*:accountID=%d:*", accountID)
if err := s.cache.DelByPattern(ctx, pattern); err != nil {
log.Printf("失效 Following 缓存失败: accountID=%d, err=%v", accountID, err)
}
}
func (s *SocialService) GetAllFollowers(ctx context.Context, VloggerID uint) ([]*account.Account, error) {
_, err := s.accountrepo.FindByID(ctx, VloggerID)
if err != nil {
return nil, err
}
return s.repo.GetAllFollowers(ctx, VloggerID)
}
func (s *SocialService) GetAllVloggers(ctx context.Context, FollowerID uint) ([]*account.Account, error) {
_, err := s.accountrepo.FindByID(ctx, FollowerID)
if err != nil {
return nil, err
}
return s.repo.GetAllVloggers(ctx, FollowerID)
}
func (s *SocialService) CountFollowers(ctx context.Context, vloggerID uint) (int64, error) {
return s.repo.CountFollowers(ctx, vloggerID)
}
func (s *SocialService) CountVloggers(ctx context.Context, followerID uint) (int64, error) {
return s.repo.CountVloggers(ctx, followerID)
}
func (s *SocialService) IsFollowed(ctx context.Context, social *Social) (bool, error) {
_, err := s.accountrepo.FindByID(ctx, social.FollowerID)
if err != nil {
return false, err
}
_, err = s.accountrepo.FindByID(ctx, social.VloggerID)
if err != nil {
return false, err
}
return s.repo.IsFollowed(ctx, social)
}