2026-05-20 16:34:19 +08:00
|
|
|
|
package social
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
"feedsystem_video_go/internal/account"
|
2026-05-23 09:23:47 +08:00
|
|
|
|
rediscache "feedsystem_video_go/internal/middleware/redis"
|
|
|
|
|
|
"log"
|
2026-05-20 16:34:19 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2026-11-11 16:18:49 +08:00
|
|
|
|
// 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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-20 16:34:19 +08:00
|
|
|
|
type SocialService struct {
|
2026-11-11 16:18:49 +08:00
|
|
|
|
repo socialStore
|
|
|
|
|
|
accountrepo accountFinder
|
|
|
|
|
|
socialMQ followNotifier
|
2026-05-23 09:23:47 +08:00
|
|
|
|
cache *rediscache.Client
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-11 16:18:49 +08:00
|
|
|
|
func NewSocialService(repo socialStore, accountrepo accountFinder, socialMQ followNotifier, cache *rediscache.Client) *SocialService {
|
2026-05-23 09:23:47 +08:00
|
|
|
|
return &SocialService{repo: repo, accountrepo: accountrepo, socialMQ: socialMQ, cache: cache}
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-11 16:18:49 +08:00
|
|
|
|
// Follow 返回本次是否真的插入关系:重复关注返回 (false, nil)。
|
|
|
|
|
|
func (s *SocialService) Follow(ctx context.Context, social *Social) (bool, error) {
|
2026-05-20 16:34:19 +08:00
|
|
|
|
if social.FollowerID == social.VloggerID {
|
2026-11-11 16:18:49 +08:00
|
|
|
|
return false, ErrCannotFollowSelf
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
2026-11-11 16:18:49 +08:00
|
|
|
|
if _, err := s.accountrepo.FindByID(ctx, social.FollowerID); err != nil {
|
|
|
|
|
|
return false, err
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
2026-11-11 16:18:49 +08:00
|
|
|
|
if _, err := s.accountrepo.FindByID(ctx, social.VloggerID); err != nil {
|
|
|
|
|
|
return false, err
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
2026-05-23 09:23:47 +08:00
|
|
|
|
|
2026-11-11 16:18:49 +08:00
|
|
|
|
created, err := s.repo.Follow(ctx, social)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return false, err
|
|
|
|
|
|
}
|
|
|
|
|
|
if !created {
|
|
|
|
|
|
// 关系已在,缓存和通知都已经是“已关注”状态。
|
|
|
|
|
|
return false, nil
|
2026-05-23 09:23:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
s.invalidateFollowingFeedCache(context.Background(), social.FollowerID)
|
2026-11-11 16:18:49 +08:00
|
|
|
|
if err := s.socialMQ.Follow(ctx, social.FollowerID, social.VloggerID); err != nil {
|
|
|
|
|
|
log.Printf("social MQ Follow 发布失败: %v", err)
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
2026-11-11 16:18:49 +08:00
|
|
|
|
return true, nil
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-11 16:18:49 +08:00
|
|
|
|
// 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
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
2026-11-11 16:18:49 +08:00
|
|
|
|
if _, err := s.accountrepo.FindByID(ctx, social.VloggerID); err != nil {
|
|
|
|
|
|
return false, err
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
2026-11-11 16:18:49 +08:00
|
|
|
|
|
|
|
|
|
|
deleted, err := s.repo.Unfollow(ctx, social)
|
2026-05-20 16:34:19 +08:00
|
|
|
|
if err != nil {
|
2026-11-11 16:18:49 +08:00
|
|
|
|
return false, err
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
2026-11-11 16:18:49 +08:00
|
|
|
|
if !deleted {
|
|
|
|
|
|
return false, nil
|
2026-05-23 09:23:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
s.invalidateFollowingFeedCache(context.Background(), social.FollowerID)
|
2026-11-11 16:18:49 +08:00
|
|
|
|
return true, nil
|
2026-05-23 09:23:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-15 11:54:01 +08:00
|
|
|
|
// 失效关注列表缓存,确保下次查询时能获取到最新的关注列表
|
2026-05-23 09:23:47 +08:00
|
|
|
|
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)
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
}
|