Skip to content

Commit

Permalink
Merge pull request GuGoOrg#227 from Maple-pro/dev
Browse files Browse the repository at this point in the history
fix: parameter check
  • Loading branch information
liaosunny123 authored Sep 1, 2023
2 parents ed0581e + bd6d195 commit fcee5d2
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/services/comment/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"GuGoTik/src/extra/tracing"
"GuGoTik/src/models"
"GuGoTik/src/rpc/comment"
"GuGoTik/src/rpc/feed"
"GuGoTik/src/rpc/user"
"GuGoTik/src/storage/cached"
"GuGoTik/src/storage/database"
Expand All @@ -26,6 +27,7 @@ import (
)

var userClient user.UserServiceClient
var feedClient feed.FeedServiceClient

var actionCommentLimitKeyPrefix = config.EnvCfg.RedisPrefix + "comment_freq_limit"

Expand Down Expand Up @@ -58,6 +60,9 @@ func (c CommentServiceImpl) New() {
userRpcConn := grpc2.Connect(config.UserRpcServerName)
userClient = user.NewUserServiceClient(userRpcConn)

feedRpcConn := grpc2.Connect(config.FeedRpcServerName)
feedClient = feed.NewFeedServiceClient(feedRpcConn)

var err error

conn, err = amqp.Dial(rabbitmq.BuildMQConnAddr())
Expand Down Expand Up @@ -189,6 +194,34 @@ func (c CommentServiceImpl) ActionComment(ctx context.Context, request *comment.
return
}

// Check if video exists
videoExistResp, err := feedClient.QueryVideoExisted(ctx, &feed.VideoExistRequest{
VideoId: request.VideoId,
})
if err != nil {
logger.WithFields(logrus.Fields{
"err": err,
}).Errorf("Query video existence happens error")
logging.SetSpanError(span, err)
resp = &comment.ActionCommentResponse{
StatusCode: strings.FeedServiceInnerErrorCode,
StatusMsg: strings.FeedServiceInnerError,
}
return
}

if !videoExistResp.Existed {
logger.WithFields(logrus.Fields{
"VideoId": request.VideoId,
}).Errorf("Video ID does not exist")
logging.SetSpanError(span, err)
resp = &comment.ActionCommentResponse{
StatusCode: strings.UnableToQueryVideoErrorCode,
StatusMsg: strings.UnableToQueryVideoError,
}
return
}

// Get target user
userResponse, err := userClient.GetUserInfo(ctx, &user.UserRequest{
UserId: request.ActorId,
Expand Down Expand Up @@ -250,6 +283,34 @@ func (c CommentServiceImpl) ListComment(ctx context.Context, request *comment.Li
"video_id": request.VideoId,
}).Debugf("Process start")

// Check if video exists
videoExistResp, err := feedClient.QueryVideoExisted(ctx, &feed.VideoExistRequest{
VideoId: request.VideoId,
})
if err != nil {
logger.WithFields(logrus.Fields{
"err": err,
}).Errorf("Query video existence happens error")
logging.SetSpanError(span, err)
resp = &comment.ListCommentResponse{
StatusCode: strings.FeedServiceInnerErrorCode,
StatusMsg: strings.FeedServiceInnerError,
}
return
}

if !videoExistResp.Existed {
logger.WithFields(logrus.Fields{
"VideoId": request.VideoId,
}).Errorf("Video ID does not exist")
logging.SetSpanError(span, err)
resp = &comment.ListCommentResponse{
StatusCode: strings.UnableToQueryVideoErrorCode,
StatusMsg: strings.UnableToQueryVideoError,
}
return
}

var pCommentList []models.Comment
result := database.Client.WithContext(ctx).
Where("video_id = ?", request.VideoId).
Expand Down
28 changes: 28 additions & 0 deletions src/services/favorite/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,34 @@ func (c FavoriteServiceServerImpl) FavoriteAction(ctx context.Context, req *favo
"action_type": req.ActionType, //点赞 1 2 取消点赞
}).Debugf("Process start")

// Check if video exists
videoExistResp, err := feedClient.QueryVideoExisted(ctx, &feed.VideoExistRequest{
VideoId: req.VideoId,
})
if err != nil {
logger.WithFields(logrus.Fields{
"err": err,
}).Errorf("Query video existence happens error")
logging.SetSpanError(span, err)
resp = &favorite.FavoriteResponse{
StatusCode: strings.FeedServiceInnerErrorCode,
StatusMsg: strings.FeedServiceInnerError,
}
return
}

if !videoExistResp.Existed {
logger.WithFields(logrus.Fields{
"VideoId": req.VideoId,
}).Errorf("Video ID does not exist")
logging.SetSpanError(span, err)
resp = &favorite.FavoriteResponse{
StatusCode: strings.UnableToQueryVideoErrorCode,
StatusMsg: strings.UnableToQueryVideoError,
}
return
}

VideosRes, err := feedClient.QueryVideos(ctx, &feed.QueryVideosRequest{
ActorId: req.ActorId,
VideoIds: []uint32{req.VideoId},
Expand Down
34 changes: 34 additions & 0 deletions src/services/publish/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"GuGoTik/src/models"
"GuGoTik/src/rpc/feed"
"GuGoTik/src/rpc/publish"
"GuGoTik/src/rpc/user"
"GuGoTik/src/storage/cached"
"GuGoTik/src/storage/database"
"GuGoTik/src/storage/file"
Expand Down Expand Up @@ -37,6 +38,7 @@ var conn *amqp.Connection
var channel *amqp.Channel

var FeedClient feed.FeedServiceClient
var userClient user.UserServiceClient

func exitOnError(err error) {
if err != nil {
Expand Down Expand Up @@ -66,6 +68,10 @@ func createVideoLimitKey(userId uint32) string {
func (a PublishServiceImpl) New() {
FeedRpcConn := grpc2.Connect(config.FeedRpcServerName)
FeedClient = feed.NewFeedServiceClient(FeedRpcConn)

userRpcConn := grpc2.Connect(config.UserRpcServerName)
userClient = user.NewUserServiceClient(userRpcConn)

var err error

conn, err = amqp.Dial(rabbitmq.BuildMQConnAddr())
Expand Down Expand Up @@ -133,6 +139,34 @@ func (a PublishServiceImpl) ListVideo(ctx context.Context, req *publish.ListVide
logging.SetSpanWithHostname(span)
logger := logging.LogService("PublishServiceImpl.ListVideo").WithContext(ctx)

// Check if user exist
userExistResp, err := userClient.GetUserExistInformation(ctx, &user.UserExistRequest{
UserId: req.UserId,
})
if err != nil {
logger.WithFields(logrus.Fields{
"err": err,
}).Errorf("Query user existence happens error")
logging.SetSpanError(span, err)
resp = &publish.ListVideoResponse{
StatusCode: strings.UserServiceInnerErrorCode,
StatusMsg: strings.UserServiceInnerError,
}
return
}

if !userExistResp.Existed {
logger.WithFields(logrus.Fields{
"UserID": req.UserId,
}).Errorf("User ID does not exist")
logging.SetSpanError(span, err)
resp = &publish.ListVideoResponse{
StatusCode: strings.UserDoNotExistedCode,
StatusMsg: strings.UserDoNotExisted,
}
return
}

var videos []models.Video
err = database.Client.WithContext(ctx).
Where("user_id = ?", req.UserId).
Expand Down
8 changes: 8 additions & 0 deletions src/web/message/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ func ActionMessageHandler(c *gin.Context) {
var res *chat.ActionResponse
var err error

if req.ActionType != 1 {
c.JSON(http.StatusOK, models.ActionCommentRes{
StatusCode: strings.GateWayParamsErrorCode,
StatusMsg: strings.GateWayParamsError,
})
return
}

res, err = Client.ChatAction(c.Request.Context(), &chat.ActionRequest{
ActorId: uint32(req.ActorId),
UserId: uint32(req.ToUserId),
Expand Down
6 changes: 6 additions & 0 deletions src/web/relation/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ func ActionRelationHandler(c *gin.Context) {
ActorId: uint32(req.ActorId),
UserId: uint32(req.UserId),
})
} else {
c.JSON(http.StatusOK, models.ActionCommentRes{
StatusCode: strings.GateWayParamsErrorCode,
StatusMsg: strings.GateWayParamsError,
})
return
}

if err != nil {
Expand Down

0 comments on commit fcee5d2

Please sign in to comment.