(userId int, userEmail string, userSetting dto.UserSetting, data dto.Notify)
| 17 | } |
| 18 | |
| 19 | func NotifyUser(userId int, userEmail string, userSetting dto.UserSetting, data dto.Notify) error { |
| 20 | notifyType := userSetting.NotifyType |
| 21 | if notifyType == "" { |
| 22 | notifyType = dto.NotifyTypeEmail |
| 23 | } |
| 24 | |
| 25 | // Check notification limit |
| 26 | canSend, err := CheckNotificationLimit(userId, data.Type) |
| 27 | if err != nil { |
| 28 | common.SysError(fmt.Sprintf("failed to check notification limit: %s", err.Error())) |
| 29 | return err |
| 30 | } |
| 31 | if !canSend { |
| 32 | return fmt.Errorf("notification limit exceeded for user %d with type %s", userId, notifyType) |
| 33 | } |
| 34 | |
| 35 | switch notifyType { |
| 36 | case dto.NotifyTypeEmail: |
| 37 | // check setting email |
| 38 | userEmail = userSetting.NotificationEmail |
| 39 | if userEmail == "" { |
| 40 | common.SysLog(fmt.Sprintf("user %d has no email, skip sending email", userId)) |
| 41 | return nil |
| 42 | } |
| 43 | return sendEmailNotify(userEmail, data) |
| 44 | case dto.NotifyTypeWebhook: |
| 45 | webhookURLStr := userSetting.WebhookUrl |
| 46 | if webhookURLStr == "" { |
| 47 | common.SysError(fmt.Sprintf("user %d has no webhook url, skip sending webhook", userId)) |
| 48 | return nil |
| 49 | } |
| 50 | |
| 51 | // 获取 webhook secret |
| 52 | webhookSecret := userSetting.WebhookSecret |
| 53 | return SendWebhookNotify(webhookURLStr, webhookSecret, data) |
| 54 | } |
| 55 | return nil |
| 56 | } |
| 57 | |
| 58 | func sendEmailNotify(userEmail string, data dto.Notify) error { |
| 59 | // make email content |
no test coverage detected