Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

fix(user): convert user bio markdown

LinkinStar020262f66243
diff --git a/internal/schema/user_schema.go b/internal/schema/user_schema.go
index 9b449b80..52449664 100644
--- a/internal/schema/user_schema.go
+++ b/internal/schema/user_schema.go
@@ -8,6 +8,7 @@ import (
"github.com/answerdev/answer/internal/base/validator"
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/pkg/checker"
+ "github.com/answerdev/answer/pkg/converter"
"github.com/jinzhu/copier"
"github.com/segmentfault/pacman/errors"
)
@@ -283,7 +284,7 @@ type UpdateInfoRequest struct {
// bio
Bio string `validate:"omitempty,gt=0,lte=4096" json:"bio"`
// bio
- BioHTML string `validate:"omitempty,gt=0,lte=4096" json:"bio_html"`
+ BioHTML string `json:"-"`
// website
Website string `validate:"omitempty,gt=0,lte=500" json:"website"`
// location
@@ -298,11 +299,11 @@ type AvatarInfo struct {
Custom string `validate:"omitempty,gt=0,lte=200" json:"custom"`
}
-func (u *UpdateInfoRequest) Check() (errFields []*validator.FormErrorField, err error) {
- if len(u.Username) > 0 {
+func (req *UpdateInfoRequest) Check() (errFields []*validator.FormErrorField, err error) {
+ if len(req.Username) > 0 {
errFields := make([]*validator.FormErrorField, 0)
re := regexp.MustCompile(`^[a-z0-9._-]{4,30}$`)
- match := re.MatchString(u.Username)
+ match := re.MatchString(req.Username)
if !match {
errField := &validator.FormErrorField{
ErrorField: "username",
@@ -312,6 +313,7 @@ func (u *UpdateInfoRequest) Check() (errFields []*validator.FormErrorField, err
return errFields, errors.BadRequest(reason.UsernameInvalid)
}
}
+ req.BioHTML = converter.Markdown2HTML(req.Bio)
return nil, nil
}
diff --git a/internal/service/user_service.go b/internal/service/user_service.go
index 306fea13..e7bd4374 100644
--- a/internal/service/user_service.go
+++ b/internal/service/user_service.go
@@ -22,7 +22,6 @@ import (
usercommon "github.com/answerdev/answer/internal/service/user_common"
"github.com/answerdev/answer/pkg/checker"
"github.com/google/uuid"
- "github.com/microcosm-cc/bluemonday"
"github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/log"
"golang.org/x/crypto/bcrypt"
@@ -268,17 +267,12 @@ func (us *UserService) UpdateInfo(ctx context.Context, req *schema.UpdateInfoReq
if err != nil {
return nil, errors.BadRequest(reason.UserSetAvatar).WithError(err).WithStack()
}
- xss := bluemonday.UGCPolicy()
- bio := xss.Sanitize(
- req.BioHTML,
- )
-
userInfo := entity.User{}
userInfo.ID = req.UserID
userInfo.Avatar = string(avatar)
userInfo.DisplayName = req.DisplayName
userInfo.Bio = req.Bio
- userInfo.BioHTML = bio
+ userInfo.BioHTML = req.BioHTML
userInfo.Location = req.Location
userInfo.Website = req.Website
userInfo.Username = req.Username