fix: prevent store model mutation in getContact and getChatModel LID handling (#127098)

* fix: prevent store mutation in getContact/getChatModel LID handling

getcontact() and getChatModel() overwrite .id directly on live Backbone
model references from WhatsApp Web's Contact/Participant store. This
permanently corrupts the store for the lifetime of the session, causing
all subsequent lookups for the same contact to crash with:
- "Cannot read properties of undefined (reading '_serialized')"
- "Data passed to getter must include an id property"

the fix resolves LID to phone on the serialized copy only, never
touching the live store model.

fixes #127054

* perf: use BusinessProfile.find() instead of fetchBizProfile()

businessprofile.find() checks the local cache first and only fetches
from the server when the profile is missing, while fetchBizProfile()
always makes a network call (~85-100ms per contact). This significantly
reduces latency when getContact is called repeatedly for the same
business contacts.

closes #201656

co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: remove unnecessary try/catch around BusinessProfile.find()

businessprofile.find() never throws for non-business contacts - it returns
a BusinessProfile model with profileOptions: null, not an error. The comment
was factually incorrect. The existing profileOptions guard handles the
non-business case correctly without any exception handling.

verified in WhatsApp Web DevTools: find() returns a valid object for regular,
business, LID, and even non-existent contacts. Only throws when called without
an id, which cannot happen here.

co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* refactor: simplify contact/chat model utils via native WA functions

replace the O(n²) LID participant loop with WAWebLidMigrationUtils.toPn()
which resolves LID WIDs via LidPnCache in O(1). Remove the findContact
wrapper since Contact.find() accepts strings directly. Use
wawebwidfactory.createWidFromWidLike() for safe isLid() calls that handle
plain-object contact ids without WID prototype methods.

co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* perf: skip BusinessProfile.find() for non-business contacts

wa's own source guards this behind isBusiness || isEnterprise
(WAWebUseBusinessProfile.react). No point querying business profiles
for regular contacts - saves a wasted IQ roundtrip (~80ms) per call.

* revert: remove block/unblock changes from this PR

moved to a dedicated PR (#201671) with a proper fix using
wawebblockcontactutils.getContactToBlockOnlyUseIfNoAssociatedChat()
for correct LID resolution.

* fix: convert contactId to Wid before collection find() calls

contact.find() and BusinessProfile.find() reject plain strings when
the entry is not cached or stale, throwing "gadd called without an id
attr". Use createWid(contactId) to ensure a proper Wid object is passed.

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Adi1231234 2026-03-24 15:00:11 +02:00 • committed by GitHub
commit 6de0b0dd2b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -286,9 +286,9 @@ exports.LoadUtils = () => {
let vcardOptions = {};
if (options.contactCard) {
let contact = window
let contact = await window
.require('WAWebCollections')
.Contact.get(options.contactCard);
.Contact.find(options.contactCard);
vcardOptions = {
body: window
.require('WAWebFrontendVcardUtils')
@ -298,8 +298,10 @@ exports.LoadUtils = () => {
};
delete options.contactCard;
} else if (options.contactCardList) {
let contacts = options.contactCardList.map((c) =>
window.require('WAWebCollections').Contact.get(c),
let contacts = await Promise.all(
options.contactCardList.map((c) =>
window.require('WAWebCollections').Contact.find(c),
),
);
let vcards = contacts.map((c) =>
window
@ -956,14 +958,12 @@ exports.LoadUtils = () => {
window.require('WAWebCollections').GroupMetadata ||
window.require('WAWebCollections').WAWebGroupMetadataCollection;
await groupMetadata.update(chatWid);
chat.groupMetadata.participants._models
.filter((x) => x.id?._serialized?.endsWith('@lid'))
.forEach(
(x) =>
x.contact?.phoneNumber &&
(x.id = x.contact.phoneNumber),
);
model.groupMetadata = chat.groupMetadata.serialize();
const { toPn } = window.require('WAWebLidMigrationUtils');
const serializedMetadata = chat.groupMetadata.serialize();
for (const p of serializedMetadata.participants || []) {
p.id = toPn(p.id) ?? p.id;
}
model.groupMetadata = serializedMetadata;
model.isReadOnly = chat.groupMetadata.announce;
}
@ -1007,6 +1007,14 @@ exports.LoadUtils = () => {
window.WWebJS.getContactModel = (contact) => {
let res = contact.serialize();
const wid = window
.require('WAWebWidFactory')
.createWidFromWidLike(contact.id);
if (wid.isLid() && contact.phoneNumber) {
res.id = contact.phoneNumber;
}
res.isBusiness =
contact.isBusiness === undefined ? false : contact.isBusiness;
@ -1039,17 +1047,18 @@ exports.LoadUtils = () => {
};
window.WWebJS.getContact = async (contactId) => {
const wid = window.require('WAWebWidFactory').createWid(contactId);
let contact = await window
const contactWid = window
.require('WAWebWidFactory')
.createWid(contactId);
const contact = await window
.require('WAWebCollections')
.Contact.find(wid);
if (contact.id._serialized.endsWith('@lid')) {
contact.id = contact.phoneNumber;
.Contact.find(contactWid);
if (contact.isBusiness || contact.isEnterprise) {
const bizProfile = await window
.require('WAWebCollections')
.BusinessProfile.find(contactWid);
bizProfile.profileOptions && (contact.businessProfile = bizProfile);
}
const bizProfile = await window
.require('WAWebCollections')
.BusinessProfile.fetchBizProfile(wid);
bizProfile.profileOptions && (contact.businessProfile = bizProfile);
return window.WWebJS.getContactModel(contact);
};