From 029c5d1448e13aacb8aa60ff7bc72b6994574dda Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Thu, 18 Jul 2024 17:41:54 +0800 Subject: [PATCH] refactor: Update NotificationChannelV2 model to simplify mail and IM settings --- core/models/models/v2/notification_channel_v2.go | 14 +++++--------- core/notification/constants.go | 3 --- core/notification/im.go | 4 ++-- core/notification/mail.go | 11 ++++------- 4 files changed, 11 insertions(+), 21 deletions(-) diff --git a/core/models/models/v2/notification_channel_v2.go b/core/models/models/v2/notification_channel_v2.go index 2f328b79..0b86a6b8 100644 --- a/core/models/models/v2/notification_channel_v2.go +++ b/core/models/models/v2/notification_channel_v2.go @@ -7,13 +7,9 @@ type NotificationChannelV2 struct { Name string `json:"name" bson:"name"` Description string `json:"description" bson:"description"` Provider string `json:"provider" bson:"provider"` - MailSettings struct { - SMTPServer string `json:"smtp_server" bson:"smtp_server"` - SMTPPort string `json:"smtp_port" bson:"smtp_port"` - SMTPUser string `json:"smtp_from_email_address" bson:"smtp_from_email_address"` - SMTPPassword string `json:"smtp_from_email_password" bson:"smtp_from_email_password"` - } `json:"mail_settings,omitempty" bson:"mail_settings,omitempty"` - IMSettings struct { - Webhook string `json:"webhook" bson:"webhook"` - } `json:"im_settings,omitempty" bson:"im_settings,omitempty"` + SMTPServer string `json:"smtp_server" bson:"smtp_server"` + SMTPPort string `json:"smtp_port" bson:"smtp_port"` + SMTPUsername string `json:"smtp_username" bson:"smtp_username"` + SMTPPassword string `json:"smtp_password" bson:"smtp_password"` + WebhookUrl string `json:"webhook_url" bson:"webhook_url"` } diff --git a/core/notification/constants.go b/core/notification/constants.go index b3904af8..383b04dc 100644 --- a/core/notification/constants.go +++ b/core/notification/constants.go @@ -19,10 +19,7 @@ const ( ChannelMailProvider126 = "126" ChannelMailProviderSina = "sina" ChannelMailProviderSohu = "sohu" - ChannelMailProvider21CN = "21cn" ChannelMailProviderTencent = "tencent" - ChannelMailProviderHuawei = "huawei" - ChannelMailProviderAliyun = "aliyun" ChannelIMProviderSlack = "slack" // https://api.slack.com/messaging/webhooks ChannelIMProviderMSTeams = "ms_teams" // https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook?tabs=newteams%2Cjavascript diff --git a/core/notification/im.go b/core/notification/im.go index 3650a4db..85d9cac5 100644 --- a/core/notification/im.go +++ b/core/notification/im.go @@ -35,7 +35,7 @@ func SendIMNotification(s *models.NotificationSettingV2, ch *models.Notification }, "text": content, } - if strings.Contains(strings.ToLower(ch.IMSettings.Webhook), "feishu") { + if strings.Contains(strings.ToLower(ch.WebhookUrl), "feishu") { data = req.Param{ "msg_type": "text", "content": req.Param{ @@ -45,7 +45,7 @@ func SendIMNotification(s *models.NotificationSettingV2, ch *models.Notification } // perform request - res, err := req.Post(ch.IMSettings.Webhook, header, req.BodyJSON(&data)) + res, err := req.Post(ch.WebhookUrl, header, req.BodyJSON(&data)) if err != nil { return trace.TraceError(err) } diff --git a/core/notification/mail.go b/core/notification/mail.go index 118ca4ae..b35764f1 100644 --- a/core/notification/mail.go +++ b/core/notification/mail.go @@ -15,23 +15,20 @@ import ( ) func SendMail(s *models.NotificationSettingV2, ch *models.NotificationChannelV2, to, cc, bcc, title, content string) error { - // mail settings - ms := ch.MailSettings - // config - port, err := strconv.Atoi(ms.SMTPPort) + port, err := strconv.Atoi(ch.SMTPPort) if err != nil { log.Errorf("failed to convert SMTP port to int: %v", err) trace.PrintError(err) return err } smtpConfig := smtpAuthentication{ - Server: ms.SMTPServer, + Server: ch.SMTPServer, Port: port, SenderIdentity: s.SenderName, SenderEmail: s.SenderEmail, - SMTPUser: ms.SMTPUser, - SMTPPassword: ms.SMTPPassword, + SMTPUser: ch.SMTPUsername, + SMTPPassword: ch.SMTPPassword, } options := sendOptions{