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(upgrade): add gravatar base url

LinkinStarse947354a68de
diff --git a/internal/base/constant/site_info.go b/internal/base/constant/site_info.go
index e6b8cd99..88f3a4d8 100644
--- a/internal/base/constant/site_info.go
+++ b/internal/base/constant/site_info.go
@@ -1,6 +1,7 @@
package constant
var (
- DefaultAvatar = "system"
- DefaultSiteURL = ""
+ DefaultAvatar = "system"
+ DefaultGravatarBaseURL = "https://www.gravatar.com/avatar/"
+ DefaultSiteURL = ""
)
diff --git a/internal/migrations/init.go b/internal/migrations/init.go
index d47014f9..5ecf1d04 100644
--- a/internal/migrations/init.go
+++ b/internal/migrations/init.go
@@ -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,
diff --git a/internal/migrations/migrations.go b/internal/migrations/migrations.go
index 20e07eb2..1a5b214e 100644
--- a/internal/migrations/migrations.go
+++ b/internal/migrations/migrations.go
@@ -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
diff --git a/internal/migrations/v13.go b/internal/migrations/v13.go
new file mode 100644
index 00000000..278b1826
--- /dev/null
+++ b/internal/migrations/v13.go
@@ -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
+}
diff --git a/internal/schema/siteinfo_schema.go b/internal/schema/siteinfo_schema.go
index f0b659e3..6f8557fc 100644
--- a/internal/schema/siteinfo_schema.go
+++ b/internal/schema/siteinfo_schema.go
@@ -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
diff --git a/internal/service/siteinfo/siteinfo_service.go b/internal/service/siteinfo/siteinfo_service.go
index 986beb78..6edefd55 100644
--- a/internal/service/siteinfo/siteinfo_service.go
+++ b/internal/service/siteinfo/siteinfo_service.go
@@ -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
}
diff --git a/pkg/gravatar/gravatar.go b/pkg/gravatar/gravatar.go
index 6166af5e..92afcdd9 100644
--- a/pkg/gravatar/gravatar.go
+++ b/pkg/gravatar/gravatar.go
@@ -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