From 76893f33f777cbade6ed141c746401d0a00461df Mon Sep 17 00:00:00 2001 From: derekwin Date: Tue, 21 Feb 2023 15:13:18 +0800 Subject: [PATCH 1/2] add GetIsFavoriteMap --- kitex_server/interaction.go | 15 +++++++++++++-- kitex_server/videoservice.go | 19 ++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/kitex_server/interaction.go b/kitex_server/interaction.go index 909fe90..69f098b 100644 --- a/kitex_server/interaction.go +++ b/kitex_server/interaction.go @@ -2,11 +2,11 @@ package kitex_server import ( "context" + "sync" + "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" @@ -160,6 +160,17 @@ func GetVideosFavoriteCountMap(idSet []int64, respVideosFavoriteCountMap chan ma func GetIsFavoriteMap() (idSet []int64, currentUser int64, respIsFavoriteMap chan map[int64]bool, wg *sync.WaitGroup, errChan chan error) { defer wg.Done() + res, err := FavoriteClient.FavoriteRelationsMethod(context.Background(), &favorite.FavoriteRelationsReq{ + UserId: currentUser, + VideoIdList: idSet, + }) + if err != nil { + respIsFavoriteMap <- map[int64]bool{} + errChan <- err + return + } + respIsFavoriteMap <- res.IsFavorites + errChan <- nil // res, err := FavoriteClient.FavoriteRelationMethod(context.Background(), &favorite.FavoriteRelationReq{}) return } diff --git a/kitex_server/videoservice.go b/kitex_server/videoservice.go index e7cd75d..ae3b046 100755 --- a/kitex_server/videoservice.go +++ b/kitex_server/videoservice.go @@ -62,7 +62,12 @@ func GetVideoLatestMap(idSet []int64, currentUser int64, respVideoMap chan map[i // 批量查询视频的评论数,传入视频id的切片,返回对应的评论数(需携带对应视频id),从comment服务 // 批量查询 is_favorite, 传入目标视频id切片和currentUser查is_favorite的切片(结果需要携带视频id,douyin里后续需要转成map):从favorite; - GetIsFavoriteMap() + respIsFavoriteMap := make(chan map[int64]int64, 1) + defer close(respIsFavoriteMap) + respIsFavoriteMapError := make(chan error, 1) + defer close(respIsFavoriteMapError) + wgVideo.Add(1) + go GetIsFavoriteMap(idSet, currentUser, respIsFavoriteMap, wgVideo, respIsFavoriteMapError) // 等待数据 wgVideo.Wait() @@ -104,6 +109,10 @@ func GetFeed(latestTime int64, currentUserId int64, limit int32) (resultList []* for index, video := range r.VideoList { authorIdSet[index] = video.AuthorId } + videoIdSet := make([]int64, len(r.VideoList)) + for index, video := range r.VideoList { + videoIdSet[index] = video.Id + } wg := &sync.WaitGroup{} @@ -123,7 +132,7 @@ func GetFeed(latestTime int64, currentUserId int64, limit int32) (resultList []* respLatestVideoMapError := make(chan []error, 1) defer close(respLatestVideoMapError) wg.Add(1) - go GetVideoLatestMap(authorIdSet, currentUserId, respLatestVideoMap, wg, respLatestVideoMapError) + go GetVideoLatestMap(videoIdSet, currentUserId, respLatestVideoMap, wg, respLatestVideoMapError) // 等待数据 wg.Wait() @@ -181,6 +190,10 @@ func GetVideosByAuthorId(id int64) (resultList []*core.Video, err error) { if r.Status { authorIdSet := []int64{id} // 只有作者本人 + videoIdSet := make([]int64, len(r.VideoList)) + for index, video := range r.VideoList { + videoIdSet[index] = video.Id + } wg := &sync.WaitGroup{} @@ -198,7 +211,7 @@ func GetVideosByAuthorId(id int64) (resultList []*core.Video, err error) { respLatestVideoMapError := make(chan []error, 1) defer close(respLatestVideoMapError) wg.Add(1) - go GetVideoLatestMap(authorIdSet, id, respLatestVideoMap, wg, respLatestVideoMapError) + go GetVideoLatestMap(videoIdSet, id, respLatestVideoMap, wg, respLatestVideoMapError) // 等待数据 wg.Wait() From ab078107458e0299ffc002de53282c640850cef5 Mon Sep 17 00:00:00 2001 From: derekwin Date: Tue, 21 Feb 2023 16:27:15 +0800 Subject: [PATCH 2/2] =?UTF-8?q?add=20IsFavorite=E5=8D=8F=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- biz/handler/core/feed_server.go | 2 +- biz/handler/core/login_server.go | 15 +++++++++++++-- biz/handler/core/register_server.go | 19 +++++++++++++++---- kitex_server/interaction.go | 7 ++----- kitex_server/relationservice.go | 11 ++++++----- kitex_server/userservice.go | 4 ++-- kitex_server/videoservice.go | 16 +++++++++++----- 7 files changed, 50 insertions(+), 24 deletions(-) diff --git a/biz/handler/core/feed_server.go b/biz/handler/core/feed_server.go index cabf58c..7721923 100755 --- a/biz/handler/core/feed_server.go +++ b/biz/handler/core/feed_server.go @@ -24,7 +24,7 @@ func FeedMethod(ctx context.Context, c *app.RequestContext) { return } - // TODO : 记录ip地址和注册api调用次数,限制统一设备短时间太多的请求,预防爬虫。 + // TODO : 记录ip地址和注册api调用次数,限制统一设备短时间太多的请求,预防爬虫。 redis msgsucceed := "获取视频流成功" msgFailed := "获取视频流失败" diff --git a/biz/handler/core/login_server.go b/biz/handler/core/login_server.go index b69b443..ba42ce5 100755 --- a/biz/handler/core/login_server.go +++ b/biz/handler/core/login_server.go @@ -8,6 +8,7 @@ import ( core "github.com/ClubWeGo/douyin/biz/model/core" "github.com/ClubWeGo/douyin/kitex_server" "github.com/ClubWeGo/douyin/tools" + "github.com/ClubWeGo/douyin/tools/safe" "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/protocol/consts" ) @@ -23,11 +24,21 @@ func LoginMethod(ctx context.Context, c *app.RequestContext) { return } + resp := new(core.LoginResp) + + // 虽然gorm提供了sql防注入,但是这里在业务层再加一次字段过滤可以尽早阻止非法请求 + err = safe.SqlInjectCheck(req.Username) + if err != nil { + msg := "非法字段" + resp.StatusCode = 1 + resp.StatusMsg = &msg + c.JSON(consts.StatusOK, resp) + return + } + msgsucceed := "登录成功" msgFailed := "登录失败" - resp := new(core.LoginResp) - userid, err := kitex_server.LoginUser(req.Username, req.Password) if err != nil { resp.StatusCode = 1 diff --git a/biz/handler/core/register_server.go b/biz/handler/core/register_server.go index df9c107..6deee27 100755 --- a/biz/handler/core/register_server.go +++ b/biz/handler/core/register_server.go @@ -9,6 +9,7 @@ import ( "github.com/ClubWeGo/douyin/kitex_server" "github.com/ClubWeGo/douyin/minio_server" "github.com/ClubWeGo/douyin/tools" + "github.com/ClubWeGo/douyin/tools/safe" "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/protocol/consts" ) @@ -24,14 +25,24 @@ func RegisterMethod(ctx context.Context, c *app.RequestContext) { return } - // TODO : 缓存记录ip地址和注册api调用次数,限制统一设备短时间太多的注册,预防黑灰产。 - // TODO : 注册字段过滤 : 注入,敏感词, + resp := new(core.RegisterResp) + + // TODO : 缓存记录ip地址和注册api调用次数,限制统一设备短时间太多的注册,预防黑灰产。同feed中要求 + + // 注册字段过滤 : 注入,敏感词检查 + // 虽然gorm提供了sql防注入,但是这里在业务层再加一次字段过滤可以尽早阻止非法请求 + err = safe.SqlInjectCheck(req.Username) + if err != nil { + msg := "非法字段" + resp.StatusCode = 1 + resp.StatusMsg = &msg + c.JSON(consts.StatusOK, resp) + return + } msgsucceed := "注册成功" msgFailed := "注册失败" - resp := new(core.RegisterResp) - // 题目要求的基础注册功能 // userid, err := kitex_server.RegisterUser(req.Username, *req.Password) // 附带个人设置的注册功能 diff --git a/kitex_server/interaction.go b/kitex_server/interaction.go index 117032a..71a9dbb 100644 --- a/kitex_server/interaction.go +++ b/kitex_server/interaction.go @@ -134,7 +134,6 @@ func CountUserFavorite(ctx context.Context, uid int64) (int64, int64, error) { return res.FavoriteCount, res.FavoritedCount, nil } - // 传入userId切片,批量查询user对应的favorite, total_favorited // map[int64][]int64 [FavoriteCount FavoritedCount] func GetUsersFavoriteCountMap(idSet []int64, respUsersFavoriteCountMap chan map[int64][]int64, wg *sync.WaitGroup, errChan chan error) { @@ -170,7 +169,7 @@ func GetVideosFavoriteCountMap(idSet []int64, respVideosFavoriteCountMap chan ma } // 传入videoId切片和当前用户id,批量查询喜欢情况 -func GetIsFavoriteMap() (idSet []int64, currentUser int64, respIsFavoriteMap chan map[int64]bool, wg *sync.WaitGroup, errChan chan error) { +func GetIsFavoriteMap(idSet []int64, currentUser int64, respIsFavoriteMap chan map[int64]bool, wg *sync.WaitGroup, errChan chan error) { defer wg.Done() res, err := FavoriteClient.FavoriteRelationsMethod(context.Background(), &favorite.FavoriteRelationsReq{ @@ -182,8 +181,6 @@ func GetIsFavoriteMap() (idSet []int64, currentUser int64, respIsFavoriteMap cha errChan <- err return } - respIsFavoriteMap <- res.IsFavorites + respIsFavoriteMap <- res.IsFavoriteMap errChan <- nil - // res, err := FavoriteClient.FavoriteRelationMethod(context.Background(), &favorite.FavoriteRelationReq{}) - return } diff --git a/kitex_server/relationservice.go b/kitex_server/relationservice.go index 66f32d2..7c4c759 100755 --- a/kitex_server/relationservice.go +++ b/kitex_server/relationservice.go @@ -6,11 +6,12 @@ import ( "fmt" "sync" + "strconv" + "github.com/ClubWeGo/douyin/biz/model/core" "github.com/ClubWeGo/douyin/biz/model/relation" relationserver "github.com/ClubWeGo/relationmicro/kitex_gen/relation" "github.com/prometheus/common/log" - "strconv" ) // 响应码 @@ -190,10 +191,10 @@ func VerifyFollowParam(myUid int64, targetUid int64, actionType int32) *string { return nil } - // TODO : .GetIsFollowMapByUserIdSet func GetIsFollowMapByUserIdSet(uid int64, idSet []int64) (isFollowMap map[int64]bool, err error) { return nil, nil +} // 协程接口 @@ -236,7 +237,7 @@ func GetRelationMap(idSet []int64, currentUser int64, respRelationMap chan map[i }(id) } wgRelation.Wait() - for range idSet { + for range idSet { // 检查协程是否出错,出错直接按请求失败处理 err := <-insideErrChan if err != nil { respRelationMap <- map[int64]FollowInfoWithId{} @@ -251,8 +252,8 @@ func GetRelationMap(idSet []int64, currentUser int64, respRelationMap chan map[i result[data.Id] = data } - respRelationMap <- result //没有显式出错,但是没有值 - errChan <- errors.New("relation server GetFollowInfoMethod error: 微服务调用成功,但是没有查到值") + respRelationMap <- result // 返回查询结构 + errChan <- nil } // kitex relationserver 数据传输 user -> kitex 回显 core.User diff --git a/kitex_server/userservice.go b/kitex_server/userservice.go index c7aa569..22b8527 100755 --- a/kitex_server/userservice.go +++ b/kitex_server/userservice.go @@ -100,9 +100,9 @@ func GetUserLatestMap(idSet []int64, currentUser int64, respUserMap chan map[int wgUser.Add(1) go GetRelationMap(idSet, currentUser, respRelationMap, wgUser, respRelationMapError) - // 批量查询TotalFavourited, FavoriteCount,传入查询的userId切片 + // 批量查询TotalFavourited, FavoriteCount,传入查询的userId切片 从favorite服务 respUsersFavoriteCountMap := make(chan map[int64][]int64, 1) // [FavoriteCount FavoritedCount] - defer close(respRelationMap) + defer close(respUsersFavoriteCountMap) respUsersFavoriteCountMapError := make(chan error, 1) defer close(respUsersFavoriteCountMapError) wgUser.Add(1) diff --git a/kitex_server/videoservice.go b/kitex_server/videoservice.go index ae3b046..f4459d0 100755 --- a/kitex_server/videoservice.go +++ b/kitex_server/videoservice.go @@ -59,10 +59,11 @@ func GetVideoLatestMap(idSet []int64, currentUser int64, respVideoMap chan map[i wgVideo.Add(1) go GetVideosFavoriteCountMap(idSet, respVideosFavoriteCountMap, wgVideo, respVideosFavoriteCountMapError) + // TODO: @weitao // 批量查询视频的评论数,传入视频id的切片,返回对应的评论数(需携带对应视频id),从comment服务 // 批量查询 is_favorite, 传入目标视频id切片和currentUser查is_favorite的切片(结果需要携带视频id,douyin里后续需要转成map):从favorite; - respIsFavoriteMap := make(chan map[int64]int64, 1) + respIsFavoriteMap := make(chan map[int64]bool, 1) defer close(respIsFavoriteMap) respIsFavoriteMapError := make(chan error, 1) defer close(respIsFavoriteMapError) @@ -79,6 +80,12 @@ func GetVideoLatestMap(idSet []int64, currentUser int64, respVideoMap chan map[i errSlice = append(errSlice, err) } + IsFavoriteMap := <-respIsFavoriteMap + err = <-respIsFavoriteMapError + if err != nil { + errSlice = append(errSlice, err) + } + errChan <- errSlice // 记录错误的切片,至少应该返回一个空切片,否则chan会阻塞 // 更新数据 @@ -87,7 +94,7 @@ func GetVideoLatestMap(idSet []int64, currentUser int64, respVideoMap chan map[i videoLatestMap[id] = core.Video{ // 视频id对应的Video存储查到的关键字段 FavoriteCount: VideosFavoriteCountMap[id], // TODO:从拿到的MAP数据更新 CommentCount: 0, // TODO:从拿到的MAP数据更新 - IsFavorite: false, // TODO:从拿到的MAP数据更新 + IsFavorite: IsFavoriteMap[id], // TODO:从拿到的MAP数据更新 } } respVideoMap <- videoLatestMap // 返回数据 @@ -140,7 +147,6 @@ func GetFeed(latestTime int64, currentUserId int64, limit int32) (resultList []* // 处理协程错误 AuthorMap := <-respLatestAuthorMap errSlice := <-respLatestAuthorMapError - for _, errItem := range errSlice { if errItem != nil { return []*core.Video{}, 0, errItem @@ -174,7 +180,7 @@ func GetFeed(latestTime int64, currentUserId int64, limit int32) (resultList []* Title: video.Title, } } - return resultList, *r.NextTime, nil + return resultList, *r.NextTime, nil // 成功 } return []*core.Video{}, 0, errors.New("向kitex请求feed失败") } @@ -239,7 +245,7 @@ func GetVideosByAuthorId(id int64) (resultList []*core.Video, err error) { resultList = make([]*core.Video, len(r.VideoList)) for index, video := range r.VideoList { // TODO:没有查询到的错误处理 - author := AuthorMap[video.AuthorId] + author := AuthorMap[video.AuthorId] // 只有作者本人,查map其实无所谓 // TODO:设置机制,慢速同步其他服务的最新数据到user服务的主表,video的主表 resultList[index] = &core.Video{