evolution-api icon indicating copy to clipboard operation
evolution-api copied to clipboard

feat(events): add isLatest and progress to messages.set event

Open alexandrereyes opened this issue 1 month ago • 1 comments

O evento messages.set não enviava as propriedades isLatest e progress do Baileys, perdendo informações importantes sobre a sincronização do histórico.

O comportamento ocorria porque o método sendDataWebhook enviava apenas o array de mensagens:

this.sendDataWebhook(Events.MESSAGES_SET, [...messagesRaw]);

Com a sincronização de histórico do WhatsApp, é importante saber quando o sync está completo (isLatest=true) e o progresso percentual (progress).

Ajuste realizado:

ANTES:

{
  "event": "messages.set",
  "instance": "minha-instancia",
  "data": [...]
}

DEPOIS:

{
  "event": "messages.set",
  "instance": "minha-instancia",
  "isLatest": true,
  "progress": 85,
  "data": [...]
}

Com essa alteração, consumidores do webhook podem identificar quando a sincronização está completa e acompanhar o progresso.

📋 Description

Add support for extra fields in event payload root, specifically isLatest and progress from Baileys' messaging-history.set event.

🔗 Related Issue

N/A

🧪 Type of Change

  • [ ] 🐛 Bug fix (non-breaking change which fixes an issue)
  • [x] ✨ New feature (non-breaking change which adds functionality)
  • [ ] 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [ ] 📚 Documentation update
  • [ ] 🔧 Refactoring (no functional changes)
  • [ ] ⚡ Performance improvement
  • [ ] 🧹 Code cleanup
  • [ ] 🔒 Security fix

🧪 Testing

  • [x] Manual testing completed
  • [x] Functionality verified in development environment
  • [x] No breaking changes introduced
  • [x] Tested with different connection types (if applicable)

📸 Screenshots (if applicable)

N/A

✅ Checklist

  • [x] My code follows the project's style guidelines
  • [x] I have performed a self-review of my code
  • [x] I have commented my code, particularly in hard-to-understand areas
  • [x] I have made corresponding changes to the documentation
  • [x] My changes generate no new warnings
  • [x] I have manually tested my changes thoroughly
  • [x] I have verified the changes work with different scenarios
  • [x] Any dependent changes have been merged and published

📝 Additional Notes

  • Added extra?: Record<string, any> field to EmitData type
  • Updated EventManager.emit() to accept extra parameters
  • Updated sendDataWebhook() to accept and pass extra fields
  • Updated all 7 event controllers to spread extra fields in payload (webhook, rabbitmq, sqs, websocket, pusher, kafka, nats)
  • Used (extra ?? {}) to handle undefined safely
  • Spread extra first to prevent overriding core fields like event, instance, data
  • Fully backward compatible - the extra parameter is optional

alexandrereyes avatar Nov 26 '25 18:11 alexandrereyes

Reviewer's Guide

Adds support for passing arbitrary extra metadata through the event pipeline so that Baileys messaging-history.set events can expose isLatest and progress on the messages.set payload across all delivery mechanisms (webhook, queues, websockets, etc.).

Sequence diagram for messages.set event with isLatest and progress propagation

sequenceDiagram
  participant BaileysStartupService
  participant ChannelStartupService
  participant EventManager
  participant WebhookController
  actor WebhookConsumer

  BaileysStartupService->>BaileysStartupService: receive messaging_history_set(messages, isLatest, progress)
  BaileysStartupService->>ChannelStartupService: sendDataWebhook(MESSAGES_SET, messagesRaw, true, undefined, {isLatest, progress})

  ChannelStartupService->>ChannelStartupService: build EmitData with extra = {isLatest, progress}
  ChannelStartupService->>EventManager: emit(eventData)

  EventManager->>WebhookController: emit(eventData)

  WebhookController->>WebhookController: build payload {
  WebhookController->>WebhookController:  event, instance, data, server_url, apikey, ...extra
  WebhookController->>WebhookConsumer: POST messages.set payload including isLatest and progress

  WebhookConsumer->>WebhookConsumer: process messages with history sync metadata

Class diagram for updated event pipeline with extra metadata support

classDiagram
  class EmitData {
    +string instanceName
    +string origin
    +string event
    +any data
    +string serverUrl
    +string dateTime
    +string sender
    +string apiKey
    +boolean local
    +string_array integration
    +Record_string_any extra
  }

  class EventManager {
    +emit(eventData: EmitData) Promise_void
  }

  class EventControllerInterface {
    <<interface>>
    +set(instanceName: string, data: any) Promise_any
    +get(instanceName: string) Promise_any
    +emit(emitData: EmitData) Promise_void
  }

  class EventController {
    +set(instanceName: string, data: any) Promise_any
    +get(instanceName: string) Promise_any
    +emit(emitData: EmitData) Promise_void
  }

  class WebhookController {
    +emit(emitData: EmitData) Promise_void
  }

  class RabbitmqController {
    +emit(emitData: EmitData) Promise_void
  }

  class SqsController {
    +emit(emitData: EmitData) Promise_void
  }

  class WebsocketController {
    +emit(emitData: EmitData) Promise_void
  }

  class PusherController {
    +emit(emitData: EmitData) Promise_void
  }

  class KafkaController {
    +emit(emitData: EmitData) Promise_void
  }

  class NatsController {
    +emit(emitData: EmitData) Promise_void
  }

  class ChannelStartupService {
    +sendDataWebhook(event: Events, data: object, local: boolean, integration: string_array, extra: Record_string_any) Promise_void
  }

  class BaileysStartupService {
    +sendDataWebhook(event: Events, data: object, local: boolean, integration: string_array, extra: Record_string_any) Promise_void
  }

  EventControllerInterface <|.. EventController
  EventController <|-- WebhookController
  EventController <|-- RabbitmqController
  EventController <|-- SqsController
  EventController <|-- WebsocketController
  EventController <|-- PusherController
  EventController <|-- KafkaController
  EventController <|-- NatsController

  EventManager --> WebhookController : uses
  EventManager --> RabbitmqController : uses
  EventManager --> SqsController : uses
  EventManager --> WebsocketController : uses
  EventManager --> PusherController : uses
  EventManager --> KafkaController : uses
  EventManager --> NatsController : uses

  ChannelStartupService --> EventManager : emit events
  BaileysStartupService --|> ChannelStartupService

  EmitData <.. EventManager : parameter
  EmitData <.. EventController : parameter
  EmitData <.. WebhookController : parameter
  EmitData <.. RabbitmqController : parameter
  EmitData <.. SqsController : parameter
  EmitData <.. WebsocketController : parameter
  EmitData <.. PusherController : parameter
  EmitData <.. KafkaController : parameter
  EmitData <.. NatsController : parameter

  note for WebhookController "In emit, builds payload with event, instance, data, and spreads extra into root"
  note for KafkaController "In emit, spreads extra into Kafka message payload"
  note for NatsController "In emit, spreads extra into NATS message payload"
  note for RabbitmqController "In emit, spreads extra into RabbitMQ message payload"
  note for SqsController "In emit, spreads extra into SQS message payload"
  note for WebsocketController "In emit, spreads extra into WebSocket message payload"
  note for PusherController "In emit, spreads extra into Pusher event payload"

File-Level Changes

Change Details Files
Extend event emission types and plumbing to carry optional extra fields through the system.
  • Add optional extra?: Record<string, any> to EmitData type and EventManager.emit signature.
  • Update EventControllerInterface.emit and all controller emit implementations to accept the new extra field.
  • Thread the extra field from ChannelStartupService.sendDataWebhook into EventManager.emit calls.
src/api/integrations/event/event.controller.ts
src/api/integrations/event/event.manager.ts
src/api/services/channel.service.ts
Propagate extra metadata onto outbound messages for all event transports.
  • In each transport controller, destructure extra from EmitData in emit and spread it onto the final payload object before publishing.
  • Ensure existing payload structure (event, instance, data, timestamps, apiKey, etc.) is preserved and only augmented.
src/api/integrations/event/kafka/kafka.controller.ts
src/api/integrations/event/nats/nats.controller.ts
src/api/integrations/event/pusher/pusher.controller.ts
src/api/integrations/event/rabbitmq/rabbitmq.controller.ts
src/api/integrations/event/sqs/sqs.controller.ts
src/api/integrations/event/webhook/webhook.controller.ts
src/api/integrations/event/websocket/websocket.controller.ts
Emit Baileys messages.set events with isLatest and progress metadata.
  • Update BaileysStartupService to call sendDataWebhook for Events.MESSAGES_SET with an extra object containing isLatest and progress.
  • Leave the messages array unchanged while augmenting the root payload only when these extra fields are provided.
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an issue from a review comment by replying to it. You can also reply to a review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull request title to generate a title at any time. You can also comment @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in the pull request body to generate a PR summary at any time exactly where you want it. You can also comment @sourcery-ai summary on the pull request to (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the pull request to resolve all Sourcery comments. Useful if you've already addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull request to dismiss all existing Sourcery reviews. Especially useful if you want to start fresh with a new review - don't forget to comment @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

  • Contact our support team for questions or feedback.
  • Visit our documentation for detailed guides and information.
  • Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.

sourcery-ai[bot] avatar Nov 26 '25 18:11 sourcery-ai[bot]

Please, fix the lint with npm run lint

DavidsonGomes avatar Dec 05 '25 13:12 DavidsonGomes