From 9880a843ed0467f15486a79376f2300cbe7756de Mon Sep 17 00:00:00 2001 From: idafurjes <36131195+idafurjes@users.noreply.github.com> Date: Wed, 1 Dec 2021 17:56:08 +0100 Subject: [PATCH] Add context to notifications (#42578) --- pkg/api/org_invite.go | 6 +++--- pkg/api/password.go | 6 +++--- pkg/services/notifications/notifications.go | 16 ++++++++-------- pkg/services/notifications/notifications_test.go | 3 ++- .../notifications/send_email_integration_test.go | 3 ++- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/pkg/api/org_invite.go b/pkg/api/org_invite.go index bdd5460a846..7a4e2db7763 100644 --- a/pkg/api/org_invite.go +++ b/pkg/api/org_invite.go @@ -86,7 +86,7 @@ func AddOrgInvite(c *models.ReqContext) response.Response { }, } - if err := bus.Dispatch(&emailCmd); err != nil { + if err := bus.DispatchCtx(c.Req.Context(), &emailCmd); err != nil { if errors.Is(err, models.ErrSmtpNotEnabled) { return response.Error(412, err.Error(), err) } @@ -95,7 +95,7 @@ func AddOrgInvite(c *models.ReqContext) response.Response { } emailSentCmd := models.UpdateTempUserWithEmailSentCommand{Code: cmd.Result.Code} - if err := bus.Dispatch(&emailSentCmd); err != nil { + if err := bus.DispatchCtx(c.Req.Context(), &emailSentCmd); err != nil { return response.Error(500, "Failed to update invite with email sent info", err) } @@ -126,7 +126,7 @@ func inviteExistingUserToOrg(c *models.ReqContext, user *models.User, inviteDto }, } - if err := bus.Dispatch(&emailCmd); err != nil { + if err := bus.DispatchCtx(c.Req.Context(), &emailCmd); err != nil { return response.Error(500, "Failed to send email invited_to_org", err) } } diff --git a/pkg/api/password.go b/pkg/api/password.go index 04219b628ee..bf624e2f5a3 100644 --- a/pkg/api/password.go +++ b/pkg/api/password.go @@ -33,7 +33,7 @@ func SendResetPasswordEmail(c *models.ReqContext) response.Response { } emailCmd := models.SendResetPasswordEmailCommand{User: userQuery.Result} - if err := bus.Dispatch(&emailCmd); err != nil { + if err := bus.DispatchCtx(c.Req.Context(), &emailCmd); err != nil { return response.Error(500, "Failed to send email", err) } @@ -47,7 +47,7 @@ func ResetPassword(c *models.ReqContext) response.Response { } query := models.ValidateResetPasswordCodeQuery{Code: form.Code} - if err := bus.Dispatch(&query); err != nil { + if err := bus.DispatchCtx(c.Req.Context(), &query); err != nil { if errors.Is(err, models.ErrInvalidEmailCode) { return response.Error(400, "Invalid or expired reset password code", nil) } @@ -66,7 +66,7 @@ func ResetPassword(c *models.ReqContext) response.Response { return response.Error(500, "Failed to encode password", err) } - if err := bus.Dispatch(&cmd); err != nil { + if err := bus.DispatchCtx(c.Req.Context(), &cmd); err != nil { return response.Error(500, "Failed to change user password", err) } diff --git a/pkg/services/notifications/notifications.go b/pkg/services/notifications/notifications.go index 5c3460d6a62..39c4d3a4d1c 100644 --- a/pkg/services/notifications/notifications.go +++ b/pkg/services/notifications/notifications.go @@ -31,9 +31,9 @@ func ProvideService(bus bus.Bus, cfg *setting.Cfg) (*NotificationService, error) webhookQueue: make(chan *Webhook, 10), } - ns.Bus.AddHandler(ns.sendResetPasswordEmail) + ns.Bus.AddHandlerCtx(ns.sendResetPasswordEmail) ns.Bus.AddHandlerCtx(ns.validateResetPasswordCode) - ns.Bus.AddHandler(ns.sendEmailCommandHandler) + ns.Bus.AddHandlerCtx(ns.sendEmailCommandHandler) ns.Bus.AddHandlerCtx(ns.sendEmailCommandHandlerSync) ns.Bus.AddHandlerCtx(ns.SendWebhookSync) @@ -137,7 +137,7 @@ func (ns *NotificationService) sendEmailCommandHandlerSync(ctx context.Context, return err } -func (ns *NotificationService) sendEmailCommandHandler(cmd *models.SendEmailCommand) error { +func (ns *NotificationService) sendEmailCommandHandler(ctx context.Context, cmd *models.SendEmailCommand) error { message, err := ns.buildEmailMessage(cmd) if err != nil { @@ -148,12 +148,12 @@ func (ns *NotificationService) sendEmailCommandHandler(cmd *models.SendEmailComm return nil } -func (ns *NotificationService) sendResetPasswordEmail(cmd *models.SendResetPasswordEmailCommand) error { +func (ns *NotificationService) sendResetPasswordEmail(ctx context.Context, cmd *models.SendResetPasswordEmailCommand) error { code, err := createUserEmailCode(ns.Cfg, cmd.User, nil) if err != nil { return err } - return ns.sendEmailCommandHandler(&models.SendEmailCommand{ + return ns.sendEmailCommandHandler(ctx, &models.SendEmailCommand{ To: []string{cmd.User.Email}, Template: tmplResetPassword, Data: map[string]interface{}{ @@ -197,7 +197,7 @@ func (ns *NotificationService) signUpStartedHandler(ctx context.Context, evt *ev return nil } - err := ns.sendEmailCommandHandler(&models.SendEmailCommand{ + err := ns.sendEmailCommandHandler(ctx, &models.SendEmailCommand{ To: []string{evt.Email}, Template: tmplSignUpStarted, Data: map[string]interface{}{ @@ -212,7 +212,7 @@ func (ns *NotificationService) signUpStartedHandler(ctx context.Context, evt *ev } emailSentCmd := models.UpdateTempUserWithEmailSentCommand{Code: evt.Code} - return bus.Dispatch(&emailSentCmd) + return bus.DispatchCtx(ctx, &emailSentCmd) } func (ns *NotificationService) signUpCompletedHandler(ctx context.Context, evt *events.SignUpCompleted) error { @@ -220,7 +220,7 @@ func (ns *NotificationService) signUpCompletedHandler(ctx context.Context, evt * return nil } - return ns.sendEmailCommandHandler(&models.SendEmailCommand{ + return ns.sendEmailCommandHandler(ctx, &models.SendEmailCommand{ To: []string{evt.Email}, Template: tmplWelcomeOnSignUp, Data: map[string]interface{}{ diff --git a/pkg/services/notifications/notifications_test.go b/pkg/services/notifications/notifications_test.go index 6ab461cc5ee..55db3105569 100644 --- a/pkg/services/notifications/notifications_test.go +++ b/pkg/services/notifications/notifications_test.go @@ -1,6 +1,7 @@ package notifications import ( + "context" "testing" "github.com/grafana/grafana/pkg/bus" @@ -26,7 +27,7 @@ func TestNotificationService(t *testing.T) { require.NoError(t, err) t.Run("When sending reset email password", func(t *testing.T) { - err := ns.sendResetPasswordEmail(&models.SendResetPasswordEmailCommand{User: &models.User{Email: "asd@asd.com"}}) + err := ns.sendResetPasswordEmail(context.Background(), &models.SendResetPasswordEmailCommand{User: &models.User{Email: "asd@asd.com"}}) require.NoError(t, err) sentMsg := <-ns.mailQueue diff --git a/pkg/services/notifications/send_email_integration_test.go b/pkg/services/notifications/send_email_integration_test.go index 6b257312357..ba9ae8e5dbb 100644 --- a/pkg/services/notifications/send_email_integration_test.go +++ b/pkg/services/notifications/send_email_integration_test.go @@ -1,6 +1,7 @@ package notifications import ( + "context" "io/ioutil" "testing" @@ -56,7 +57,7 @@ func TestEmailIntegrationTest(t *testing.T) { Template: "alert_notification", } - err := ns.sendEmailCommandHandler(cmd) + err := ns.sendEmailCommandHandler(context.Background(), cmd) require.NoError(t, err) sentMsg := <-ns.mailQueue