From dfaf18fbd6bd5aa1d855ff012c2197de14d600c1 Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Thu, 14 Nov 2024 16:44:49 +0800 Subject: [PATCH] feat: added notification test --- core/models/models/notification_request.go | 1 + core/notification/mail.go | 10 ++- core/notification/service.go | 83 +++++++++++++++++++--- 3 files changed, 81 insertions(+), 13 deletions(-) diff --git a/core/models/models/notification_request.go b/core/models/models/notification_request.go index 93adf0c0..480cef85 100644 --- a/core/models/models/notification_request.go +++ b/core/models/models/notification_request.go @@ -18,4 +18,5 @@ type NotificationRequest struct { ChannelId primitive.ObjectID `json:"channel_id" bson:"channel_id"` Setting *NotificationSetting `json:"setting,omitempty" bson:"-"` Channel *NotificationChannel `json:"channel,omitempty" bson:"-"` + Test bool `json:"test,omitempty" bson:"test,omitempty"` } diff --git a/core/notification/mail.go b/core/notification/mail.go index 8546d47b..533482de 100644 --- a/core/notification/mail.go +++ b/core/notification/mail.go @@ -13,9 +13,15 @@ import ( ) func SendMail(s *models.NotificationSetting, ch *models.NotificationChannel, to, cc, bcc []string, title, content string) error { + // sender identity + senderIdentity := ch.SMTPUsername + // sender email senderEmail := ch.SMTPUsername - if s.UseCustomSenderEmail { + if s != nil && s.UseCustomSenderEmail { + if s.SenderName != "" { + senderIdentity = s.SenderName + } senderEmail = s.SenderEmail } @@ -23,7 +29,7 @@ func SendMail(s *models.NotificationSetting, ch *models.NotificationChannel, to, smtpConfig := smtpAuthentication{ Server: ch.SMTPServer, Port: ch.SMTPPort, - SenderIdentity: s.SenderName, + SenderIdentity: senderIdentity, SenderEmail: senderEmail, SMTPUser: ch.SMTPUsername, SMTPPassword: ch.SMTPPassword, diff --git a/core/notification/service.go b/core/notification/service.go index 3794011f..d4ae00c5 100644 --- a/core/notification/service.go +++ b/core/notification/service.go @@ -39,7 +39,7 @@ func (svc *Service) Send(s *models.NotificationSetting, args ...any) { case TypeMail: svc.SendMail(s, ch, title, content) case TypeIM: - svc.SendIM(s, ch, title, content) + svc.SendIM(ch, title, content) } }(chId) } @@ -52,7 +52,7 @@ func (svc *Service) SendMail(s *models.NotificationSetting, ch *models.Notificat mailBcc := s.MailBcc // request - r, _ := svc.createRequest(s, ch, title, content) + r, _ := svc.createRequestMail(s, ch, title, content) // send mail err := SendMail(s, ch, mailTo, mailCc, mailBcc, title, content) @@ -64,9 +64,9 @@ func (svc *Service) SendMail(s *models.NotificationSetting, ch *models.Notificat go svc.saveRequest(r, err) } -func (svc *Service) SendIM(s *models.NotificationSetting, ch *models.NotificationChannel, title, content string) { +func (svc *Service) SendIM(ch *models.NotificationChannel, title, content string) { // request - r, _ := svc.createRequest(s, ch, title, content) + r, _ := svc.createRequestIM(ch, title, content, false) // send mobile notification err := SendIMNotification(ch, title, content) @@ -78,7 +78,7 @@ func (svc *Service) SendIM(s *models.NotificationSetting, ch *models.Notificatio go svc.saveRequest(r, err) } -func (svc *Service) SendTestMessage(locale string, ch *models.NotificationChannel) (err error) { +func (svc *Service) SendTestMessage(locale string, ch *models.NotificationChannel, toMail []string) (err error) { // Test message content var title, content string switch locale { @@ -90,27 +90,44 @@ func (svc *Service) SendTestMessage(locale string, ch *models.NotificationChanne content = "This is a test notification from Crawlab. If you receive this message, your notification channel is configured correctly." } + // Notification request + var r *models.NotificationRequest + // Send test message based on channel type switch ch.Type { case TypeMail: - // For email, we'll send to the SMTP username as a test - err = SendMail(nil, ch, []string{ch.SMTPUsername}, nil, nil, title, content) + // If toMail is nil, use the default email address + if toMail == nil { + toMail = []string{ch.SMTPUsername} + } + + // Create request + r, _ = svc.createRequestMailTest(ch, title, content, toMail) + + // For email + err = SendMail(nil, ch, toMail, nil, nil, title, content) if err != nil { - return fmt.Errorf("failed to send test email: %v", err) + log.Errorf("failed to send test email: %v", err) } case TypeIM: + // Create request + r, _ = svc.createRequestIM(ch, title, content, true) + // For instant messaging err = SendIMNotification(ch, title, content) if err != nil { - return fmt.Errorf("failed to send test IM notification: %v", err) + log.Errorf("failed to send test IM notification: %v", err) } default: return fmt.Errorf("unsupported notification channel type: %s", ch.Type) } - return nil + // Save request + go svc.saveRequest(r, err) + + return err } func (svc *Service) getContent(s *models.NotificationSetting, ch *models.NotificationChannel, args ...any) (content string) { @@ -499,7 +516,7 @@ func (svc *Service) SendNodeNotification(node *models.Node) { } } -func (svc *Service) createRequest(s *models.NotificationSetting, ch *models.NotificationChannel, title, content string) (res *models.NotificationRequest, err error) { +func (svc *Service) createRequestMail(s *models.NotificationSetting, ch *models.NotificationChannel, title, content string) (res *models.NotificationRequest, err error) { senderEmail := ch.SMTPUsername if s.UseCustomSenderEmail { senderEmail = s.SenderEmail @@ -526,6 +543,50 @@ func (svc *Service) createRequest(s *models.NotificationSetting, ch *models.Noti return &r, nil } +func (svc *Service) createRequestMailTest(ch *models.NotificationChannel, title, content string, mailTo []string) (res *models.NotificationRequest, err error) { + if mailTo == nil { + mailTo = []string{ch.SMTPUsername} + } + + r := models.NotificationRequest{ + Status: StatusSending, + ChannelId: ch.Id, + Title: title, + Content: content, + SenderEmail: ch.SMTPUsername, + SenderName: ch.SMTPUsername, + MailTo: mailTo, + Test: true, + } + + r.SetCreatedAt(time.Now()) + r.SetUpdatedAt(time.Now()) + r.Id, err = service.NewModelService[models.NotificationRequest]().InsertOne(r) + if err != nil { + log.Errorf("[NotificationService] save request error: %v", err) + return nil, err + } + return &r, nil +} + +func (svc *Service) createRequestIM(ch *models.NotificationChannel, title, content string, test bool) (res *models.NotificationRequest, err error) { + r := models.NotificationRequest{ + Status: StatusSending, + ChannelId: ch.Id, + Title: title, + Content: content, + Test: test, + } + r.SetCreatedAt(time.Now()) + r.SetUpdatedAt(time.Now()) + r.Id, err = service.NewModelService[models.NotificationRequest]().InsertOne(r) + if err != nil { + log.Errorf("[NotificationService] save request error: %v", err) + return nil, err + } + return &r, nil +} + func (svc *Service) saveRequest(r *models.NotificationRequest, err error) { if r == nil { return