Skip to content

Commit

Permalink
fix(publish): check user_id #220
Browse files Browse the repository at this point in the history
  • Loading branch information
Maple-pro committed Sep 1, 2023
1 parent ebed599 commit bd6d195
Showing 1 changed file with 34 additions and 0 deletions.
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

0 comments on commit bd6d195

Please sign in to comment.