Skip to content

fix(baileys): unwrap ephemeralMessage so disappearing messages are not dropped (#2773) - #2817

Open
Faelst wants to merge 1 commit into
evolution-foundation:developfrom
Faelst:fix/unwrap-disappearing-messages
Open

Faelst wants to merge 1 commit into
evolution-foundation:developfrom
Faelst:fix/unwrap-disappearing-messages

Conversation

@Faelst

@Faelst Faelst commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

📋 Description

When a contact has disappearing messages enabled, WhatsApp delivers the inbound payload wrapped in an ephemeralMessage envelope. In prepareMessage(), messageType is derived from getContentType(message.message), which returns ephemeralMessage, so the real content (text, media, etc.) stays buried one level down. Downstream consumers don't recognise that type, so the message is never stored or forwarded — effectively dropped, which is what #2773 reports.

This unwraps the ephemeralMessage envelope before deriving the type, payload and contextInfo, mirroring how prepareMessage() already flattens extendedTextMessage and documentWithCaptionMessage. Non-ephemeral messages are left untouched by the ?? message.message fallback, so there is no behaviour change for regular messages.

const content = message.message?.ephemeralMessage?.message ?? message.message;
// ...
message: this.deserializeMessageBuffers({ ...content }),
messageType: getContentType(content),
// ...
contextInfo: this.deserializeMessageBuffers(content?.messageContextInfo),

🔗 Related Issue

Closes #2773

🧪 Type of Change

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

🧪 Testing

  • No breaking changes introduced
  • Functionality verified in development environment

Validated on a clean checkout of develop:

  • npm run build (tsc --noEmit + tsup) passes.
  • eslint reports no issues on the changed file.
  • Root cause confirmed by inspection: for an ephemeralMessage payload getContentType(message.message) returns ephemeralMessage, so the existing code never reaches the inner content. The one-level unwrap resolves the actual inner type; regular messages are unchanged because content === message.message when there is no envelope.

✅ Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings

Summary by Sourcery

Bug Fixes:

  • Process incoming WhatsApp messages wrapped in ephemeralMessage envelopes so disappearing messages are no longer dropped.

…t dropped

When a contact has disappearing messages enabled, WhatsApp delivers the
payload wrapped in an `ephemeralMessage` envelope. `prepareMessage` derived
`messageType` from `getContentType(message.message)`, which returns
`ephemeralMessage`, leaving the real content (text, media, etc.) buried.
Downstream consumers don't recognise that type, so the message is never
stored or forwarded and the event is effectively dropped.

Unwrap the envelope before deriving the type, payload and contextInfo,
mirroring how `prepareMessage` already flattens `extendedTextMessage` and
`documentWithCaptionMessage`. Non-ephemeral messages are unaffected thanks
to the `?? message.message` fallback.

Fixes evolution-foundation#2773

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@sourcery-ai

sourcery-ai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Implementation

Unwraps ephemeral message envelopes before deriving the payload, message type, and context info so disappearing messages reach downstream consumers.

  • src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts L5158-5167, L5179-5180, L5187

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 3 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="5166" />
<code_context>
+    // event is effectively dropped (issue #2773). Unwrap the envelope so the
+    // message is processed as its inner type. Non-ephemeral messages are left
+    // untouched by the `?? message.message` fallback.
+    const content = message.message?.ephemeralMessage?.message ?? message.message;
+
     const messageRaw: any = {
</code_context>
<issue_to_address>
**Ephemeral media processing is skipped**

When an ephemeral message contains media and the corresponding media handling is enabled, `prepareMessage` unwraps the content for `messageRaw`, but `messages.upsert` still checks the wrapped `received.message`. Media detection therefore fails, skipping S3 and local-webhook base64 processing, media-record creation, and audio transcription; callers receive messages without the configured media data or transcript.

Use the unwrapped content consistently in `messages.upsert` for media detection and downstream media handling.

Also at `src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:5180`.
</issue_to_address>

### Comment 2
<location path="src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts" line_range="5187" />
<code_context>
       instanceId: this.instanceId,
       status: status[message.status],
-      contextInfo: this.deserializeMessageBuffers(message.message?.messageContextInfo),
+      contextInfo: this.deserializeMessageBuffers(content?.messageContextInfo),
     };

</code_context>
<issue_to_address>
**Poll votes cannot be decrypted**

When an ephemeral poll carries `messageContextInfo` on the outer envelope rather than inside the wrapped message, `prepareMessage` reads `messageContextInfo` only from the inner content and drops the outer `messageSecret`. `getMessage` and `baileysDecryptPollVote` cannot retrieve the stored secret, so votes on these polls cannot be decrypted.

When storing `contextInfo`, fall back to the outer envelope's `messageContextInfo` if the inner content does not provide it.
</issue_to_address>

### Comment 3
<location path="src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts" line_range="5166" />
<code_context>
+    // event is effectively dropped (issue #2773). Unwrap the envelope so the
+    // message is processed as its inner type. Non-ephemeral messages are left
+    // untouched by the `?? message.message` fallback.
+    const content = message.message?.ephemeralMessage?.message ?? message.message;
+
     const messageRaw: any = {
</code_context>
<issue_to_address>
**Disappearing edits leave originals stale**

When an ephemeral envelope contains an edit protocol message, `messages.upsert` checks only `received.message` for an edit protocol, so a wrapped edit is treated as an ordinary message instead. The existing message is never updated and the edit event is handled as a new message, leaving stored content stale.

Use the unwrapped content when detecting and handling edit protocol messages in `messages.upsert`.
</issue_to_address>

Sourcery assessment

Approval pending. 3 findings to address first.

Blocking findings: src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:5166, src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:5187, src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:5166


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

// event is effectively dropped (issue #2773). Unwrap the envelope so the
// message is processed as its inner type. Non-ephemeral messages are left
// untouched by the `?? message.message` fallback.
const content = message.message?.ephemeralMessage?.message ?? message.message;

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 · Ephemeral media processing is skipped

When an ephemeral message contains media and the corresponding media handling is enabled, prepareMessage unwraps the content for messageRaw, but messages.upsert still checks the wrapped received.message. Media detection therefore fails, skipping S3 and local-webhook base64 processing, media-record creation, and audio transcription; callers receive messages without the configured media data or transcript.

Use the unwrapped content consistently in messages.upsert for media detection and downstream media handling.

Also at src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:5180.

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

**Ephemeral media processing is skipped**

When an ephemeral message contains media and the corresponding media handling is enabled, `prepareMessage` unwraps the content for `messageRaw`, but `messages.upsert` still checks the wrapped `received.message`. Media detection therefore fails, skipping S3 and local-webhook base64 processing, media-record creation, and audio transcription; callers receive messages without the configured media data or transcript.

Use the unwrapped content consistently in `messages.upsert` for media detection and downstream media handling.

Also at `src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:5180`.

instanceId: this.instanceId,
status: status[message.status],
contextInfo: this.deserializeMessageBuffers(message.message?.messageContextInfo),
contextInfo: this.deserializeMessageBuffers(content?.messageContextInfo),

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 · Poll votes cannot be decrypted

When an ephemeral poll carries messageContextInfo on the outer envelope rather than inside the wrapped message, prepareMessage reads messageContextInfo only from the inner content and drops the outer messageSecret. getMessage and baileysDecryptPollVote cannot retrieve the stored secret, so votes on these polls cannot be decrypted.

When storing contextInfo, fall back to the outer envelope's messageContextInfo if the inner content does not provide it.

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

**Poll votes cannot be decrypted**

When an ephemeral poll carries `messageContextInfo` on the outer envelope rather than inside the wrapped message, `prepareMessage` reads `messageContextInfo` only from the inner content and drops the outer `messageSecret`. `getMessage` and `baileysDecryptPollVote` cannot retrieve the stored secret, so votes on these polls cannot be decrypted.

When storing `contextInfo`, fall back to the outer envelope's `messageContextInfo` if the inner content does not provide it.

// event is effectively dropped (issue #2773). Unwrap the envelope so the
// message is processed as its inner type. Non-ephemeral messages are left
// untouched by the `?? message.message` fallback.
const content = message.message?.ephemeralMessage?.message ?? message.message;

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 · Disappearing edits leave originals stale

When an ephemeral envelope contains an edit protocol message, messages.upsert checks only received.message for an edit protocol, so a wrapped edit is treated as an ordinary message instead. The existing message is never updated and the edit event is handled as a new message, leaving stored content stale.

Use the unwrapped content when detecting and handling edit protocol messages in messages.upsert.

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

**Disappearing edits leave originals stale**

When an ephemeral envelope contains an edit protocol message, `messages.upsert` checks only `received.message` for an edit protocol, so a wrapped edit is treated as an ordinary message instead. The existing message is never updated and the edit event is handled as a new message, leaving stored content stale.

Use the unwrapped content when detecting and handling edit protocol messages in `messages.upsert`.

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