service backend
All checks were successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/nats Pipeline was successful

This commit is contained in:
Stephan D
2025-11-07 18:35:26 +01:00
parent 20e8f9acc4
commit 62a6631b9a
537 changed files with 48453 additions and 0 deletions

1
api/pkg/messaging/internal/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
generated

View File

@@ -0,0 +1,101 @@
package messagingimp
import (
"time"
"github.com/google/uuid"
"github.com/tech/sendico/pkg/merrors"
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
"github.com/tech/sendico/pkg/model"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
type EnvelopeImp struct {
uid uuid.UUID
dateTime time.Time
data []byte
sender string
signature model.NotificationEvent
}
func (e *EnvelopeImp) GetTimeStamp() time.Time {
return e.dateTime
}
func (e *EnvelopeImp) GetMessageId() uuid.UUID {
return e.uid
}
func (e *EnvelopeImp) GetSender() string {
return e.sender
}
func (e *EnvelopeImp) GetData() []byte {
return e.data
}
func (e *EnvelopeImp) GetSignature() model.NotificationEvent {
return e.signature
}
func (e *EnvelopeImp) Serialize() ([]byte, error) {
if e.data == nil {
return nil, merrors.Internal("Envelope data is not initialized")
}
msg := gmessaging.Envelope{
Event: &gmessaging.NotificationEvent{
Type: e.signature.StringType(),
Action: e.signature.StringAction(),
},
MessageData: e.data,
Metadata: &gmessaging.EventMetadata{
MessageId: e.uid.String(),
Sender: e.sender,
Timestamp: timestamppb.New(e.dateTime),
},
}
return proto.Marshal(&msg)
}
func (e *EnvelopeImp) Wrap(data []byte) ([]byte, error) {
e.data = data
return e.Serialize()
}
func DeserializeImp(data []byte) (*EnvelopeImp, error) {
var envelope gmessaging.Envelope
if err := proto.Unmarshal(data, &envelope); err != nil {
return nil, err
}
var e EnvelopeImp
var err error
if e.uid, err = uuid.Parse(envelope.Metadata.MessageId); err != nil {
return nil, err
}
if envelope.Metadata.Timestamp != nil {
e.dateTime = envelope.Metadata.Timestamp.AsTime()
} else {
e.dateTime = time.Now()
}
if e.signature, err = model.StringToNotificationEvent(envelope.Event.Type, envelope.Event.Action); err != nil {
return nil, err
}
e.data = envelope.MessageData
e.sender = envelope.Metadata.Sender
return &e, nil
}
func CreateEnvelopeImp(sender string, signature model.NotificationEvent) *EnvelopeImp {
return &EnvelopeImp{
dateTime: time.Now(),
sender: sender,
uid: uuid.New(),
signature: signature,
}
}

View File

@@ -0,0 +1,87 @@
package inprocess
import (
"fmt"
"sync"
"github.com/tech/sendico/pkg/merrors"
me "github.com/tech/sendico/pkg/messaging/envelope"
"github.com/tech/sendico/pkg/mlogger"
"github.com/tech/sendico/pkg/model"
"github.com/tech/sendico/pkg/mutil/mzap"
"go.uber.org/zap"
)
type MessageBroker struct {
logger mlogger.Logger
subscribers map[string][]chan me.Envelope
lock sync.RWMutex
bufferSize int
}
func (b *MessageBroker) Publish(envelope me.Envelope) error {
topic := envelope.GetSignature().ToString()
b.logger.Debug("Publishing message", mzap.Envelope(envelope))
b.lock.RLock()
defer b.lock.RUnlock()
if subs, ok := b.subscribers[topic]; ok {
for _, sub := range subs {
select {
case sub <- envelope:
default:
}
}
return nil
}
b.logger.Warn("Topic not found", mzap.Envelope(envelope))
return merrors.NoMessagingTopic(topic)
}
func (b *MessageBroker) Subscribe(event model.NotificationEvent) (<-chan me.Envelope, error) {
topic := event.ToString()
b.logger.Info("New topic subscriber", zap.String("topic", topic))
ch := make(chan me.Envelope, b.bufferSize) // Buffered channel to avoid blocking publishers
{
b.lock.Lock()
defer b.lock.Unlock()
b.subscribers[topic] = append(b.subscribers[topic], ch)
}
return ch, nil
}
func (b *MessageBroker) Unsubscribe(event model.NotificationEvent, subChan <-chan me.Envelope) error {
topic := event.ToString()
b.logger.Info("Unsubscribing topic", zap.String("topic", topic))
b.lock.Lock()
defer b.lock.Unlock()
subs, ok := b.subscribers[topic]
if !ok {
b.logger.Debug("No subscribers for topic", zap.String("topic", topic))
return nil
}
for i, ch := range subs {
if ch == subChan {
b.subscribers[topic] = append(subs[:i], subs[i+1:]...)
close(ch)
return nil
}
}
b.logger.Warn("No topic found", zap.String("topic", topic))
return merrors.NoMessagingTopic(topic)
}
func NewInProcessBroker(logger mlogger.Logger, bufferSize int) (*MessageBroker, error) {
if bufferSize < 1 {
return nil, merrors.InvalidArgument(fmt.Sprintf("Invelid buffer size %d. It must be greater than 1", bufferSize))
}
logger.Info("Created in-process logger", zap.Int("buffer_size", bufferSize))
return &MessageBroker{
logger: logger.Named("in_process"),
subscribers: make(map[string][]chan me.Envelope),
bufferSize: bufferSize,
}, nil
}

View File

@@ -0,0 +1,5 @@
package inprocess
type MessagingConfig struct {
BufferSize int `mapstructure:"buffer_size" yaml:"buffer_size"`
}

View File

@@ -0,0 +1,86 @@
package natsb
import (
me "github.com/tech/sendico/pkg/messaging/envelope"
"github.com/tech/sendico/pkg/model"
"github.com/tech/sendico/pkg/mutil/mzap"
"go.uber.org/zap"
)
func (b *NatsBroker) Publish(envelope me.Envelope) error {
subject := envelope.GetSignature().ToString()
b.logger.Debug("Publishing message", mzap.Envelope(envelope))
// Serialize the message
data, err := envelope.Serialize()
if err != nil {
b.logger.Error("Failed to serialize message", zap.Error(err), mzap.Envelope(envelope))
return err
}
if err := b.nc.Publish(subject, data); err != nil {
b.logger.Error("Error publishing message", zap.Error(err), mzap.Envelope(envelope))
return err
}
b.logger.Debug("Message published", zap.String("subject", subject))
return nil
}
// Subscribe subscribes to a NATS subject and returns a channel for messages
func (b *NatsBroker) Subscribe(event model.NotificationEvent) (<-chan me.Envelope, error) {
subject := event.ToString()
b.logger.Info("Subscribing to subject", zap.String("subject", subject))
// Create a bidirectional channel to send messages to
messageChan := make(chan me.Envelope)
b.mu.Lock()
defer b.mu.Unlock()
topicSub, exists := b.topicSubs[subject]
if !exists {
var err error
topicSub, err = NewTopicSubscription(b.logger, b.nc, subject)
if err != nil {
return nil, err
}
b.topicSubs[subject] = topicSub
}
// Add the consumer's channel to the topic subscription
topicSub.AddConsumer(messageChan)
// Return the channel as a receive-only channel
return messageChan, nil
}
// Unsubscribe unsubscribes a consumer from a NATS subject
func (b *NatsBroker) Unsubscribe(event model.NotificationEvent, messageChan <-chan me.Envelope) error {
subject := event.ToString()
b.logger.Info("Unsubscribing from subject", zap.String("subject", subject))
b.mu.Lock()
topicSub, exists := b.topicSubs[subject]
b.mu.Unlock()
if !exists {
b.logger.Warn("No subscription found for subject", zap.String("subject", subject))
return nil
}
// Remove the consumer's channel from the topic subscription
topicSub.RemoveConsumer(messageChan)
if !topicSub.HasConsumers() {
if err := topicSub.Unsubscribe(); err != nil {
b.logger.Error("Error unsubscribing from subject", zap.String("subject", subject), zap.Error(err))
return err
}
b.mu.Lock()
delete(b.topicSubs, subject)
b.mu.Unlock()
}
b.logger.Info("Unsubscribed from subject", zap.String("subject", subject))
return nil
}

View File

@@ -0,0 +1,113 @@
package natsb
import (
"fmt"
"net"
"net/url"
"os"
"strconv"
"sync"
"time"
"github.com/nats-io/nats.go"
"github.com/tech/sendico/pkg/merrors"
nc "github.com/tech/sendico/pkg/messaging/internal/natsb/config"
"github.com/tech/sendico/pkg/mlogger"
"go.uber.org/zap"
)
type natsSubscriotions = map[string]*TopicSubscription
type NatsBroker struct {
nc *nats.Conn
logger *zap.Logger
topicSubs natsSubscriotions
mu sync.Mutex
}
type envConfig struct {
User, Password, Host string
Port int
}
// loadEnv gathers and validates connection details from environment variables
// listed in the Settings struct. Invalid or missing values surface as a typed
// InvalidArgument error so callers can decide how to handle them.
func loadEnv(settings *nc.Settings, l *zap.Logger) (*envConfig, error) {
get := func(key, label string) (string, error) {
if v := os.Getenv(key); v != "" {
return v, nil
}
l.Error(fmt.Sprintf("NATS %s not found in environment", label), zap.String("env_var", key))
return "", merrors.InvalidArgument(fmt.Sprintf("NATS %s not found in environment variable: %s", label, key))
}
user, err := get(settings.UsernameEnv, "user name")
if err != nil {
return nil, err
}
password, err := get(settings.PasswordEnv, "password")
if err != nil {
return nil, err
}
host, err := get(settings.HostEnv, "host")
if err != nil {
return nil, err
}
portStr, err := get(settings.PortEnv, "port")
if err != nil {
return nil, err
}
port, err := strconv.Atoi(portStr)
if err != nil || port <= 0 || port > 65535 {
l.Error("Invalid NATS port value", zap.String("port", portStr))
return nil, merrors.InvalidArgument("Invalid NATS port: " + portStr)
}
return &envConfig{
User: user,
Password: password,
Host: host,
Port: port,
}, nil
}
func NewNatsBroker(logger mlogger.Logger, settings *nc.Settings) (*NatsBroker, error) {
l := logger.Named("broker")
// Helper function to get environment variables
cfg, err := loadEnv(settings, l)
if err != nil {
return nil, err
}
u := &url.URL{
Scheme: "nats",
Host: net.JoinHostPort(cfg.Host, strconv.Itoa(cfg.Port)),
}
natsURL := u.String()
opts := []nats.Option{
nats.Name(settings.NATSName),
nats.MaxReconnects(settings.MaxReconnects),
nats.ReconnectWait(time.Duration(settings.ReconnectWait) * time.Second),
nats.UserInfo(cfg.User, cfg.Password),
}
res := &NatsBroker{
logger: l.Named("nats"),
topicSubs: natsSubscriotions{},
}
if res.nc, err = nats.Connect(natsURL, opts...); err != nil {
l.Error("Failed to connect to NATS", zap.String("url", natsURL), zap.Error(err))
return nil, err
}
logger.Info("Connected to NATS", zap.String("broker", settings.NATSName),
zap.String("url", fmt.Sprintf("nats://%s@%s", cfg.User, net.JoinHostPort(cfg.Host, strconv.Itoa(cfg.Port)))))
return res, nil
}

View File

@@ -0,0 +1,12 @@
package natsb
type Settings struct {
URLEnv string `mapstructure:"url_env" yaml:"url_env"`
HostEnv string `mapstructure:"host_env" yaml:"host_env"`
PortEnv string `mapstructure:"port_env" yaml:"port_env"`
UsernameEnv string `mapstructure:"username_env" yaml:"username_env"`
PasswordEnv string `mapstructure:"password_env" yaml:"password_env"`
NATSName string `mapstructure:"broker_name" yaml:"broker_name"`
MaxReconnects int `mapstructure:"max_reconnects" yaml:"max_reconnects"`
ReconnectWait int `mapstructure:"reconnect_wait" yaml:"reconnect_wait"`
}

View File

@@ -0,0 +1,78 @@
package natsb
import (
"sync"
"github.com/nats-io/nats.go"
me "github.com/tech/sendico/pkg/messaging/envelope"
"github.com/tech/sendico/pkg/mlogger"
"go.uber.org/zap"
)
type TopicSubscription struct {
sub *nats.Subscription
consumers map[<-chan me.Envelope]chan me.Envelope
mu sync.Mutex
logger mlogger.Logger
}
func NewTopicSubscription(logger mlogger.Logger, nc *nats.Conn, subject string) (*TopicSubscription, error) {
ts := &TopicSubscription{
consumers: make(map[<-chan me.Envelope]chan me.Envelope),
logger: logger.Named(subject),
}
sub, err := nc.Subscribe(subject, ts.handleMessage)
if err != nil {
logger.Error("Error subscribing to subject", zap.String("subject", subject), zap.Error(err))
return nil, err
}
ts.sub = sub
return ts, nil
}
func (ts *TopicSubscription) handleMessage(m *nats.Msg) {
ts.logger.Debug("Received message", zap.String("subject", m.Subject))
envelope, err := me.Deserialize(m.Data)
if err != nil {
ts.logger.Warn("Failed to deserialize message", zap.String("subject", m.Subject), zap.Error(err))
return // Do not push invalid data to the channels
}
ts.mu.Lock()
defer ts.mu.Unlock()
for _, c := range ts.consumers {
select {
case c <- envelope:
default:
ts.logger.Warn("Consumer is slow or not receiving messages", zap.String("subject", m.Subject))
}
}
}
func (ts *TopicSubscription) AddConsumer(messageChan chan me.Envelope) {
ts.mu.Lock()
ts.consumers[messageChan] = messageChan
ts.mu.Unlock()
}
func (ts *TopicSubscription) RemoveConsumer(messageChan <-chan me.Envelope) {
ts.mu.Lock()
if c, ok := ts.consumers[messageChan]; ok {
delete(ts.consumers, messageChan)
close(c)
}
ts.mu.Unlock()
}
func (ts *TopicSubscription) HasConsumers() bool {
ts.mu.Lock()
defer ts.mu.Unlock()
return len(ts.consumers) > 0
}
func (ts *TopicSubscription) Unsubscribe() error {
return ts.sub.Drain()
}

View File

@@ -0,0 +1,37 @@
package notifications
import (
messaging "github.com/tech/sendico/pkg/messaging/envelope"
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
"github.com/tech/sendico/pkg/model"
nm "github.com/tech/sendico/pkg/model/notification"
"github.com/tech/sendico/pkg/mservice"
"go.mongodb.org/mongo-driver/bson/primitive"
"google.golang.org/protobuf/proto"
)
type AccountNotification struct {
messaging.Envelope
accountRef primitive.ObjectID
}
func (acn *AccountNotification) Serialize() ([]byte, error) {
var msg gmessaging.AccountCreatedEvent
msg.AccountRef = acn.accountRef.Hex()
data, err := proto.Marshal(&msg)
if err != nil {
return nil, err
}
return acn.Envelope.Wrap(data)
}
func NewAccountNotification(action nm.NotificationAction) model.NotificationEvent {
return model.NewNotification(mservice.Accounts, action)
}
func NewAccountImp(sender string, accountRef primitive.ObjectID, action nm.NotificationAction) messaging.Envelope {
return &AccountNotification{
Envelope: messaging.CreateEnvelope(sender, NewAccountNotification(action)),
accountRef: accountRef,
}
}

View File

@@ -0,0 +1,40 @@
package notifications
import (
messaging "github.com/tech/sendico/pkg/messaging/envelope"
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
"github.com/tech/sendico/pkg/model"
nm "github.com/tech/sendico/pkg/model/notification"
"github.com/tech/sendico/pkg/mservice"
"go.mongodb.org/mongo-driver/bson/primitive"
"google.golang.org/protobuf/proto"
)
type PasswordResetNotification struct {
messaging.Envelope
accountRef primitive.ObjectID
resetToken string
}
func (prn *PasswordResetNotification) Serialize() ([]byte, error) {
var msg gmessaging.PasswordResetEvent
msg.AccountRef = prn.accountRef.Hex()
msg.ResetToken = prn.resetToken
data, err := proto.Marshal(&msg)
if err != nil {
return nil, err
}
return prn.Envelope.Wrap(data)
}
func NewPasswordResetNotification(action nm.NotificationAction) model.NotificationEvent {
return model.NewNotification(mservice.Accounts, action)
}
func NewPasswordResetImp(sender string, accountRef primitive.ObjectID, resetToken string, action nm.NotificationAction) messaging.Envelope {
return &PasswordResetNotification{
Envelope: messaging.CreateEnvelope(sender, NewPasswordResetNotification(action)),
accountRef: accountRef,
resetToken: resetToken,
}
}

View File

@@ -0,0 +1,57 @@
package notifications
import (
"context"
"github.com/tech/sendico/pkg/db/account"
me "github.com/tech/sendico/pkg/messaging/envelope"
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
mah "github.com/tech/sendico/pkg/messaging/notifications/account/handler"
np "github.com/tech/sendico/pkg/messaging/notifications/processor"
"github.com/tech/sendico/pkg/mlogger"
"github.com/tech/sendico/pkg/model"
nm "github.com/tech/sendico/pkg/model/notification"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
)
type PasswordResetNotificationProcessor struct {
logger mlogger.Logger
handler mah.PasswordResetHandler
db account.DB
event model.NotificationEvent
}
func (prnp *PasswordResetNotificationProcessor) Process(ctx context.Context, envelope me.Envelope) error {
var msg gmessaging.PasswordResetEvent
if err := proto.Unmarshal(envelope.GetData(), &msg); err != nil {
prnp.logger.Warn("Failed to unmarshall envelope", zap.Error(err), zap.String("topic", prnp.event.ToString()))
return err
}
accountRef, err := primitive.ObjectIDFromHex(msg.AccountRef)
if err != nil {
prnp.logger.Warn("Failed to restore object ID", zap.Error(err), zap.String("topic", prnp.event.ToString()), zap.String("account_ref", msg.AccountRef))
return err
}
var account model.Account
if err := prnp.db.Get(ctx, accountRef, &account); err != nil {
prnp.logger.Warn("Failed to fetch account", zap.Error(err), zap.String("topic", prnp.event.ToString()), zap.String("account_ref", msg.AccountRef))
return err
}
return prnp.handler(ctx, &account, msg.ResetToken)
}
func (prnp *PasswordResetNotificationProcessor) GetSubject() model.NotificationEvent {
return prnp.event
}
func NewPasswordResetMessageProcessor(logger mlogger.Logger, handler mah.PasswordResetHandler, db account.DB, action nm.NotificationAction) np.EnvelopeProcessor {
event := NewPasswordResetNotification(action)
return &PasswordResetNotificationProcessor{
logger: logger.Named("password_reset_processor"),
handler: handler,
db: db,
event: event,
}
}

View File

@@ -0,0 +1,57 @@
package notifications
import (
"context"
"github.com/tech/sendico/pkg/db/account"
me "github.com/tech/sendico/pkg/messaging/envelope"
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
mah "github.com/tech/sendico/pkg/messaging/notifications/account/handler"
np "github.com/tech/sendico/pkg/messaging/notifications/processor"
"github.com/tech/sendico/pkg/mlogger"
"github.com/tech/sendico/pkg/model"
nm "github.com/tech/sendico/pkg/model/notification"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
)
type AccoountNotificaionProcessor struct {
logger mlogger.Logger
handler mah.AccountHandler
db account.DB
event model.NotificationEvent
}
func (acnp *AccoountNotificaionProcessor) Process(ctx context.Context, envelope me.Envelope) error {
var msg gmessaging.AccountCreatedEvent
if err := proto.Unmarshal(envelope.GetData(), &msg); err != nil {
acnp.logger.Warn("Failed to unmarshall envelope", zap.Error(err), zap.String("topic", acnp.event.ToString()))
return err
}
accountRef, err := primitive.ObjectIDFromHex(msg.AccountRef)
if err != nil {
acnp.logger.Warn("Failed to restore object ID", zap.Error(err), zap.String("topic", acnp.event.ToString()), zap.String("account_ref", msg.AccountRef))
return err
}
var account model.Account
if err := acnp.db.Get(ctx, accountRef, &account); err != nil {
acnp.logger.Warn("Failed to fetch account", zap.Error(err), zap.String("topic", acnp.event.ToString()), zap.String("account_ref", msg.AccountRef))
return err
}
return acnp.handler(ctx, &account)
}
func (acnp *AccoountNotificaionProcessor) GetSubject() model.NotificationEvent {
return acnp.event
}
func NewAccountMessageProcessor(logger mlogger.Logger, handler mah.AccountHandler, db account.DB, action nm.NotificationAction) np.EnvelopeProcessor {
event := NewAccountNotification(action)
return &AccoountNotificaionProcessor{
logger: logger.Named("message_processor"),
handler: handler,
db: db,
event: event,
}
}

View File

@@ -0,0 +1,63 @@
package notifications
import (
"context"
"github.com/tech/sendico/pkg/db/account"
"github.com/tech/sendico/pkg/db/invitation"
mih "github.com/tech/sendico/pkg/messaging/notifications/invitation/handler"
no "github.com/tech/sendico/pkg/messaging/notifications/object"
np "github.com/tech/sendico/pkg/messaging/notifications/processor"
"github.com/tech/sendico/pkg/mlogger"
"github.com/tech/sendico/pkg/model"
nm "github.com/tech/sendico/pkg/model/notification"
"github.com/tech/sendico/pkg/mservice"
"github.com/tech/sendico/pkg/mutil/mzap"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.uber.org/zap"
)
type InvitationNotificaionProcessor struct {
np.EnvelopeProcessor
logger mlogger.Logger
handler mih.InvitationHandler
db invitation.DB
adb account.DB
}
func (ianp *InvitationNotificaionProcessor) onInvitation(
ctx context.Context,
objectType mservice.Type,
objectRef, actorAccountRef primitive.ObjectID,
action nm.NotificationAction,
) error {
var invitation model.Invitation
if err := ianp.db.Unprotected().Get(ctx, objectRef, &invitation); err != nil {
ianp.logger.Warn("Failed to fetch invitation object", zap.Error(err), mzap.ObjRef("object_ref", objectRef))
return err
}
var account model.Account
if err := ianp.adb.Get(ctx, actorAccountRef, &account); err != nil {
ianp.logger.Warn("Failed to fetch actor account", zap.Error(err), mzap.ObjRef("actor_account_ref", actorAccountRef))
return err
}
return ianp.handler(ctx, &account, &invitation)
}
func NewInvitationMessageProcessor(
logger mlogger.Logger,
handler mih.InvitationHandler,
db invitation.DB,
adb account.DB,
action nm.NotificationAction,
) np.EnvelopeProcessor {
l := logger.Named(mservice.Invitations)
res := &InvitationNotificaionProcessor{
logger: l,
db: db,
adb: adb,
handler: handler,
}
res.EnvelopeProcessor = no.NewObjectChangedMessageProcessor(l, mservice.Invitations, action, res.onInvitation)
return res
}

View File

@@ -0,0 +1,44 @@
package notifications
import (
messaging "github.com/tech/sendico/pkg/messaging/envelope"
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
"github.com/tech/sendico/pkg/model"
nm "github.com/tech/sendico/pkg/model/notification"
"github.com/tech/sendico/pkg/mservice"
"google.golang.org/protobuf/proto"
)
type NResultNotification struct {
messaging.Envelope
result *model.NotificationResult
}
func (nrn *NResultNotification) Serialize() ([]byte, error) {
msg := gmessaging.NotificationSentEvent{
UserID: nrn.result.UserID,
Channel: nrn.result.Channel,
Locale: nrn.result.Locale,
TemplateID: nrn.result.TemplateID,
Status: &gmessaging.OperationResult{
IsSuccessful: nrn.result.Result.IsSuccessful,
ErrorDescription: nrn.result.Result.Error,
},
}
data, err := proto.Marshal(&msg)
if err != nil {
return nil, err
}
return nrn.Envelope.Wrap(data)
}
func NewNRNotification() model.NotificationEvent {
return model.NewNotification(mservice.Notifications, nm.NASent)
}
func NewNResultNotification(sender string, result *model.NotificationResult) messaging.Envelope {
return &NResultNotification{
Envelope: messaging.CreateEnvelope(sender, NewNRNotification()),
result: result,
}
}

View File

@@ -0,0 +1,53 @@
package notifications
import (
"context"
me "github.com/tech/sendico/pkg/messaging/envelope"
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
nh "github.com/tech/sendico/pkg/messaging/notifications/notification/handler"
np "github.com/tech/sendico/pkg/messaging/notifications/processor"
"github.com/tech/sendico/pkg/mlogger"
"github.com/tech/sendico/pkg/model"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
)
type NResultNotificaionProcessor struct {
logger mlogger.Logger
handler nh.NResultHandler
event model.NotificationEvent
}
func (nrp *NResultNotificaionProcessor) Process(ctx context.Context, envelope me.Envelope) error {
var msg gmessaging.NotificationSentEvent
if err := proto.Unmarshal(envelope.GetData(), &msg); err != nil {
nrp.logger.Warn("Failed to unmarshall envelope", zap.Error(err), zap.String("topic", nrp.event.ToString()))
return err
}
nresult := &model.NotificationResult{
AmpliEvent: model.AmpliEvent{
UserID: msg.UserID,
},
Channel: msg.Channel,
TemplateID: msg.TemplateID,
Locale: msg.Locale,
Result: model.OperationResult{
IsSuccessful: msg.Status.IsSuccessful,
Error: msg.Status.ErrorDescription,
},
}
return nrp.handler(ctx, nresult)
}
func (nrp *NResultNotificaionProcessor) GetSubject() model.NotificationEvent {
return nrp.event
}
func NewAccountMessageProcessor(logger mlogger.Logger, handler nh.NResultHandler) np.EnvelopeProcessor {
return &NResultNotificaionProcessor{
logger: logger.Named("message_processor"),
handler: handler,
event: NewNRNotification(),
}
}

View File

@@ -0,0 +1,46 @@
package notifications
import (
messaging "github.com/tech/sendico/pkg/messaging/envelope"
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
"github.com/tech/sendico/pkg/model"
nm "github.com/tech/sendico/pkg/model/notification"
"github.com/tech/sendico/pkg/mservice"
"go.mongodb.org/mongo-driver/bson/primitive"
"google.golang.org/protobuf/proto"
)
type ObjectNotification struct {
messaging.Envelope
actorAccountRef primitive.ObjectID
objectRef primitive.ObjectID
}
func (acn *ObjectNotification) Serialize() ([]byte, error) {
var msg gmessaging.ObjectUpdatedEvent
msg.ActorAccountRef = acn.actorAccountRef.Hex()
msg.ObjectRef = acn.objectRef.Hex()
data, err := proto.Marshal(&msg)
if err != nil {
return nil, err
}
return acn.Envelope.Wrap(data)
}
func NewObjectNotification(t mservice.Type, action nm.NotificationAction) model.NotificationEvent {
return model.NewNotification(t, action)
}
func NewObjectImp(
sender string,
actorAccountRef primitive.ObjectID,
objectType mservice.Type,
objectRef primitive.ObjectID,
action nm.NotificationAction,
) messaging.Envelope {
return &ObjectNotification{
Envelope: messaging.CreateEnvelope(sender, NewObjectNotification(objectType, action)),
actorAccountRef: actorAccountRef,
objectRef: objectRef,
}
}

View File

@@ -0,0 +1,55 @@
package notifications
import (
"context"
me "github.com/tech/sendico/pkg/messaging/envelope"
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
moh "github.com/tech/sendico/pkg/messaging/notifications/object/handler"
np "github.com/tech/sendico/pkg/messaging/notifications/processor"
"github.com/tech/sendico/pkg/mlogger"
"github.com/tech/sendico/pkg/model"
nm "github.com/tech/sendico/pkg/model/notification"
"github.com/tech/sendico/pkg/mservice"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
)
type ObjectNotificaionProcessor struct {
logger mlogger.Logger
handler moh.ObjectUpdateHandler
event model.NotificationEvent
}
func (ounp *ObjectNotificaionProcessor) Process(ctx context.Context, envelope me.Envelope) error {
var msg gmessaging.ObjectUpdatedEvent
if err := proto.Unmarshal(envelope.GetData(), &msg); err != nil {
ounp.logger.Warn("Failed to unmarshall envelope", zap.Error(err), zap.String("topic", ounp.event.ToString()))
return err
}
actorAccountRef, err := primitive.ObjectIDFromHex(msg.ActorAccountRef)
if err != nil {
ounp.logger.Warn("Failed to restore actor account reference", zap.Error(err), zap.String("topic", ounp.event.ToString()), zap.String("actor_account_ref", msg.ActorAccountRef))
return err
}
objectRef, err := primitive.ObjectIDFromHex(msg.ObjectRef)
if err != nil {
ounp.logger.Warn("Failed to restore object reference", zap.Error(err), zap.String("topic", ounp.event.ToString()), zap.String("object_ref", msg.ObjectRef))
return err
}
return ounp.handler(ctx, envelope.GetSignature().GetType(), objectRef, actorAccountRef, envelope.GetSignature().GetAction())
}
func (acnp *ObjectNotificaionProcessor) GetSubject() model.NotificationEvent {
return acnp.event
}
func NewObjectChangeMessageProcessor(logger mlogger.Logger, handler moh.ObjectUpdateHandler, objectType mservice.Type, action nm.NotificationAction) np.EnvelopeProcessor {
return &ObjectNotificaionProcessor{
logger: logger.Named("message_processor"),
handler: handler,
event: NewObjectNotification(objectType, action),
}
}

View File

@@ -0,0 +1,26 @@
package messagingimp
import (
mb "github.com/tech/sendico/pkg/messaging/broker"
me "github.com/tech/sendico/pkg/messaging/envelope"
"github.com/tech/sendico/pkg/mlogger"
"github.com/tech/sendico/pkg/mutil/mzap"
"go.uber.org/zap"
)
type ChannelProducer struct {
logger mlogger.Logger
broker mb.Broker
}
func (p *ChannelProducer) SendMessage(envelope me.Envelope) error {
// TODO: won't work with Kafka, need to serialize/deserialize
if err := p.broker.Publish(envelope); err != nil {
p.logger.Warn("Failed to publish message", zap.Error(err), mzap.Envelope(envelope))
}
return nil
}
func NewProducer(logger mlogger.Logger, broker mb.Broker) *ChannelProducer {
return &ChannelProducer{logger: logger.Named("producer"), broker: broker}
}