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(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,10 @@ func ConfirmationResult(sender string, result *model.ConfirmationResult, sourceS
|
||||
return cinternal.NewConfirmationResultEnvelope(sender, result, sourceService, rail)
|
||||
}
|
||||
|
||||
func ConfirmationDispatch(sender string, dispatch *model.ConfirmationRequestDispatch, sourceService, rail string) messaging.Envelope {
|
||||
return cinternal.NewConfirmationDispatchEnvelope(sender, dispatch, sourceService, rail)
|
||||
}
|
||||
|
||||
func NewConfirmationRequestProcessor(logger mlogger.Logger, handler ch.ConfirmationRequestHandler) np.EnvelopeProcessor {
|
||||
return cinternal.NewConfirmationRequestProcessor(logger, handler)
|
||||
}
|
||||
@@ -24,3 +28,7 @@ func NewConfirmationRequestProcessor(logger mlogger.Logger, handler ch.Confirmat
|
||||
func NewConfirmationResultProcessor(logger mlogger.Logger, sourceService, rail string, handler ch.ConfirmationResultHandler) np.EnvelopeProcessor {
|
||||
return cinternal.NewConfirmationResultProcessor(logger, sourceService, rail, handler)
|
||||
}
|
||||
|
||||
func NewConfirmationDispatchProcessor(logger mlogger.Logger, sourceService, rail string, handler ch.ConfirmationDispatchHandler) np.EnvelopeProcessor {
|
||||
return cinternal.NewConfirmationDispatchProcessor(logger, sourceService, rail, handler)
|
||||
}
|
||||
|
||||
@@ -9,3 +9,5 @@ import (
|
||||
type ConfirmationRequestHandler = func(context.Context, *model.ConfirmationRequest) error
|
||||
|
||||
type ConfirmationResultHandler = func(context.Context, *model.ConfirmationResult) error
|
||||
|
||||
type ConfirmationDispatchHandler = func(context.Context, *model.ConfirmationRequestDispatch) error
|
||||
|
||||
@@ -7,3 +7,7 @@ import (
|
||||
)
|
||||
|
||||
type TelegramReactionHandler = func(context.Context, *model.TelegramReactionRequest) error
|
||||
|
||||
type TelegramTextHandler = func(context.Context, *model.TelegramTextRequest) error
|
||||
|
||||
type TelegramUpdateHandler = func(context.Context, *model.TelegramWebhookUpdate) error
|
||||
|
||||
@@ -13,6 +13,22 @@ func TelegramReaction(sender string, request *model.TelegramReactionRequest) mes
|
||||
return tinternal.NewTelegramReactionEnvelope(sender, request)
|
||||
}
|
||||
|
||||
func TelegramText(sender string, request *model.TelegramTextRequest) messaging.Envelope {
|
||||
return tinternal.NewTelegramTextEnvelope(sender, request)
|
||||
}
|
||||
|
||||
func TelegramUpdate(sender string, update *model.TelegramWebhookUpdate) messaging.Envelope {
|
||||
return tinternal.NewTelegramUpdateEnvelope(sender, update)
|
||||
}
|
||||
|
||||
func NewTelegramReactionProcessor(logger mlogger.Logger, handler ch.TelegramReactionHandler) np.EnvelopeProcessor {
|
||||
return tinternal.NewTelegramReactionProcessor(logger, handler)
|
||||
}
|
||||
|
||||
func NewTelegramTextProcessor(logger mlogger.Logger, handler ch.TelegramTextHandler) np.EnvelopeProcessor {
|
||||
return tinternal.NewTelegramTextProcessor(logger, handler)
|
||||
}
|
||||
|
||||
func NewTelegramUpdateProcessor(logger mlogger.Logger, handler ch.TelegramUpdateHandler) np.EnvelopeProcessor {
|
||||
return tinternal.NewTelegramUpdateProcessor(logger, handler)
|
||||
}
|
||||
|
||||
@@ -33,3 +33,13 @@ type ConfirmationResult struct {
|
||||
Status ConfirmationStatus `bson:"status,omitempty" json:"status,omitempty"`
|
||||
ParseError string `bson:"parseError,omitempty" json:"parse_error,omitempty"`
|
||||
}
|
||||
|
||||
// ConfirmationRequestDispatch is emitted by the notification service after it sends
|
||||
// a confirmation prompt message to Telegram.
|
||||
type ConfirmationRequestDispatch struct {
|
||||
RequestID string `bson:"requestId,omitempty" json:"request_id,omitempty"`
|
||||
ChatID string `bson:"chatId,omitempty" json:"chat_id,omitempty"`
|
||||
MessageID string `bson:"messageId,omitempty" json:"message_id,omitempty"`
|
||||
Rail string `bson:"rail,omitempty" json:"rail,omitempty"`
|
||||
SourceService string `bson:"sourceService,omitempty" json:"source_service,omitempty"`
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ func FromStringImp(s string) (*NotificationEventImp, error) {
|
||||
|
||||
func StringToNotificationAction(s string) (nm.NotificationAction, error) {
|
||||
switch nm.NotificationAction(s) {
|
||||
case nm.NACreated, nm.NAPending, nm.NAUpdated, nm.NAArchived, nm.NADeleted, nm.NAAssigned, nm.NAPasswordReset, nm.NAConfirmationRequest, nm.NATelegramReaction, nm.NAPaymentGatewayIntent, nm.NAPaymentGatewayExecution, nm.NADiscoveryServiceAnnounce, nm.NADiscoveryGatewayAnnounce, nm.NADiscoveryHeartbeat, nm.NADiscoveryLookupRequest, nm.NADiscoveryLookupResponse, nm.NADiscoveryRefreshUI:
|
||||
case nm.NACreated, nm.NAPending, nm.NAUpdated, nm.NAArchived, nm.NADeleted, nm.NAAssigned, nm.NAPasswordReset, nm.NAConfirmationRequest, nm.NATelegramReaction, nm.NATelegramText, nm.NATelegramUpdate, nm.NAPaymentGatewayIntent, nm.NAPaymentGatewayExecution, nm.NADiscoveryServiceAnnounce, nm.NADiscoveryGatewayAnnounce, nm.NADiscoveryHeartbeat, nm.NADiscoveryLookupRequest, nm.NADiscoveryLookupResponse, nm.NADiscoveryRefreshUI:
|
||||
return nm.NotificationAction(s), nil
|
||||
default:
|
||||
return "", merrors.DataConflict("invalid Notification action: " + s)
|
||||
|
||||
@@ -14,6 +14,8 @@ const (
|
||||
|
||||
NAConfirmationRequest NotificationAction = "confirmation.request"
|
||||
NATelegramReaction NotificationAction = "telegram.reaction"
|
||||
NATelegramText NotificationAction = "telegram.text"
|
||||
NATelegramUpdate NotificationAction = "telegram.update"
|
||||
NAPaymentGatewayIntent NotificationAction = "intent.request"
|
||||
NAPaymentGatewayExecution NotificationAction = "execution.result"
|
||||
|
||||
|
||||
@@ -85,6 +85,8 @@ func StringToNotificationAction(s string) (nm.NotificationAction, error) {
|
||||
nm.NAPasswordReset,
|
||||
nm.NAConfirmationRequest,
|
||||
nm.NATelegramReaction,
|
||||
nm.NATelegramText,
|
||||
nm.NATelegramUpdate,
|
||||
nm.NAPaymentGatewayIntent,
|
||||
nm.NAPaymentGatewayExecution,
|
||||
nm.NADiscoveryServiceAnnounce,
|
||||
|
||||
@@ -16,3 +16,15 @@ type TelegramReactionRequest struct {
|
||||
MessageID string `bson:"messageId,omitempty" json:"message_id,omitempty"`
|
||||
Emoji string `bson:"emoji,omitempty" json:"emoji,omitempty"`
|
||||
}
|
||||
|
||||
type TelegramTextRequest struct {
|
||||
RequestID string `bson:"requestId,omitempty" json:"request_id,omitempty"`
|
||||
ChatID string `bson:"chatId,omitempty" json:"chat_id,omitempty"`
|
||||
Text string `bson:"text,omitempty" json:"text,omitempty"`
|
||||
ReplyToMessageID string `bson:"replyToMessageId,omitempty" json:"reply_to_message_id,omitempty"`
|
||||
}
|
||||
|
||||
type TelegramWebhookUpdate struct {
|
||||
UpdateID int64 `bson:"updateId,omitempty" json:"update_id,omitempty"`
|
||||
Message *TelegramMessage `bson:"message,omitempty" json:"message,omitempty"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user