Merge branch 'feat/1.0.7/short-id' into test

This commit is contained in:
aichy126 2023-03-17 17:16:33 +08:00
commit 13733a226b
4 changed files with 21 additions and 18 deletions

View File

@ -41,7 +41,7 @@ func (vc *VoteController) VoteUp(ctx *gin.Context) {
}
req.ObjectID = uid.DeShortID(req.ObjectID)
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
can, err := vc.rankService.CheckVotePermission(ctx, req.UserID, req.ObjectID, true)
can, _, err := vc.rankService.CheckVotePermission(ctx, req.UserID, req.ObjectID, true)
if err != nil {
handler.HandleResponse(ctx, err, nil)
return
@ -78,7 +78,7 @@ func (vc *VoteController) VoteDown(ctx *gin.Context) {
}
req.ObjectID = uid.DeShortID(req.ObjectID)
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
can, err := vc.rankService.CheckVotePermission(ctx, req.UserID, req.ObjectID, false)
can, _, err := vc.rankService.CheckVotePermission(ctx, req.UserID, req.ObjectID, false)
if err != nil {
handler.HandleResponse(ctx, err, nil)
return

View File

@ -235,6 +235,7 @@ func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.Question
tagNameList := make([]string, 0)
for _, tag := range req.Tags {
tag.SlugName = strings.ReplaceAll(tag.SlugName, " ", "-")
tagNameList = append(tagNameList, tag.SlugName)
}
Tags, tagerr := qs.tagCommon.GetTagListByNames(ctx, tagNameList)
@ -495,6 +496,7 @@ func (qs *QuestionService) UpdateQuestion(ctx context.Context, req *schema.Quest
tagNameList := make([]string, 0)
oldtagNameList := make([]string, 0)
for _, tag := range req.Tags {
tag.SlugName = strings.ReplaceAll(tag.SlugName, " ", "-")
tagNameList = append(tagNameList, tag.SlugName)
}
for _, tag := range oldTags {

View File

@ -90,7 +90,7 @@ func (rs *RankService) CheckOperationPermission(ctx context.Context, userID stri
}
}
can = rs.checkUserRank(ctx, userInfo.ID, userInfo.Rank, PermissionPrefix+action)
can, _ = rs.checkUserRank(ctx, userInfo.ID, userInfo.Rank, PermissionPrefix+action)
return can, nil
}
@ -117,7 +117,7 @@ func (rs *RankService) CheckOperationPermissions(ctx context.Context, userID str
can[idx] = true
continue
}
meetRank := rs.checkUserRank(ctx, userInfo.ID, userInfo.Rank, PermissionPrefix+action)
meetRank, _ := rs.checkUserRank(ctx, userInfo.ID, userInfo.Rank, PermissionPrefix+action)
can[idx] = meetRank
}
return can, nil
@ -141,22 +141,22 @@ func (rs *RankService) CheckOperationObjectOwner(ctx context.Context, userID, ob
// CheckVotePermission verify that the user has vote permission
func (rs *RankService) CheckVotePermission(ctx context.Context, userID, objectID string, voteUp bool) (
can bool, err error) {
can bool, rank int, err error) {
if len(userID) == 0 || len(objectID) == 0 {
return false, nil
return false, 0, nil
}
// get the rank of the current user
userInfo, exist, err := rs.userCommon.GetUserBasicInfoByID(ctx, userID)
if err != nil {
return can, err
return can, 0, err
}
if !exist {
return can, nil
return can, 0, nil
}
objectInfo, err := rs.objectInfoService.GetInfo(ctx, objectID)
if err != nil {
return can, err
return can, 0, err
}
action := ""
switch objectInfo.ObjectType {
@ -179,13 +179,13 @@ func (rs *RankService) CheckVotePermission(ctx context.Context, userID, objectID
action = permission.CommentVoteDown
}
}
meetRank, rank := rs.checkUserRank(ctx, userInfo.ID, userInfo.Rank, PermissionPrefix+action)
powerMapping := rs.getUserPowerMapping(ctx, userID)
if powerMapping[action] {
return true, nil
return true, rank, nil
}
meetRank := rs.checkUserRank(ctx, userInfo.ID, userInfo.Rank, PermissionPrefix+action)
return meetRank, nil
return meetRank, rank, nil
}
// getUserPowerMapping get user power mapping
@ -210,19 +210,19 @@ func (rs *RankService) getUserPowerMapping(ctx context.Context, userID string) (
// CheckRankPermission verify that the user meets the prestige criteria
func (rs *RankService) checkUserRank(ctx context.Context, userID string, userRank int, action string) (
can bool) {
can bool, rank int) {
// get the amount of rank required for the current operation
requireRank, err := rs.configRepo.GetInt(action)
if err != nil {
log.Error(err)
return false
return false, requireRank
}
if userRank < requireRank || requireRank < 0 {
log.Debugf("user %s want to do action %s, but rank %d < %d",
userID, action, userRank, requireRank)
return false
return false, requireRank
}
return true
return true, requireRank
}
// GetRankPersonalWithPage get personal comment list page

View File

@ -200,6 +200,7 @@ func (ts *TagCommonService) ExistRecommend(ctx context.Context, tags []*schema.T
}
tagNames := make([]string, 0)
for _, item := range tags {
item.SlugName = strings.ReplaceAll(item.SlugName, " ", "-")
tagNames = append(tagNames, item.SlugName)
}
list, err := ts.GetTagListByNames(ctx, tagNames)
@ -233,7 +234,7 @@ func (ts *TagCommonService) AddTag(ctx context.Context, req *schema.AddTagReq) (
return nil, errors.BadRequest(reason.TagAlreadyExist)
}
tagInfo := &entity.Tag{
SlugName: req.SlugName,
SlugName: strings.ReplaceAll(req.SlugName, " ", "-"),
DisplayName: req.DisplayName,
OriginalText: req.OriginalText,
ParsedText: req.ParsedText,
@ -557,7 +558,7 @@ func (ts *TagCommonService) ObjectChangeTag(ctx context.Context, objectTagData *
continue
}
item := &entity.Tag{}
item.SlugName = tag.SlugName
item.SlugName = strings.ReplaceAll(tag.SlugName, " ", "-")
item.DisplayName = tag.DisplayName
item.OriginalText = tag.OriginalText
item.ParsedText = tag.ParsedText