mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Add context to notifications (#42578)
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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{}{
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user