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.

feat(avatar): Set default user avatar by config

LinkinStars8c871f1399cd
diff --git a/internal/base/constant/site_info.go b/internal/base/constant/site_info.go
new file mode 100644
index 00000000..dced604f
--- /dev/null
+++ b/internal/base/constant/site_info.go
@@ -0,0 +1,5 @@
+package constant
+
+var (
+ DefaultAvatar = "system"
+)
diff --git a/internal/schema/siteinfo_schema.go b/internal/schema/siteinfo_schema.go
index ec17b64b..31709545 100644
--- a/internal/schema/siteinfo_schema.go
+++ b/internal/schema/siteinfo_schema.go
@@ -40,8 +40,9 @@ func (r *SiteGeneralReq) FormatSiteUrl() {
// SiteInterfaceReq site interface request
type SiteInterfaceReq struct {
- Language string `validate:"required,gt=1,lte=128" form:"language" json:"language"`
- TimeZone string `validate:"required,gt=1,lte=128" form:"time_zone" json:"time_zone"`
+ Language string `validate:"required,gt=1,lte=128" form:"language" json:"language"`
+ TimeZone string `validate:"required,gt=1,lte=128" form:"time_zone" json:"time_zone"`
+ DefaultAvatar string `validate:"required,oneof=system gravatar" form:"default_avatar" json:"default_avatar"`
}
// SiteBrandingReq site branding request
diff --git a/internal/schema/user_schema.go b/internal/schema/user_schema.go
index 308568c8..7e6e2cc1 100644
--- a/internal/schema/user_schema.go
+++ b/internal/schema/user_schema.go
@@ -3,11 +3,13 @@ package schema
import (
"encoding/json"
+ "github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/reason"
"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/answerdev/answer/pkg/gravatar"
"github.com/jinzhu/copier"
"github.com/segmentfault/pacman/errors"
)
@@ -74,7 +76,7 @@ type GetUserResp struct {
func (r *GetUserResp) GetFromUserEntity(userInfo *entity.User) {
_ = copier.Copy(r, userInfo)
- r.Avatar = FormatAvatarInfo(userInfo.Avatar)
+ r.Avatar = FormatAvatarInfo(userInfo.Avatar, userInfo.EMail)
r.CreatedAt = userInfo.CreatedAt.Unix()
r.LastLoginDate = userInfo.LastLoginDate.Unix()
statusShow, ok := UserStatusShow[userInfo.Status]
@@ -102,7 +104,13 @@ func (r *GetUserToSetShowResp) GetFromUserEntity(userInfo *entity.User) {
r.Avatar = avatarInfo
}
-func FormatAvatarInfo(avatarJson string) string {
+func FormatAvatarInfo(avatarJson, email string) (res string) {
+ defer func() {
+ if constant.DefaultAvatar == "gravatar" && len(res) == 0 {
+ res = gravatar.GetAvatarURL(email)
+ }
+ }()
+
if avatarJson == "" {
return ""
}
@@ -170,7 +178,7 @@ type GetOtherUserInfoByUsernameResp struct {
func (r *GetOtherUserInfoByUsernameResp) GetFromUserEntity(userInfo *entity.User) {
_ = copier.Copy(r, userInfo)
- Avatar := FormatAvatarInfo(userInfo.Avatar)
+ Avatar := FormatAvatarInfo(userInfo.Avatar, userInfo.EMail)
r.Avatar = Avatar
r.CreatedAt = userInfo.CreatedAt.Unix()
diff --git a/internal/service/siteinfo/siteinfo_service.go b/internal/service/siteinfo/siteinfo_service.go
index 50a5bf45..119012cb 100644
--- a/internal/service/siteinfo/siteinfo_service.go
+++ b/internal/service/siteinfo/siteinfo_service.go
@@ -29,6 +29,14 @@ func NewSiteInfoService(
siteInfoCommonService *siteinfo_common.SiteInfoCommonService,
emailService *export.EmailService,
tagCommonService *tagcommon.TagCommonService) *SiteInfoService {
+
+ resp, err := siteInfoCommonService.GetSiteInterface(context.Background())
+ if err != nil {
+ log.Error(err)
+ } else {
+ constant.DefaultAvatar = resp.DefaultAvatar
+ }
+
return &SiteInfoService{
siteInfoRepo: siteInfoRepo,
siteInfoCommonService: siteInfoCommonService,
@@ -132,6 +140,9 @@ func (s *SiteInfoService) SaveSiteInterface(ctx context.Context, req schema.Site
}
err = s.siteInfoRepo.SaveByType(ctx, siteType, &data)
+ if err == nil {
+ constant.DefaultAvatar = req.DefaultAvatar
+ }
return
}
diff --git a/internal/service/user_admin/user_backyard.go b/internal/service/user_admin/user_backyard.go
index ae09ce85..96bbe8ca 100644
--- a/internal/service/user_admin/user_backyard.go
+++ b/internal/service/user_admin/user_backyard.go
@@ -233,7 +233,7 @@ func (us *UserAdminService) GetUserPage(ctx context.Context, req *schema.GetUser
resp := make([]*schema.GetUserPageResp, 0)
for _, u := range users {
- avatar := schema.FormatAvatarInfo(u.Avatar)
+ avatar := schema.FormatAvatarInfo(u.Avatar, u.EMail)
t := &schema.GetUserPageResp{
UserID: u.ID,
CreatedAt: u.CreatedAt.Unix(),
diff --git a/internal/service/user_common/user.go b/internal/service/user_common/user.go
index da66f4ce..9eb91d9d 100644
--- a/internal/service/user_common/user.go
+++ b/internal/service/user_common/user.go
@@ -90,7 +90,7 @@ func (us *UserCommon) FormatUserBasicInfo(ctx context.Context, userInfo *entity.
userBasicInfo.Username = userInfo.Username
userBasicInfo.Rank = userInfo.Rank
userBasicInfo.DisplayName = userInfo.DisplayName
- userBasicInfo.Avatar = schema.FormatAvatarInfo(userInfo.Avatar)
+ userBasicInfo.Avatar = schema.FormatAvatarInfo(userInfo.Avatar, userInfo.EMail)
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 e7bd4374..a2984100 100644
--- a/internal/service/user_service.go
+++ b/internal/service/user_service.go
@@ -700,7 +700,7 @@ func (us *UserService) getUserInfoMapping(ctx context.Context, userIDs []string)
return nil, err
}
for _, user := range userInfoList {
- user.Avatar = schema.FormatAvatarInfo(user.Avatar)
+ user.Avatar = schema.FormatAvatarInfo(user.Avatar, user.EMail)
userInfoMapping[user.ID] = user
}
return userInfoMapping, nil