Skip to content

Commit

Permalink
Merge pull request #5 from ClubWeGo/master
Browse files Browse the repository at this point in the history
merge daley's changes
  • Loading branch information
derekwin authored Feb 21, 2023
2 parents ce89b8c + 30d18e6 commit 0e6f11e
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 9 deletions.
58 changes: 49 additions & 9 deletions kitex_server/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package kitex_server

import (
"context"
"github.com/ClubWeGo/douyin/pack"
"github.com/ClubWeGo/usermicro/kitex_gen/usermicro"
"github.com/ClubWeGo/videomicro/kitex_gen/videomicro"
"sync"


"github.com/ClubWeGo/douyin/biz/model/interaction"
"github.com/ClubWeGo/douyin/tools/errno"
"github.com/ClubWeGo/favoritemicro/kitex_gen/favorite"
Expand Down Expand Up @@ -39,16 +43,52 @@ func DeleteFavorite(ctx context.Context, uid int64, vid int64) (*interaction.Fav
}, nil
}

func GetFavoriteList(ctx context.Context, uid int64) (favorites []*interaction.FavoriteListReq, err error) {
//res, err := FavoriteClient.FavoriteListMethod(ctx, &favorite.FavoriteListReq{
// UserId: uid,
//})
//Videoclient.GetVideoMethod(ctx, &videomicro.GetVideoReq{})
//if err != nil {
// return nil, errno.RPCErr
//}
func GetFavoriteList(ctx context.Context, uid int64) (favorites *interaction.FavoriteListResp, err error) {
favoriteListRes, err := FavoriteClient.FavoriteListMethod(ctx, &favorite.FavoriteListReq{
UserId: uid,
})
if err != nil {
return nil, err
}
favoriteIdList := favoriteListRes.VideoIdList
videosResp, err := Videoclient.GetVideoSetByIdSetMethod(
ctx, &videomicro.GetVideoSetByIdSetReq{
IdSet: favoriteIdList,
})
if err != nil {
return nil, err
}
videos := videosResp.VideoSet
authorIdList := make([]int64, 0)
for _, v := range videos {
authorIdList = append(authorIdList, v.AuthorId)
}

return
if err != nil {
return nil, err
}
resp, err := Userclient.GetUserSetByIdSetMethod(ctx, &usermicro.GetUserSetByIdSetReq{
IdSet: authorIdList,
})
if err != nil {
return nil, errno.RPCErr
}
authors := resp.UserSet
isFavorites := make(map[int64]bool)
//isFollows := make(map[int64]bool)
isFollowSet, err := GetIsFollowSetByUserIdSet(uid, authorIdList)
if err != nil {
return nil, err
}
for _, v := range videos {
isFavorites[v.Id], _ = GetFavoriteRelation(ctx, uid, v.Id)
//isFollows[v.AuthorId] = Get
}
return &interaction.FavoriteListResp{
StatusCode: favoriteListRes.BaseResp.StatusCode,
StatusMsg: favoriteListRes.BaseResp.StatusMsg,
VideoList: pack.Videos(videos, authors, isFavorites, isFollowSet),
}, nil
}

func GetFavoriteRelation(ctx context.Context, uid int64, vid int64) (bool, error) {
Expand Down
24 changes: 24 additions & 0 deletions pack/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pack

import (
"github.com/ClubWeGo/douyin/biz/model/core"
"github.com/ClubWeGo/usermicro/kitex_gen/usermicro"
"strconv"
)

// User 将 usermicro.UserInfo 转换为 core.User,针对每个用户,需要预先把对作者的关注状态传入
func User(user *usermicro.UserInfo, isFollow bool) *core.User {
return &core.User{
ID: user.Id,
Name: user.Name,
FollowCount: user.FollowCount,
FollowerCount: user.FollowerCount,
IsFollow: isFollow,
Avatar: user.Avatar,
BackgroundImage: user.BackgroundImage,
Signature: user.Signature,
TotalFavourited: strconv.FormatInt(user.TotalFavorited, 10),
WorkCount: user.WorkCount,
FavoriteCount: user.FavoriteCount,
}
}
36 changes: 36 additions & 0 deletions pack/video.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package pack

import (
"github.com/ClubWeGo/douyin/biz/model/core"
"github.com/ClubWeGo/usermicro/kitex_gen/usermicro"
"github.com/ClubWeGo/videomicro/kitex_gen/videomicro"
)

// Videos 将 videomicro.Video 列表转换为 core.Video 列表,针对每个用户,需要预先把用户对每个视频id的点赞状态和对作者id的关注状态传入
func Videos(videos []*videomicro.Video, authors []*usermicro.UserInfo, isFavorites map[int64]bool, isFollows map[int64]bool) []*core.Video {
// 一个用户 id 对应一个用户信息,方便后续查询根据视频的作者 id 查询作者信息
authorMap := make(map[int64]*usermicro.UserInfo, len(authors))
for _, a := range authors {
authorMap[a.Id] = a
}

var res []*core.Video
for _, v := range videos {
res = append(res, Video(v, authorMap[v.AuthorId], isFavorites[v.Id], isFollows[v.AuthorId]))
}
return res
}

// Video 将 videomicro.Video 转换为 core.Video,针对每个用户,需要预先把用户对视频的点赞状态和对作者的关注状态传入
func Video(video *videomicro.Video, author *usermicro.UserInfo, isFavorite bool, isFollow bool) *core.Video {
return &core.Video{
ID: video.Id,
Author: User(author, isFollow),
PlayURL: video.PlayUrl,
CoverURL: video.CoverUrl,
FavoriteCount: video.FavoriteCount,
CommentCount: video.CommentCount,
IsFavorite: isFavorite,
Title: video.Title,
}
}

0 comments on commit 0e6f11e

Please sign in to comment.