
Update User to carry webhook from circle if assigned Refactor notification handling and update models for webhook support
54 lines
1.5 KiB
Go
54 lines
1.5 KiB
Go
package notifier
|
|
|
|
import (
|
|
"context"
|
|
|
|
"donetick.com/core/internal/events"
|
|
nModel "donetick.com/core/internal/notifier/model"
|
|
pushover "donetick.com/core/internal/notifier/service/pushover"
|
|
telegram "donetick.com/core/internal/notifier/service/telegram"
|
|
|
|
"donetick.com/core/logging"
|
|
)
|
|
|
|
type Notifier struct {
|
|
Telegram *telegram.TelegramNotifier
|
|
Pushover *pushover.Pushover
|
|
eventsProducer *events.EventsProducer
|
|
}
|
|
|
|
func NewNotifier(t *telegram.TelegramNotifier, p *pushover.Pushover, ep *events.EventsProducer) *Notifier {
|
|
return &Notifier{
|
|
Telegram: t,
|
|
Pushover: p,
|
|
eventsProducer: ep,
|
|
}
|
|
}
|
|
|
|
func (n *Notifier) SendNotification(c context.Context, notification *nModel.NotificationDetails) error {
|
|
log := logging.FromContext(c)
|
|
var err error
|
|
switch notification.TypeID {
|
|
case nModel.NotificationPlatformTelegram:
|
|
if n.Telegram == nil {
|
|
log.Error("Telegram bot is not initialized, Skipping sending message")
|
|
return nil
|
|
}
|
|
err = n.Telegram.SendNotification(c, notification)
|
|
case nModel.NotificationPlatformPushover:
|
|
if n.Pushover == nil {
|
|
log.Error("Pushover is not initialized, Skipping sending message")
|
|
return nil
|
|
}
|
|
err = n.Pushover.SendNotification(c, notification)
|
|
}
|
|
if err != nil {
|
|
log.Error("Failed to send notification", "err", err)
|
|
}
|
|
if notification.RawEvent != nil && notification.WebhookURL != nil {
|
|
// if we have a webhook url, we should send the event to the webhook
|
|
n.eventsProducer.NotificaitonEvent(c, *notification.WebhookURL, notification.RawEvent)
|
|
}
|
|
|
|
return nil
|
|
}
|