gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

noopsender.go (3001B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package email
     19 
     20 import (
     21 	"bytes"
     22 	"text/template"
     23 
     24 	"github.com/superseriousbusiness/gotosocial/internal/config"
     25 	"github.com/superseriousbusiness/gotosocial/internal/log"
     26 )
     27 
     28 // NewNoopSender returns a no-op email sender that will just execute the given sendCallback
     29 // every time it would otherwise send an email to the given toAddress with the given message value.
     30 //
     31 // Passing a nil function is also acceptable, in which case the send functions will just return nil.
     32 func NewNoopSender(sendCallback func(toAddress string, message string)) (Sender, error) {
     33 	templateBaseDir := config.GetWebTemplateBaseDir()
     34 
     35 	t, err := loadTemplates(templateBaseDir)
     36 	if err != nil {
     37 		return nil, err
     38 	}
     39 
     40 	return &noopSender{
     41 		sendCallback: sendCallback,
     42 		template:     t,
     43 	}, nil
     44 }
     45 
     46 type noopSender struct {
     47 	sendCallback func(toAddress string, message string)
     48 	template     *template.Template
     49 }
     50 
     51 func (s *noopSender) SendConfirmEmail(toAddress string, data ConfirmData) error {
     52 	return s.sendTemplate(confirmTemplate, confirmSubject, data, toAddress)
     53 }
     54 
     55 func (s *noopSender) SendResetEmail(toAddress string, data ResetData) error {
     56 	return s.sendTemplate(resetTemplate, resetSubject, data, toAddress)
     57 }
     58 
     59 func (s *noopSender) SendTestEmail(toAddress string, data TestData) error {
     60 	return s.sendTemplate(testTemplate, testSubject, data, toAddress)
     61 }
     62 
     63 func (s *noopSender) SendNewReportEmail(toAddresses []string, data NewReportData) error {
     64 	return s.sendTemplate(newReportTemplate, newReportSubject, data, toAddresses...)
     65 }
     66 
     67 func (s *noopSender) SendReportClosedEmail(toAddress string, data ReportClosedData) error {
     68 	return s.sendTemplate(reportClosedTemplate, reportClosedSubject, data, toAddress)
     69 }
     70 
     71 func (s *noopSender) sendTemplate(template string, subject string, data any, toAddresses ...string) error {
     72 	buf := &bytes.Buffer{}
     73 	if err := s.template.ExecuteTemplate(buf, template, data); err != nil {
     74 		return err
     75 	}
     76 
     77 	msg, err := assembleMessage(subject, buf.String(), "test@example.org", toAddresses...)
     78 	if err != nil {
     79 		return err
     80 	}
     81 
     82 	log.Tracef(nil, "NOT SENDING email to %s with contents: %s", toAddresses, msg)
     83 
     84 	if s.sendCallback != nil {
     85 		s.sendCallback(toAddresses[0], string(msg))
     86 	}
     87 
     88 	return nil
     89 }