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
This commit is contained in:
parent
54a77cf254
commit
d077716315
3 changed files with 48 additions and 28 deletions
|
|
@ -2343,19 +2343,17 @@ class Client extends EventEmitter {
|
||||||
* @returns {Promise<Array<{ lid: string, pn: string }>>}
|
* @returns {Promise<Array<{ lid: string, pn: string }>>}
|
||||||
*/
|
*/
|
||||||
async getContactLidAndPhone(userIds) {
|
async getContactLidAndPhone(userIds) {
|
||||||
return await this.pupPage.evaluate((userIds) => {
|
return await this.pupPage.evaluate(async (userIds) => {
|
||||||
!Array.isArray(userIds) && (userIds = [userIds]);
|
if (!Array.isArray(userIds)) userIds = [userIds];
|
||||||
return userIds.map(userId => {
|
|
||||||
const wid = window.Store.WidFactory.createWid(userId);
|
return await Promise.all(userIds.map(async (userId) => {
|
||||||
const isLid = wid.server === 'lid';
|
const { lid, phone } = await window.WWebJS.enforceLidAndPnRetrieval(userId);
|
||||||
const lid = isLid ? wid : window.Store.LidUtils.getCurrentLid(wid);
|
|
||||||
const phone = isLid ? window.Store.LidUtils.getPhoneNumber(wid) : wid;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
lid: lid._serialized,
|
lid: lid?._serialized,
|
||||||
pn: phone._serialized
|
pn: phone?._serialized
|
||||||
};
|
};
|
||||||
});
|
}));
|
||||||
}, userIds);
|
}, userIds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -195,12 +195,12 @@ class GroupChat extends Chat {
|
||||||
async removeParticipants(participantIds) {
|
async removeParticipants(participantIds) {
|
||||||
return await this.client.pupPage.evaluate(async (chatId, participantIds) => {
|
return await this.client.pupPage.evaluate(async (chatId, participantIds) => {
|
||||||
const chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
|
const chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
|
||||||
const participants = participantIds.map(p => {
|
const participants = (await Promise.all(participantIds.map(async p => {
|
||||||
const wid = window.Store.WidFactory.createWid(p);
|
const { lid, phone } = await window.WWebJS.enforceLidAndPnRetrieval(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) ||
|
||||||
return chat.groupMetadata.participants.get(lid?._serialized) || chat.groupMetadata.participants.get(phone?._serialized);
|
chat.groupMetadata.participants.get(phone?._serialized);
|
||||||
}).filter(p => Boolean(p));
|
}))).filter(Boolean);
|
||||||
await window.Store.GroupParticipants.removeParticipants(chat, participants);
|
await window.Store.GroupParticipants.removeParticipants(chat, participants);
|
||||||
return { status: 200 };
|
return { status: 200 };
|
||||||
}, this.id._serialized, participantIds);
|
}, this.id._serialized, participantIds);
|
||||||
|
|
@ -214,12 +214,12 @@ class GroupChat extends Chat {
|
||||||
async promoteParticipants(participantIds) {
|
async promoteParticipants(participantIds) {
|
||||||
return await this.client.pupPage.evaluate(async (chatId, participantIds) => {
|
return await this.client.pupPage.evaluate(async (chatId, participantIds) => {
|
||||||
const chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
|
const chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
|
||||||
const participants = participantIds.map(p => {
|
const participants = (await Promise.all(participantIds.map(async p => {
|
||||||
const wid = window.Store.WidFactory.createWid(p);
|
const { lid, phone } = await window.WWebJS.enforceLidAndPnRetrieval(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) ||
|
||||||
return chat.groupMetadata.participants.get(lid?._serialized) || chat.groupMetadata.participants.get(phone?._serialized);
|
chat.groupMetadata.participants.get(phone?._serialized);
|
||||||
}).filter(p => Boolean(p));
|
}))).filter(Boolean);
|
||||||
await window.Store.GroupParticipants.promoteParticipants(chat, participants);
|
await window.Store.GroupParticipants.promoteParticipants(chat, participants);
|
||||||
return { status: 200 };
|
return { status: 200 };
|
||||||
}, this.id._serialized, participantIds);
|
}, this.id._serialized, participantIds);
|
||||||
|
|
@ -233,12 +233,12 @@ class GroupChat extends Chat {
|
||||||
async demoteParticipants(participantIds) {
|
async demoteParticipants(participantIds) {
|
||||||
return await this.client.pupPage.evaluate(async (chatId, participantIds) => {
|
return await this.client.pupPage.evaluate(async (chatId, participantIds) => {
|
||||||
const chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
|
const chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
|
||||||
const participants = participantIds.map(p => {
|
const participants = (await Promise.all(participantIds.map(async p => {
|
||||||
const wid = window.Store.WidFactory.createWid(p);
|
const { lid, phone } = await window.WWebJS.enforceLidAndPnRetrieval(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) ||
|
||||||
return chat.groupMetadata.participants.get(lid?._serialized) || chat.groupMetadata.participants.get(phone?._serialized);
|
chat.groupMetadata.participants.get(phone?._serialized);
|
||||||
}).filter(p => Boolean(p));
|
}))).filter(Boolean);
|
||||||
await window.Store.GroupParticipants.demoteParticipants(chat, participants);
|
await window.Store.GroupParticipants.demoteParticipants(chat, participants);
|
||||||
return { status: 200 };
|
return { status: 200 };
|
||||||
}, this.id._serialized, participantIds);
|
}, this.id._serialized, participantIds);
|
||||||
|
|
|
||||||
|
|
@ -1144,4 +1144,26 @@ exports.LoadUtils = () => {
|
||||||
const statuses = window.Store.Status.getModelsArray();
|
const statuses = window.Store.Status.getModelsArray();
|
||||||
return statuses.map(status => window.WWebJS.getStatusModel(status));
|
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 };
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue