Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(renderer): abstract func of template renderer for multi-cases #585

Merged
merged 6 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conf/artalk.example.simple.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ email:
send_type: "smtp"
send_name: "{{reply_nick}}"
send_addr: "[email protected]"
mail_subject: "[{{site_name}}] You got a reply from @{{reply_nick}}"
mail_tpl: "default"
smtp:
host: "smtp.qq.com"
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/backend/email.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ email:
</div>
```

Artalk 内置许多预设的邮件模板,例如 `mail_tpl: "default"` 使用的就是:[/email-tpl/default.html](https://github.com/ArtalkJS/Artalk/blob/master/email-tpl/default.html)
Artalk 内置许多预设的邮件模板,例如 `mail_tpl: "default"` 使用的就是:[@ArtalkJS/Artalk:/internal/email/email_tpl/default.html](https://github.com/ArtalkJS/Artalk/blob/master/internal/email/email_tpl/default.html)

## 发向管理员的邮件

Expand Down
43 changes: 21 additions & 22 deletions internal/core/service_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/ArtalkJS/Artalk/internal/email"
"github.com/ArtalkJS/Artalk/internal/entity"
"github.com/ArtalkJS/Artalk/internal/log"
"github.com/ArtalkJS/Artalk/internal/template"
)

var _ Service = (*EmailService)(nil)
Expand Down Expand Up @@ -45,21 +46,7 @@ func (e *EmailService) Dispose() error {
return nil
}

func (e *EmailService) AsyncSendTo(subject string, body string, toAddr string) {
if !e.app.Conf().Email.Enabled {
return
}

e.queue.Push(&email.Email{
FromAddr: e.app.Conf().Email.SendAddr,
FromName: e.app.Conf().Email.SendName,
ToAddr: toAddr,
Subject: subject,
Body: body,
})
}

func (e *EmailService) GetRender(useAdminTplParam ...bool) *email.Render {
func (e *EmailService) GetRenderer(useAdminTplParam ...bool) *template.Renderer {
useAdminTpl := false
if len(useAdminTplParam) > 0 {
useAdminTpl = useAdminTplParam[0]
Expand All @@ -74,9 +61,21 @@ func (e *EmailService) GetRender(useAdminTplParam ...bool) *email.Render {
}

// create new email render instance
render := email.NewRender(e.app.Dao(), mailTplName)
return template.NewRenderer(e.app.Dao(), template.TYPE_EMAIL, template.NewFileLoader(mailTplName))
}

return render
func (e *EmailService) AsyncSendTo(subject string, body string, toAddr string) {
if !e.app.Conf().Email.Enabled {
return
}

e.queue.Push(&email.Email{
FromAddr: e.app.Conf().Email.SendAddr,
FromName: e.app.Conf().Email.SendName,
ToAddr: toAddr,
Subject: subject,
Body: body,
})
}

func (e *EmailService) AsyncSend(notify *entity.Notify) {
Expand All @@ -85,23 +84,23 @@ func (e *EmailService) AsyncSend(notify *entity.Notify) {
}

receiveUser := e.app.Dao().FetchUserForNotify(notify)
render := e.GetRender(receiveUser.IsAdmin)
renderer := e.GetRenderer(receiveUser.IsAdmin)

// render email body
mailBody := render.RenderEmailBody(notify)
mailBody := renderer.Render(notify)
mailSubject := ""
if !receiveUser.IsAdmin {
mailSubject = render.RenderCommon(e.app.Conf().Email.MailSubject, notify)
mailSubject = renderer.Render(notify, e.app.Conf().Email.MailSubject)
} else {
mailSubject = render.RenderCommon(e.app.Conf().AdminNotify.Email.MailSubject, notify)
mailSubject = renderer.Render(notify, e.app.Conf().AdminNotify.Email.MailSubject)
}

log.Debug(time.Now(), " "+receiveUser.Email)

// add email send task to queue
e.queue.Push(&email.Email{
FromAddr: e.app.Conf().Email.SendAddr,
FromName: render.RenderCommon(e.app.Conf().Email.SendName, notify),
FromName: renderer.Render(notify, e.app.Conf().Email.SendName),
ToAddr: receiveUser.Email,
Subject: mailSubject,
Body: mailBody,
Expand Down
8 changes: 0 additions & 8 deletions internal/core/service_notify.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package core

import (
"github.com/ArtalkJS/Artalk/internal/email"
"github.com/ArtalkJS/Artalk/internal/entity"
"github.com/ArtalkJS/Artalk/internal/notify_pusher"
)
Expand Down Expand Up @@ -29,13 +28,6 @@ func (s *NotifyService) Init() error {
emailService.AsyncSend(notify)
return nil
},
EmailRender: func() (*email.Render, error) {
emailService, err := AppService[*EmailService](s.app)
if err != nil {
return nil, err
}
return emailService.GetRender(), nil
},
})

return nil
Expand Down
98 changes: 0 additions & 98 deletions internal/email/render.go

This file was deleted.

124 changes: 0 additions & 124 deletions internal/email/render_utils.go

This file was deleted.

8 changes: 3 additions & 5 deletions internal/notify_pusher/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/ArtalkJS/Artalk/internal/config"
"github.com/ArtalkJS/Artalk/internal/dao"
"github.com/ArtalkJS/Artalk/internal/email"
"github.com/ArtalkJS/Artalk/internal/entity"
"github.com/nikoksr/notify"
"github.com/nikoksr/notify/service/dingding"
Expand All @@ -17,9 +16,9 @@ import (
type NotifyPusherConf struct {
config.AdminNotifyConf
Dao *dao.Dao
// bridge func to email push
EmailPush func(notify *entity.Notify) error
EmailRender func() (*email.Render, error)

// Provide a custom function to bridge the gap between Notify pusher and Email pusher
EmailPush func(notify *entity.Notify) error
}

type NotifyPusher struct {
Expand All @@ -30,7 +29,6 @@ type NotifyPusher struct {
}

func NewNotifyPusher(conf *NotifyPusherConf) *NotifyPusher {
// 初始化 Notify
pusher := &NotifyPusher{
conf: conf,
dao: conf.Dao,
Expand Down
Loading