From e8bdfd228db15c969e1d47d46a772b5b51a21d4b Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Sat, 27 Jul 2024 16:06:26 +0800 Subject: [PATCH] refactor: Update models to include sender email, name, and mail recipients for notification requests --- .../models/v2/notification_request_v2.go | 10 ++--- .../models/v2/notification_setting_v2.go | 10 ++--- core/notification/mail.go | 44 +++++-------------- 3 files changed, 20 insertions(+), 44 deletions(-) diff --git a/core/models/models/v2/notification_request_v2.go b/core/models/models/v2/notification_request_v2.go index be53a8d5..bbfe6023 100644 --- a/core/models/models/v2/notification_request_v2.go +++ b/core/models/models/v2/notification_request_v2.go @@ -9,11 +9,11 @@ type NotificationRequestV2 struct { Error string `json:"error,omitempty" bson:"error,omitempty"` Title string `json:"title" bson:"title"` Content string `json:"content" bson:"content"` - SenderEmail string `json:"sender_email" bson:"sender_email"` - SenderName string `json:"sender_name" bson:"sender_name"` - MailTo string `json:"mail_to" bson:"mail_to"` - MailCc string `json:"mail_cc" bson:"mail_cc"` - MailBcc string `json:"mail_bcc" bson:"mail_bcc"` + SenderEmail string `json:"sender_email,omitempty" bson:"sender_email,omitempty"` + SenderName string `json:"sender_name,omitempty" bson:"sender_name,omitempty"` + MailTo []string `json:"mail_to,omitempty" bson:"mail_to,omitempty"` + MailCc []string `json:"mail_cc,omitempty" bson:"mail_cc,omitempty"` + MailBcc []string `json:"mail_bcc,omitempty" bson:"mail_bcc,omitempty"` SettingId primitive.ObjectID `json:"setting_id" bson:"setting_id"` ChannelId primitive.ObjectID `json:"channel_id" bson:"channel_id"` Setting *NotificationSettingV2 `json:"setting,omitempty" bson:"-"` diff --git a/core/models/models/v2/notification_setting_v2.go b/core/models/models/v2/notification_setting_v2.go index 517ae086..eb8f4be8 100644 --- a/core/models/models/v2/notification_setting_v2.go +++ b/core/models/models/v2/notification_setting_v2.go @@ -21,11 +21,11 @@ type NotificationSettingV2 struct { TriggerTarget string `json:"trigger_target" bson:"trigger_target"` Trigger string `json:"trigger" bson:"trigger"` - SenderEmail string `json:"sender_email,omitempty" bson:"sender_email,omitempty"` - SenderName string `json:"sender_name,omitempty" bson:"sender_name,omitempty"` - MailTo string `json:"mail_to" bson:"mail_to,omitempty"` - MailCc string `json:"mail_cc,omitempty" bson:"mail_cc,omitempty"` - MailBcc string `json:"mail_bcc,omitempty" bson:"mail_bcc,omitempty"` + SenderEmail string `json:"sender_email,omitempty" bson:"sender_email,omitempty"` + SenderName string `json:"sender_name,omitempty" bson:"sender_name,omitempty"` + MailTo []string `json:"mail_to,omitempty" bson:"mail_to,omitempty"` + MailCc []string `json:"mail_cc,omitempty" bson:"mail_cc,omitempty"` + MailBcc []string `json:"mail_bcc,omitempty" bson:"mail_bcc,omitempty"` ChannelIds []primitive.ObjectID `json:"channel_ids,omitempty" bson:"channel_ids,omitempty"` Channels []NotificationChannelV2 `json:"channels,omitempty" bson:"-"` diff --git a/core/notification/mail.go b/core/notification/mail.go index 88c4a716..c24de04f 100644 --- a/core/notification/mail.go +++ b/core/notification/mail.go @@ -12,7 +12,7 @@ import ( "strings" ) -func SendMail(s *models.NotificationSettingV2, ch *models.NotificationChannelV2, to, cc, bcc, title, content string) error { +func SendMail(s *models.NotificationSettingV2, ch *models.NotificationChannelV2, to, cc, bcc []string, title, content string) error { // compatibility for different providers var auth *XOAuth2Auth if ch.Provider == ChannelMailProviderOutlook { @@ -92,9 +92,9 @@ type smtpAuthentication struct { // sendOptions are options for sending an email type sendOptions struct { Subject string - To string - Cc string - Bcc string + To []string + Cc []string + Bcc []string } // send email @@ -111,7 +111,7 @@ func sendMail(smtpConfig smtpAuthentication, options sendOptions, htmlBody strin return errors.New("SMTP user is empty") } - if options.To == "" { + if len(options.To) == 0 { return errors.New("no receiver emails configured") } @@ -120,26 +120,15 @@ func sendMail(smtpConfig smtpAuthentication, options sendOptions, htmlBody strin Address: smtpConfig.SenderEmail, } - var toList []string - if strings.Contains(options.To, ";") { - toList = strings.Split(options.To, ";") - // trim space - for i, to := range toList { - toList[i] = strings.TrimSpace(to) - } - } else { - toList = []string{options.To} - } - m := gomail.NewMessage() m.SetHeader("From", from.String()) - m.SetHeader("To", getRecipientList(options.To)...) + m.SetHeader("To", options.To...) m.SetHeader("Subject", options.Subject) - if options.Cc != "" { - m.SetHeader("Cc", getRecipientList(options.Cc)...) + if len(options.Cc) > 0 { + m.SetHeader("Cc", options.Cc...) } - if options.Bcc != "" { - m.SetHeader("Bcc", getRecipientList(options.Bcc)...) + if len(options.Bcc) > 0 { + m.SetHeader("Bcc", options.Bcc...) } m.SetBody("text/plain", txtBody) @@ -152,16 +141,3 @@ func sendMail(smtpConfig smtpAuthentication, options sendOptions, htmlBody strin return d.DialAndSend(m) } - -func getRecipientList(value string) (values []string) { - if strings.Contains(value, ";") { - values = strings.Split(value, ";") - // trim space - for i, v := range values { - values[i] = strings.TrimSpace(v) - } - } else { - values = []string{value} - } - return values -}