feat: add notification events handling and MQTT integration
- Implemented notification event publishing with a new NotificationEventPublisher interface. - Created a noopNotificationEventPublisher for testing purposes. - Added functionality to publish notification created events via MQTT. - Introduced a new stream event publisher for handling job logs and updates. - Added database migration for popup_ads table. - Created tests for notification events and popup ads functionality. - Established MQTT connection and publishing helpers for event messages.
This commit is contained in:
44
internal/transport/mqtt/notification_publisher.go
Normal file
44
internal/transport/mqtt/notification_publisher.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package mqtt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
pahomqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
"stream.api/internal/database/model"
|
||||
"stream.api/internal/service"
|
||||
"stream.api/pkg/logger"
|
||||
)
|
||||
|
||||
type notificationPublisher struct {
|
||||
client pahomqtt.Client
|
||||
logger logger.Logger
|
||||
prefix string
|
||||
}
|
||||
|
||||
func NewNotificationPublisher(client pahomqtt.Client, appLogger logger.Logger) service.NotificationEventPublisher {
|
||||
if client == nil {
|
||||
return service.NotificationEventPublisher(serviceNotificationNoop{})
|
||||
}
|
||||
return ¬ificationPublisher{client: client, logger: appLogger, prefix: defaultMQTTPrefix}
|
||||
}
|
||||
|
||||
type serviceNotificationNoop struct{}
|
||||
|
||||
func (serviceNotificationNoop) PublishNotificationCreated(context.Context, *model.Notification) error { return nil }
|
||||
|
||||
func (p *notificationPublisher) PublishNotificationCreated(_ context.Context, notification *model.Notification) error {
|
||||
if p == nil || notification == nil {
|
||||
return nil
|
||||
}
|
||||
message := mqttEvent{
|
||||
Type: "notification.created",
|
||||
Payload: service.BuildNotificationCreatedPayload(notification),
|
||||
}
|
||||
return publishMQTTJSON(p.client, p.notificationTopic(notification.UserID), message)
|
||||
}
|
||||
|
||||
func (p *notificationPublisher) notificationTopic(userID string) string {
|
||||
return fmt.Sprintf("%s/notifications/%s", p.prefix, userID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user