From 1ce3cf3d1ffc25b11fbdc7bb5c780ba2aef7202b Mon Sep 17 00:00:00 2001 From: Dmitry Verkhoturov Date: Mon, 19 Oct 2020 21:54:46 +0200 Subject: [PATCH] simplify admin email notifications --- backend/app/cmd/server.go | 7 +++---- backend/app/notify/email.go | 17 +++++++++-------- backend/app/notify/email_test.go | 20 ++++++++++---------- backend/app/notify/notify.go | 1 - backend/app/rest/api/rest.go | 2 -- backend/app/rest/api/rest_private.go | 8 +------- backend/app/rest/api/rest_private_test.go | 6 +----- backend/app/rest/api/rest_test.go | 1 - 8 files changed, 24 insertions(+), 38 deletions(-) diff --git a/backend/app/cmd/server.go b/backend/app/cmd/server.go index 4aaf403a..b35bc325 100644 --- a/backend/app/cmd/server.go +++ b/backend/app/cmd/server.go @@ -457,10 +457,6 @@ func (s *ServerCommand) newServerApp() (*serverApp, error) { ProxyCORS: s.ProxyCORS, } - if s.Notify.Email.AdminNotifications { - srv.AdminEmail = s.Admin.Shared.Email - } - srv.ScoreThresholds.Low, srv.ScoreThresholds.Critical = s.LowScore, s.CriticalScore var devAuth *provider.DevAuthServer @@ -847,6 +843,9 @@ func (s *ServerCommand) makeNotify(dataStore *service.DataStore, authenticator * return tkn, nil }, } + if s.Notify.Email.AdminNotifications { + emailParams.AdminEmails = s.Admin.Shared.Email + } smtpParams := notify.SMTPParams{ Host: s.SMTP.Host, Port: s.SMTP.Port, diff --git a/backend/app/notify/email.go b/backend/app/notify/email.go index e08321f1..1ea0a26a 100644 --- a/backend/app/notify/email.go +++ b/backend/app/notify/email.go @@ -22,12 +22,13 @@ import ( // EmailParams contain settings for email notifications type EmailParams struct { - From string // from email address - MsgTemplatePath string // path to request message template - VerificationSubject string // verification message sub - VerificationTemplatePath string // path to verification template - SubscribeURL string // full subscribe handler URL - UnsubscribeURL string // full unsubscribe handler URL + From string // from email address + AdminEmails []string // administrator emails to send copy of comment notification to + MsgTemplatePath string // path to request message template + VerificationSubject string // verification message sub + VerificationTemplatePath string // path to verification template + SubscribeURL string // full subscribe handler URL + UnsubscribeURL string // full unsubscribe handler URL TokenGenFn func(userID, email, site string) (string, error) // Unsubscribe token generation function } @@ -165,7 +166,7 @@ func (e *Email) setTemplates() error { return nil } -// Send email about comment reply to Request.Emails and Request.AdminEmails +// Send email about comment reply to Request.Emails and Email.AdminEmails // if they're set. // Thread safe func (e *Email) Send(ctx context.Context, req Request) error { @@ -182,7 +183,7 @@ func (e *Email) Send(ctx context.Context, req Request) error { result = multierror.Append(errors.Wrapf(err, "problem sending user email notification to %q", email)) } - for _, email := range req.AdminEmails { + for _, email := range e.AdminEmails { err := e.buildAndSendMessage(ctx, req, email, true) result = multierror.Append(errors.Wrapf(err, "problem sending admin email notification to %q", email)) } diff --git a/backend/app/notify/email_test.go b/backend/app/notify/email_test.go index 6d4fee2c..1d7090bb 100644 --- a/backend/app/notify/email_test.go +++ b/backend/app/notify/email_test.go @@ -114,10 +114,10 @@ func TestEmailSendErrors(t *testing.T) { e.msgTmpl, err = template.New("test").Parse("{{.Test}}") assert.NoError(t, err) assert.EqualError(t, e.Send(context.Background(), Request{Comment: store.Comment{ID: "999"}, parent: store.Comment{User: store.User{ID: "test"}}, Emails: []string{"bad@example.org"}}), - "1 error occurred:\n\t* problem sending user email notification to \"bad@example.org\": " + - "error executing template to build comment reply message: " + - "template: test:1:2: executing \"test\" at <.Test>: " + - "can't evaluate field Test in type notify.msgTmplData\n\n") + "1 error occurred:\n\t* problem sending user email notification to \"bad@example.org\": "+ + "error executing template to build comment reply message: "+ + "template: test:1:2: executing \"test\" at <.Test>: "+ + "can't evaluate field Test in type notify.msgTmplData\n\n") ctx, cancel := context.WithCancel(context.Background()) cancel() @@ -126,8 +126,8 @@ func TestEmailSendErrors(t *testing.T) { e.smtp = &fakeTestSMTP{} assert.EqualError(t, e.Send(context.Background(), Request{Comment: store.Comment{ID: "999"}, parent: store.Comment{User: store.User{ID: "error"}}, Emails: []string{"bad@example.org"}}), - "1 error occurred:\n\t* problem sending user email notification to \"bad@example.org\":" + - " error creating token for unsubscribe link: token generation error\n\n") + "1 error occurred:\n\t* problem sending user email notification to \"bad@example.org\":"+ + " error creating token for unsubscribe link: token generation error\n\n") } func TestEmailSend_ExitConditions(t *testing.T) { @@ -229,16 +229,16 @@ List-Unsubscribe: