Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:GuGoOrg/GuGoTik into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
liaosunny123 committed Aug 27, 2023
2 parents ecffc6f + 49c597d commit 96e34fb
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 85 deletions.
46 changes: 25 additions & 21 deletions src/constant/strings/err.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
UnableToDeleteCommentErrorCode = 50008
UnableToDeleteCommentError = "无法删除视频评论"
UnableToAddMessageErrorCode = 50009
UnableToAddMessageRrror = "发送消息出错"
UnableToAddMessageError = "发送消息出错"
UnableToQueryMessageErrorCode = 50010
UnableToQueryMessageError = "查消息出错"
PublishServiceInnerErrorCode = 50011
Expand All @@ -48,30 +48,34 @@ const (
StringToIntError = "字符串转数字失败"
RelationServiceIntErrorCode = 50019
RelationServiceIntError = "关系服务出现内部错误"
FavorivateServiceErrorCode = 50020
FavorivateServiceError = "点赞服务内部出错"
FavoriteServiceErrorCode = 50020
FavoriteServiceError = "点赞服务内部出错"
UserServiceInnerErrorCode = 50021
UserServiceInnerError = "登录服务出现内部错误,请稍后重试!"
)

// Expected Error
const (
AuthUserExistedCode = 10001
AuthUserExisted = "用户已存在,请更换用户名或尝试登录!"
UserNotExistedCode = 10002
UserNotExisted = "用户不存在,请先注册或检查你的用户名是否正确!"
AuthUserLoginFailedCode = 10003
AuthUserLoginFailed = "用户信息错误,请检查账号密码是否正确"
AuthUserNeededCode = 10004
AuthUserNeeded = "用户无权限操作,请登陆后重试!"
ActionCommentTypeInvalidCode = 10005
ActionCommentTypeInvalid = "不合法的评论类型"
ActionCommentLimitedCode = 10006
ActionCommentLimited = "评论频繁,请稍后再试!"
InvalidContentTypeCode = 10007
InvalidContentType = "不合法的内容类型"
FavorivateServiceDuplicateCode = 10008
FavorivateServiceDuplicateError = "不能重复点赞"
FavorivateServiceCancelCode = 10009
FavorivateServiceCancelError = "没有点赞,不能取消点赞"
AuthUserExistedCode = 10001
AuthUserExisted = "用户已存在,请更换用户名或尝试登录!"
UserNotExistedCode = 10002
UserNotExisted = "用户不存在,请先注册或检查你的用户名是否正确!"
AuthUserLoginFailedCode = 10003
AuthUserLoginFailed = "用户信息错误,请检查账号密码是否正确"
AuthUserNeededCode = 10004
AuthUserNeeded = "用户无权限操作,请登陆后重试!"
ActionCommentTypeInvalidCode = 10005
ActionCommentTypeInvalid = "不合法的评论类型"
ActionCommentLimitedCode = 10006
ActionCommentLimited = "评论频繁,请稍后再试!"
InvalidContentTypeCode = 10007
InvalidContentType = "不合法的内容类型"
FavoriteServiceDuplicateCode = 10008
FavoriteServiceDuplicateError = "不能重复点赞"
FavoriteServiceCancelCode = 10009
FavoriteServiceCancelError = "没有点赞,不能取消点赞"
PublishVideoLimitedCode = 10010
PublishVideoLimited = "视频发布频繁,请稍后再试!"
ChatActionLimitedCode = 10011
ChatActionLimitedError = "发送消息频繁,请稍后再试!"
)
20 changes: 20 additions & 0 deletions src/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package models

import (
"GuGoTik/src/storage/database"
"GuGoTik/src/utils/logging"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"regexp"
)

Expand Down Expand Up @@ -36,4 +38,22 @@ func init() {
if err := database.Client.AutoMigrate(&User{}); err != nil {
panic(err)
}

// Create magic user (id = 0): show video summary and keywords, and act as ChatGPT
magicUser := User{
ID: 999999,
UserName: "ChatGPT",
Password: "chatgpt",
Role: 0,
Avatar: "https://maples31-blog.oss-cn-beijing.aliyuncs.com/img/ChatGPT_logo.svg.png",
BackgroundImage: "https://maples31-blog.oss-cn-beijing.aliyuncs.com/img/ChatGPT.jpg",
Signature: "GuGoTik 小助手",
}
result := database.Client.Clauses(clause.OnConflict{
UpdateAll: true,
}).Create(&magicUser)

if result.Error != nil {
logging.Logger.Errorf("Cannot create magic user because of %s", result.Error)
}
}
68 changes: 67 additions & 1 deletion src/services/auth/handler.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package main

import (
"GuGoTik/src/constant/config"
"GuGoTik/src/constant/strings"
"GuGoTik/src/extra/tracing"
"GuGoTik/src/models"
"GuGoTik/src/rpc/auth"
"GuGoTik/src/rpc/relation"
user2 "GuGoTik/src/rpc/user"
"GuGoTik/src/storage/cached"
"GuGoTik/src/storage/database"
grpc2 "GuGoTik/src/utils/grpc"
"GuGoTik/src/utils/logging"
"context"
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
Expand All @@ -25,12 +30,18 @@ import (
"sync"
)

var relationClient relation.RelationServiceClient
var userClient user2.UserServiceClient

type AuthServiceImpl struct {
auth.AuthServiceServer
}

func (a AuthServiceImpl) New() {

relationConn := grpc2.Connect(config.RelationRpcServerName)
relationClient = relation.NewRelationServiceClient(relationConn)
userRpcConn := grpc2.Connect(config.UserRpcServerName)
userClient = user2.NewUserServiceClient(userRpcConn)
}

func (a AuthServiceImpl) Authenticate(ctx context.Context, request *auth.AuthenticateRequest) (resp *auth.AuthenticateResponse, err error) {
Expand Down Expand Up @@ -206,6 +217,9 @@ func (a AuthServiceImpl) Register(ctx context.Context, request *auth.RegisterReq
resp.UserId = user.ID
resp.StatusCode = strings.ServiceOKCode
resp.StatusMsg = strings.ServiceOK

addMagicUserFriend(ctx, &span, user.ID)

return
}

Expand Down Expand Up @@ -385,3 +399,55 @@ func getEmailMD5(ctx context.Context, email string) (md5String string) {
md5String = hex.EncodeToString(md5Bytes)
return
}

func addMagicUserFriend(ctx context.Context, span *trace.Span, userId uint32) {
logger := logging.LogService("AuthService.Register.AddMagicUserFriend").WithContext(ctx)

isMagicUserExist, err := userClient.GetUserExistInformation(ctx, &user2.UserExistRequest{
UserId: 999999,
})
if err != nil {
logger.WithFields(logrus.Fields{
"UserId": userId,
"Err": err,
}).Errorf("Failed to check if the magic user exists")
logging.SetSpanError(*span, err)
return
}

if !isMagicUserExist.Existed {
logger.WithFields(logrus.Fields{
"UserId": userId,
}).Errorf("Magic user does not exist")
logging.SetSpanError(*span, errors.New("magic user does not exist"))
return
}

// User follow magic user
_, err = relationClient.Follow(ctx, &relation.RelationActionRequest{
ActorId: userId,
UserId: 999999,
})
if err != nil {
logger.WithFields(logrus.Fields{
"UserId": userId,
"Err": err,
}).Errorf("Failed to follow magic user")
logging.SetSpanError(*span, err)
return
}

// Magic user follow user
_, err = relationClient.Follow(ctx, &relation.RelationActionRequest{
ActorId: 999999,
UserId: userId,
})
if err != nil {
logger.WithFields(logrus.Fields{
"UserId": userId,
"Err": err,
}).Errorf("Magic user failed to follow user")
logging.SetSpanError(*span, err)
return
}
}
19 changes: 19 additions & 0 deletions src/services/comment/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ func (c CommentServiceImpl) ListComment(ctx context.Context, request *comment.Li
return
}

// Put the magic comment to the first front
reindexCommentList(&pCommentList)

// Get user info of each comment
rCommentList := make([]*comment.Comment, 0, result.RowsAffected)
userMap := make(map[uint32]*user.User)
Expand Down Expand Up @@ -546,3 +549,19 @@ func count(ctx context.Context, videoId uint32) (count int64, err error) {
}
return count, result.Error
}

// Put the magic comment to the front
func reindexCommentList(commentList *[]models.Comment) {
var magicComments []models.Comment
var commonComments []models.Comment

for _, c := range *commentList {
if c.UserId == 999999 {
magicComments = append(magicComments, c)
} else {
commonComments = append(commonComments, c)
}
}

*commentList = append(magicComments, commonComments...)
}
60 changes: 30 additions & 30 deletions src/services/favorite/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ func (c FavoriteServiceServerImpl) FavoriteAction(ctx context.Context, req *favo
logging.SetSpanError(span, err)

return &favorite.FavoriteResponse{
StatusCode: strings.FavorivateServiceErrorCode,
StatusMsg: strings.FavorivateServiceError,
StatusCode: strings.FavoriteServiceErrorCode,
StatusMsg: strings.FavoriteServiceError,
}, err
}

Expand Down Expand Up @@ -163,8 +163,8 @@ func (c FavoriteServiceServerImpl) FavoriteAction(ctx context.Context, req *favo
//重复点赞
if value > 0 {
resp = &favorite.FavoriteResponse{
StatusCode: strings.FavorivateServiceDuplicateCode,
StatusMsg: strings.FavorivateServiceDuplicateError,
StatusCode: strings.FavoriteServiceDuplicateCode,
StatusMsg: strings.FavoriteServiceDuplicateError,
}
logger.WithFields(logrus.Fields{
"ActorId": req.ActorId,
Expand Down Expand Up @@ -197,8 +197,8 @@ func (c FavoriteServiceServerImpl) FavoriteAction(ctx context.Context, req *favo
//没有的点过赞
if value == 0 {
resp = &favorite.FavoriteResponse{
StatusCode: strings.FavorivateServiceCancelCode,
StatusMsg: strings.FavorivateServiceCancelError,
StatusCode: strings.FavoriteServiceCancelCode,
StatusMsg: strings.FavoriteServiceCancelError,
}

logger.WithFields(logrus.Fields{
Expand Down Expand Up @@ -233,8 +233,8 @@ func (c FavoriteServiceServerImpl) FavoriteAction(ctx context.Context, req *favo
logging.SetSpanError(span, err)

return &favorite.FavoriteResponse{
StatusCode: strings.FavorivateServiceErrorCode,
StatusMsg: strings.FavorivateServiceError,
StatusCode: strings.FavoriteServiceErrorCode,
StatusMsg: strings.FavoriteServiceError,
}, err
}
resp = &favorite.FavoriteResponse{
Expand Down Expand Up @@ -273,8 +273,8 @@ func (c FavoriteServiceServerImpl) FavoriteList(ctx context.Context, req *favori
logging.SetSpanError(span, err)

return &favorite.FavoriteListResponse{
StatusCode: strings.FavorivateServiceErrorCode,
StatusMsg: strings.FavorivateServiceError,
StatusCode: strings.FavoriteServiceErrorCode,
StatusMsg: strings.FavoriteServiceError,
}, err
}

Expand All @@ -288,8 +288,8 @@ func (c FavoriteServiceServerImpl) FavoriteList(ctx context.Context, req *favori
logging.SetSpanError(span, err)

return &favorite.FavoriteListResponse{
StatusCode: strings.FavorivateServiceErrorCode,
StatusMsg: strings.FavorivateServiceError,
StatusCode: strings.FavoriteServiceErrorCode,
StatusMsg: strings.FavoriteServiceError,
}, err
}
if len(arr) == 0 {
Expand Down Expand Up @@ -320,8 +320,8 @@ func (c FavoriteServiceServerImpl) FavoriteList(ctx context.Context, req *favori
}).Errorf("feed Service error")
logging.SetSpanError(span, err)
return &favorite.FavoriteListResponse{
StatusCode: strings.FavorivateServiceErrorCode,
StatusMsg: strings.FavorivateServiceError,
StatusCode: strings.FavoriteServiceErrorCode,
StatusMsg: strings.FavoriteServiceError,
}, err
}

Expand Down Expand Up @@ -356,8 +356,8 @@ func (c FavoriteServiceServerImpl) IsFavorite(ctx context.Context, req *favorite
}).Errorf("feed Service error")
logging.SetSpanError(span, err)
return &favorite.IsFavoriteResponse{
StatusCode: strings.FavorivateServiceErrorCode,
StatusMsg: strings.FavorivateServiceError,
StatusCode: strings.FavoriteServiceErrorCode,
StatusMsg: strings.FavoriteServiceError,
}, err
}

Expand All @@ -377,8 +377,8 @@ func (c FavoriteServiceServerImpl) IsFavorite(ctx context.Context, req *favorite
logging.SetSpanError(span, err)

return &favorite.IsFavoriteResponse{
StatusCode: strings.FavorivateServiceErrorCode,
StatusMsg: strings.FavorivateServiceError,
StatusCode: strings.FavoriteServiceErrorCode,
StatusMsg: strings.FavoriteServiceError,
}, err
}

Expand Down Expand Up @@ -423,8 +423,8 @@ func (c FavoriteServiceServerImpl) CountFavorite(ctx context.Context, req *favor
}).Errorf("feed Service error")
logging.SetSpanError(span, err)
return &favorite.CountFavoriteResponse{
StatusCode: strings.FavorivateServiceErrorCode,
StatusMsg: strings.FavorivateServiceError,
StatusCode: strings.FavoriteServiceErrorCode,
StatusMsg: strings.FavoriteServiceError,
}, err
}
videoId := fmt.Sprintf("%svideo_like_%d", config.EnvCfg.RedisPrefix, req.VideoId)
Expand All @@ -440,8 +440,8 @@ func (c FavoriteServiceServerImpl) CountFavorite(ctx context.Context, req *favor
logging.SetSpanError(span, err)

return &favorite.CountFavoriteResponse{
StatusCode: strings.FavorivateServiceErrorCode,
StatusMsg: strings.FavorivateServiceError,
StatusCode: strings.FavoriteServiceErrorCode,
StatusMsg: strings.FavoriteServiceError,
}, err
} else {
num, _ = strconv.Atoi(value)
Expand Down Expand Up @@ -481,8 +481,8 @@ func (c FavoriteServiceServerImpl) CountUserFavorite(ctx context.Context, req *f
logging.SetSpanError(span, err)

return &favorite.CountUserFavoriteResponse{
StatusCode: strings.FavorivateServiceErrorCode,
StatusMsg: strings.FavorivateServiceError,
StatusCode: strings.FavoriteServiceErrorCode,
StatusMsg: strings.FavoriteServiceError,
}, err
}
user_like_id := fmt.Sprintf("%suser_like_%d", config.EnvCfg.RedisPrefix, req.UserId)
Expand All @@ -499,8 +499,8 @@ func (c FavoriteServiceServerImpl) CountUserFavorite(ctx context.Context, req *f
logging.SetSpanError(span, err)

return &favorite.CountUserFavoriteResponse{
StatusCode: strings.FavorivateServiceErrorCode,
StatusMsg: strings.FavorivateServiceError,
StatusCode: strings.FavoriteServiceErrorCode,
StatusMsg: strings.FavoriteServiceError,
}, err
} else {
num = value
Expand Down Expand Up @@ -542,8 +542,8 @@ func (c FavoriteServiceServerImpl) CountUserTotalFavorited(ctx context.Context,
logging.SetSpanError(span, err)

return &favorite.CountUserTotalFavoritedResponse{
StatusCode: strings.FavorivateServiceErrorCode,
StatusMsg: strings.FavorivateServiceError,
StatusCode: strings.FavoriteServiceErrorCode,
StatusMsg: strings.FavoriteServiceError,
}, err
}
user_liked_id := fmt.Sprintf("%suser_liked_%d", config.EnvCfg.RedisPrefix, req.UserId)
Expand All @@ -562,8 +562,8 @@ func (c FavoriteServiceServerImpl) CountUserTotalFavorited(ctx context.Context,
logging.SetSpanError(span, err)

return &favorite.CountUserTotalFavoritedResponse{
StatusCode: strings.FavorivateServiceErrorCode,
StatusMsg: strings.FavorivateServiceError,
StatusCode: strings.FavoriteServiceErrorCode,
StatusMsg: strings.FavoriteServiceError,
}, err
} else {
num, _ = strconv.Atoi(value)
Expand Down
Loading

0 comments on commit 96e34fb

Please sign in to comment.