gotosocial/internal/email/noopsender.go

75 lines
2.2 KiB
Go
Raw Normal View History

2021-10-17 16:19:13 +02:00
/*
GoToSocial
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package email
2021-10-17 16:59:57 +02:00
import (
"bytes"
"html/template"
)
2021-10-17 16:19:13 +02:00
// NewNoopSender returns a no-op email sender that will just execute the given sendCallback
2021-10-17 16:59:57 +02:00
// every time it would otherwise send an email to the given toAddress with the given message value.
2021-10-17 16:19:13 +02:00
//
// Passing a nil function is also acceptable, in which case the send functions will just return nil.
2021-10-17 16:59:57 +02:00
func NewNoopSender(templateBaseDir string, sendCallback func(toAddress string, message string)) (Sender, error) {
t, err := loadTemplates(templateBaseDir)
if err != nil {
return nil, err
}
2021-10-17 16:19:13 +02:00
return &noopSender{
sendCallback: sendCallback,
2021-10-17 16:59:57 +02:00
template: t,
}, nil
2021-10-17 16:19:13 +02:00
}
type noopSender struct {
2021-10-17 16:59:57 +02:00
sendCallback func(toAddress string, message string)
template *template.Template
2021-10-17 16:19:13 +02:00
}
func (s *noopSender) SendConfirmEmail(toAddress string, data ConfirmData) error {
2021-10-17 16:59:57 +02:00
buf := &bytes.Buffer{}
if err := s.template.ExecuteTemplate(buf, confirmTemplate, data); err != nil {
return err
}
confirmBody := buf.String()
msg := assembleMessage(confirmSubject, confirmBody, toAddress, "test@example.org")
2021-10-17 16:19:13 +02:00
if s.sendCallback != nil {
2021-10-17 16:59:57 +02:00
s.sendCallback(toAddress, string(msg))
2021-10-17 16:19:13 +02:00
}
return nil
}
func (s *noopSender) SendResetEmail(toAddress string, data ResetData) error {
2021-10-17 16:59:57 +02:00
buf := &bytes.Buffer{}
if err := s.template.ExecuteTemplate(buf, resetTemplate, data); err != nil {
return err
2021-10-17 16:19:13 +02:00
}
2021-10-17 16:59:57 +02:00
resetBody := buf.String()
2021-10-17 16:19:13 +02:00
2021-10-17 16:59:57 +02:00
msg := assembleMessage(resetSubject, resetBody, toAddress, "test@example.org")
2021-10-17 16:19:13 +02:00
2021-10-17 16:59:57 +02:00
if s.sendCallback != nil {
s.sendCallback(toAddress, string(msg))
}
return nil
2021-10-17 16:19:13 +02:00
}