ZBLOG

// 视频流 func Feed(ctx *gin.Context) { var tokenId int64 // current 表示获取当前截止时间之前的视频 currentTime, err := strconv.ParseInt(ctx.Query("latest_time"), 10, 64) if err != nil || currentTime == int64(0) { currentTime = utils.GetCurrentTime() } // t...

这段代码是一个名为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助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=10605

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?