From d07771631516d1faeb629d43967fa212d63fd3a3 Mon Sep 17 00:00:00 2001 From: alechkos <93551621+alechkos@users.noreply.github.com> Date: Sat, 6 Sep 2025 18:24:19 +0300 Subject: [PATCH] fix: enforce retrieval lid and phone for previously unknown contacts (#3702) * fix: check existance if undefined * fix: handle missing lid/phone in group participant resolution * refactor: remove duplicated logic --- src/Client.js | 18 ++++++++---------- src/structures/GroupChat.js | 36 ++++++++++++++++++------------------ src/util/Injected/Utils.js | 22 ++++++++++++++++++++++ 3 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/Client.js b/src/Client.js index c1bd939..5162321 100644 --- a/src/Client.js +++ b/src/Client.js @@ -2343,19 +2343,17 @@ class Client extends EventEmitter { * @returns {Promise>} */ async getContactLidAndPhone(userIds) { - return await this.pupPage.evaluate((userIds) => { - !Array.isArray(userIds) && (userIds = [userIds]); - return userIds.map(userId => { - const wid = window.Store.WidFactory.createWid(userId); - const isLid = wid.server === 'lid'; - const lid = isLid ? wid : window.Store.LidUtils.getCurrentLid(wid); - const phone = isLid ? window.Store.LidUtils.getPhoneNumber(wid) : wid; + return await this.pupPage.evaluate(async (userIds) => { + if (!Array.isArray(userIds)) userIds = [userIds]; + + return await Promise.all(userIds.map(async (userId) => { + const { lid, phone } = await window.WWebJS.enforceLidAndPnRetrieval(userId); return { - lid: lid._serialized, - pn: phone._serialized + lid: lid?._serialized, + pn: phone?._serialized }; - }); + })); }, userIds); } } diff --git a/src/structures/GroupChat.js b/src/structures/GroupChat.js index bfc8f82..fd4e16a 100644 --- a/src/structures/GroupChat.js +++ b/src/structures/GroupChat.js @@ -195,12 +195,12 @@ class GroupChat extends Chat { async removeParticipants(participantIds) { return await this.client.pupPage.evaluate(async (chatId, participantIds) => { const chat = await window.WWebJS.getChat(chatId, { getAsModel: false }); - const participants = participantIds.map(p => { - const wid = window.Store.WidFactory.createWid(p); - const lid = wid.server!=='lid' ? window.Store.LidUtils.getCurrentLid(wid) : wid; - const phone = wid.server=='lid' ? window.Store.LidUtils.getPhoneNumber(wid) : wid; - return chat.groupMetadata.participants.get(lid?._serialized) || chat.groupMetadata.participants.get(phone?._serialized); - }).filter(p => Boolean(p)); + const participants = (await Promise.all(participantIds.map(async p => { + const { lid, phone } = await window.WWebJS.enforceLidAndPnRetrieval(p); + + return chat.groupMetadata.participants.get(lid?._serialized) || + chat.groupMetadata.participants.get(phone?._serialized); + }))).filter(Boolean); await window.Store.GroupParticipants.removeParticipants(chat, participants); return { status: 200 }; }, this.id._serialized, participantIds); @@ -214,12 +214,12 @@ class GroupChat extends Chat { async promoteParticipants(participantIds) { return await this.client.pupPage.evaluate(async (chatId, participantIds) => { const chat = await window.WWebJS.getChat(chatId, { getAsModel: false }); - const participants = participantIds.map(p => { - const wid = window.Store.WidFactory.createWid(p); - const lid = wid.server!=='lid' ? window.Store.LidUtils.getCurrentLid(wid) : wid; - const phone = wid.server=='lid' ? window.Store.LidUtils.getPhoneNumber(wid) : wid; - return chat.groupMetadata.participants.get(lid?._serialized) || chat.groupMetadata.participants.get(phone?._serialized); - }).filter(p => Boolean(p)); + const participants = (await Promise.all(participantIds.map(async p => { + const { lid, phone } = await window.WWebJS.enforceLidAndPnRetrieval(p); + + return chat.groupMetadata.participants.get(lid?._serialized) || + chat.groupMetadata.participants.get(phone?._serialized); + }))).filter(Boolean); await window.Store.GroupParticipants.promoteParticipants(chat, participants); return { status: 200 }; }, this.id._serialized, participantIds); @@ -233,12 +233,12 @@ class GroupChat extends Chat { async demoteParticipants(participantIds) { return await this.client.pupPage.evaluate(async (chatId, participantIds) => { const chat = await window.WWebJS.getChat(chatId, { getAsModel: false }); - const participants = participantIds.map(p => { - const wid = window.Store.WidFactory.createWid(p); - const lid = wid.server!=='lid' ? window.Store.LidUtils.getCurrentLid(wid) : wid; - const phone = wid.server=='lid' ? window.Store.LidUtils.getPhoneNumber(wid) : wid; - return chat.groupMetadata.participants.get(lid?._serialized) || chat.groupMetadata.participants.get(phone?._serialized); - }).filter(p => Boolean(p)); + const participants = (await Promise.all(participantIds.map(async p => { + const { lid, phone } = await window.WWebJS.enforceLidAndPnRetrieval(p); + + return chat.groupMetadata.participants.get(lid?._serialized) || + chat.groupMetadata.participants.get(phone?._serialized); + }))).filter(Boolean); await window.Store.GroupParticipants.demoteParticipants(chat, participants); return { status: 200 }; }, this.id._serialized, participantIds); diff --git a/src/util/Injected/Utils.js b/src/util/Injected/Utils.js index 0a53c62..ee0553f 100644 --- a/src/util/Injected/Utils.js +++ b/src/util/Injected/Utils.js @@ -1144,4 +1144,26 @@ exports.LoadUtils = () => { const statuses = window.Store.Status.getModelsArray(); return statuses.map(status => window.WWebJS.getStatusModel(status)); }; + + window.WWebJS.enforceLidAndPnRetrieval = async (userId) => { + const wid = window.Store.WidFactory.createWid(userId); + const isLid = wid.server === 'lid'; + + let lid = isLid ? wid : window.Store.LidUtils.getCurrentLid(wid); + let phone = isLid ? window.Store.LidUtils.getPhoneNumber(wid) : wid; + + if (!isLid && !lid) { + const queryResult = await window.Store.QueryExist(wid); + if (!queryResult?.wid) return {}; + lid = window.Store.LidUtils.getCurrentLid(wid); + } + + if (isLid && !phone) { + const queryResult = await window.Store.QueryExist(wid); + if (!queryResult?.wid) return {}; + phone = window.Store.LidUtils.getPhoneNumber(wid); + } + + return { lid, phone }; + }; };