| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| package service |
|
|
| import ( |
| "fmt" |
| "strings" |
| "veloera/common" |
| "veloera/constant" |
| "veloera/dto" |
| "veloera/model" |
| ) |
|
|
| func NotifyRootUser(t string, subject string, content string) { |
| user := model.GetRootUser().ToBaseUser() |
| err := NotifyUser(user.Id, user.Email, user.GetSetting(), dto.NewNotify(t, subject, content, nil)) |
| if err != nil { |
| common.SysError(fmt.Sprintf("failed to notify root user: %s", err.Error())) |
| } |
| } |
|
|
| func NotifyUser(userId int, userEmail string, userSetting map[string]interface{}, data dto.Notify) error { |
| notifyType, ok := userSetting[constant.UserSettingNotifyType] |
| if !ok { |
| notifyType = constant.NotifyTypeEmail |
| } |
|
|
| |
| canSend, err := CheckNotificationLimit(userId, data.Type) |
| if err != nil { |
| common.SysError(fmt.Sprintf("failed to check notification limit: %s", err.Error())) |
| return err |
| } |
| if !canSend { |
| return fmt.Errorf("notification limit exceeded for user %d with type %s", userId, notifyType) |
| } |
|
|
| switch notifyType { |
| case constant.NotifyTypeEmail: |
| |
| if settingEmail, ok := userSetting[constant.UserSettingNotificationEmail]; ok { |
| userEmail = settingEmail.(string) |
| } |
| if userEmail == "" { |
| common.SysLog(fmt.Sprintf("user %d has no email, skip sending email", userId)) |
| return nil |
| } |
| return sendEmailNotify(userEmail, data) |
| case constant.NotifyTypeWebhook: |
| webhookURL, ok := userSetting[constant.UserSettingWebhookUrl] |
| if !ok { |
| common.SysError(fmt.Sprintf("user %d has no webhook url, skip sending webhook", userId)) |
| return nil |
| } |
| webhookURLStr, ok := webhookURL.(string) |
| if !ok { |
| common.SysError(fmt.Sprintf("user %d webhook url is not string type", userId)) |
| return nil |
| } |
|
|
| |
| var webhookSecret string |
| if secret, ok := userSetting[constant.UserSettingWebhookSecret]; ok { |
| webhookSecret, _ = secret.(string) |
| } |
|
|
| return SendWebhookNotify(webhookURLStr, webhookSecret, data) |
| } |
| return nil |
| } |
|
|
| func sendEmailNotify(userEmail string, data dto.Notify) error { |
| |
| content := data.Content |
| |
| for _, value := range data.Values { |
| content = strings.Replace(content, dto.ContentValueParam, fmt.Sprintf("%v", value), 1) |
| } |
| return common.SendEmail(data.Title, userEmail, content) |
| } |
|
|