simplify admin email notifications

This commit is contained in:
Dmitry Verkhoturov
2020-10-20 11:02:04 -05:00
committed by Umputun
parent 91580194e9
commit 1ce3cf3d1f
8 changed files with 24 additions and 38 deletions
+3 -4
View File
@@ -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,
+9 -8
View File
@@ -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))
}
+10 -10
View File
@@ -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: <https://remark42.com/api/v1/email/unsubscribe?site=&tkn=token
Date: `)
// send email to both user and admin, without parent set
email.AdminEmails = []string{"admin@example.org"}
req = Request{
Comment: store.Comment{ID: "999", User: store.User{ID: "1", Name: "test_user"}, PostTitle: "test_title"},
Emails: []string{"test@example.org"},
AdminEmails: []string{"admin@example.org"},
Comment: store.Comment{ID: "999", User: store.User{ID: "1", Name: "test_user"}, PostTitle: "test_title"},
Emails: []string{"test@example.org"},
}
assert.NoError(t, email.Send(context.TODO(), req))
assert.Equal(t, "from@example.org", fakeSMTP.readMail())
assert.Equal(t, 3, fakeSMTP.readQuitCount(), "plus two emails: one for user and one for admin")
assert.Equal(t, "admin@example.org", fakeSMTP.readRcpt())
res, err = email.buildMessageFromRequest(req, req.AdminEmails[0], true)
res, err = email.buildMessageFromRequest(req, email.AdminEmails[0], true)
assert.NoError(t, err)
assert.Contains(t, res, `From: from@example.org
To: admin@example.org
-1
View File
@@ -42,7 +42,6 @@ type Request struct {
Comment store.Comment
parent store.Comment
Emails []string
AdminEmails []string
}
// VerificationRequest notification for user
-2
View File
@@ -51,7 +51,6 @@ type Rest struct {
AnonVote bool
WebRoot string
RemarkURL string
AdminEmail []string
ReadOnlyAge int
SharedSecret string
ScoreThresholds struct {
@@ -371,7 +370,6 @@ func (s *Rest) controllerGroups() (public, private, admin, rss) {
authenticator: s.Authenticator,
notifyService: s.NotifyService,
remarkURL: s.RemarkURL,
adminEmail: s.AdminEmail,
anonVote: s.AnonVote,
templates: templates.NewFS(),
}
+1 -7
View File
@@ -40,7 +40,6 @@ type private struct {
notifyService *notify.Service
authenticator *auth.Service
remarkURL string
adminEmail []string
anonVote bool
templates templates.FileReader
}
@@ -120,12 +119,7 @@ func (s *private) createCommentCtrl(w http.ResponseWriter, r *http.Request) {
Scopes(comment.Locator.URL, lastCommentsScope, comment.User.ID, comment.Locator.SiteID))
if s.notifyService != nil {
s.notifyService.Submit(
notify.Request{
Comment: finalComment,
AdminEmails: s.adminEmail,
},
)
s.notifyService.Submit(notify.Request{Comment: finalComment})
}
log.Printf("[DEBUG] created commend %+v", finalComment)
+1 -5
View File
@@ -620,7 +620,6 @@ func TestRest_EmailNotification(t *testing.T) {
time.Sleep(time.Millisecond * 30)
require.Equal(t, 1, len(mockDestination.Get()))
assert.Empty(t, mockDestination.Get()[0].Emails)
assert.Equal(t, []string{"admin@example.org"}, mockDestination.Get()[0].AdminEmails)
// create child comment from another user, email notification only to admin expected
req, err = http.NewRequest("POST", ts.URL+"/api/v1/comment", strings.NewReader(fmt.Sprintf(
@@ -641,7 +640,6 @@ func TestRest_EmailNotification(t *testing.T) {
time.Sleep(time.Millisecond * 30)
require.Equal(t, 2, len(mockDestination.Get()))
assert.Empty(t, mockDestination.Get()[1].Emails)
assert.Equal(t, []string{"admin@example.org"}, mockDestination.Get()[1].AdminEmails)
// send confirmation token for email
req, err = http.NewRequest(http.MethodPost, ts.URL+"/api/v1/email/subscribe?site=remark42&address=good@example.com", nil)
@@ -705,7 +703,6 @@ func TestRest_EmailNotification(t *testing.T) {
time.Sleep(time.Millisecond * 30)
require.Equal(t, 3, len(mockDestination.Get()))
assert.Equal(t, []string{"good@example.com"}, mockDestination.Get()[2].Emails)
assert.Equal(t, []string{"admin@example.org"}, mockDestination.Get()[2].AdminEmails)
// delete user's email
req, err = http.NewRequest(http.MethodDelete, ts.URL+"/api/v1/email?site=remark42", nil)
@@ -718,7 +715,7 @@ func TestRest_EmailNotification(t *testing.T) {
require.NoError(t, resp.Body.Close())
assert.Equal(t, http.StatusOK, resp.StatusCode, string(body))
// create child comment from another user, no email notification expected except for admin
// create child comment from another user, no email notification
req, err = http.NewRequest("POST", ts.URL+"/api/v1/comment", strings.NewReader(
`{"text": "test 321",
"user": {"name": "other_user"},
@@ -736,7 +733,6 @@ func TestRest_EmailNotification(t *testing.T) {
time.Sleep(time.Millisecond * 30)
require.Equal(t, 4, len(mockDestination.Get()))
assert.Empty(t, mockDestination.Get()[3].Emails)
assert.Equal(t, []string{"admin@example.org"}, mockDestination.Get()[3].AdminEmails)
}
func TestRest_UserAllData(t *testing.T) {
-1
View File
@@ -381,7 +381,6 @@ func startupT(t *testing.T) (ts *httptest.Server, srv *Rest, teardown func()) {
Cache: memCache,
WebRoot: tmp,
RemarkURL: "https://demo.remark42.com",
AdminEmail: []string{"admin@example.org"},
ImageService: image.NewService(&image.FileSystem{
Location: tmp + "/pics-remark42",
Partitions: 100,