fix: use WA's own LID resolution for block/unblock#201671
fix: use WA's own LID resolution for block/unblock#201671Adi1231234 wants to merge 2 commits intowwebjs:mainfrom
Conversation
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.
moved to a dedicated PR (wwebjs#201671) with a proper fix using wawebblockcontactutils.getContactToBlockOnlyUseIfNoAssociatedChat() for correct LID resolution.
moved to a dedicated PR (wwebjs#201671) with a proper fix using wawebblockcontactutils.getContactToBlockOnlyUseIfNoAssociatedChat() for correct LID resolution.
|
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. |
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.
0ef58b2 to
00ee4d9
Compare
|
@BenyFilho Thanks for the approval! |
lindionez
left a comment
There was a problem hiding this comment.
THANK YOU VERY MUCH, I had been without this function for about 2 weeks, it worked perfectly.
Summary
block() and unblock() have two issues:
blockContact(), which expects a Contact model. It works by accident because both have.id, but it's the wrong type. ThegetChat()call also creates an unnecessary chat as a side effect viafindOrCreateLatestChat.Contact.get()which returnsundefinedwhen the contact isn't in the local cache yet.With blocklist migration enabled (
isBlocklistMigrated() === true),blockContact()internally callsS()which needs either a LID or an existing chat withaccountLidto resolve the target. The oldgetChat()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 callsgetCurrentLidContact()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