This is an automated email from the ASF dual-hosted git repository. linkinstar pushed a commit to branch feat/1.6.0/user in repository https://gitbox.apache.org/repos/asf/answer.git
commit 550a92684e1936200350e9db57ebf1106087a5f7 Author: LinkinStars <[email protected]> AuthorDate: Thu Jun 26 11:20:23 2025 +0800 feat(migrations): move user config to interface --- internal/migrations/init.go | 6 +- internal/migrations/migrations.go | 1 + internal/migrations/v27.go | 71 ++++++++++++++++++++++ internal/schema/siteinfo_schema.go | 6 +- internal/service/content/user_service.go | 30 +++------ .../service/siteinfo_common/siteinfo_service.go | 2 +- 6 files changed, 91 insertions(+), 25 deletions(-) diff --git a/internal/migrations/init.go b/internal/migrations/init.go index 610e24d8..fde27cdd 100644 --- a/internal/migrations/init.go +++ b/internal/migrations/init.go @@ -180,8 +180,10 @@ func (m *Mentor) initSiteInfoInterface() { } interfaceData := map[string]string{ - "language": m.userData.Language, - "time_zone": localTimezone, + "language": m.userData.Language, + "time_zone": localTimezone, + "default_avatar": "gravatar", + "gravatar_base_url": "https://www.gravatar.com/avatar/", } interfaceDataBytes, _ := json.Marshal(interfaceData) _, m.err = m.engine.Context(m.ctx).Insert(&entity.SiteInfo{ diff --git a/internal/migrations/migrations.go b/internal/migrations/migrations.go index 212bf14b..4b5335b0 100644 --- a/internal/migrations/migrations.go +++ b/internal/migrations/migrations.go @@ -102,6 +102,7 @@ var migrations = []Migration{ NewMigration("v1.4.2", "add the number of question links", addQuestionLinkedCount, true), NewMigration("v1.4.5", "add file record", addFileRecord, true), NewMigration("v1.5.1", "add plugin kv storage", addPluginKVStorage, true), + NewMigration("v1.6.0", "move user config to interface", moveUserConfigToInterface, true), } func GetMigrations() []Migration { diff --git a/internal/migrations/v27.go b/internal/migrations/v27.go new file mode 100644 index 00000000..7f089a87 --- /dev/null +++ b/internal/migrations/v27.go @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package migrations + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/apache/answer/internal/base/constant" + "github.com/apache/answer/internal/entity" + "github.com/apache/answer/internal/schema" + "xorm.io/xorm" +) + +func moveUserConfigToInterface(ctx context.Context, x *xorm.Engine) error { + // Get old interface config + interfaceSiteInfo := &entity.SiteInfo{Type: constant.SiteTypeInterface} + exist, err := x.Context(ctx).Get(interfaceSiteInfo) + if err != nil { + return fmt.Errorf("get config failed: %w", err) + } + if !exist { + return fmt.Errorf("interface site info not found") + } + + interfaceConfig := &schema.SiteInterfaceReq{} + _ = json.Unmarshal([]byte(interfaceSiteInfo.Content), interfaceSiteInfo) + + // Get old user config + usersConfig := &entity.SiteInfo{Type: constant.SiteTypeUsers} + exist, err = x.Context(ctx).Get(usersConfig) + if err != nil { + return fmt.Errorf("get config failed: %w", err) + } + if !exist { + return fmt.Errorf("users site info not found") + } + + siteUsers := &schema.SiteUsersReq{} + _ = json.Unmarshal([]byte(usersConfig.Content), siteUsers) + + interfaceConfig.DefaultAvatar = siteUsers.DefaultAvatar + interfaceConfig.GravatarBaseURL = siteUsers.GravatarBaseURL + + interfaceConfigByte, _ := json.Marshal(interfaceConfig) + interfaceSiteInfo.Content = string(interfaceConfigByte) + + _, err = x.Context(ctx).ID(interfaceSiteInfo.ID).Update(interfaceSiteInfo) + if err != nil { + return fmt.Errorf("insert site info failed: %w", err) + } + return nil +} diff --git a/internal/schema/siteinfo_schema.go b/internal/schema/siteinfo_schema.go index 7e0c408f..19f01ffd 100644 --- a/internal/schema/siteinfo_schema.go +++ b/internal/schema/siteinfo_schema.go @@ -59,8 +59,10 @@ 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" json:"default_avatar"` + GravatarBaseURL string `validate:"omitempty" json:"gravatar_base_url"` } // SiteBrandingReq site branding request diff --git a/internal/service/content/user_service.go b/internal/service/content/user_service.go index ece3a86d..1c94d22d 100644 --- a/internal/service/content/user_service.go +++ b/internal/service/content/user_service.go @@ -314,12 +314,8 @@ func (us *UserService) UserModifyPassword(ctx context.Context, req *schema.UserM // UpdateInfo update user info func (us *UserService) UpdateInfo(ctx context.Context, req *schema.UpdateInfoRequest) ( errFields []*validator.FormErrorField, err error) { - siteUsers, err := us.siteInfoService.GetSiteUsers(ctx) - if err != nil { - return nil, err - } - if siteUsers.AllowUpdateUsername && len(req.Username) > 0 { + if len(req.Username) > 0 { if checker.IsInvalidUsername(req.Username) { return append(errFields, &validator.FormErrorField{ ErrorField: "username", @@ -359,7 +355,7 @@ func (us *UserService) UpdateInfo(ctx context.Context, req *schema.UpdateInfoReq return nil, errors.BadRequest(reason.UserNotFound) } - cond := us.formatUserInfoForUpdateInfo(oldUserInfo, req, siteUsers) + cond := us.formatUserInfoForUpdateInfo(oldUserInfo, req) us.cleanUpRemovedAvatar(ctx, oldUserInfo.Avatar, cond.Avatar) @@ -407,7 +403,7 @@ func (us *UserService) cleanUpRemovedAvatar( } func (us *UserService) formatUserInfoForUpdateInfo( - oldUserInfo *entity.User, req *schema.UpdateInfoRequest, siteUsersConf *schema.SiteUsersResp) *entity.User { + oldUserInfo *entity.User, req *schema.UpdateInfoRequest) *entity.User { avatar, _ := json.Marshal(req.Avatar) userInfo := &entity.User{} @@ -420,25 +416,19 @@ func (us *UserService) formatUserInfoForUpdateInfo( userInfo.Location = oldUserInfo.Location userInfo.ID = req.UserID - if len(req.DisplayName) > 0 && siteUsersConf.AllowUpdateDisplayName { + if len(req.DisplayName) > 0 { userInfo.DisplayName = req.DisplayName } - if len(req.Username) > 0 && siteUsersConf.AllowUpdateUsername { + if len(req.Username) > 0 { userInfo.Username = req.Username } - if len(avatar) > 0 && siteUsersConf.AllowUpdateAvatar { + if len(avatar) > 0 { userInfo.Avatar = string(avatar) } - if siteUsersConf.AllowUpdateBio { - userInfo.Bio = req.Bio - userInfo.BioHTML = req.BioHTML - } - if siteUsersConf.AllowUpdateWebsite { - userInfo.Website = req.Website - } - if siteUsersConf.AllowUpdateLocation { - userInfo.Location = req.Location - } + userInfo.Bio = req.Bio + userInfo.BioHTML = req.BioHTML + userInfo.Website = req.Website + userInfo.Location = req.Location return userInfo } diff --git a/internal/service/siteinfo_common/siteinfo_service.go b/internal/service/siteinfo_common/siteinfo_service.go index 0c896c2b..ef4869cc 100644 --- a/internal/service/siteinfo_common/siteinfo_service.go +++ b/internal/service/siteinfo_common/siteinfo_service.go @@ -123,7 +123,7 @@ func (s *siteInfoCommonService) FormatListAvatar(ctx context.Context, userList [ func (s *siteInfoCommonService) getAvatarDefaultConfig(ctx context.Context) (string, string) { gravatarBaseURL, defaultAvatar := constant.DefaultGravatarBaseURL, constant.DefaultAvatar - usersConfig, err := s.GetSiteUsers(ctx) + usersConfig, err := s.GetSiteInterface(ctx) if err != nil { log.Error(err) }
