feat(upgrade): add gravatar base url

This commit is contained in:
LinkinStars 2023-05-15 11:20:50 +08:00
parent 0aa98fb2af
commit e947354a68
7 changed files with 53 additions and 15 deletions

View File

@ -1,6 +1,7 @@
package constant
var (
DefaultAvatar = "system"
DefaultSiteURL = ""
DefaultAvatar = "system"
DefaultGravatarBaseURL = "https://www.gravatar.com/avatar/"
DefaultSiteURL = ""
)

View File

@ -182,6 +182,7 @@ func initSiteInfo(engine *xorm.Engine, language, siteName, siteURL, contactEmail
usersData := map[string]any{
"default_avatar": "gravatar",
"default_gravatar_base_url": "https://www.gravatar.com/avatar/",
"allow_update_display_name": true,
"allow_update_username": true,
"allow_update_avatar": true,

View File

@ -56,12 +56,13 @@ var migrations = []Migration{
NewMigration("add user role", addRoleFeatures, false),
NewMigration("add theme and private mode", addThemeAndPrivateMode, true),
NewMigration("add new answer notification", addNewAnswerNotification, true),
NewMigration("add plugin", addPlugin, false),
NewMigration("add user pin hide features", addRolePinAndHideFeatures, true),
NewMigration("update accept answer rank", updateAcceptAnswerRank, true),
NewMigration("add plugin", addPlugin, false),
NewMigration("add login limitations", addLoginLimitations, true),
NewMigration("update user pin hide features", updateRolePinAndHideFeatures, true),
NewMigration("update question post time", updateQuestionPostTime, true),
NewMigration("add login limitations", addLoginLimitations, true),
NewMigration("add gravatar base url", addGravatarBaseURL, false),
}
// GetCurrentDBVersion returns the current db version

View File

@ -0,0 +1,34 @@
package migrations
import (
"encoding/json"
"fmt"
"github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/schema"
"xorm.io/xorm"
)
func addGravatarBaseURL(x *xorm.Engine) error {
usersSiteInfo := &entity.SiteInfo{
Type: constant.SiteTypeUsers,
}
exist, err := x.Get(usersSiteInfo)
if err != nil {
return fmt.Errorf("get config failed: %w", err)
}
if exist {
content := &schema.SiteUsersReq{}
_ = json.Unmarshal([]byte(usersSiteInfo.Content), content)
content.GravatarBaseURL = "https://www.gravatar.com/avatar/"
data, _ := json.Marshal(content)
usersSiteInfo.Content = string(data)
_, err = x.ID(usersSiteInfo.ID).Cols("content").Update(usersSiteInfo)
if err != nil {
return fmt.Errorf("update site info failed: %w", err)
}
}
return nil
}

View File

@ -94,13 +94,14 @@ type GetSiteLegalInfoResp struct {
// SiteUsersReq site users config request
type SiteUsersReq struct {
DefaultAvatar string `validate:"required,oneof=system gravatar" form:"default_avatar" json:"default_avatar"`
AllowUpdateDisplayName bool `form:"allow_update_display_name" json:"allow_update_display_name"`
AllowUpdateUsername bool `form:"allow_update_username" json:"allow_update_username"`
AllowUpdateAvatar bool `form:"allow_update_avatar" json:"allow_update_avatar"`
AllowUpdateBio bool `form:"allow_update_bio" json:"allow_update_bio"`
AllowUpdateWebsite bool `form:"allow_update_website" json:"allow_update_website"`
AllowUpdateLocation bool `form:"allow_update_location" json:"allow_update_location"`
DefaultAvatar string `validate:"required,oneof=system gravatar" json:"default_avatar"`
GravatarBaseURL string `json:"gravatar_base_url"`
AllowUpdateDisplayName bool `json:"allow_update_display_name"`
AllowUpdateUsername bool `json:"allow_update_username"`
AllowUpdateAvatar bool `json:"allow_update_avatar"`
AllowUpdateBio bool `json:"allow_update_bio"`
AllowUpdateWebsite bool `json:"allow_update_website"`
AllowUpdateLocation bool `json:"allow_update_location"`
}
// SiteLoginReq site login request

View File

@ -39,6 +39,7 @@ func NewSiteInfoService(
usersSiteInfo, _ := siteInfoCommonService.GetSiteUsers(context.Background())
if usersSiteInfo != nil {
constant.DefaultAvatar = usersSiteInfo.DefaultAvatar
constant.DefaultGravatarBaseURL = usersSiteInfo.GravatarBaseURL
}
generalSiteInfo, _ := siteInfoCommonService.GetSiteGeneral(context.Background())
if generalSiteInfo != nil {
@ -229,6 +230,7 @@ func (s *SiteInfoService) SaveSiteUsers(ctx context.Context, req *schema.SiteUse
err = s.siteInfoRepo.SaveByType(ctx, constant.SiteTypeUsers, data)
if err == nil {
constant.DefaultAvatar = req.DefaultAvatar
constant.DefaultGravatarBaseURL = req.GravatarBaseURL
}
return err
}

View File

@ -5,17 +5,15 @@ import (
"encoding/hex"
"fmt"
"net/url"
)
const (
defaultURLPrefix = "https://www.gravatar.com/avatar/"
"github.com/answerdev/answer/internal/base/constant"
)
// GetAvatarURL get avatar url from gravatar by email
func GetAvatarURL(email string) string {
h := md5.New()
h.Write([]byte(email))
return defaultURLPrefix + hex.EncodeToString(h.Sum(nil))
return constant.DefaultGravatarBaseURL + hex.EncodeToString(h.Sum(nil))
}
// Resize resize avatar by pixel