Visitar URL original
fix(baileys): forward chat state in the CHATS_UPDATE webhook by lschiaffino · Pull Request #2791 · evolution-foundation/evolution-api · GitHub
Skip to content

fix(baileys): forward chat state in the CHATS_UPDATE webhook - #2791

Open
lschiaffino wants to merge 1 commit into
evolution-foundation:developfrom
lschiaffino:fix/chats-update-forward-state
Open

lschiaffino wants to merge 1 commit into
evolution-foundation:developfrom
lschiaffino:fix/chats-update-forward-state

Conversation

@lschiaffino

@lschiaffino lschiaffino commented Nov 14, 2026 •

Copy link
Copy Markdown

📋 Description

The chats.update handler in whatsapp.baileys.service.ts sends the webhook with only { remoteJid, instanceId }. Baileys emits chats.update when a chat is archived, unarchived, pinned or read from the phone (app-state sync), but that state is dropped, so webhook consumers can't mirror it. This forwards archived, pinned and unreadCount when Baileys includes them (spread only when present, so existing consumers that read remoteJid are unaffected).

🧪 Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)

🧪 Testing

  • Not yet verified on a live instance. We're deploying this same change as a patch on 2.3.7 for our own lines and will report back here with the observed payloads.

✅ Checklist

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas

📝 Additional Notes

Prepared with AI assistance (Claude) and reviewed by me.

Summary by Sourcery

Bug Fixes:

  • Preserve archived, pinned, and unread-count chat state in Baileys CHATS_UPDATE webhook payloads when provided.

The chats.update handler sent only { remoteJid, instanceId }, so webhook
consumers never learned that a chat was archived, pinned or read from the
phone. Forward archived, pinned and unreadCount when Baileys reports them.
@sourcery-ai

sourcery-ai Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

API

Forward Baileys chat-state fields in the CHATS_UPDATE webhook so consumers can mirror archive, pin, and read changes.

  • src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts L900-909

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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts" line_range="906-908" />
<code_context>
+        return {
+          remoteJid: chat.id,
+          instanceId: this.instanceId,
+          ...(typeof chat.archived === 'boolean' ? { archived: chat.archived } : {}),
+          ...(chat.pinned !== undefined && chat.pinned !== null ? { pinned: Number(chat.pinned) } : {}),
+          ...(typeof chat.unreadCount === 'number' ? { unreadCount: chat.unreadCount } : {}),
+        };
       });
</code_context>
<issue_to_address>
**Older chat state overwrites newer state**

When two updates change the same chat field before the earlier webhook delivery completes, `chatHandle['chats.update']` starts each `sendDataWebhook` without awaiting it, so consecutive updates can be delivered out of order; a webhook consumer applying the included state fields then ends with the older value.

Serialize webhook emissions per instance or chat so later state updates cannot overtake earlier ones.
</issue_to_address>

### Comment 2
<location path="src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts" line_range="907" />
<code_context>
+          remoteJid: chat.id,
+          instanceId: this.instanceId,
+          ...(typeof chat.archived === 'boolean' ? { archived: chat.archived } : {}),
+          ...(chat.pinned !== undefined && chat.pinned !== null ? { pinned: Number(chat.pinned) } : {}),
+          ...(typeof chat.unreadCount === 'number' ? { unreadCount: chat.unreadCount } : {}),
+        };
</code_context>
<issue_to_address>
**Pinned state arrives as a number**

When baileys emits `pinned` as a boolean, `Number(chat.pinned)` converts Baileys’ boolean pin flag to `0` or `1`, so consumers expecting a boolean receive the wrong wire type and cannot reliably mirror pin state.

Forward `chat.pinned` without converting it to a number.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 2 findings to address first, and if the forwarded chat state is wrong or should not be shared, the webhook sends that information to consumers and the delivery cannot be recalled. Reverting stops future payloads but does not undo values already received.

Blocking findings: src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:908, src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:907


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment on lines +906 to +908
...(typeof chat.archived === 'boolean' ? { archived: chat.archived } : {}),
...(chat.pinned !== undefined && chat.pinned !== null ? { pinned: Number(chat.pinned) } : {}),
...(typeof chat.unreadCount === 'number' ? { unreadCount: chat.unreadCount } : {}),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Older chat state overwrites newer state

When two updates change the same chat field before the earlier webhook delivery completes, chatHandle['chats.update'] starts each sendDataWebhook without awaiting it, so consecutive updates can be delivered out of order; a webhook consumer applying the included state fields then ends with the older value.

Serialize webhook emissions per instance or chat so later state updates cannot overtake earlier ones.

Prompt for AI agents
In `src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts` at lines 906-908:

**Older chat state overwrites newer state**

When two updates change the same chat field before the earlier webhook delivery completes, `chatHandle['chats.update']` starts each `sendDataWebhook` without awaiting it, so consecutive updates can be delivered out of order; a webhook consumer applying the included state fields then ends with the older value.

Serialize webhook emissions per instance or chat so later state updates cannot overtake earlier ones.

remoteJid: chat.id,
instanceId: this.instanceId,
...(typeof chat.archived === 'boolean' ? { archived: chat.archived } : {}),
...(chat.pinned !== undefined && chat.pinned !== null ? { pinned: Number(chat.pinned) } : {}),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Pinned state arrives as a number

When baileys emits pinned as a boolean, Number(chat.pinned) converts Baileys’ boolean pin flag to 0 or 1, so consumers expecting a boolean receive the wrong wire type and cannot reliably mirror pin state.

Forward chat.pinned without converting it to a number.

Prompt for AI agents
In `src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts` at line 907:

**Pinned state arrives as a number**

When baileys emits `pinned` as a boolean, `Number(chat.pinned)` converts Baileys’ boolean pin flag to `0` or `1`, so consumers expecting a boolean receive the wrong wire type and cannot reliably mirror pin state.

Forward `chat.pinned` without converting it to a number.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant