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