Skip to content

Commit

Permalink
refactor: Update SendNotification function to handle old and new sett…
Browse files Browse the repository at this point in the history
…ings triggers
  • Loading branch information
tikazyq committed Jul 17, 2024
1 parent a510466 commit 821383a
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 251 deletions.
1 change: 1 addition & 0 deletions core/grpc/server/model_base_service_v2_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
*new(models2.GitV2),
*new(models2.MetricV2),
*new(models2.NodeV2),
*new(models2.NotificationChannelV2),
*new(models2.NotificationSettingV2),
*new(models2.PermissionV2),
*new(models2.ProjectV2),
Expand Down
10 changes: 5 additions & 5 deletions core/models/models/v2/notification_channel_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package models
type NotificationChannelV2 struct {
any `collection:"notification_channels"`
BaseModelV2[NotificationChannelV2] `bson:",inline"`
Type string `json:"type" bson:"type"`
Name string `json:"name" bson:"name"`
Description string `json:"description" bson:"description"`
Type string `json:"type" bson:"type"`
Provider string `json:"provider" bson:"provider"`
MailSettings struct {
SMTPServer string `json:"smtp_server" bson:"smtp_server"`
SMTPPort string `json:"smtp_port" bson:"smtp_port"`
SMTPFromEmailAddress string `json:"smtp_from_email_address" bson:"smtp_from_email_address"`
SMTPFromEmailPassword string `json:"smtp_from_email_password" bson:"smtp_from_email_password"`
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"`
Expand Down
55 changes: 25 additions & 30 deletions core/models/models/v2/notification_setting_v2.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
package models

import "go.mongodb.org/mongo-driver/bson/primitive"

type NotificationSettingV2 struct {
any `collection:"notification_settings"`
BaseModelV2[NotificationSettingV2] `bson:",inline"`
Type string `json:"type" bson:"type"`
Name string `json:"name" bson:"name"`
Description string `json:"description" bson:"description"`
Enabled bool `json:"enabled" bson:"enabled"`
Global bool `json:"global" bson:"global"`
Title string `json:"title,omitempty" bson:"title,omitempty"`
Template string `json:"template" bson:"template"`
TemplateMode string `json:"template_mode" bson:"template_mode"`
TemplateMarkdown string `json:"template_markdown,omitempty" bson:"template_markdown,omitempty"`
TemplateRichText string `json:"template_rich_text,omitempty" bson:"template_rich_text,omitempty"`
TemplateRichTextJson string `json:"template_rich_text_json,omitempty" bson:"template_rich_text_json,omitempty"`
TemplateTheme string `json:"template_theme,omitempty" bson:"template_theme,omitempty"`
TaskTrigger string `json:"task_trigger" bson:"task_trigger"`
TriggerTarget string `json:"trigger_target" bson:"trigger_target"`
Trigger string `json:"trigger" bson:"trigger"`
Mail NotificationSettingMail `json:"mail,omitempty" bson:"mail,omitempty"`
Mobile NotificationSettingMobile `json:"mobile,omitempty" bson:"mobile,omitempty"`
}
Name string `json:"name" bson:"name"`
Description string `json:"description" bson:"description"`
Enabled bool `json:"enabled" bson:"enabled"`

type NotificationSettingMail struct {
Server string `json:"server" bson:"server"`
Port string `json:"port,omitempty" bson:"port,omitempty"`
User string `json:"user,omitempty" bson:"user,omitempty"`
Password string `json:"password,omitempty" bson:"password,omitempty"`
SenderEmail string `json:"sender_email,omitempty" bson:"sender_email,omitempty"`
SenderIdentity string `json:"sender_identity,omitempty" bson:"sender_identity,omitempty"`
To string `json:"to,omitempty" bson:"to,omitempty"`
Cc string `json:"cc,omitempty" bson:"cc,omitempty"`
}
Title string `json:"title,omitempty" bson:"title,omitempty"`
Template string `json:"template" bson:"template"`
TemplateMode string `json:"template_mode" bson:"template_mode"`
TemplateMarkdown string `json:"template_markdown,omitempty" bson:"template_markdown,omitempty"`
TemplateRichText string `json:"template_rich_text,omitempty" bson:"template_rich_text,omitempty"`
TemplateRichTextJson string `json:"template_rich_text_json,omitempty" bson:"template_rich_text_json,omitempty"`
TemplateTheme string `json:"template_theme,omitempty" bson:"template_theme,omitempty"`

TaskTrigger string `json:"task_trigger" bson:"task_trigger"`
TriggerTarget string `json:"trigger_target" bson:"trigger_target"`
Trigger string `json:"trigger" bson:"trigger"`

HasMail bool `json:"has_mail" bson:"has_mail"`
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"`

type NotificationSettingMobile struct {
Webhook string `json:"webhook" bson:"webhook"`
ChannelIds []primitive.ObjectID `json:"channel_ids,omitempty" bson:"channel_ids,omitempty"`
Channels []NotificationChannelV2 `json:"channels,omitempty" bson:"-"`
}
31 changes: 6 additions & 25 deletions core/node/service/master_service_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import (
models2 "github.com/crawlab-team/crawlab/core/models/models/v2"
"github.com/crawlab-team/crawlab/core/models/service"
"github.com/crawlab-team/crawlab/core/node/config"
"github.com/crawlab-team/crawlab/core/notification"
"github.com/crawlab-team/crawlab/core/schedule"
"github.com/crawlab-team/crawlab/core/spider/admin"
"github.com/crawlab-team/crawlab/core/system"
"github.com/crawlab-team/crawlab/core/task/handler"
"github.com/crawlab-team/crawlab/core/task/scheduler"
Expand All @@ -30,14 +28,12 @@ import (

type MasterServiceV2 struct {
// dependencies
cfgSvc interfaces.NodeConfigService
server *server.GrpcServerV2
schedulerSvc *scheduler.ServiceV2
handlerSvc *handler.ServiceV2
scheduleSvc *schedule.ServiceV2
notificationSvc *notification.ServiceV2
spiderAdminSvc *admin.ServiceV2
systemSvc *system.ServiceV2
cfgSvc interfaces.NodeConfigService
server *server.GrpcServerV2
schedulerSvc *scheduler.ServiceV2
handlerSvc *handler.ServiceV2
scheduleSvc *schedule.ServiceV2
systemSvc *system.ServiceV2

// settings
cfgPath string
Expand Down Expand Up @@ -77,12 +73,6 @@ func (svc *MasterServiceV2) Start() {
// start schedule service
go svc.scheduleSvc.Start()

// start notification service
go svc.notificationSvc.Start()

// start spider admin service
go svc.spiderAdminSvc.Start()

// wait for quit signal
svc.Wait()

Expand Down Expand Up @@ -361,15 +351,6 @@ func newMasterServiceV2() (res *MasterServiceV2, err error) {
return nil, err
}

// notification service
svc.notificationSvc = notification.GetNotificationServiceV2()

// spider admin service
svc.spiderAdminSvc, err = admin.GetSpiderAdminServiceV2()
if err != nil {
return nil, err
}

// system service
svc.systemSvc = system.GetSystemServiceV2()

Expand Down
4 changes: 2 additions & 2 deletions core/notification/constants.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package notification

const (
TypeMail = "mail"
TypeMobile = "mobile"
TypeMail = "mail"
TypeIM = "im"
)

const (
Expand Down
11 changes: 7 additions & 4 deletions core/notification/mobile.go → core/notification/im.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package notification

import (
"errors"
"github.com/crawlab-team/crawlab/core/models/models/v2"
"github.com/crawlab-team/crawlab/trace"
"github.com/imroc/req"
"strings"
Expand All @@ -12,7 +13,9 @@ type ResBody struct {
ErrMsg string `json:"errmsg"`
}

func SendMobileNotification(webhook string, title string, content string) error {
func SendIMNotification(s *models.NotificationSettingV2, ch *models.NotificationChannelV2, content string) error {
// TODO: compatibility with different IM providers

// request header
header := req.Header{
"Content-Type": "application/json; charset=utf-8",
Expand All @@ -22,7 +25,7 @@ func SendMobileNotification(webhook string, title string, content string) error
data := req.Param{
"msgtype": "markdown",
"markdown": req.Param{
"title": title,
"title": s.Title,
"text": content,
"content": content,
},
Expand All @@ -32,7 +35,7 @@ func SendMobileNotification(webhook string, title string, content string) error
},
"text": content,
}
if strings.Contains(strings.ToLower(webhook), "feishu") {
if strings.Contains(strings.ToLower(ch.IMSettings.Webhook), "feishu") {
data = req.Param{
"msg_type": "text",
"content": req.Param{
Expand All @@ -42,7 +45,7 @@ func SendMobileNotification(webhook string, title string, content string) error
}

// perform request
res, err := req.Post(webhook, header, req.BodyJSON(&data))
res, err := req.Post(ch.IMSettings.Webhook, header, req.BodyJSON(&data))
if err != nil {
return trace.TraceError(err)
}
Expand Down
42 changes: 23 additions & 19 deletions core/notification/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,31 @@ import (
"strings"
)

func SendMail(m *models.NotificationSettingMail, to, cc, title, content string) error {
func SendMail(s *models.NotificationSettingV2, ch *models.NotificationChannelV2, to, cc, bcc, title, content string) error {
// mail settings
ms := ch.MailSettings

// config
port, _ := strconv.Atoi(m.Port)
password := m.Password
SMTPUser := m.User
port, err := strconv.Atoi(ms.SMTPPort)
if err != nil {
log.Errorf("failed to convert SMTP port to int: %v", err)
trace.PrintError(err)
return err
}
smtpConfig := smtpAuthentication{
Server: m.Server,
Server: ms.SMTPServer,
Port: port,
SenderEmail: m.SenderEmail,
SenderIdentity: m.SenderIdentity,
SMTPPassword: password,
SMTPUser: SMTPUser,
SenderIdentity: s.SenderName,
SenderEmail: s.SenderEmail,
SMTPUser: ms.SMTPUser,
SMTPPassword: ms.SMTPPassword,
}

options := sendOptions{
Subject: title,
To: to,
Cc: cc,
Subject: title,
Bcc: bcc,
}

// convert html to text
Expand Down Expand Up @@ -80,9 +88,10 @@ type smtpAuthentication struct {

// sendOptions are options for sending an email
type sendOptions struct {
To string
Subject string
To string
Cc string
Bcc string
}

// send email
Expand All @@ -99,14 +108,6 @@ func send(smtpConfig smtpAuthentication, options sendOptions, htmlBody string, t
return errors.New("SMTP user is empty")
}

if smtpConfig.SenderIdentity == "" {
return errors.New("SMTP sender identity is empty")
}

if smtpConfig.SenderEmail == "" {
return errors.New("SMTP sender email is empty")
}

if options.To == "" {
return errors.New("no receiver emails configured")
}
Expand Down Expand Up @@ -134,6 +135,9 @@ func send(smtpConfig smtpAuthentication, options sendOptions, htmlBody string, t
if options.Cc != "" {
m.SetHeader("Cc", getRecipientList(options.Cc)...)
}
if options.Bcc != "" {
m.SetHeader("Bcc", getRecipientList(options.Bcc)...)
}

m.SetBody("text/plain", txtBody)
m.AddAlternative("text/html", htmlBody)
Expand Down
Loading

0 comments on commit 821383a

Please sign in to comment.