fix(email): fix the old email config format

This commit is contained in:
LinkinStars 2023-06-06 11:26:02 +08:00
parent 432bf21c98
commit 4a0fa27a34
5 changed files with 17 additions and 30 deletions

View File

@ -261,6 +261,9 @@ backend:
upload: upload:
unsupported_file_format: unsupported_file_format:
other: Unsupported file format. other: Unsupported file format.
site_info:
config_not_found:
other: Site config not found.
reason: reason:
spam: spam:
name: name:

View File

@ -61,7 +61,7 @@ const (
DatabaseConnectionFailed = "error.database.connection_failed" DatabaseConnectionFailed = "error.database.connection_failed"
InstallCreateTableFailed = "error.database.create_table_failed" InstallCreateTableFailed = "error.database.create_table_failed"
InstallConfigFailed = "error.install.create_config_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" UploadFileSourceUnsupported = "error.upload.source_unsupported"
UploadFileUnsupportedFileFormat = "error.upload.unsupported_file_format" UploadFileUnsupportedFileFormat = "error.upload.unsupported_file_format"
RecommendTagNotExist = "error.tag.recommend_tag_not_found" RecommendTagNotExist = "error.tag.recommend_tag_not_found"

View File

@ -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) { func (cr configRepo) UpdateConfig(ctx context.Context, key string, value string) (err error) {
// check if key exists // check if key exists
cf := &entity.Config{} oldConfig := &entity.Config{}
exist, err := cr.data.DB.Context(ctx).Get(cf) exist, err := cr.data.DB.Context(ctx).Get(oldConfig)
if err != nil { if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() 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 // 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 { if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
} }
cacheVal := cf.JsonString() oldConfig.Value = value
cacheVal := oldConfig.JsonString()
// update cache // update cache
if err := cr.data.Cache.SetString(ctx, if err := cr.data.Cache.SetString(ctx,
constant.ConfigKEY2ContentCacheKeyPrefix+key, cacheVal, -1); err != nil { constant.ConfigKEY2ContentCacheKeyPrefix+key, cacheVal, -1); err != nil {
log.Error(err) log.Error(err)
} }
if err := cr.data.Cache.SetString(ctx, 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) log.Error(err)
} }
return return

View File

@ -54,19 +54,6 @@ type EmailConfig struct {
SMTPUsername string `json:"smtp_username"` SMTPUsername string `json:"smtp_username"`
SMTPPassword string `json:"smtp_password"` SMTPPassword string `json:"smtp_password"`
SMTPAuthentication bool `json:"smtp_authentication"` 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 { func (e *EmailConfig) IsSSL() bool {
@ -306,7 +293,8 @@ func (es *EmailService) GetEmailConfig(ctx context.Context) (ec *EmailConfig, er
ec = &EmailConfig{} ec = &EmailConfig{}
err = json.Unmarshal([]byte(emailConf), ec) err = json.Unmarshal([]byte(emailConf), ec)
if err != nil { 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 return ec, nil
} }

View File

@ -227,9 +227,7 @@ func (s *SiteInfoService) SaveSiteUsers(ctx context.Context, req *schema.SiteUse
} }
// GetSMTPConfig get smtp config // GetSMTPConfig get smtp config
func (s *SiteInfoService) GetSMTPConfig(ctx context.Context) ( func (s *SiteInfoService) GetSMTPConfig(ctx context.Context) (resp *schema.GetSMTPConfigResp, err error) {
resp *schema.GetSMTPConfigResp, err error,
) {
emailConfig, err := s.emailService.GetEmailConfig(ctx) emailConfig, err := s.emailService.GetEmailConfig(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
@ -241,13 +239,10 @@ func (s *SiteInfoService) GetSMTPConfig(ctx context.Context) (
// UpdateSMTPConfig get smtp config // UpdateSMTPConfig get smtp config
func (s *SiteInfoService) UpdateSMTPConfig(ctx context.Context, req *schema.UpdateSMTPConfigReq) (err error) { func (s *SiteInfoService) UpdateSMTPConfig(ctx context.Context, req *schema.UpdateSMTPConfigReq) (err error) {
oldEmailConfig, err := s.emailService.GetEmailConfig(ctx) ec := &export.EmailConfig{}
if err != nil { _ = copier.Copy(ec, req)
return err
}
_ = copier.Copy(oldEmailConfig, req)
err = s.emailService.SetEmailConfig(ctx, oldEmailConfig) err = s.emailService.SetEmailConfig(ctx, ec)
if err != nil { if err != nil {
return err 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, "", "") go s.emailService.SendAndSaveCode(ctx, req.TestEmailRecipient, title, body, "", "")
} }
return return nil
} }
func (s *SiteInfoService) GetSeo(ctx context.Context) (resp *schema.SiteSeoReq, err error) { func (s *SiteInfoService) GetSeo(ctx context.Context) (resp *schema.SiteSeoReq, err error) {