From f97d8232edc1ae27bf99b0dd6fefbff138905bdc Mon Sep 17 00:00:00 2001 From: Dmitry Verkhoturov Date: Tue, 21 Jan 2020 01:27:23 +0100 Subject: [PATCH] Add subscription link support to notification email verification template (#573) * add subscription link support to notification email verification template * always show token for email subscription in verification email * hide SubscribeURL from users --- backend/app/cmd/server.go | 2 ++ backend/app/notify/email.go | 23 ++++++++++------ backend/app/notify/email_test.go | 47 +++++++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 9 deletions(-) diff --git a/backend/app/cmd/server.go b/backend/app/cmd/server.go index 4bc33df396..43c72da0f5 100644 --- a/backend/app/cmd/server.go +++ b/backend/app/cmd/server.go @@ -766,6 +766,8 @@ func (s *ServerCommand) makeNotify(dataStore *service.DataStore, authenticator * From: s.Notify.Email.From, VerificationSubject: s.Notify.Email.VerificationSubject, UnsubscribeURL: s.RemarkURL + "/email/unsubscribe.html", + // TODO: uncomment after #560 frontend part is ready and URL is known + //SubscribeURL: s.RemarkURL + "/subscribe.html?token=", TokenGenFn: func(userID, email, site string) (string, error) { claims := token.Claims{ Handshake: &token.Handshake{ID: userID + "::" + email}, diff --git a/backend/app/notify/email.go b/backend/app/notify/email.go index 530c378fdc..20622da0bf 100644 --- a/backend/app/notify/email.go +++ b/backend/app/notify/email.go @@ -23,6 +23,7 @@ type EmailParams struct { MsgTemplate string // request message template VerificationSubject string // verification message subject VerificationTemplate string // verification message 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 @@ -91,10 +92,11 @@ type msgTmplData struct { // verifyTmplData store data for verification message template execution type verifyTmplData struct { - User string - Token string - Email string - Site string + User string + Token string + Email string + Site string + SubscribeURL string } const ( @@ -178,6 +180,10 @@ const (

Remark42

Confirmation for {{.User}} on site {{.Site}}

+ {{if .SubscribeURL}} +

Click here to subscribe to email notifications

+

Alternatively, you can use code below for subscription.

+ {{ end }}

TOKEN

Copy and paste this text into “token” field on comments page

@@ -273,10 +279,11 @@ func (e *Email) buildVerificationMessage(user, email, token, site string) (strin subject := e.VerificationSubject msg := bytes.Buffer{} err := e.verifyTmpl.Execute(&msg, verifyTmplData{ - User: user, - Token: token, - Email: email, - Site: site, + User: user, + Token: token, + Email: email, + Site: site, + SubscribeURL: e.SubscribeURL, }) if err != nil { return "", errors.Wrapf(err, "error executing template to build verification message") diff --git a/backend/app/notify/email_test.go b/backend/app/notify/email_test.go index a194fe4860..f05784a61e 100644 --- a/backend/app/notify/email_test.go +++ b/backend/app/notify/email_test.go @@ -189,7 +189,8 @@ func TestEmail_Send(t *testing.T) { req := Request{ Comment: store.Comment{ID: "999", User: store.User{ID: "1", Name: "test_user"}, PostTitle: "test_title"}, parent: store.Comment{ID: "1", User: store.User{ID: "999", Name: "parent_user"}}, - Email: "test@example.org"} + Email: "test@example.org", + } assert.NoError(t, email.Send(context.TODO(), req)) assert.Equal(t, "from@example.org", fakeSmtp.readMail()) assert.Equal(t, 1, fakeSmtp.readQuitCount()) @@ -208,6 +209,50 @@ List-Unsubscribe: