这段代码是一个名为Feed的函数,用于处理视频流的请求。以下是对每行代码进行详细注释:
func Feed(ctx *gin.Context) {
var tokenId int64
currentTime, err := strconv.ParseInt(ctx.Query("latest_time"), 10, 64)
if err != nil || currentTime == int64(0) {
currentTime = utils.GetCurrentTime()
}
userId, _ := ctx.Get("UserId")
tokenId = userId.(int64)
feedListResponse, err := utils.GetVideoSvrClient().GetFeedList(ctx, &pb.GetFeedListRequest{
CurrentTime: currentTime,
TokenUserId: tokenId,
})
var resp = &DouyinFeedResponse{VideoList: make([]*Video, 0), NextTime: feedListResponse.NextTime}
var userIdList = make([]int64, 0)
var followUintList = make([]*pb.FollowUint, 0)
var favoriteUnitList = make([]*pb.FavoriteUnit, 0)
// 遍历视频列表,获取作者ID并构建相应的请求结构体
for _, video := range feedListResponse.VideoList {
userIdList = append(userIdList, video.AuthorId)
followUintList = append(followUintList, &pb.FollowUint{
SelfUserId: tokenId,
UserIdList: video.AuthorId,
})
favoriteUnitList = append(favoriteUnitList, &pb.FavoriteUnit{
UserId: tokenId,
VideoId: video.Id,
})
}
// 调用远程方法获取视频作者信息(一次性)
getUserInfoRsp, err := utils.GetUserSvrClient().GetUserInfoDict(ctx, &pb.GetUserInfoDictRequest{
UserIdList: userIdList,
})
if err != nil {
log.Errorf("GetUserSvrClient GetUserInfoDict err %v", err.Error())
response.Fail(ctx, fmt.Sprintf("GetUserSvrClient GetUserInfoDict err %v", err.Error()), nil)
return
}
isFollowedRsp, err := utils.GetRelationSvrClient().IsFollowDict(ctx, &pb.IsFollowDictReq{
FollowUintList: followUintList,
})
if err != nil {
log.Errorf("GetRelationSvrClient IsFollowDict err %v", err.Error())
response.Fail(ctx, fmt.Sprintf("GetRelationSvrClient IsFollowDict err %v", err.Error()), nil)
return
}
isFavoriteVideoRsp, err := utils.GetFavoriteSvrClient().IsFavoriteVideoDict(ctx, &pb.IsFavoriteVideoDictReq{
FavoriteUnitList: favoriteUnitList,
})
log.Infof("IsFavoriteVideoDict isFavoriteVideoRsp %+v", isFavoriteVideoRsp)
if err != nil {
log.Errorf("GetFavoriteSvrClient IsFavoriteVideoDict err %v", err.Error())
response.Fail(ctx, fmt.Sprintf("GetFavoriteSvrClient IsFavoriteVideoDict err %v", err.Error()), nil)
return
}
for _, video := range feedListResponse.VideoList {
videoRsp := &Video{
Id: video.Id,
PlayUrl: video.PlayUrl,
CoverUrl: video.CoverUrl,
FavoriteCount: video.FavoriteCount,
CommentCount: video.CommentCount,
IsFavorite: video.IsFavorite,
Title: video.Title,
}
videoRsp.Author = getUserInfoRsp.UserInfoDict[video.AuthorId]
var followUint = strconv.FormatInt(tokenId, 10) + "_" + strconv.FormatInt(videoRsp.Author.Id, 10)
videoRsp.Author.IsFollow = isFollowedRsp.IsFollowDict[followUint]
var favoriteUint = strconv.FormatInt(tokenId, 10) + "_" + strconv.FormatInt(videoRsp.Id, 10)
videoRsp.IsFavorite = isFavoriteVideoRsp.IsFavoriteDict[favoriteUint]
resp.VideoList = append(resp.VideoList, videoRsp)
}
response.Success(ctx, "success", resp)
}
请注意,这只是对代码的注释解释,并不包含完整的函数依赖关系和其他相关信息。如果你有进一步的问题,请继续提问。
内容由零声教学AI助手提供,问题来源于学员提问




