Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Darley-Wey authored Feb 21, 2023
2 parents d381fcd + 30d18e6 commit e917864
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 25 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ go get github.com/ClubWeGo/relationmicro@latest

go get github.com/ClubWeGo/favoritemicro@latest


# 说明
1. 当前注册用户时,前端并未提供个人参数配置的能力,也没有提供更新用户信息的能力,所以在core.register_server处写死了初始化的用户背景图像和头像。在配置本项目时,请将对应文件名换成minio中存储的文件名。
2 changes: 0 additions & 2 deletions biz/handler/core/feed_server.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions kitex_server/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"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"
Expand Down Expand Up @@ -131,3 +133,46 @@ 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) {
defer wg.Done()

res, err := FavoriteClient.UsersFavoriteCountMethod(context.Background(), &favorite.UsersFavoriteCountReq{
UserIdList: idSet,
})
if err != nil {
respUsersFavoriteCountMap <- map[int64][]int64{}
errChan <- err
return
}
respUsersFavoriteCountMap <- res.FavoriteCountMap
errChan <- nil
}

// 传入videoId切片,批量查询video对应的favorite, favorited
// map[int64]int64 FavoriteCount
func GetVideosFavoriteCountMap(idSet []int64, respVideosFavoriteCountMap chan map[int64]int64, wg *sync.WaitGroup, errChan chan error) {
defer wg.Done()

res, err := FavoriteClient.VideosFavoriteCountMethod(context.Background(), &favorite.VideosFavoriteCountReq{
VideoIdList: idSet,
})
if err != nil {
respVideosFavoriteCountMap <- map[int64]int64{}
errChan <- err
return
}
respVideosFavoriteCountMap <- res.FavoriteCountMap
errChan <- nil
}

// 传入videoId切片和当前用户id,批量查询喜欢情况
func GetIsFavoriteMap() (idSet []int64, currentUser int64, respIsFavoriteMap chan map[int64]bool, wg *sync.WaitGroup, errChan chan error) {
defer wg.Done()

// res, err := FavoriteClient.FavoriteRelationMethod(context.Background(), &favorite.FavoriteRelationReq{})
return
}
62 changes: 62 additions & 0 deletions kitex_server/relationservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"errors"
"fmt"
"sync"

"github.com/ClubWeGo/douyin/biz/model/core"
"github.com/ClubWeGo/douyin/biz/model/relation"
relationserver "github.com/ClubWeGo/relationmicro/kitex_gen/relation"
Expand Down Expand Up @@ -188,9 +190,69 @@ 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

// 协程接口

type FollowInfoWithId struct {
Id int64
followInfo relationserver.FollowInfo
}

// 通过GetFollowInfoMethod批量获取FollowInfo,包装为Map
func GetRelationMap(idSet []int64, currentUser int64, respRelationMap chan map[int64]FollowInfoWithId, wg *sync.WaitGroup, errChan chan error) {
defer wg.Done()

wgRelation := &sync.WaitGroup{}
insideResultChan := make(chan FollowInfoWithId, len(idSet))
insideErrChan := make(chan error, len(idSet))
for _, id := range idSet {
wgRelation.Add(1)
go func(userid int64) {
defer wgRelation.Done()
r, err := Relationclient.GetFollowInfoMethod(context.Background(), &relationserver.GetFollowInfoReq{
MyUid: &currentUser,
TargetUid: userid,
})
if err != nil {
insideResultChan <- FollowInfoWithId{} // 出错,传回一个空值
insideErrChan <- err
return
}
if r.StatusCode == 1 { // 0 error, 1 success, 2 参数不通过
followInfoWithId := FollowInfoWithId{
Id: userid,
followInfo: *r.FollowInfo,
}
insideResultChan <- followInfoWithId
insideErrChan <- nil
return // 成功
}
insideResultChan <- FollowInfoWithId{} //没有显式出错,但是没有值
insideErrChan <- nil
}(id)
}
wgRelation.Wait()
for range idSet {
err := <-insideErrChan
if err != nil {
respRelationMap <- map[int64]FollowInfoWithId{}
errChan <- err
return // 有协程查数据错误,直接报错返回
}
}
// 封装数据
result := make(map[int64]FollowInfoWithId, len(idSet))
for range idSet {
data := <-insideResultChan
result[data.Id] = data
}

respRelationMap <- result //没有显式出错,但是没有值
errChan <- errors.New("relation server GetFollowInfoMethod error: 微服务调用成功,但是没有查到值")
}

// kitex relationserver 数据传输 user -> kitex 回显 core.User
Expand Down
44 changes: 37 additions & 7 deletions kitex_server/userservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package kitex_server
import (
"context"
"errors"
"github.com/prometheus/common/log"
"strconv"
"sync"

"github.com/ClubWeGo/douyin/biz/model/core"
"github.com/ClubWeGo/usermicro/kitex_gen/usermicro"
"github.com/ClubWeGo/videomicro/kitex_gen/videomicro"
"github.com/prometheus/common/log"
)

// 工具函数
Expand Down Expand Up @@ -90,6 +91,23 @@ func GetUserLatestMap(idSet []int64, currentUser int64, respUserMap chan map[int
wgUser.Add(1)
go GetVideoCountMap(idSet, respVideoCountMap, wgUser, respVideoCountMapError)

// 批量查询FollowCount, FollowerCount,Is_follow 从relation服务
// Videoclient.GetVideoCountSetByIdUserSetMethod(context.Background(), &videomicro.GetVideoCountSetByIdUserSetReq{})
respRelationMap := make(chan map[int64]FollowInfoWithId, 1)
defer close(respRelationMap)
respRelationMapError := make(chan error, 1)
defer close(respRelationMapError)
wgUser.Add(1)
go GetRelationMap(idSet, currentUser, respRelationMap, wgUser, respRelationMapError)

// 批量查询TotalFavourited, FavoriteCount,传入查询的userId切片
respUsersFavoriteCountMap := make(chan map[int64][]int64, 1) // [FavoriteCount FavoritedCount]
defer close(respRelationMap)
respUsersFavoriteCountMapError := make(chan error, 1)
defer close(respUsersFavoriteCountMapError)
wgUser.Add(1)
go GetUsersFavoriteCountMap(idSet, respUsersFavoriteCountMap, wgUser, respUsersFavoriteCountMapError)

// 等待数据
wgUser.Wait()

Expand All @@ -106,6 +124,18 @@ func GetUserLatestMap(idSet []int64, currentUser int64, respUserMap chan map[int
if err != nil {
errSlice = append(errSlice, err)
}

RelationMap := <-respRelationMap
err = <-respRelationMapError
if err != nil {
errSlice = append(errSlice, err)
}

FavoriteCountMap := <-respUsersFavoriteCountMap
err = <-respUsersFavoriteCountMapError
if err != nil {
errSlice = append(errSlice, err)
}
// TODO: 其他协程的错误处理

errChan <- errSlice // 错误切片
Expand All @@ -115,15 +145,15 @@ func GetUserLatestMap(idSet []int64, currentUser int64, respUserMap chan map[int
AuthorMap[id] = core.User{
ID: user.ID,
Name: user.Name,
FollowCount: 0, // TODO: 从获取的数据中拿
FollowerCount: 0, // TODO: 从获取的数据中拿
IsFollow: false, // TODO: 从获取的数据中拿
FollowCount: RelationMap[id].followInfo.FollowCount, // Relation服务 最新的followCount
FollowerCount: RelationMap[id].followInfo.FollowerCount, // Relation服务 最新的followerCount
IsFollow: RelationMap[id].followInfo.IsFollow, // Relation服务 最新的isFollow
Avatar: user.Avatar,
BackgroundImage: user.BackgroundImage,
Signature: user.Signature,
TotalFavourited: "", // TODO: 从获取的数据中拿
WorkCount: VideoCountMap[id].Count, // 最新的count数据
FavoriteCount: 0, // TODO: 从获取的数据中拿
TotalFavourited: strconv.FormatInt(FavoriteCountMap[id][1], 10), // TODO: 从获取的数据中拿
WorkCount: VideoCountMap[id].Count, // 最新的count数据
FavoriteCount: FavoriteCountMap[id][0], // TODO: 从获取的数据中拿
}

}
Expand Down
35 changes: 19 additions & 16 deletions kitex_server/videoservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,35 +51,38 @@ func GetVideoLatestMap(idSet []int64, currentUser int64, respVideoMap chan map[i

wgVideo := &sync.WaitGroup{} // 本函数子协程的wg

// 批量查询视频的 被喜欢数 ,Favorite 从Favorite服务
// 批量查询 favorite_count, total_favourited 从favorite服务: kitex_server.FavoriteClient.UserFavoriteCountMethod()
// TODO: 结果要 videoId与对应的数据 map
// 批量查询视频的 被喜欢数 ,传入视频id的切片,返回对应的FavoriteCount的切片(需携带对应视频id) 从Favorite服务
respVideosFavoriteCountMap := make(chan map[int64]int64, 1)
defer close(respVideosFavoriteCountMap)
respVideosFavoriteCountMapError := make(chan error, 1)
defer close(respVideosFavoriteCountMapError)
wgVideo.Add(1)
go GetVideosFavoriteCountMap(idSet, respVideosFavoriteCountMap, wgVideo, respVideosFavoriteCountMapError)

// 批量查询 is_follow, 从relation服务; 传入目标userID和currentUser
// 批量查询视频的评论数,传入视频id的切片,返回对应的评论数(需携带对应视频id),从comment服务

// 批量查询 follow_count, follower_cout 从relation服务
// 批量查询 is_favorite, 传入目标视频id切片和currentUser查is_favorite的切片(结果需要携带视频id,douyin里后续需要转成map):从favorite;
GetIsFavoriteMap()

// 等待数据
wgVideo.Wait()

// // 处理协程错误
var errSlice = []error{} // 防止外部设置的chan缓存不够造成阻塞,要求外部设置长度为1的error切片类型
// err := <-respAuthorMapError
// if err != nil {
// errSlice = append(errSlice, err)
// }

// // TODO: 其他协程的错误处理
var errSlice = []error{}
VideosFavoriteCountMap := <-respVideosFavoriteCountMap
err := <-respVideosFavoriteCountMapError
if err != nil {
errSlice = append(errSlice, err)
}

errChan <- errSlice // 记录错误的切片,至少应该返回一个空切片,否则chan会阻塞

// 更新数据
videoLatestMap := make(map[int64]core.Video, len(idSet)) // 视频切片的id是没有重复的
for _, id := range idSet {
videoLatestMap[id] = core.Video{ // 视频id对应的Video存储查到的关键字段
FavoriteCount: 0, // TODO:从拿到的MAP数据更新
CommentCount: 0, // TODO:从拿到的MAP数据更新
IsFavorite: false, // TODO:从拿到的MAP数据更新
FavoriteCount: VideosFavoriteCountMap[id], // TODO:从拿到的MAP数据更新
CommentCount: 0, // TODO:从拿到的MAP数据更新
IsFavorite: false, // TODO:从拿到的MAP数据更新
}
}
respVideoMap <- videoLatestMap // 返回数据
Expand Down
17 changes: 17 additions & 0 deletions tools/safe/sqlsafe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package safe

import (
"errors"
"regexp"
)

// ref: https://blog.csdn.net/qq_40127376/article/details/108516561
var sqlInjectReg = regexp.MustCompile(`(.*\=.*\-\-.*)|(.*(\+|\-).*)|(.*\w+(%|\$|#|&)\w+.*)|(.*\|\|.*)|(.*\s+(and|or)\s+.*)|(.*\b(select|update|union|and|or|delete|insert|trancate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute)\b.*)`)

func SqlInjectCheck(input string) error {
reg := sqlInjectReg.FindAllString(input, 1) // 匹配一个就行
if reg != nil {
return errors.New("输入存在非法字段")
}
return nil
}
16 changes: 16 additions & 0 deletions tools/safe/sqlsafe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package safe

import (
"log"
"testing"
)

func TestSqlInjectCheck(t *testing.T) {
str1 := "select 1"
err := SqlInjectCheck(str1)
if err != nil {
log.Println(err)
return
}
log.Println("no")
}

0 comments on commit e917864

Please sign in to comment.