From d4387b9e7a6a3769f097242236b13afb1a7d270b Mon Sep 17 00:00:00 2001 From: Dmitry Verkhoturov Date: Sun, 19 Jan 2020 20:46:45 +0100 Subject: [PATCH 1/3] add subscription link support to notification email verification template --- README.md | 1 + backend/app/cmd/server.go | 4 +++ backend/app/notify/email.go | 23 ++++++++++------ backend/app/notify/email_test.go | 47 +++++++++++++++++++++++++++++++- 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f3758ac2d5..cff3d589cd 100644 --- a/README.md +++ b/README.md @@ -155,6 +155,7 @@ _this is the recommended way to run remark42_ | notify.telegram.timeout | NOTIFY_TELEGRAM_TIMEOUT | `5s` | telegram timeout | | notify.email.fromAddress | NOTIFY_EMAIL_FROM | | from email address | | notify.email.verification_subj | NOTIFY_EMAIL_VERIFICATION_SUBJ | `Email verification` | verification message subject | +| notify.email.subscribe-url | NOTIFY_EMAIL_SUBSCRIBE_URL | `` | URL parameters to add to SITE in order to include to subscription email instead of plain token | | smtp.host | SMTP_HOST | | SMTP host | | smtp.port | SMTP_PORT | | SMTP port | | smtp.username | SMTP_USERNAME | | SMTP user name | diff --git a/backend/app/cmd/server.go b/backend/app/cmd/server.go index 4bc33df396..db5e40b88b 100644 --- a/backend/app/cmd/server.go +++ b/backend/app/cmd/server.go @@ -199,6 +199,7 @@ type NotifyGroup struct { Email struct { From string `long:"fromAddress" env:"FROM" description:"from email address"` VerificationSubject string `long:"verification_subj" env:"VERIFICATION_SUBJ" description:"verification message subject"` + SubscribeURL string `long:"subscribe-url" env:"SUBSCRIBE_URL" description:"URL parameters to add to SITE in order to include to subscription email instead of plain token"` } `group:"email" namespace:"email" env-namespace:"EMAIL"` } @@ -783,6 +784,9 @@ func (s *ServerCommand) makeNotify(dataStore *service.DataStore, authenticator * return tkn, nil }, } + if s.Notify.Email.SubscribeURL != "" { + emailParams.SubscribeURL = s.RemarkURL + s.Notify.Email.SubscribeURL + } 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 530c378fdc..f2927be211 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,11 +180,15 @@ const (

Remark42

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

+ {{if .SubscribeURL}} +

Click here to subscribe to email notifications

+ {{ else }}

TOKEN

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

{{.Token}}

+ {{ end }}

Sent to {{.Email}}

@@ -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: Date: Tue, 21 Jan 2020 00:39:26 +0100 Subject: [PATCH 2/3] always show token for email subscription in verification email --- backend/app/notify/email.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/app/notify/email.go b/backend/app/notify/email.go index f2927be211..20622da0bf 100644 --- a/backend/app/notify/email.go +++ b/backend/app/notify/email.go @@ -182,13 +182,13 @@ const (

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

{{if .SubscribeURL}}

Click here to subscribe to email notifications

- {{ else }} +

Alternatively, you can use code below for subscription.

+ {{ end }}

TOKEN

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

{{.Token}}

- {{ end }}

Sent to {{.Email}}

From c336dee3223a43f298590ca0c8aa4b22ebb99261 Mon Sep 17 00:00:00 2001 From: Dmitry Verkhoturov Date: Tue, 21 Jan 2020 01:00:55 +0100 Subject: [PATCH 3/3] hide SubscribeURL from users --- README.md | 1 - backend/app/cmd/server.go | 6 ++---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index cff3d589cd..f3758ac2d5 100644 --- a/README.md +++ b/README.md @@ -155,7 +155,6 @@ _this is the recommended way to run remark42_ | notify.telegram.timeout | NOTIFY_TELEGRAM_TIMEOUT | `5s` | telegram timeout | | notify.email.fromAddress | NOTIFY_EMAIL_FROM | | from email address | | notify.email.verification_subj | NOTIFY_EMAIL_VERIFICATION_SUBJ | `Email verification` | verification message subject | -| notify.email.subscribe-url | NOTIFY_EMAIL_SUBSCRIBE_URL | `` | URL parameters to add to SITE in order to include to subscription email instead of plain token | | smtp.host | SMTP_HOST | | SMTP host | | smtp.port | SMTP_PORT | | SMTP port | | smtp.username | SMTP_USERNAME | | SMTP user name | diff --git a/backend/app/cmd/server.go b/backend/app/cmd/server.go index db5e40b88b..43c72da0f5 100644 --- a/backend/app/cmd/server.go +++ b/backend/app/cmd/server.go @@ -199,7 +199,6 @@ type NotifyGroup struct { Email struct { From string `long:"fromAddress" env:"FROM" description:"from email address"` VerificationSubject string `long:"verification_subj" env:"VERIFICATION_SUBJ" description:"verification message subject"` - SubscribeURL string `long:"subscribe-url" env:"SUBSCRIBE_URL" description:"URL parameters to add to SITE in order to include to subscription email instead of plain token"` } `group:"email" namespace:"email" env-namespace:"EMAIL"` } @@ -767,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}, @@ -784,9 +785,6 @@ func (s *ServerCommand) makeNotify(dataStore *service.DataStore, authenticator * return tkn, nil }, } - if s.Notify.Email.SubscribeURL != "" { - emailParams.SubscribeURL = s.RemarkURL + s.Notify.Email.SubscribeURL - } smtpParams := notify.SmtpParams{ Host: s.SMTP.Host, Port: s.SMTP.Port,