2024-10-24 16:56:22 +00:00
|
|
|
package scheduler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex/rx"
|
|
|
|
"git.solsynth.dev/hypernet/pusher/pkg/internal/gap"
|
|
|
|
"git.solsynth.dev/hypernet/pusher/pkg/internal/provider"
|
|
|
|
"git.solsynth.dev/hypernet/pusher/pkg/pushkit"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
"github.com/goccy/go-json"
|
|
|
|
"github.com/nats-io/nats.go"
|
2024-11-23 04:00:29 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2024-10-24 16:56:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var validate = validator.New(validator.WithRequiredStructEnabled())
|
|
|
|
|
|
|
|
func SubscribeToQueue() error {
|
|
|
|
mq, err := rx.NewMqConn(gap.Nx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to initialize Nex.Rx connection: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = mq.Nt.Subscribe(pushkit.PushNotificationMqTopic, func(msg *nats.Msg) {
|
|
|
|
var req pushkit.NotificationPushRequest
|
2024-11-23 04:00:29 +00:00
|
|
|
if err := json.Unmarshal(msg.Data, &req); err != nil {
|
|
|
|
log.Warn().Err(err).Msg("Dropped a notify request, unable to parse request body")
|
2024-10-24 16:56:22 +00:00
|
|
|
return
|
2024-11-23 04:00:29 +00:00
|
|
|
} else if err := validate.Struct(&req); err != nil {
|
|
|
|
log.Warn().Err(err).Msg("Dropped a notify request, failed to validate request body")
|
2024-10-24 16:56:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-26 13:46:03 +00:00
|
|
|
provider.PushNotification(req)
|
2024-10-24 16:56:22 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to subscribe notification topic: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = mq.Nt.Subscribe(pushkit.PushNotificationBatchMqTopic, func(msg *nats.Msg) {
|
|
|
|
var req pushkit.NotificationPushBatchRequest
|
2024-11-23 04:00:29 +00:00
|
|
|
if err := json.Unmarshal(msg.Data, &req); err != nil {
|
|
|
|
log.Warn().Err(err).Msg("Dropped a notify batch request, unable to parse request body")
|
2024-10-24 16:56:22 +00:00
|
|
|
return
|
2024-11-23 04:00:29 +00:00
|
|
|
} else if err := validate.Struct(&req); err != nil {
|
|
|
|
log.Warn().Err(err).Msg("Dropped a notify batch request, failed to validate request body")
|
2024-10-24 16:56:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-26 13:46:03 +00:00
|
|
|
provider.PushNotificationBatch(req)
|
2024-10-24 16:56:22 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to subscribe notification batch topic: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-10-26 15:01:40 +00:00
|
|
|
_, err = mq.Nt.Subscribe(pushkit.PushEmailMqTopic, func(msg *nats.Msg) {
|
|
|
|
var req pushkit.EmailDeliverRequest
|
2024-11-23 04:00:29 +00:00
|
|
|
if err := json.Unmarshal(msg.Data, &req); err != nil {
|
|
|
|
log.Warn().Err(err).Msg("Dropped a push email request, unable to parse request body")
|
2024-10-26 15:01:40 +00:00
|
|
|
return
|
2024-11-23 04:00:29 +00:00
|
|
|
} else if err := validate.Struct(&req); err != nil {
|
|
|
|
log.Warn().Err(err).Msg("Dropped a push email request, failed to validate request body")
|
2024-10-26 15:01:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-26 13:46:03 +00:00
|
|
|
provider.SendMail(req.To, req.Email)
|
2024-10-26 15:01:40 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to subscribe email topic: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = mq.Nt.Subscribe(pushkit.PushEmailBatchMqTopic, func(msg *nats.Msg) {
|
|
|
|
var req pushkit.EmailDeliverBatchRequest
|
2024-11-23 04:00:29 +00:00
|
|
|
if err := json.Unmarshal(msg.Data, &req); err != nil {
|
|
|
|
log.Warn().Err(err).Msg("Dropped a push email batch request, unable to parse request body")
|
2024-10-26 15:01:40 +00:00
|
|
|
return
|
2024-11-23 04:00:29 +00:00
|
|
|
} else if err := validate.Struct(&req); err != nil {
|
|
|
|
log.Warn().Err(err).Msg("Dropped a push email batch request, failed to validate request body")
|
2024-10-26 15:01:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-26 13:46:03 +00:00
|
|
|
for _, to := range req.To {
|
|
|
|
_ = provider.SendMail(to, req.Email)
|
|
|
|
}
|
2024-10-26 15:01:40 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to subscribe email batch topic: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-10-24 16:56:22 +00:00
|
|
|
return nil
|
|
|
|
}
|