Skip to content

fix: use WA's own LID resolution for block/unblock#201671

Open
Adi1231234 wants to merge 1 commit intowwebjs:mainfrom
Adi1231234:fix/block-unblock-lid-resolution
Open

fix: use WA's own LID resolution for block/unblock#201671
Adi1231234 wants to merge 1 commit intowwebjs:mainfrom
Adi1231234:fix/block-unblock-lid-resolution

Conversation

@Adi1231234
Copy link
Contributor

Summary

block() and unblock() have two issues:

  • block() passes a Chat object to blockContact(), which expects a Contact model. It works by accident because both have .id, but it's the wrong type. The getChat() call also creates an unnecessary chat as a side effect via findOrCreateLatestChat.
  • unblock() uses the synchronous Contact.get() which returns undefined when the contact isn't in the local cache yet.

With blocklist migration enabled (isBlocklistMigrated() === true), blockContact() internally calls S() which needs either a LID or an existing chat with accountLid to resolve the target. The old getChat() workaround created a chat to satisfy this, but the proper way is to resolve the contact's LID directly.

This PR uses WAWebBlockContactUtils.getContactToBlockOnlyUseIfNoAssociatedChat(), which is WA's own utility for this exact scenario. It calls getCurrentLidContact() to convert PN contacts to their LID equivalent without creating unnecessary chats.

Tested in WhatsApp Web DevTools: block + unblock cycle works correctly for contacts with and without existing chats.

Test plan

  • Block a contact you have an existing chat with
  • Block a contact you have never interacted with before
  • Unblock both contacts
  • Verify no unnecessary chats are created

block() was passing a Chat object to blockContact() which expects a
contact. unblock() was using synchronous Contact.get() which returns
undefined if the contact isn't cached.

with blocklist migration enabled, blocking a PN contact without a chat
fails because the internal S() function needs a LID or an existing chat
to resolve the accountLid. The old getChat() approach worked around this
by creating a chat via findOrCreateLatestChat, but that's a side effect
and passes the wrong type.

use WAWebBlockContactUtils.getContactToBlockOnlyUseIfNoAssociatedChat()
instead, which is WA's own utility for this exact case. It resolves the
contact's LID via getCurrentLidContact() without needing to create a
chat.
@github-actions github-actions bot added the api changes API modifications label Mar 18, 2026
Adi1231234 added a commit to Adi1231234/whatsapp-web.js that referenced this pull request Mar 18, 2026
moved to a dedicated PR (wwebjs#201671) with a proper fix using
wawebblockcontactutils.getContactToBlockOnlyUseIfNoAssociatedChat()
for correct LID resolution.
Adi1231234 added a commit to Adi1231234/whatsapp-web.js that referenced this pull request Mar 18, 2026
moved to a dedicated PR (wwebjs#201671) with a proper fix using
wawebblockcontactutils.getContactToBlockOnlyUseIfNoAssociatedChat()
for correct LID resolution.
@BenyFilho BenyFilho added the approved Confirmed by maintainers label Mar 18, 2026
@lindionez
Copy link
Contributor

lindionez commented Mar 18, 2026

This resolved contact.unblock() for me, which hadn't been working for about 15 days. However, the contact still marks isBlocked: false even when the contact is blocked.

It was tested on 4 different numbers running the code.

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

Labels

api changes API modifications approved Confirmed by maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants