Merge branch 'feat/0.6.0/seo' of git.backyard.segmentfault.com:opensource/answer into feat/0.6.0/seo

This commit is contained in:
aichy126
2022-12-09 18:06:01 +08:00
parent 30f05eda76
commit ff536deff3
+36 -9
View File
@@ -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