diff --git a/internal/service/question_service.go b/internal/service/question_service.go index 3707d2fd..3788c702 100644 --- a/internal/service/question_service.go +++ b/internal/service/question_service.go @@ -140,6 +140,19 @@ func (qs *QuestionService) CloseMsgList(ctx context.Context, lang i18n.Language) return resp, err } +func (qs *QuestionService) AddQuestionCheckTags(ctx context.Context, Tags []*entity.Tag) ([]string, error) { + list := make([]string, 0) + for _, tag := range Tags { + if tag.Reserved { + list = append(list, tag.DisplayName) + } + } + if len(list) > 0 { + return list, errors.BadRequest(reason.RequestFormatError) + } + return []string{}, nil +} + // AddQuestion add question func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.QuestionAdd) (questionInfo any, err error) { recommendExist, err := qs.tagCommon.ExistRecommend(ctx, req.Tags) @@ -156,6 +169,29 @@ func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.Question return errorlist, err } + tagNameList := make([]string, 0) + for _, tag := range req.Tags { + tagNameList = append(tagNameList, tag.SlugName) + } + Tags, tagerr := qs.tagCommon.GetTagListByNames(ctx, tagNameList) + if tagerr != nil { + return questionInfo, tagerr + } + if !req.QuestionPermission.CanUseReservedTag { + taglist, err := qs.AddQuestionCheckTags(ctx, Tags) + errMsg := fmt.Sprintf(`"%s" can only be used by moderators.`, + strings.Join(taglist, ",")) + if err != nil { + errorlist := make([]*validator.FormErrorField, 0) + errorlist = append(errorlist, &validator.FormErrorField{ + ErrorField: "tags", + ErrorMsg: errMsg, + }) + err = errors.BadRequest(reason.RecommendTagEnter) + return errorlist, err + } + } + question := &entity.Question{} now := time.Now() question.UserID = req.UserID @@ -189,15 +225,6 @@ func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.Question Title: question.Title, } - tagNameList := make([]string, 0) - for _, tag := range req.Tags { - tagNameList = append(tagNameList, tag.SlugName) - } - Tags, tagerr := qs.tagCommon.GetTagListByNames(ctx, tagNameList) - if tagerr != nil { - return questionInfo, tagerr - } - questionWithTagsRevision, err := qs.changeQuestionToRevision(ctx, question, Tags) if err != nil { return nil, err