refactored notificatoin / tgsettle responsibility boundaries
This commit is contained in:
@@ -36,11 +36,34 @@ func (crn *ConfirmationResultNotification) Serialize() ([]byte, error) {
|
||||
return crn.Envelope.Wrap(data)
|
||||
}
|
||||
|
||||
type ConfirmationDispatchNotification struct {
|
||||
messaging.Envelope
|
||||
payload model.ConfirmationRequestDispatch
|
||||
}
|
||||
|
||||
func (cdn *ConfirmationDispatchNotification) Serialize() ([]byte, error) {
|
||||
data, err := json.Marshal(cdn.payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cdn.Envelope.Wrap(data)
|
||||
}
|
||||
|
||||
func confirmationRequestEvent() model.NotificationEvent {
|
||||
return model.NewNotification(mservice.Notifications, nm.NAConfirmationRequest)
|
||||
}
|
||||
|
||||
func confirmationResultEvent(sourceService, rail string) model.NotificationEvent {
|
||||
sourceService, rail = normalizeSourceRail(sourceService, rail)
|
||||
return model.NewNotification(mservice.Verification, nm.NotificationAction(sourceService+"."+rail))
|
||||
}
|
||||
|
||||
func confirmationDispatchEvent(sourceService, rail string) model.NotificationEvent {
|
||||
sourceService, rail = normalizeSourceRail(sourceService, rail)
|
||||
return model.NewNotification(mservice.Verification, nm.NotificationAction(sourceService+"."+rail+".dispatch"))
|
||||
}
|
||||
|
||||
func normalizeSourceRail(sourceService, rail string) (string, string) {
|
||||
action := strings.TrimSpace(sourceService)
|
||||
if action == "" {
|
||||
action = "unknown"
|
||||
@@ -51,7 +74,7 @@ func confirmationResultEvent(sourceService, rail string) model.NotificationEvent
|
||||
rail = "default"
|
||||
}
|
||||
rail = strings.ToLower(rail)
|
||||
return model.NewNotification(mservice.Verification, nm.NotificationAction(action+"."+rail))
|
||||
return action, rail
|
||||
}
|
||||
|
||||
func NewConfirmationRequestEnvelope(sender string, request *model.ConfirmationRequest) messaging.Envelope {
|
||||
@@ -75,3 +98,14 @@ func NewConfirmationResultEnvelope(sender string, result *model.ConfirmationResu
|
||||
payload: payload,
|
||||
}
|
||||
}
|
||||
|
||||
func NewConfirmationDispatchEnvelope(sender string, dispatch *model.ConfirmationRequestDispatch, sourceService, rail string) messaging.Envelope {
|
||||
var payload model.ConfirmationRequestDispatch
|
||||
if dispatch != nil {
|
||||
payload = *dispatch
|
||||
}
|
||||
return &ConfirmationDispatchNotification{
|
||||
Envelope: messaging.CreateEnvelope(sender, confirmationDispatchEvent(sourceService, rail)),
|
||||
payload: payload,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,6 +58,29 @@ func (crp *ConfirmationResultProcessor) GetSubject() model.NotificationEvent {
|
||||
return crp.event
|
||||
}
|
||||
|
||||
type ConfirmationDispatchProcessor struct {
|
||||
logger mlogger.Logger
|
||||
handler ch.ConfirmationDispatchHandler
|
||||
event model.NotificationEvent
|
||||
}
|
||||
|
||||
func (cdp *ConfirmationDispatchProcessor) Process(ctx context.Context, envelope me.Envelope) error {
|
||||
var msg model.ConfirmationRequestDispatch
|
||||
if err := json.Unmarshal(envelope.GetData(), &msg); err != nil {
|
||||
cdp.logger.Warn("Failed to decode confirmation dispatch envelope", zap.Error(err), zap.String("topic", cdp.event.ToString()))
|
||||
return err
|
||||
}
|
||||
if cdp.handler == nil {
|
||||
cdp.logger.Warn("Confirmation dispatch handler is not configured", zap.String("topic", cdp.event.ToString()))
|
||||
return nil
|
||||
}
|
||||
return cdp.handler(ctx, &msg)
|
||||
}
|
||||
|
||||
func (cdp *ConfirmationDispatchProcessor) GetSubject() model.NotificationEvent {
|
||||
return cdp.event
|
||||
}
|
||||
|
||||
func NewConfirmationRequestProcessor(logger mlogger.Logger, handler ch.ConfirmationRequestHandler) np.EnvelopeProcessor {
|
||||
if logger != nil {
|
||||
logger = logger.Named("confirmation_request_processor")
|
||||
@@ -79,3 +102,14 @@ func NewConfirmationResultProcessor(logger mlogger.Logger, sourceService, rail s
|
||||
event: confirmationResultEvent(sourceService, rail),
|
||||
}
|
||||
}
|
||||
|
||||
func NewConfirmationDispatchProcessor(logger mlogger.Logger, sourceService, rail string, handler ch.ConfirmationDispatchHandler) np.EnvelopeProcessor {
|
||||
if logger != nil {
|
||||
logger = logger.Named("confirmation_dispatch_processor")
|
||||
}
|
||||
return &ConfirmationDispatchProcessor{
|
||||
logger: logger,
|
||||
handler: handler,
|
||||
event: confirmationDispatchEvent(sourceService, rail),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,40 @@ func telegramReactionEvent() model.NotificationEvent {
|
||||
return model.NewNotification(mservice.Notifications, nm.NATelegramReaction)
|
||||
}
|
||||
|
||||
type TelegramTextNotification struct {
|
||||
messaging.Envelope
|
||||
payload model.TelegramTextRequest
|
||||
}
|
||||
|
||||
func (ttn *TelegramTextNotification) Serialize() ([]byte, error) {
|
||||
data, err := json.Marshal(ttn.payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ttn.Envelope.Wrap(data)
|
||||
}
|
||||
|
||||
func telegramTextEvent() model.NotificationEvent {
|
||||
return model.NewNotification(mservice.Notifications, nm.NATelegramText)
|
||||
}
|
||||
|
||||
type TelegramUpdateNotification struct {
|
||||
messaging.Envelope
|
||||
payload model.TelegramWebhookUpdate
|
||||
}
|
||||
|
||||
func (tun *TelegramUpdateNotification) Serialize() ([]byte, error) {
|
||||
data, err := json.Marshal(tun.payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tun.Envelope.Wrap(data)
|
||||
}
|
||||
|
||||
func telegramUpdateEvent() model.NotificationEvent {
|
||||
return model.NewNotification(mservice.Notifications, nm.NATelegramUpdate)
|
||||
}
|
||||
|
||||
func NewTelegramReactionEnvelope(sender string, request *model.TelegramReactionRequest) messaging.Envelope {
|
||||
var payload model.TelegramReactionRequest
|
||||
if request != nil {
|
||||
@@ -36,3 +70,25 @@ func NewTelegramReactionEnvelope(sender string, request *model.TelegramReactionR
|
||||
payload: payload,
|
||||
}
|
||||
}
|
||||
|
||||
func NewTelegramTextEnvelope(sender string, request *model.TelegramTextRequest) messaging.Envelope {
|
||||
var payload model.TelegramTextRequest
|
||||
if request != nil {
|
||||
payload = *request
|
||||
}
|
||||
return &TelegramTextNotification{
|
||||
Envelope: messaging.CreateEnvelope(sender, telegramTextEvent()),
|
||||
payload: payload,
|
||||
}
|
||||
}
|
||||
|
||||
func NewTelegramUpdateEnvelope(sender string, update *model.TelegramWebhookUpdate) messaging.Envelope {
|
||||
var payload model.TelegramWebhookUpdate
|
||||
if update != nil {
|
||||
payload = *update
|
||||
}
|
||||
return &TelegramUpdateNotification{
|
||||
Envelope: messaging.CreateEnvelope(sender, telegramUpdateEvent()),
|
||||
payload: payload,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,3 +45,71 @@ func NewTelegramReactionProcessor(logger mlogger.Logger, handler ch.TelegramReac
|
||||
event: telegramReactionEvent(),
|
||||
}
|
||||
}
|
||||
|
||||
type TelegramTextProcessor struct {
|
||||
logger mlogger.Logger
|
||||
handler ch.TelegramTextHandler
|
||||
event model.NotificationEvent
|
||||
}
|
||||
|
||||
func (ttp *TelegramTextProcessor) Process(ctx context.Context, envelope me.Envelope) error {
|
||||
var msg model.TelegramTextRequest
|
||||
if err := json.Unmarshal(envelope.GetData(), &msg); err != nil {
|
||||
ttp.logger.Warn("Failed to decode telegram text envelope", zap.Error(err), zap.String("topic", ttp.event.ToString()))
|
||||
return err
|
||||
}
|
||||
if ttp.handler == nil {
|
||||
ttp.logger.Warn("Telegram text handler is not configured", zap.String("topic", ttp.event.ToString()))
|
||||
return nil
|
||||
}
|
||||
return ttp.handler(ctx, &msg)
|
||||
}
|
||||
|
||||
func (ttp *TelegramTextProcessor) GetSubject() model.NotificationEvent {
|
||||
return ttp.event
|
||||
}
|
||||
|
||||
func NewTelegramTextProcessor(logger mlogger.Logger, handler ch.TelegramTextHandler) np.EnvelopeProcessor {
|
||||
if logger != nil {
|
||||
logger = logger.Named("telegram_text_processor")
|
||||
}
|
||||
return &TelegramTextProcessor{
|
||||
logger: logger,
|
||||
handler: handler,
|
||||
event: telegramTextEvent(),
|
||||
}
|
||||
}
|
||||
|
||||
type TelegramUpdateProcessor struct {
|
||||
logger mlogger.Logger
|
||||
handler ch.TelegramUpdateHandler
|
||||
event model.NotificationEvent
|
||||
}
|
||||
|
||||
func (tup *TelegramUpdateProcessor) Process(ctx context.Context, envelope me.Envelope) error {
|
||||
var msg model.TelegramWebhookUpdate
|
||||
if err := json.Unmarshal(envelope.GetData(), &msg); err != nil {
|
||||
tup.logger.Warn("Failed to decode telegram webhook update envelope", zap.Error(err), zap.String("topic", tup.event.ToString()))
|
||||
return err
|
||||
}
|
||||
if tup.handler == nil {
|
||||
tup.logger.Warn("Telegram update handler is not configured", zap.String("topic", tup.event.ToString()))
|
||||
return nil
|
||||
}
|
||||
return tup.handler(ctx, &msg)
|
||||
}
|
||||
|
||||
func (tup *TelegramUpdateProcessor) GetSubject() model.NotificationEvent {
|
||||
return tup.event
|
||||
}
|
||||
|
||||
func NewTelegramUpdateProcessor(logger mlogger.Logger, handler ch.TelegramUpdateHandler) np.EnvelopeProcessor {
|
||||
if logger != nil {
|
||||
logger = logger.Named("telegram_update_processor")
|
||||
}
|
||||
return &TelegramUpdateProcessor{
|
||||
logger: logger,
|
||||
handler: handler,
|
||||
event: telegramUpdateEvent(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user