diff --git a/docs/docs.go b/docs/docs.go index e7df3ce0..8ba38fcb 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -4108,6 +4108,23 @@ const docTemplate = `{ } } }, + "schema.AvatarInfo": { + "type": "object", + "properties": { + "custom": { + "type": "string", + "maxLength": 200 + }, + "gravatar": { + "type": "string", + "maxLength": 200 + }, + "type": { + "type": "string", + "maxLength": 100 + } + } + }, "schema.CloseQuestionReq": { "type": "object", "required": [ @@ -5318,8 +5335,7 @@ const docTemplate = `{ "properties": { "avatar": { "description": "avatar", - "type": "string", - "maxLength": 500 + "$ref": "#/definitions/schema.AvatarInfo" }, "bio": { "description": "bio", diff --git a/docs/swagger.json b/docs/swagger.json index 1063944b..3d1859d3 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -4096,6 +4096,23 @@ } } }, + "schema.AvatarInfo": { + "type": "object", + "properties": { + "custom": { + "type": "string", + "maxLength": 200 + }, + "gravatar": { + "type": "string", + "maxLength": 200 + }, + "type": { + "type": "string", + "maxLength": 100 + } + } + }, "schema.CloseQuestionReq": { "type": "object", "required": [ @@ -5306,8 +5323,7 @@ "properties": { "avatar": { "description": "avatar", - "type": "string", - "maxLength": 500 + "$ref": "#/definitions/schema.AvatarInfo" }, "bio": { "description": "bio", diff --git a/docs/swagger.yaml b/docs/swagger.yaml index bde9a77d..aaa1a77d 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -139,6 +139,18 @@ definitions: description: title type: string type: object + schema.AvatarInfo: + properties: + custom: + maxLength: 200 + type: string + gravatar: + maxLength: 200 + type: string + type: + maxLength: 100 + type: string + type: object schema.CloseQuestionReq: properties: close_msg: @@ -1013,9 +1025,8 @@ definitions: schema.UpdateInfoRequest: properties: avatar: + $ref: '#/definitions/schema.AvatarInfo' description: avatar - maxLength: 500 - type: string bio: description: bio maxLength: 4096 diff --git a/i18n/en_US.yaml b/i18n/en_US.yaml index 2d08b02a..cc863635 100644 --- a/i18n/en_US.yaml +++ b/i18n/en_US.yaml @@ -86,6 +86,8 @@ error: other: "username is invalid" username_duplicate: other: "username is already in use" + set_avatar: + other: "avatar set failure" report: spam: diff --git a/i18n/zh_CN.yaml b/i18n/zh_CN.yaml index 15c95126..7d76cc06 100644 --- a/i18n/zh_CN.yaml +++ b/i18n/zh_CN.yaml @@ -86,6 +86,8 @@ error: other: "用户名无效" username_duplicate: other: "用户名已被使用" + set_avatar: + other: "头像设置错误" report: spam: diff --git a/internal/base/reason/reason.go b/internal/base/reason/reason.go index fb64cd78..f9fd7a66 100644 --- a/internal/base/reason/reason.go +++ b/internal/base/reason/reason.go @@ -26,6 +26,7 @@ const ( UserNotFound = "error.user.not_found" UsernameInvalid = "error.user.username_invalid" UsernameDuplicate = "error.user.username_duplicate" + UserSetAvatar = "error.user.set_avatar" EmailDuplicate = "error.email.duplicate" EmailVerifyUrlExpired = "error.email.verify_url_expired" EmailNeedToBeVerified = "error.email.need_to_be_verified" diff --git a/internal/schema/user_schema.go b/internal/schema/user_schema.go index f6d88e66..21fb7e9e 100644 --- a/internal/schema/user_schema.go +++ b/internal/schema/user_schema.go @@ -10,6 +10,7 @@ import ( "github.com/answerdev/answer/pkg/checker" "github.com/jinzhu/copier" "github.com/segmentfault/pacman/errors" + "github.com/segmentfault/pacman/log" ) // UserVerifyEmailReq user verify email request @@ -72,6 +73,7 @@ type GetUserResp struct { func (r *GetUserResp) GetFromUserEntity(userInfo *entity.User) { _ = copier.Copy(r, userInfo) + r.Avatar = r.AvatarInfo(userInfo.Avatar) r.CreatedAt = userInfo.CreatedAt.Unix() r.LastLoginDate = userInfo.LastLoginDate.Unix() statusShow, ok := UserStatusShow[userInfo.Status] @@ -80,6 +82,26 @@ func (r *GetUserResp) GetFromUserEntity(userInfo *entity.User) { } } +func (us *GetUserResp) AvatarInfo(avatarJson string) string { + if avatarJson == "" { + return "" + } + AvatarInfo := &AvatarInfo{} + err := json.Unmarshal([]byte(avatarJson), AvatarInfo) + if err != nil { + log.Error("AvatarInfo json.Unmarshal Error", err) + return "" + } + switch AvatarInfo.Type { + case "gravatar": + return AvatarInfo.Gravatar + case "custom": + return AvatarInfo.Custom + default: + return "" + } +} + // GetUserStatusResp get user status info type GetUserStatusResp struct { // user status @@ -230,7 +252,7 @@ type UpdateInfoRequest struct { // username Username string `validate:"omitempty,gt=0,lte=30" json:"username"` // avatar - Avatar string `validate:"omitempty,gt=0,lte=500" json:"avatar"` + Avatar AvatarInfo `json:"avatar"` // bio Bio string `validate:"omitempty,gt=0,lte=4096" json:"bio"` // bio @@ -243,6 +265,12 @@ type UpdateInfoRequest struct { UserId string `json:"-" ` } +type AvatarInfo struct { + Type string `validate:"omitempty,gt=0,lte=100" json:"type"` + Gravatar string `validate:"omitempty,gt=0,lte=200" json:"gravatar"` + Custom string `validate:"omitempty,gt=0,lte=200" json:"custom"` +} + func (u *UpdateInfoRequest) Check() (errField *validator.ErrorField, err error) { if len(u.Username) > 0 { re := regexp.MustCompile(`^[a-z0-9._-]{4,30}$`) diff --git a/internal/service/user_common/user.go b/internal/service/user_common/user.go index 0297ae47..adb87317 100644 --- a/internal/service/user_common/user.go +++ b/internal/service/user_common/user.go @@ -76,11 +76,13 @@ func (us *UserCommon) BatchUserBasicInfoByID(ctx context.Context, IDs []string) // UserBasicInfoFormat func (us *UserCommon) UserBasicInfoFormat(ctx context.Context, userInfo *entity.User) *schema.UserBasicInfo { userBasicInfo := &schema.UserBasicInfo{} + uinfo := &schema.GetUserResp{} + uinfo.AvatarInfo(userInfo.Avatar) userBasicInfo.ID = userInfo.ID userBasicInfo.Username = userInfo.Username userBasicInfo.Rank = userInfo.Rank userBasicInfo.DisplayName = userInfo.DisplayName - userBasicInfo.Avatar = userInfo.Avatar + userBasicInfo.Avatar = uinfo.Avatar userBasicInfo.Website = userInfo.Website userBasicInfo.Location = userInfo.Location userBasicInfo.IpInfo = userInfo.IPInfo diff --git a/internal/service/user_service.go b/internal/service/user_service.go index af2d4d6f..7af14d55 100644 --- a/internal/service/user_service.go +++ b/internal/service/user_service.go @@ -3,6 +3,7 @@ package service import ( "context" "encoding/hex" + "encoding/json" "fmt" "math/rand" "regexp" @@ -255,10 +256,14 @@ func (us *UserService) UpdateInfo(ctx context.Context, req *schema.UpdateInfoReq return errors.BadRequest(reason.UsernameDuplicate) } } - + Avatar, err := json.Marshal(req.Avatar) + if err != nil { + err = errors.BadRequest(reason.UserSetAvatar).WithError(err).WithStack() + return err + } userInfo := entity.User{} userInfo.ID = req.UserId - userInfo.Avatar = req.Avatar + userInfo.Avatar = string(Avatar) userInfo.DisplayName = req.DisplayName userInfo.Bio = req.Bio userInfo.BioHtml = req.BioHtml @@ -544,7 +549,7 @@ func (us *UserService) UserChangeEmailVerify(ctx context.Context, content string } err = us.userRepo.UpdateEmail(ctx, data.UserID, data.Email) if err != nil { - return err + return errors.BadRequest(reason.UserNotFound) } return nil }