Merge branch 'ai_fix_something' into 'main'

fix send email use web site name

See merge request opensource/answer!102
This commit is contained in:
linkinstar 2022-10-25 03:39:12 +00:00
commit eaa3672f2f
4 changed files with 60 additions and 20 deletions

View File

@ -96,7 +96,8 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
userRankRepo := rank.NewUserRankRepo(dataData, configRepo)
userActiveActivityRepo := activity.NewUserActiveActivityRepo(dataData, activityRepo, userRankRepo, configRepo)
emailRepo := export.NewEmailRepo(dataData)
emailService := export2.NewEmailService(configRepo, emailRepo)
siteInfoRepo := repo.NewSiteInfo(dataData)
emailService := export2.NewEmailService(configRepo, emailRepo, siteInfoRepo)
userService := service.NewUserService(userRepo, userActiveActivityRepo, emailService, authService, serviceConf)
captchaRepo := captcha.NewCaptchaRepo(dataData)
captchaService := action.NewCaptchaService(captchaRepo)
@ -162,7 +163,6 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
reasonService := reason2.NewReasonService(reasonRepo)
reasonController := controller.NewReasonController(reasonService)
themeController := controller_backyard.NewThemeController()
siteInfoRepo := repo.NewSiteInfo(dataData)
siteInfoService := service.NewSiteInfoService(siteInfoRepo, emailService)
siteInfoController := controller_backyard.NewSiteInfoController(siteInfoService)
siteinfoController := controller.NewSiteinfoController(siteInfoService)

View File

@ -7,7 +7,10 @@ import (
"html/template"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/internal/service/config"
"github.com/answerdev/answer/internal/service/siteinfo_common"
"github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/log"
"golang.org/x/net/context"
@ -16,8 +19,9 @@ import (
// EmailService kit service
type EmailService struct {
configRepo config.ConfigRepo
emailRepo EmailRepo
configRepo config.ConfigRepo
emailRepo EmailRepo
siteInfoRepo siteinfo_common.SiteInfoRepo
}
// EmailRepo email repository
@ -27,10 +31,11 @@ type EmailRepo interface {
}
// NewEmailService email service
func NewEmailService(configRepo config.ConfigRepo, emailRepo EmailRepo) *EmailService {
func NewEmailService(configRepo config.ConfigRepo, emailRepo EmailRepo, siteInfoRepo siteinfo_common.SiteInfoRepo) *EmailService {
return &EmailService{
configRepo: configRepo,
emailRepo: emailRepo,
configRepo: configRepo,
emailRepo: emailRepo,
siteInfoRepo: siteInfoRepo,
}
}
@ -120,14 +125,35 @@ func (es *EmailService) VerifyUrlExpired(ctx context.Context, code string) (cont
return content
}
func (es *EmailService) RegisterTemplate(registerUrl string) (title, body string, err error) {
func (es *EmailService) GetSiteGeneral(ctx context.Context) (resp schema.SiteGeneralResp, err error) {
var (
siteType = "general"
siteInfo *entity.SiteInfo
exist bool
)
resp = schema.SiteGeneralResp{}
siteInfo, exist, err = es.siteInfoRepo.GetByType(ctx, siteType)
if !exist {
return
}
_ = json.Unmarshal([]byte(siteInfo.Content), &resp)
return
}
func (es *EmailService) RegisterTemplate(ctx context.Context, registerUrl string) (title, body string, err error) {
ec, err := es.GetEmailConfig()
if err != nil {
return
}
siteinfo, err := es.GetSiteGeneral(ctx)
if err != nil {
return
}
templateData := RegisterTemplateData{
SiteName: ec.FromName, RegisterUrl: registerUrl,
SiteName: siteinfo.Name, RegisterUrl: registerUrl,
}
tmpl, err := template.New("register_title").Parse(ec.RegisterTitle)
if err != nil {
@ -152,13 +178,18 @@ func (es *EmailService) RegisterTemplate(registerUrl string) (title, body string
return titleBuf.String(), bodyBuf.String(), nil
}
func (es *EmailService) PassResetTemplate(passResetUrl string) (title, body string, err error) {
func (es *EmailService) PassResetTemplate(ctx context.Context, passResetUrl string) (title, body string, err error) {
ec, err := es.GetEmailConfig()
if err != nil {
return
}
templateData := PassResetTemplateData{SiteName: ec.FromName, PassResetUrl: passResetUrl}
siteinfo, err := es.GetSiteGeneral(ctx)
if err != nil {
return
}
templateData := PassResetTemplateData{SiteName: siteinfo.Name, PassResetUrl: passResetUrl}
tmpl, err := template.New("pass_reset_title").Parse(ec.PassResetTitle)
if err != nil {
return "", "", err
@ -181,14 +212,18 @@ func (es *EmailService) PassResetTemplate(passResetUrl string) (title, body stri
return titleBuf.String(), bodyBuf.String(), nil
}
func (es *EmailService) ChangeEmailTemplate(changeEmailUrl string) (title, body string, err error) {
func (es *EmailService) ChangeEmailTemplate(ctx context.Context, changeEmailUrl string) (title, body string, err error) {
ec, err := es.GetEmailConfig()
if err != nil {
return
}
siteinfo, err := es.GetSiteGeneral(ctx)
if err != nil {
return
}
templateData := ChangeEmailTemplateData{
SiteName: ec.FromName,
SiteName: siteinfo.Name,
ChangeEmailUrl: changeEmailUrl,
}
tmpl, err := template.New("email_change_title").Parse(ec.ChangeTitle)
@ -213,14 +248,19 @@ func (es *EmailService) ChangeEmailTemplate(changeEmailUrl string) (title, body
return titleBuf.String(), bodyBuf.String(), nil
}
func (es *EmailService) TestTemplate() (title, body string, err error) {
func (es *EmailService) TestTemplate(ctx context.Context) (title, body string, err error) {
ec, err := es.GetEmailConfig()
if err != nil {
return
}
siteinfo, err := es.GetSiteGeneral(ctx)
if err != nil {
return
}
templateData := TestTemplateData{
SiteName: ec.FromName,
SiteName: siteinfo.Name,
}
titleBuf := &bytes.Buffer{}

View File

@ -143,7 +143,7 @@ func (s *SiteInfoService) UpdateSMTPConfig(ctx context.Context, req *schema.Upda
return err
}
if len(req.TestEmailRecipient) > 0 {
title, body, err := s.emailService.TestTemplate()
title, body, err := s.emailService.TestTemplate(ctx)
if err != nil {
return err
}

View File

@ -166,7 +166,7 @@ func (us *UserService) RetrievePassWord(ctx context.Context, req *schema.UserRet
}
code := uuid.NewString()
verifyEmailUrl := fmt.Sprintf("%s/users/password-reset?code=%s", us.serviceConfig.WebHost, code)
title, body, err := us.emailService.PassResetTemplate(verifyEmailUrl)
title, body, err := us.emailService.PassResetTemplate(ctx, verifyEmailUrl)
if err != nil {
return "", err
}
@ -316,7 +316,7 @@ func (us *UserService) UserRegisterByEmail(ctx context.Context, registerUserInfo
}
code := uuid.NewString()
verifyEmailUrl := fmt.Sprintf("%s/users/account-activation?code=%s", us.serviceConfig.WebHost, code)
title, body, err := us.emailService.RegisterTemplate(verifyEmailUrl)
title, body, err := us.emailService.RegisterTemplate(ctx, verifyEmailUrl)
if err != nil {
return nil, err
}
@ -359,7 +359,7 @@ func (us *UserService) UserVerifyEmailSend(ctx context.Context, userID string) e
}
code := uuid.NewString()
verifyEmailUrl := fmt.Sprintf("%s/users/account-activation?code=%s", us.serviceConfig.WebHost, code)
title, body, err := us.emailService.RegisterTemplate(verifyEmailUrl)
title, body, err := us.emailService.RegisterTemplate(ctx, verifyEmailUrl)
if err != nil {
return err
}
@ -509,7 +509,7 @@ func (us *UserService) UserChangeEmailSendCode(ctx context.Context, req *schema.
}
code := uuid.NewString()
verifyEmailUrl := fmt.Sprintf("%s/users/confirm-new-email?code=%s", us.serviceConfig.WebHost, code)
title, body, err := us.emailService.ChangeEmailTemplate(verifyEmailUrl)
title, body, err := us.emailService.ChangeEmailTemplate(ctx, verifyEmailUrl)
if err != nil {
return err
}