Skip to content

fix: Tratar conversas @lid no inicio do recebimento dos eventos e novo erro da libsignal #1705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

KokeroO
Copy link
Contributor

@KokeroO KokeroO commented Jul 9, 2025

Mudanças

  • [Baileys] Trocar @LIDS em remoteJid por senderPn em todos os serviços;
  • [Baileys] Adicionar valor @lid recebido em remoteJid para previousRemoteJid (Posteriormente utilizasse em ChatwootService);
  • [Baileys] Adicionado um novo recebimento de erro da libsignal ao descarte de processamento de evento messages.upsert;
  • Minors fixes;

Summary by Sourcery

Handle @lid-specific remoteJid swapping across Baileys event handlers and Chatwoot integration, ignore a new libsignal error, update image processing to the Jimp v1 API, and bump Jimp and Sharp dependencies.

Bug Fixes:

  • Swap remoteJid to senderPn when incoming Baileys messages contain '@lid' and preserve the original value in previousRemoteJid
  • Add '@lid' handling to messages.update events in BaileysStartupService
  • Ignore the new 'Invalid PreKey ID' libsignal error in messages.upsert processing
  • Adjust createConversation logic in ChatwootService to use previousRemoteJid for '@lid' contacts
  • Fix image resizing in ChatwootService by updating to the Jimp v1 API method signatures

Enhancements:

  • Refine Baileys event handler signatures to use WAMessage and WAMessageKey types
  • Refactor 'messages.update' to accept structured key objects instead of WAMessageUpdate arrays

Chores:

  • Bump jimp to ^1.6.0 and sharp to ^0.34.2 in package.json

KokeroO added 2 commits July 9, 2025 18:35
 - [Baileys] Trocar @LIDS em remoteJid por senderPn em todos os serviços;
 - [Baileys] Adicionar valor @lid recebido em remoteJid para previousRemoteJid (Posteriormente utilizasse em ChatwootService);
 - Minors fixes;
Copy link
Contributor

sourcery-ai bot commented Jul 9, 2025

Reviewer's Guide

This PR centralizes handling of '@lid' identifiers by rewriting remoteJid to senderPn in Baileys event handlers (while preserving the original in previousRemoteJid), extends the decryption-error filter, tightens TypeScript types in messaging events, adjusts contact logic in ChatwootService using the new previousRemoteJid, updates Jimp image‐processing calls, and bumps several dependencies.

Sequence diagram for handling @lid in Baileys message events

sequenceDiagram
    participant BaileysService as BaileysStartupService
    participant ChatwootService
    participant Database

    BaileysService->>BaileysService: Receive message event (messages.upsert)
    alt remoteJid contains '@lid' and senderPn exists
        BaileysService->>BaileysService: Set previousRemoteJid = remoteJid
        BaileysService->>BaileysService: Set remoteJid = senderPn
    end
    BaileysService->>ChatwootService: Pass message with updated remoteJid and previousRemoteJid
    ChatwootService->>ChatwootService: Use previousRemoteJid to check for @lid
    ChatwootService->>Database: Update or create contact/conversation using senderPn
    Database-->>ChatwootService: Confirmation
    ChatwootService-->>BaileysService: Conversation/contact created/updated
Loading

Class diagram for updated message key handling

classDiagram
    class WAMessageKey {
      +string remoteJid
      +string senderPn
      +string previousRemoteJid
    }
    class WAMessage {
      +WAMessageKey key
      +number messageTimestamp
      +string[] messageStubParameters
    }
    WAMessage --> WAMessageKey
Loading

Class diagram for ChatwootService createConversation logic

classDiagram
    class ChatwootService {
      +createConversation(instance, body)
      +findContact(instance, id)
      +createContact(instance, chatId, isGroup, nameContact, pictureUrl, remoteJid)
    }
    class InstanceDto
    class Body {
      +WAMessageKey key
    }
    ChatwootService --> InstanceDto
    ChatwootService --> Body
    Body --> WAMessageKey
Loading

File-Level Changes

Change Details Files
Rewrite remoteJid when '@lid' is detected in Baileys handlers
  • Add checks in startup, messages.upsert and messages.update to replace key.remoteJid with key.senderPn
  • Store original remoteJid in previousRemoteJid before rewriting
  • Adjust event handler type signatures to use WAMessage and explicit update/key pairs
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Extend ignored libsignal errors
  • Include 'Invalid PreKey ID' in the list of stub-parameter errors to drop
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Enhance ChatwootService contact logic for '@lid' flows
  • Use previousRemoteJid to detect original '@lid' JID
  • Always derive remoteJid from the rewritten key
  • Streamline createContact invocation with the new remoteJid
src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts
Update Jimp API usage
  • Change img.cover call to use object signature {w,h}
  • Replace getBufferAsync with getBuffer and correct MIME constant
src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts
Bump image-processing dependencies
  • Upgrade jimp from ^0.16.13 to ^1.6.0
  • Upgrade sharp from ^0.32.6 to ^0.34.2
package.json
package-lock.json

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

@KokeroO
Copy link
Contributor Author

KokeroO commented Jul 9, 2025

@DavidsonGomes

  • Precisei atualizar as lib jimp e sharp porque estavam conflitando com as versões das mesmas da Baileys. Se mante-se ia precisar dar um force no npm. Então tive que ajustar em uma parte o código que usa resize para atender a nova versão.

  • A troca da definição de tipo proto.IWebMessageInfo para WAMessage foi necessária para atender o evento da Baileys, isso já é previsto na própria Baileys, já que wproto ainda não esta atualizado. (fix: add lid sutff to message.key type WhiskeySockets/Baileys#1586)

@Santosl2
Copy link

Perfeito!

@sourcery-ai sourcery-ai bot mentioned this pull request Jul 11, 2025
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.

2 participants