Skip to content

Corrigindo um bug ao atualizar o push name no evento MESSAGES_UPSERT e MESSAGES_UPDATE #1366

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

Merged
merged 2 commits into from
Apr 14, 2025

Conversation

pedro-php
Copy link
Contributor

@pedro-php pedro-php commented Apr 9, 2025

Fix

Problema

Ao enviar uma mensagem via dispositivo, dois comportamentos distintos ocorriam:

  1. Inicialmente, o evento MESSAGES_UPSERT atualizava o campo name da tabela Chat com o pushname do remetente.
  2. Em seguida, o evento MESSAGES_UPDATE sobrescrevia o campo name com uma string vazia de forma consistente, pois tentava fazer um update utilizando uma variável que sequer era definida — já que esse campo não era enviado pelo respectivo evento da Baileys.

O que foi alterado?

  • O campo name da tabela Chat não é mais atualizado durante o evento MESSAGES_UPDATE. Isto ocorre pois o campo name sequer é enviado no evento, causando ele sempre ser atualizado como uma string vazia.
  • No evento MESSAGES_UPSERT, o campo name só é atualizado quando fromMe for falso, ou seja, apenas quando a mensagem for recebida (não enviada pelo próprio usuário).

Resolução

Agora, ao chamar um endpoint que retorna o campo name da tabela Chat (como o /chat/findChats/), o valor retornado será o correto — evitando strings vazias ou o nome do próprio remetente. O campo continua sendo atualizado normalmente ao receber mensagens.

Summary by Sourcery

Fix the behavior of updating chat names during message events to prevent incorrect name updates

Bug Fixes:

  • Prevent overwriting chat names with empty strings during MESSAGES_UPDATE event
  • Ensure chat names are only updated with push names for received messages

Enhancements:

  • Improve chat name update logic to maintain accurate contact names

…der to return always the chatname from the user correctly
Copy link
Contributor

sourcery-ai bot commented Apr 9, 2025

Reviewer's Guide by Sourcery

This pull request fixes a bug where the name field in the Chat table was being incorrectly updated with an empty string or the sender's name. The fix ensures that the name field is only updated with the correct push name when a message is received, not when it's sent.

Sequence diagram for MESSAGES_UPSERT event

sequenceDiagram
  participant WhatsApp
  participant BaileysService
  participant PrismaRepository
  participant WebhookService

  WhatsApp->>BaileysService: Event: MESSAGES_UPSERT (message received)
  BaileysService->>BaileysService: Check if chat exists
  alt Chat exists and pushName is valid and fromMe is false and not a group
    BaileysService->>PrismaRepository: Update Chat.name with pushName
    PrismaRepository-->>BaileysService: Success
    BaileysService->>WebhookService: Send CHATS_UPSERT event with updated Chat
    WebhookService-->>BaileysService: Acknowledged
  else Chat does not exist or pushName is invalid or fromMe is true or is a group
    BaileysService->>BaileysService: Do not update Chat.name
  end
Loading

Updated class diagram for Chat

classDiagram
  class Chat {
    remoteJid: string
    instanceId: string
    unreadMessages: number
    // name: string - Removed from initial creation
  }
Loading

File-Level Changes

Change Details Files
Stopped updating the name field of the Chat table during the MESSAGES_UPDATE event.
  • Removed the logic that updated the name field during the MESSAGES_UPDATE event.
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Modified the MESSAGES_UPSERT event to update the name field in the Chat table only when the message is received (i.e., fromMe is false).
  • Added a condition to check if fromMe is false before updating the name field.
  • Added a condition to prevent updating group names.
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Removed the initial name value assignment when inserting a new chat.
  • Removed the `name: message.pushName

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!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

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

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @pedro-php - I've reviewed your changes - here's some feedback:

Overall Comments:

  • The changes look good, but it would be helpful to add a comment explaining why the name field is no longer updated during the MESSAGES_UPDATE event.
  • Consider adding a test case to specifically verify that the chat name is updated correctly when a new message is received from a contact.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@rafwell
Copy link
Contributor

rafwell commented Apr 9, 2025

Talvez vc consiga resolver este bug também: #1367

chegou a ocorrer com vc?

@DavidsonGomes DavidsonGomes merged commit 2ded197 into EvolutionAPI:develop Apr 14, 2025
1 check passed
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.

3 participants