refactor: Update NotificationChannelV2 model to simplify mail and IM settings

This commit is contained in:
Marvin Zhang
2024-07-18 17:41:54 +08:00
parent 821383a677
commit 029c5d1448
4 changed files with 11 additions and 21 deletions

View File

@@ -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"`
}

View File

@@ -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

View File

@@ -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)
}

View File

@@ -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{