Skip to content

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

Open
Adi1231234 wants to merge 2 commits intowwebjs:mainfrom
Adi1231234:fix/block-unblock-lid-resolution
Open

fix: use WA's own LID resolution for block/unblock#201671
Adi1231234 wants to merge 2 commits 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.

@github-actions github-actions bot added the utility Utility code label Mar 19, 2026
iscontactblocked on PN contacts returns false because the Blocklist
only contains LID entries. Use getAlternateUserWid() to resolve
the PN/LID mapping and check the Blocklist directly, matching WA
web's own $Contact$p_3 fallback logic.
@Adi1231234 Adi1231234 force-pushed the fix/block-unblock-lid-resolution branch from 0ef58b2 to 00ee4d9 Compare March 19, 2026 19:30
@Adi1231234
Copy link
Contributor Author

@BenyFilho Thanks for the approval!
@lindionez Thanks for reporting this! That bug was actually there before my changes but I went ahead and fixed it anyway. The blocklist stores LIDs so when you look up a phone number contact it never finds it. I'm now using getAlternateUserWid() to check the right ID against the blocklist, which is how WA handles it internally.
Would love to hear if it works on your end!

Copy link
Contributor

@lindionez lindionez left a comment

Choose a reason for hiding this comment

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

THANK YOU VERY MUCH, I had been without this function for about 2 weeks, it worked perfectly.

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 utility Utility code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants