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.

fix(email): fix the old email config format

LinkinStarsfbb3269f08c8
diff --git a/i18n/en_US.yaml b/i18n/en_US.yaml
index 3ec38cd3..09aa2e3b 100644
--- a/i18n/en_US.yaml
+++ b/i18n/en_US.yaml
@@ -261,6 +261,9 @@ backend:
upload:
unsupported_file_format:
other: Unsupported file format.
+ site_info:
+ config_not_found:
+ other: Site config not found.
reason:
spam:
name:
diff --git a/internal/base/reason/reason.go b/internal/base/reason/reason.go
index 91ee74c6..0d8c2144 100644
--- a/internal/base/reason/reason.go
+++ b/internal/base/reason/reason.go
@@ -61,7 +61,7 @@ const (
DatabaseConnectionFailed = "error.database.connection_failed"
InstallCreateTableFailed = "error.database.create_table_failed"
InstallConfigFailed = "error.install.create_config_failed"
- SiteInfoNotFound = "error.site_info.not_found"
+ SiteInfoConfigNotFound = "error.site_info.config_not_found"
UploadFileSourceUnsupported = "error.upload.source_unsupported"
UploadFileUnsupportedFileFormat = "error.upload.unsupported_file_format"
RecommendTagNotExist = "error.tag.recommend_tag_not_found"
diff --git a/internal/repo/config/config_repo.go b/internal/repo/config/config_repo.go
index c81b9af7..c5a155bd 100644
--- a/internal/repo/config/config_repo.go
+++ b/internal/repo/config/config_repo.go
@@ -80,8 +80,8 @@ func (cr configRepo) GetConfigByKey(ctx context.Context, key string) (c *entity.
func (cr configRepo) UpdateConfig(ctx context.Context, key string, value string) (err error) {
// check if key exists
- cf := &entity.Config{}
- exist, err := cr.data.DB.Context(ctx).Get(cf)
+ oldConfig := &entity.Config{}
+ exist, err := cr.data.DB.Context(ctx).Get(oldConfig)
if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
@@ -90,19 +90,20 @@ func (cr configRepo) UpdateConfig(ctx context.Context, key string, value string)
}
// update database
- _, err = cr.data.DB.Context(ctx).ID(cf.ID).Update(&entity.Config{Value: value})
+ _, err = cr.data.DB.Context(ctx).ID(oldConfig.ID).Update(&entity.Config{Value: value})
if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
- cacheVal := cf.JsonString()
+ oldConfig.Value = value
+ cacheVal := oldConfig.JsonString()
// update cache
if err := cr.data.Cache.SetString(ctx,
constant.ConfigKEY2ContentCacheKeyPrefix+key, cacheVal, -1); err != nil {
log.Error(err)
}
if err := cr.data.Cache.SetString(ctx,
- fmt.Sprintf("%s%d", constant.ConfigID2KEYCacheKeyPrefix, cf.ID), cacheVal, -1); err != nil {
+ fmt.Sprintf("%s%d", constant.ConfigID2KEYCacheKeyPrefix, oldConfig.ID), cacheVal, -1); err != nil {
log.Error(err)
}
return
diff --git a/internal/service/export/email_service.go b/internal/service/export/email_service.go
index 8ab8139f..2812aa66 100644
--- a/internal/service/export/email_service.go
+++ b/internal/service/export/email_service.go
@@ -54,19 +54,6 @@ type EmailConfig struct {
SMTPUsername string `json:"smtp_username"`
SMTPPassword string `json:"smtp_password"`
SMTPAuthentication bool `json:"smtp_authentication"`
-
- RegisterTitle string `json:"register_title"`
- RegisterBody string `json:"register_body"`
- PassResetTitle string `json:"pass_reset_title"`
- PassResetBody string `json:"pass_reset_body"`
- ChangeTitle string `json:"change_title"`
- ChangeBody string `json:"change_body"`
- TestTitle string `json:"test_title"`
- TestBody string `json:"test_body"`
- NewAnswerTitle string `json:"new_answer_title"`
- NewAnswerBody string `json:"new_answer_body"`
- NewCommentTitle string `json:"new_comment_title"`
- NewCommentBody string `json:"new_comment_body"`
}
func (e *EmailConfig) IsSSL() bool {
@@ -306,7 +293,8 @@ func (es *EmailService) GetEmailConfig(ctx context.Context) (ec *EmailConfig, er
ec = &EmailConfig{}
err = json.Unmarshal([]byte(emailConf), ec)
if err != nil {
- return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ log.Errorf("old email config format is invalid, you need to update smtp config: %v", err)
+ return nil, errors.BadRequest(reason.SiteInfoConfigNotFound)
}
return ec, nil
}
diff --git a/internal/service/siteinfo/siteinfo_service.go b/internal/service/siteinfo/siteinfo_service.go
index 0778bbbd..b8cb276e 100644
--- a/internal/service/siteinfo/siteinfo_service.go
+++ b/internal/service/siteinfo/siteinfo_service.go
@@ -227,9 +227,7 @@ func (s *SiteInfoService) SaveSiteUsers(ctx context.Context, req *schema.SiteUse
}
// GetSMTPConfig get smtp config
-func (s *SiteInfoService) GetSMTPConfig(ctx context.Context) (
- resp *schema.GetSMTPConfigResp, err error,
-) {
+func (s *SiteInfoService) GetSMTPConfig(ctx context.Context) (resp *schema.GetSMTPConfigResp, err error) {
emailConfig, err := s.emailService.GetEmailConfig(ctx)
if err != nil {
return nil, err
@@ -241,13 +239,10 @@ func (s *SiteInfoService) GetSMTPConfig(ctx context.Context) (
// UpdateSMTPConfig get smtp config
func (s *SiteInfoService) UpdateSMTPConfig(ctx context.Context, req *schema.UpdateSMTPConfigReq) (err error) {
- oldEmailConfig, err := s.emailService.GetEmailConfig(ctx)
- if err != nil {
- return err
- }
- _ = copier.Copy(oldEmailConfig, req)
+ ec := &export.EmailConfig{}
+ _ = copier.Copy(ec, req)
- err = s.emailService.SetEmailConfig(ctx, oldEmailConfig)
+ err = s.emailService.SetEmailConfig(ctx, ec)
if err != nil {
return err
}
@@ -258,7 +253,7 @@ func (s *SiteInfoService) UpdateSMTPConfig(ctx context.Context, req *schema.Upda
}
go s.emailService.SendAndSaveCode(ctx, req.TestEmailRecipient, title, body, "", "")
}
- return
+ return nil
}
func (s *SiteInfoService) GetSeo(ctx context.Context) (resp *schema.SiteSeoReq, err error) {